Hinzufügen von abstract getClassDescriptor in IntermediateType und Implementierung von IntermediateGenericType.

This commit is contained in:
Etienne Zink 2022-03-19 17:22:23 +01:00
parent c681396061
commit eaec7f613a
4 changed files with 93 additions and 7 deletions

View File

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

View File

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

View File

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

View File

@ -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<Ljava/lang/Integer;>;";
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());
}
}