diff --git a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateRefType.java b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateRefType.java index 84d66b1e..b8e9704a 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateRefType.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateRefType.java @@ -15,6 +15,16 @@ public final class IntermediateRefType extends IntermediateType{ private final List typParameters; + /** + * Caches the hashCode after first computation. + */ + private int hashCode; + + /** + * Caches the classSignature after first computation. + */ + private String classSignature = ""; + public IntermediateRefType(JavaClassName className) { this(className, new ArrayList<>()); } @@ -26,14 +36,24 @@ public final class IntermediateRefType extends IntermediateType{ @Override public String getClassSignature() { - //ToDo - return null; - } + String signature = classSignature; + if (!signature.equals("")) return classSignature; - @Override - public String getClassDescriptor() { - //ToDo - return null; + if (getClassName().equals("void")) signature = "V"; + else { + signature += String.format("L%s", getFullyQualifiedName()); + if (getTypParameterSize() != 0){ + signature += "<"; + for (IntermediateType typParameter:typParameters) { + signature += String.format("L%s;", typParameter.getFullyQualifiedName()); + } + signature += ">"; + } + signature += ";"; + } + signature.replace('.','/'); + classSignature = signature; + return signature; } @Override @@ -51,8 +71,16 @@ public final class IntermediateRefType extends IntermediateType{ @Override public int hashCode() { - //ToDo korrekte Definition auf Basis von equals - return 0; + int prime = 31; + int result = hashCode; + if (result == 0){ + result += getFullyQualifiedName().hashCode(); + for (IntermediateType typeParameter:typParameters) { + result = prime * result + typeParameter.hashCode(); + } + hashCode = result; + } + return result; } public int getTypParameterSize(){ return typParameters.size(); } diff --git a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java index 3c66558f..ba0fd5e3 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java @@ -23,7 +23,6 @@ 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 new file mode 100644 index 00000000..3c795312 --- /dev/null +++ b/src/test/java/intermediate/types/IntermediateRefTypeTest.java @@ -0,0 +1,54 @@ +package intermediate.types; + +import de.dhbwstuttgart.intermediate.types.IntermediateRefType; +import de.dhbwstuttgart.parser.scope.JavaClassName; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.objectweb.asm.Type; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class IntermediateRefTypeTest { + + private static String integerSignature; + private static IntermediateRefType typeToTest_Integer; + + private static String listOfIntegerSignature; + private static IntermediateRefType typeToTest_ListofInteger; + + private static String voidSignature; + private static IntermediateRefType typeToTest_void; + + @BeforeClass + public static void StartUp(){ + integerSignature = "Ljava/lang/Integer;"; + typeToTest_Integer = new IntermediateRefType(new JavaClassName(Type.getInternalName(Integer.class))); + + listOfIntegerSignature = "Ljava/util/List;"; + typeToTest_ListofInteger = new IntermediateRefType( + new JavaClassName(Type.getInternalName(List.class)), + Arrays.asList(typeToTest_Integer)); + + voidSignature = "V"; + typeToTest_void = new IntermediateRefType(new JavaClassName("void")); + } + + @Test + public void SignatureTest_Integer(){ + assertEquals(integerSignature, typeToTest_Integer.getClassSignature()); + } + + @Test + public void SignatureTest_List(){ + assertEquals(listOfIntegerSignature, typeToTest_ListofInteger.getClassSignature()); + } + + @Test + public void SignatureTest_Void(){ + assertEquals(voidSignature, typeToTest_void.getClassSignature()); + } +}