From c32a0cc2227a75f133de8914274db553720fe6c1 Mon Sep 17 00:00:00 2001 From: Etienne Zink Date: Sat, 19 Mar 2022 18:03:54 +0100 Subject: [PATCH] Refactoring access-Modifier von IntermediateType (Klassen-Name Methoden). Refactoring Signature- und Descriptor-Methoden und caching des Descriptors. --- .../types/IntermediateGenericType.java | 43 +++++++++++-------- .../types/IntermediateRefType.java | 25 +++++++---- .../intermediate/types/IntermediateType.java | 10 ++--- .../types/IntermediateRefTypeTest.java | 12 +++--- 4 files changed, 53 insertions(+), 37 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateGenericType.java b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateGenericType.java index 6efc26b7b..82a1b6fb8 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateGenericType.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateGenericType.java @@ -9,8 +9,6 @@ import org.objectweb.asm.Type; * @since Studienarbeit Type Erasure * @author etiennezink */ -//ToDo besprechen ob so korrekt -//z.b. Verwendeung von getFullyQualifiedName und co. public final class IntermediateGenericType extends IntermediateType { private final String genericName; @@ -21,9 +19,14 @@ public final class IntermediateGenericType extends IntermediateType { private int hashCode; /** - * Caches the classSignature after first computation. + * Caches the signature after first computation. */ - private String classSignature = ""; + private String signature = ""; + + /** + * Caches the descriptor after first computation. + */ + private String descriptor = ""; public IntermediateGenericType(String genericName){ this(genericName, new JavaClassName(Type.getInternalName(Object.class))); @@ -34,6 +37,24 @@ public final class IntermediateGenericType extends IntermediateType { this.genericName = genericName; } + @Override + public String getSignature() { + String signature = this.signature; + if (!signature.equals("")) return this.signature; + signature += String.format("T%s;", genericName); + this.signature = signature; + return signature; + } + + @Override + public String getDescriptor() { + String descriptor = this.descriptor; + if (!descriptor.equals("")) return this.descriptor; + descriptor = String.format("L%s;", getFullyQualifiedName()).replace('.','/'); + this.descriptor = descriptor; + return descriptor; + } + @Override public boolean equals(Object o) { if (!(o instanceof IntermediateGenericType)) return false; @@ -56,20 +77,6 @@ public final class IntermediateGenericType extends IntermediateType { return result; } - @Override - public String getClassSignature() { - String signature = classSignature; - if (!signature.equals("")) return classSignature; - signature += String.format("T%s;", genericName); - classSignature = signature; - return signature; - } - - @Override - public String getClassDescriptor() { - return String.format("L%s;", getFullyQualifiedName()).replace('.','/'); - } - public String getGenericName() { return genericName; } diff --git a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateRefType.java b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateRefType.java index 43db30df2..f8b908b99 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateRefType.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateRefType.java @@ -21,9 +21,14 @@ public final class IntermediateRefType extends IntermediateType{ private int hashCode; /** - * Caches the classSignature after first computation. + * Caches the signature after first computation. */ - private String classSignature = ""; + private String signature = ""; + + /** + * Caches the descriptor after first computation. + */ + private String descriptor = ""; public IntermediateRefType(JavaClassName className) { this(className, new ArrayList<>()); @@ -35,9 +40,9 @@ public final class IntermediateRefType extends IntermediateType{ } @Override - public String getClassSignature() { - String signature = classSignature; - if (!signature.equals("")) return classSignature; + public String getSignature() { + String signature = this.signature; + if (!signature.equals("")) return this.signature; if (getClassName().equals("void")) signature = "V"; else { @@ -52,13 +57,17 @@ public final class IntermediateRefType extends IntermediateType{ signature += ";"; } signature.replace('.','/'); - classSignature = signature; + this.signature = signature; return signature; } @Override - public String getClassDescriptor() { - return getClassSignature().replaceAll("<.+>", ""); + public String getDescriptor() { + String descriptor = this.descriptor; + if (!descriptor.equals("")) return this.descriptor; + descriptor = getSignature().replaceAll("<.+>", ""); + this.descriptor = descriptor; + return descriptor; } @Override diff --git a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java index 3c66558f2..b0cb8509c 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java @@ -22,15 +22,15 @@ public abstract class IntermediateType { @Override public abstract int hashCode(); - public abstract String getClassSignature(); - public abstract String getClassDescriptor(); + public abstract String getSignature(); + public abstract String getDescriptor(); @Override public String toString() { return getFullyQualifiedName(); } - public String getClassName() { + protected String getClassName() { return className.getClassName(); } - public String getPackageName() { return className.getPackageName(); } - public String getFullyQualifiedName() { return className.toString(); } + protected String getPackageName() { return className.getPackageName(); } + protected String getFullyQualifiedName() { return className.toString(); } } diff --git a/src/test/java/intermediate/types/IntermediateRefTypeTest.java b/src/test/java/intermediate/types/IntermediateRefTypeTest.java index d70287b2e..b9cfa6d0f 100644 --- a/src/test/java/intermediate/types/IntermediateRefTypeTest.java +++ b/src/test/java/intermediate/types/IntermediateRefTypeTest.java @@ -45,27 +45,27 @@ public class IntermediateRefTypeTest { @Test public void SignatureTest_Integer(){ - assertEquals(integerSignature, typeToTest_Integer.getClassSignature()); + assertEquals(integerSignature, typeToTest_Integer.getSignature()); } @Test public void DescriptorTest_Integer(){ - assertEquals(integerDescriptor, typeToTest_Integer.getClassDescriptor()); + assertEquals(integerDescriptor, typeToTest_Integer.getDescriptor()); } @Test - public void SignatureTest_List(){ assertEquals(listOfIntegerSignature, typeToTest_ListofInteger.getClassSignature()); } + public void SignatureTest_List(){ assertEquals(listOfIntegerSignature, typeToTest_ListofInteger.getSignature()); } @Test - public void DescriptorTest_List(){ assertEquals(listOfIntegerDescriptor, typeToTest_ListofInteger.getClassDescriptor()); } + public void DescriptorTest_List(){ assertEquals(listOfIntegerDescriptor, typeToTest_ListofInteger.getDescriptor()); } @Test public void SignatureTest_Void(){ - assertEquals(voidSignature, typeToTest_void.getClassSignature()); + assertEquals(voidSignature, typeToTest_void.getSignature()); } @Test public void DescriptorTest_Void(){ - assertEquals(voidDescriptor, typeToTest_void.getClassDescriptor()); + assertEquals(voidDescriptor, typeToTest_void.getDescriptor()); } }