Refactoring für innere Typen, welche ausschließlich einen Descriptor besitzen.

Innere Typen sind dabei IntermediateRefType und IntermediateGenericType.
Implementierung der IntermediateWildcard Typen und Hinzufügen leerer Tests.
This commit is contained in:
Etienne Zink 2022-03-20 16:08:38 +01:00
parent 06b27dabd2
commit 55a75b04a0
9 changed files with 124 additions and 48 deletions

View File

@ -3,35 +3,57 @@ package de.dhbwstuttgart.intermediate.types;
import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassName;
/** /**
* //ToDo Beschreiben * Represents a Java Wildcard with a extends-operator.
* E.g. {@code &lt? extends String>}
* *
* @since Studienarbeit Type Erasure * @since Studienarbeit Type Erasure
* @author etiennezink * @author etiennezink
*/ */
public final class IntermediateExtendsWildcard extends IntermediateType { public final class IntermediateExtendsWildcard extends IntermediateType {
//ToDo
public IntermediateExtendsWildcard(JavaClassName className) { private final IntermediateInnerType innerType;
super(className);
/**
* Caches the hashCode after first computation.
*/
private int hashCode;
/**
* Caches the signature after first computation.
*/
private String signature = "";
public IntermediateExtendsWildcard(IntermediateInnerType innerType) {
this.innerType = innerType;
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
return false; if (!(o instanceof IntermediateExtendsWildcard)) return false;
IntermediateExtendsWildcard intermediateExtendsWildcard = (IntermediateExtendsWildcard) o;
if(!innerType.equals(intermediateExtendsWildcard.innerType)) return false;
return true;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return 0; int prime = 31;
int hashCode = this.hashCode;
if (hashCode == 0){
hashCode += innerType.hashCode();
hashCode = prime * hashCode + "extends".hashCode();
this.hashCode = hashCode;
}
return hashCode;
} }
@Override @Override
public String getSignature() { public String getSignature() {
return null; String signature = this.signature;
} if (!signature.equals("")) return this.signature;
signature = String.format("+%s", innerType.getSignature());
@Override this.signature = signature;
public String getDescriptor() { return signature;
return null;
} }
} }

View File

@ -10,9 +10,10 @@ import org.objectweb.asm.Type;
* @since Studienarbeit Type Erasure * @since Studienarbeit Type Erasure
* @author etiennezink * @author etiennezink
*/ */
public final class IntermediateGenericType extends IntermediateType { public final class IntermediateGenericType extends IntermediateInnerType {
private final String genericName; private final String genericName;
private final IntermediateInnerType innerType;
/** /**
* Caches the hashCode after first computation. * Caches the hashCode after first computation.
@ -30,11 +31,11 @@ public final class IntermediateGenericType extends IntermediateType {
private String descriptor = ""; private String descriptor = "";
public IntermediateGenericType(String genericName){ public IntermediateGenericType(String genericName){
this(genericName, new JavaClassName(Type.getInternalName(Object.class))); this(genericName, new IntermediateRefType(new JavaClassName(Type.getInternalName(Object.class))));
} }
public IntermediateGenericType(String genericName, JavaClassName className) { public IntermediateGenericType(String genericName, IntermediateInnerType innerType) {
super(className); this.innerType = innerType;
this.genericName = genericName; this.genericName = genericName;
} }
@ -43,7 +44,7 @@ public final class IntermediateGenericType extends IntermediateType {
if (!(o instanceof IntermediateGenericType)) return false; if (!(o instanceof IntermediateGenericType)) return false;
IntermediateGenericType intermediateGenericType = (IntermediateGenericType) o; IntermediateGenericType intermediateGenericType = (IntermediateGenericType) o;
if(!getFullyQualifiedName().equals(intermediateGenericType.getFullyQualifiedName())) return false; if(!innerType.equals(intermediateGenericType.innerType)) return false;
if(!genericName.equals(intermediateGenericType.genericName)) return false; if(!genericName.equals(intermediateGenericType.genericName)) return false;
return true; return true;
} }
@ -53,7 +54,7 @@ public final class IntermediateGenericType extends IntermediateType {
int prime = 31; int prime = 31;
int hashCode = this.hashCode; int hashCode = this.hashCode;
if (hashCode == 0){ if (hashCode == 0){
hashCode += getFullyQualifiedName().hashCode(); hashCode += innerType.hashCode();
hashCode = prime * hashCode + genericName.hashCode(); hashCode = prime * hashCode + genericName.hashCode();
this.hashCode = hashCode; this.hashCode = hashCode;
} }
@ -73,7 +74,7 @@ public final class IntermediateGenericType extends IntermediateType {
public String getDescriptor() { public String getDescriptor() {
String descriptor = this.descriptor; String descriptor = this.descriptor;
if (!descriptor.equals("")) return this.descriptor; if (!descriptor.equals("")) return this.descriptor;
descriptor = String.format("L%s;", getFullyQualifiedName()).replace('.','/'); descriptor = innerType.getDescriptor();
this.descriptor = descriptor; this.descriptor = descriptor;
return descriptor; return descriptor;
} }

View File

@ -0,0 +1,9 @@
package de.dhbwstuttgart.intermediate.types;
/**
* Only Java types which can be used as an inner type, have a descriptor definition.
*/
public abstract class IntermediateInnerType extends IntermediateType {
public abstract String getDescriptor();
}

View File

@ -11,9 +11,10 @@ import java.util.List;
* @since Studienarbeit Type Erasure * @since Studienarbeit Type Erasure
* @author etiennezink * @author etiennezink
*/ */
public final class IntermediateRefType extends IntermediateType{ public final class IntermediateRefType extends IntermediateInnerType{
private final List<IntermediateRefType> typParameters; private final List<IntermediateRefType> typParameters;
private final JavaClassName className;
/** /**
* Caches the hashCode after first computation. * Caches the hashCode after first computation.
@ -35,7 +36,7 @@ public final class IntermediateRefType extends IntermediateType{
} }
public IntermediateRefType(JavaClassName className, List<IntermediateRefType> typParameters){ public IntermediateRefType(JavaClassName className, List<IntermediateRefType> typParameters){
super(className); this.className = className;
this.typParameters = Collections.unmodifiableList(typParameters); this.typParameters = Collections.unmodifiableList(typParameters);
} }
@ -71,13 +72,13 @@ public final class IntermediateRefType extends IntermediateType{
String signature = this.signature; String signature = this.signature;
if (!signature.equals("")) return this.signature; if (!signature.equals("")) return this.signature;
if (getClassName().equals("void")) signature = "V"; if (className.getClassName().equals("void")) signature = "V";
else { else {
signature += String.format("L%s", getFullyQualifiedName()); signature += String.format("L%s", getFullyQualifiedName());
if (typParameters.size() != 0){ if (typParameters.size() != 0){
signature += "<"; signature += "<";
for (IntermediateType typParameter:typParameters) { for (IntermediateType typParameter:typParameters) {
signature += String.format("L%s;", typParameter.getFullyQualifiedName()); signature += typParameter.getSignature();
} }
signature += ">"; signature += ">";
} }
@ -109,4 +110,6 @@ public final class IntermediateRefType extends IntermediateType{
if(typParameters.size() < index) return null; if(typParameters.size() < index) return null;
return typParameters.get(index); return typParameters.get(index);
} }
private String getFullyQualifiedName(){ return className.toString(); }
} }

View File

@ -3,35 +3,56 @@ package de.dhbwstuttgart.intermediate.types;
import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassName;
/** /**
* //ToDo Beschreiben * Represents a Java Wildcard with a super-operator.
* E.g. {@code &lt? super String&gt;}
* *
* @since Studienarbeit Type Erasure * @since Studienarbeit Type Erasure
* @author etiennezink * @author etiennezink
*/ */
public final class IntermediateSuperWildcard extends IntermediateType { public final class IntermediateSuperWildcard extends IntermediateType {
//ToDo private final IntermediateInnerType innerType;
public IntermediateSuperWildcard(JavaClassName className) { /**
super(className); * Caches the hashCode after first computation.
*/
private int hashCode;
/**
* Caches the signature after first computation.
*/
private String signature = "";
public IntermediateSuperWildcard(IntermediateInnerType innerType) {
this.innerType = innerType;
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
return false; if (!(o instanceof IntermediateSuperWildcard)) return false;
IntermediateSuperWildcard intermediateSuperWildcard = (IntermediateSuperWildcard) o;
if(!innerType.equals(intermediateSuperWildcard.innerType)) return false;
return true;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return 0; int prime = 31;
int hashCode = this.hashCode;
if (hashCode == 0){
hashCode += innerType.hashCode();
hashCode = prime * hashCode + "super".hashCode();
this.hashCode = hashCode;
}
return hashCode;
} }
@Override @Override
public String getSignature() { public String getSignature() {
return null; String signature = this.signature;
} if (!signature.equals("")) return this.signature;
signature = String.format("-%s", innerType.getSignature());
@Override this.signature = signature;
public String getDescriptor() { return signature;
return null;
} }
} }

View File

@ -1,7 +1,5 @@
package de.dhbwstuttgart.intermediate.types; package de.dhbwstuttgart.intermediate.types;
import de.dhbwstuttgart.parser.scope.JavaClassName;
/** /**
* Base class for Java data types which is used as an intermediate representation after type resolution. * Base class for Java data types which is used as an intermediate representation after type resolution.
* This class and it's subtypes have to be immutable (e.g. no mutators) to ensure side effect freedom. * This class and it's subtypes have to be immutable (e.g. no mutators) to ensure side effect freedom.
@ -12,10 +10,6 @@ import de.dhbwstuttgart.parser.scope.JavaClassName;
*/ */
public abstract class IntermediateType { public abstract class IntermediateType {
private final JavaClassName className;
public IntermediateType(JavaClassName className){ this.className = className; }
@Override @Override
public abstract boolean equals(Object o); public abstract boolean equals(Object o);
@ -23,14 +17,7 @@ public abstract class IntermediateType {
public abstract int hashCode(); public abstract int hashCode();
public abstract String getSignature(); public abstract String getSignature();
public abstract String getDescriptor();
@Override @Override
public String toString() { return getSignature(); } public String toString() { return getSignature(); }
protected String getClassName() {
return className.getClassName();
}
protected String getPackageName() { return className.getPackageName(); }
protected String getFullyQualifiedName() { return className.toString(); }
} }

View File

@ -0,0 +1,11 @@
package intermediate.types;
import org.junit.Assert;
import org.junit.Test;
public class IntermediateExtensWildcardTest {
@Test
public void ToDo(){
Assert.fail();
}
}

View File

@ -0,0 +1,11 @@
package intermediate.types;
import org.junit.Assert;
import org.junit.Test;
public class IntermediateGenericTypeTest {
@Test
public void ToDo(){
Assert.fail();
}
}

View File

@ -0,0 +1,11 @@
package intermediate.types;
import org.junit.Assert;
import org.junit.Test;
public class IntermediateSuperWildcardTest {
@Test
public void ToDo(){
Assert.fail();
}
}