From eebb07ec8b591cca0cf4abb6d7f164432db1d704 Mon Sep 17 00:00:00 2001 From: Etienne Zink Date: Tue, 29 Mar 2022 19:19:31 +0200 Subject: [PATCH] =?UTF-8?q?Verbesserung=20FunNGenerator:=20Generics=20werd?= =?UTF-8?q?en=20nicht=20mehr=20mehrfach=20hinzugef=C3=BCgt.=20TPHS=20werde?= =?UTF-8?q?n=20im=20Klassenname=20als=20LTPH=20angegeben,=20so=20entsteht?= =?UTF-8?q?=20keine=20Uneindeutigkeit=20zu=20Klassen=20mit=20Object=20(ohn?= =?UTF-8?q?e=20TPH)=20im=20Klassenname.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bytecode/funN/FunNGenerator.java | 29 ++++++++++--------- .../resources/bytecode/javFiles/OLFun.jav | 4 +-- .../resources/bytecode/javFiles/OLFun2.jav | 4 +-- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java b/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java index 5b6a9e442..3a651c938 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java @@ -39,13 +39,13 @@ public class FunNGenerator implements FunNUtilities{ private final String methodName = "apply"; private final int bytecodeVersion = V1_8; - private static final String objectSuperType = Type.getInternalName(Object.class).replace('.','/'); - private static final RefType objectRefType = new RefType(new JavaClassName(objectSuperType), null); - private static final String objectSignature = applySignature(objectRefType); + private final String objectSuperType = Type.getInternalName(Object.class).replace('.','/'); + private final RefType objectRefType = new RefType(new JavaClassName(objectSuperType), null); + private final String objectSignature = applySignature(objectRefType); - private static String applyDescriptor(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToDescriptor(true)); } - private static String applySignature(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToSignature(true)); } - private static String applyNameDescriptor(RefTypeOrTPHOrWildcardOrGeneric a){ return a instanceof TypePlaceholder ? applyNameDescriptor(objectRefType) : String.format("L%s;", applyDescriptor(a)); } + private String applyDescriptor(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToDescriptor(true)); } + private String applySignature(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToSignature(true)); } + private String applyNameDescriptor(RefTypeOrTPHOrWildcardOrGeneric a){ return a instanceof TypePlaceholder ? "LTPH;" : String.format("L%s;", applyDescriptor(a)); } @Override public byte[] generateSuperBytecode(int numberArguments) { @@ -86,14 +86,16 @@ public class FunNGenerator implements FunNUtilities{ boolean containsGeneric = false; String genericSignature = "<"; - for (int currentTypArgumentIndex = 0; currentTypArgumentIndex < argumentTypes.size(); currentTypArgumentIndex++) { - RefTypeOrTPHOrWildcardOrGeneric typArgument = argumentTypes.get(currentTypArgumentIndex); - if (typArgument instanceof GenericRefType){ - GenericRefType generic = (GenericRefType) typArgument; + for (RefTypeOrTPHOrWildcardOrGeneric typeArgument : parameters) { + //ToDo Etienne: Refactor + if (typeArgument instanceof GenericRefType){ + GenericRefType generic = (GenericRefType) typeArgument; + if(genericSignature.contains(generic.getParsedName())) continue; genericSignature += String.format("%s:%s", generic.getParsedName(), applyDescriptor(generic)); containsGeneric = true; - } else if(typArgument instanceof TypePlaceholder){ - TypePlaceholder placeholder = (TypePlaceholder) typArgument; + } else if(typeArgument instanceof TypePlaceholder){ + TypePlaceholder placeholder = (TypePlaceholder) typeArgument; + if(genericSignature.contains(applySignature(placeholder).substring(1))) continue; genericSignature += String.format("%s:%s", applySignature(placeholder).substring(1), objectSignature); containsGeneric = true; } @@ -109,12 +111,11 @@ public class FunNGenerator implements FunNUtilities{ @Override public String getSpecializedClassName(List argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType) { - if (returnType instanceof TypePlaceholder) returnType = objectRefType; return String.format("Fun%d$$%s%s", argumentTypes.size(), argumentTypes .stream() - .map(FunNGenerator::applyNameDescriptor) + .map(this::applyNameDescriptor) .collect(Collectors.joining()), applyNameDescriptor(returnType)) .replace('/', '$') diff --git a/src/test/resources/bytecode/javFiles/OLFun.jav b/src/test/resources/bytecode/javFiles/OLFun.jav index 4896f948f..adf8c6a85 100644 --- a/src/test/resources/bytecode/javFiles/OLFun.jav +++ b/src/test/resources/bytecode/javFiles/OLFun.jav @@ -1,6 +1,6 @@ import java.lang.String; -//import java.lang.Integer; -//import java.lang.Double; +import java.lang.Integer; +import java.lang.Double; import java.util.Vector; import java.lang.Boolean; diff --git a/src/test/resources/bytecode/javFiles/OLFun2.jav b/src/test/resources/bytecode/javFiles/OLFun2.jav index fd57e1841..406c0d98d 100644 --- a/src/test/resources/bytecode/javFiles/OLFun2.jav +++ b/src/test/resources/bytecode/javFiles/OLFun2.jav @@ -1,6 +1,6 @@ import java.lang.String; -//import java.lang.Integer; -//import java.lang.Double; +import java.lang.Integer; +import java.lang.Double; import java.util.Vector; import java.lang.Boolean;