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
* @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;
}

View File

@ -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

View File

@ -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(); }
}

View File

@ -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());
}
}