From eaec7f613ab1abb01e35fc2b043a052f821396fb Mon Sep 17 00:00:00 2001 From: Etienne Zink Date: Sat, 19 Mar 2022 17:22:23 +0100 Subject: [PATCH] =?UTF-8?q?Hinzuf=C3=BCgen=20von=20abstract=20getClassDesc?= =?UTF-8?q?riptor=20in=20IntermediateType=20und=20Implementierung=20von=20?= =?UTF-8?q?IntermediateGenericType.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../types/IntermediateGenericType.java | 69 ++++++++++++++++++- .../types/IntermediateRefType.java | 9 ++- .../intermediate/types/IntermediateType.java | 1 + .../types/IntermediateRefTypeTest.java | 21 +++++- 4 files changed, 93 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateGenericType.java b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateGenericType.java index 8e176a49..6efc26b7 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateGenericType.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateGenericType.java @@ -1,13 +1,76 @@ package de.dhbwstuttgart.intermediate.types; import de.dhbwstuttgart.parser.scope.JavaClassName; +import org.objectweb.asm.Type; /** - * //ToDo Beschreiben + * Represents a Java typ variable like {@code <T>} or {@code <T extends String>}. * * @since Studienarbeit Type Erasure * @author etiennezink */ -public final class IntermediateGenericType { - //ToDo +//ToDo besprechen ob so korrekt +//z.b. Verwendeung von getFullyQualifiedName und co. +public final class IntermediateGenericType extends IntermediateType { + + private final String genericName; + + /** + * Caches the hashCode after first computation. + */ + private int hashCode; + + /** + * Caches the classSignature after first computation. + */ + private String classSignature = ""; + + public IntermediateGenericType(String genericName){ + this(genericName, new JavaClassName(Type.getInternalName(Object.class))); + } + + public IntermediateGenericType(String genericName, JavaClassName className) { + super(className); + this.genericName = genericName; + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof IntermediateGenericType)) return false; + + IntermediateGenericType intermediateGenericType = (IntermediateGenericType) o; + if(!getFullyQualifiedName().equals(intermediateGenericType.getFullyQualifiedName())) return false; + if(!genericName.equals(intermediateGenericType.getGenericName())) return false; + return true; + } + + @Override + public int hashCode() { + int prime = 31; + int result = hashCode; + if (result == 0){ + result += getFullyQualifiedName().hashCode(); + result = prime * result + genericName.hashCode(); + hashCode = 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() { + 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 b8e9704a..43db30df 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateRefType.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateRefType.java @@ -42,7 +42,7 @@ public final class IntermediateRefType extends IntermediateType{ if (getClassName().equals("void")) signature = "V"; else { signature += String.format("L%s", getFullyQualifiedName()); - if (getTypParameterSize() != 0){ + if (typParameters.size() != 0){ signature += "<"; for (IntermediateType typParameter:typParameters) { signature += String.format("L%s;", typParameter.getFullyQualifiedName()); @@ -56,6 +56,11 @@ public final class IntermediateRefType extends IntermediateType{ return signature; } + @Override + public String getClassDescriptor() { + return getClassSignature().replaceAll("<.+>", ""); + } + @Override public boolean equals(Object o) { if (!(o instanceof IntermediateRefType)) return false; @@ -90,7 +95,7 @@ public final class IntermediateRefType extends IntermediateType{ * @return the typ parameter at {@code index} or {@code null}, iff {@code |typ parameters| < index} */ public IntermediateType getTypParameter(int index) { - if(getTypParameterSize() < index) return null; + if(typParameters.size() < index) return null; return typParameters.get(index); } } diff --git a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java index ba0fd5e3..3c66558f 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java @@ -23,6 +23,7 @@ public abstract class IntermediateType { public abstract int hashCode(); public abstract String getClassSignature(); + public abstract String getClassDescriptor(); @Override public String toString() { return getFullyQualifiedName(); } diff --git a/src/test/java/intermediate/types/IntermediateRefTypeTest.java b/src/test/java/intermediate/types/IntermediateRefTypeTest.java index 3c795312..d70287b2 100644 --- a/src/test/java/intermediate/types/IntermediateRefTypeTest.java +++ b/src/test/java/intermediate/types/IntermediateRefTypeTest.java @@ -15,25 +15,31 @@ import static org.junit.Assert.assertEquals; public class IntermediateRefTypeTest { private static String integerSignature; + private static String integerDescriptor; private static IntermediateRefType typeToTest_Integer; private static String listOfIntegerSignature; + private static String listOfIntegerDescriptor; private static IntermediateRefType typeToTest_ListofInteger; private static String voidSignature; + private static String voidDescriptor; private static IntermediateRefType typeToTest_void; @BeforeClass public static void StartUp(){ integerSignature = "Ljava/lang/Integer;"; + integerDescriptor = "Ljava/lang/Integer;"; typeToTest_Integer = new IntermediateRefType(new JavaClassName(Type.getInternalName(Integer.class))); listOfIntegerSignature = "Ljava/util/List;"; + listOfIntegerDescriptor = "Ljava/util/List;"; typeToTest_ListofInteger = new IntermediateRefType( new JavaClassName(Type.getInternalName(List.class)), Arrays.asList(typeToTest_Integer)); voidSignature = "V"; + voidDescriptor = "V"; typeToTest_void = new IntermediateRefType(new JavaClassName("void")); } @@ -43,12 +49,23 @@ public class IntermediateRefTypeTest { } @Test - public void SignatureTest_List(){ - assertEquals(listOfIntegerSignature, typeToTest_ListofInteger.getClassSignature()); + public void DescriptorTest_Integer(){ + assertEquals(integerDescriptor, typeToTest_Integer.getClassDescriptor()); } + @Test + public void SignatureTest_List(){ assertEquals(listOfIntegerSignature, typeToTest_ListofInteger.getClassSignature()); } + + @Test + public void DescriptorTest_List(){ assertEquals(listOfIntegerDescriptor, typeToTest_ListofInteger.getClassDescriptor()); } + @Test public void SignatureTest_Void(){ assertEquals(voidSignature, typeToTest_void.getClassSignature()); } + + @Test + public void DescriptorTest_Void(){ + assertEquals(voidDescriptor, typeToTest_void.getClassDescriptor()); + } }