Implementierung und Tests von IntermediateRefType.

This commit is contained in:
Etienne Zink 2022-03-19 16:33:13 +01:00
parent 3d19d8d729
commit c681396061
3 changed files with 91 additions and 10 deletions

View File

@ -15,6 +15,16 @@ public final class IntermediateRefType extends IntermediateType{
private final List<IntermediateRefType> 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(); }

View File

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

View File

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