Refactoring access-Modifier von IntermediateType (Klassen-Name Methoden).

Refactoring Signature- und Descriptor-Methoden und caching des Descriptors.
This commit is contained in:
Etienne Zink 2022-03-19 18:03:54 +01:00
parent eaec7f613a
commit c32a0cc222
4 changed files with 53 additions and 37 deletions

View File

@ -9,8 +9,6 @@ import org.objectweb.asm.Type;
* @since Studienarbeit Type Erasure * @since Studienarbeit Type Erasure
* @author etiennezink * @author etiennezink
*/ */
//ToDo besprechen ob so korrekt
//z.b. Verwendeung von getFullyQualifiedName und co.
public final class IntermediateGenericType extends IntermediateType { public final class IntermediateGenericType extends IntermediateType {
private final String genericName; private final String genericName;
@ -21,9 +19,14 @@ public final class IntermediateGenericType extends IntermediateType {
private int hashCode; 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){ public IntermediateGenericType(String genericName){
this(genericName, new JavaClassName(Type.getInternalName(Object.class))); this(genericName, new JavaClassName(Type.getInternalName(Object.class)));
@ -34,6 +37,24 @@ public final class IntermediateGenericType extends IntermediateType {
this.genericName = genericName; 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 @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof IntermediateGenericType)) return false; if (!(o instanceof IntermediateGenericType)) return false;
@ -56,20 +77,6 @@ public final class IntermediateGenericType extends IntermediateType {
return result; 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() { public String getGenericName() {
return genericName; return genericName;
} }

View File

@ -21,9 +21,14 @@ public final class IntermediateRefType extends IntermediateType{
private int hashCode; 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) { public IntermediateRefType(JavaClassName className) {
this(className, new ArrayList<>()); this(className, new ArrayList<>());
@ -35,9 +40,9 @@ public final class IntermediateRefType extends IntermediateType{
} }
@Override @Override
public String getClassSignature() { public String getSignature() {
String signature = classSignature; String signature = this.signature;
if (!signature.equals("")) return classSignature; if (!signature.equals("")) return this.signature;
if (getClassName().equals("void")) signature = "V"; if (getClassName().equals("void")) signature = "V";
else { else {
@ -52,13 +57,17 @@ public final class IntermediateRefType extends IntermediateType{
signature += ";"; signature += ";";
} }
signature.replace('.','/'); signature.replace('.','/');
classSignature = signature; this.signature = signature;
return signature; return signature;
} }
@Override @Override
public String getClassDescriptor() { public String getDescriptor() {
return getClassSignature().replaceAll("<.+>", ""); String descriptor = this.descriptor;
if (!descriptor.equals("")) return this.descriptor;
descriptor = getSignature().replaceAll("<.+>", "");
this.descriptor = descriptor;
return descriptor;
} }
@Override @Override

View File

@ -22,15 +22,15 @@ public abstract class IntermediateType {
@Override @Override
public abstract int hashCode(); public abstract int hashCode();
public abstract String getClassSignature(); public abstract String getSignature();
public abstract String getClassDescriptor(); public abstract String getDescriptor();
@Override @Override
public String toString() { return getFullyQualifiedName(); } public String toString() { return getFullyQualifiedName(); }
public String getClassName() { protected String getClassName() {
return className.getClassName(); return className.getClassName();
} }
public String getPackageName() { return className.getPackageName(); } protected String getPackageName() { return className.getPackageName(); }
public String getFullyQualifiedName() { return className.toString(); } protected String getFullyQualifiedName() { return className.toString(); }
} }

View File

@ -45,27 +45,27 @@ public class IntermediateRefTypeTest {
@Test @Test
public void SignatureTest_Integer(){ public void SignatureTest_Integer(){
assertEquals(integerSignature, typeToTest_Integer.getClassSignature()); assertEquals(integerSignature, typeToTest_Integer.getSignature());
} }
@Test @Test
public void DescriptorTest_Integer(){ public void DescriptorTest_Integer(){
assertEquals(integerDescriptor, typeToTest_Integer.getClassDescriptor()); assertEquals(integerDescriptor, typeToTest_Integer.getDescriptor());
} }
@Test @Test
public void SignatureTest_List(){ assertEquals(listOfIntegerSignature, typeToTest_ListofInteger.getClassSignature()); } public void SignatureTest_List(){ assertEquals(listOfIntegerSignature, typeToTest_ListofInteger.getSignature()); }
@Test @Test
public void DescriptorTest_List(){ assertEquals(listOfIntegerDescriptor, typeToTest_ListofInteger.getClassDescriptor()); } public void DescriptorTest_List(){ assertEquals(listOfIntegerDescriptor, typeToTest_ListofInteger.getDescriptor()); }
@Test @Test
public void SignatureTest_Void(){ public void SignatureTest_Void(){
assertEquals(voidSignature, typeToTest_void.getClassSignature()); assertEquals(voidSignature, typeToTest_void.getSignature());
} }
@Test @Test
public void DescriptorTest_Void(){ public void DescriptorTest_Void(){
assertEquals(voidDescriptor, typeToTest_void.getClassDescriptor()); assertEquals(voidDescriptor, typeToTest_void.getDescriptor());
} }
} }