From 55a75b04a06c3238094dda7ca42433671d9ce9cd Mon Sep 17 00:00:00 2001 From: Etienne Zink Date: Sun, 20 Mar 2022 16:08:38 +0100 Subject: [PATCH] =?UTF-8?q?Refactoring=20f=C3=BCr=20innere=20Typen,=20welc?= =?UTF-8?q?he=20ausschlie=C3=9Flich=20einen=20Descriptor=20besitzen.=20Inn?= =?UTF-8?q?ere=20Typen=20sind=20dabei=20IntermediateRefType=20und=20Interm?= =?UTF-8?q?ediateGenericType.=20Implementierung=20der=20IntermediateWildca?= =?UTF-8?q?rd=20Typen=20und=20Hinzuf=C3=BCgen=20leerer=20Tests.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../types/IntermediateExtendsWildcard.java | 46 ++++++++++++++----- .../types/IntermediateGenericType.java | 15 +++--- .../types/IntermediateInnerType.java | 9 ++++ .../types/IntermediateRefType.java | 11 +++-- .../types/IntermediateSuperWildcard.java | 45 +++++++++++++----- .../intermediate/types/IntermediateType.java | 13 ------ .../types/IntermediateExtensWildcardTest.java | 11 +++++ .../types/IntermediateGenericTypeTest.java | 11 +++++ .../types/IntermediateSuperWildcardTest.java | 11 +++++ 9 files changed, 124 insertions(+), 48 deletions(-) create mode 100644 src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateInnerType.java create mode 100644 src/test/java/intermediate/types/IntermediateExtensWildcardTest.java create mode 100644 src/test/java/intermediate/types/IntermediateGenericTypeTest.java create mode 100644 src/test/java/intermediate/types/IntermediateSuperWildcardTest.java diff --git a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateExtendsWildcard.java b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateExtendsWildcard.java index eda1eafd..d1be0aeb 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateExtendsWildcard.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateExtendsWildcard.java @@ -3,35 +3,57 @@ package de.dhbwstuttgart.intermediate.types; import de.dhbwstuttgart.parser.scope.JavaClassName; /** - * //ToDo Beschreiben + * Represents a Java Wildcard with a extends-operator. + * E.g. {@code <? extends String>} * * @since Studienarbeit Type Erasure * @author etiennezink */ public final class IntermediateExtendsWildcard extends IntermediateType { - //ToDo - public IntermediateExtendsWildcard(JavaClassName className) { - super(className); + private final IntermediateInnerType innerType; + + /** + * 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 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 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 public String getSignature() { - return null; - } - - @Override - public String getDescriptor() { - return null; + String signature = this.signature; + if (!signature.equals("")) return this.signature; + signature = String.format("+%s", innerType.getSignature()); + this.signature = signature; + return signature; } } diff --git a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateGenericType.java b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateGenericType.java index 41da2502..a222dc2c 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateGenericType.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateGenericType.java @@ -10,9 +10,10 @@ import org.objectweb.asm.Type; * @since Studienarbeit Type Erasure * @author etiennezink */ -public final class IntermediateGenericType extends IntermediateType { +public final class IntermediateGenericType extends IntermediateInnerType { private final String genericName; + private final IntermediateInnerType innerType; /** * Caches the hashCode after first computation. @@ -30,11 +31,11 @@ public final class IntermediateGenericType extends IntermediateType { private String descriptor = ""; 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) { - super(className); + public IntermediateGenericType(String genericName, IntermediateInnerType innerType) { + this.innerType = innerType; this.genericName = genericName; } @@ -43,7 +44,7 @@ public final class IntermediateGenericType extends IntermediateType { if (!(o instanceof IntermediateGenericType)) return false; 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; return true; } @@ -53,7 +54,7 @@ public final class IntermediateGenericType extends IntermediateType { int prime = 31; int hashCode = this.hashCode; if (hashCode == 0){ - hashCode += getFullyQualifiedName().hashCode(); + hashCode += innerType.hashCode(); hashCode = prime * hashCode + genericName.hashCode(); this.hashCode = hashCode; } @@ -73,7 +74,7 @@ public final class IntermediateGenericType extends IntermediateType { public String getDescriptor() { String descriptor = this.descriptor; if (!descriptor.equals("")) return this.descriptor; - descriptor = String.format("L%s;", getFullyQualifiedName()).replace('.','/'); + descriptor = innerType.getDescriptor(); this.descriptor = descriptor; return descriptor; } diff --git a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateInnerType.java b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateInnerType.java new file mode 100644 index 00000000..a83c974f --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateInnerType.java @@ -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(); +} diff --git a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateRefType.java b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateRefType.java index e42f5258..81ef371a 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateRefType.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateRefType.java @@ -11,9 +11,10 @@ import java.util.List; * @since Studienarbeit Type Erasure * @author etiennezink */ -public final class IntermediateRefType extends IntermediateType{ +public final class IntermediateRefType extends IntermediateInnerType{ private final List typParameters; + private final JavaClassName className; /** * Caches the hashCode after first computation. @@ -35,7 +36,7 @@ public final class IntermediateRefType extends IntermediateType{ } public IntermediateRefType(JavaClassName className, List typParameters){ - super(className); + this.className = className; this.typParameters = Collections.unmodifiableList(typParameters); } @@ -71,13 +72,13 @@ public final class IntermediateRefType extends IntermediateType{ String signature = this.signature; if (!signature.equals("")) return this.signature; - if (getClassName().equals("void")) signature = "V"; + if (className.getClassName().equals("void")) signature = "V"; else { signature += String.format("L%s", getFullyQualifiedName()); if (typParameters.size() != 0){ signature += "<"; for (IntermediateType typParameter:typParameters) { - signature += String.format("L%s;", typParameter.getFullyQualifiedName()); + signature += typParameter.getSignature(); } signature += ">"; } @@ -109,4 +110,6 @@ public final class IntermediateRefType extends IntermediateType{ if(typParameters.size() < index) return null; return typParameters.get(index); } + + private String getFullyQualifiedName(){ return className.toString(); } } diff --git a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateSuperWildcard.java b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateSuperWildcard.java index 42fdaf37..2e806828 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateSuperWildcard.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateSuperWildcard.java @@ -3,35 +3,56 @@ package de.dhbwstuttgart.intermediate.types; import de.dhbwstuttgart.parser.scope.JavaClassName; /** - * //ToDo Beschreiben + * Represents a Java Wildcard with a super-operator. + * E.g. {@code <? super String>} * * @since Studienarbeit Type Erasure * @author etiennezink */ 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 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 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 public String getSignature() { - return null; - } - - @Override - public String getDescriptor() { - return null; + String signature = this.signature; + if (!signature.equals("")) return this.signature; + signature = String.format("-%s", innerType.getSignature()); + this.signature = signature; + return signature; } } diff --git a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java index c8dae87e..3edaf9b0 100644 --- a/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java +++ b/src/main/java/de/dhbwstuttgart/intermediate/types/IntermediateType.java @@ -1,7 +1,5 @@ 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. * 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 { - private final JavaClassName className; - - public IntermediateType(JavaClassName className){ this.className = className; } - @Override public abstract boolean equals(Object o); @@ -23,14 +17,7 @@ public abstract class IntermediateType { public abstract int hashCode(); public abstract String getSignature(); - public abstract String getDescriptor(); @Override public String toString() { return getSignature(); } - - protected String getClassName() { - return className.getClassName(); - } - protected String getPackageName() { return className.getPackageName(); } - protected String getFullyQualifiedName() { return className.toString(); } } diff --git a/src/test/java/intermediate/types/IntermediateExtensWildcardTest.java b/src/test/java/intermediate/types/IntermediateExtensWildcardTest.java new file mode 100644 index 00000000..d82e7baf --- /dev/null +++ b/src/test/java/intermediate/types/IntermediateExtensWildcardTest.java @@ -0,0 +1,11 @@ +package intermediate.types; + +import org.junit.Assert; +import org.junit.Test; + +public class IntermediateExtensWildcardTest { + @Test + public void ToDo(){ + Assert.fail(); + } +} diff --git a/src/test/java/intermediate/types/IntermediateGenericTypeTest.java b/src/test/java/intermediate/types/IntermediateGenericTypeTest.java new file mode 100644 index 00000000..c7dbed45 --- /dev/null +++ b/src/test/java/intermediate/types/IntermediateGenericTypeTest.java @@ -0,0 +1,11 @@ +package intermediate.types; + +import org.junit.Assert; +import org.junit.Test; + +public class IntermediateGenericTypeTest { + @Test + public void ToDo(){ + Assert.fail(); + } +} diff --git a/src/test/java/intermediate/types/IntermediateSuperWildcardTest.java b/src/test/java/intermediate/types/IntermediateSuperWildcardTest.java new file mode 100644 index 00000000..7ba44c31 --- /dev/null +++ b/src/test/java/intermediate/types/IntermediateSuperWildcardTest.java @@ -0,0 +1,11 @@ +package intermediate.types; + +import org.junit.Assert; +import org.junit.Test; + +public class IntermediateSuperWildcardTest { + @Test + public void ToDo(){ + Assert.fail(); + } +}