Verbesserung FunNGenerator:

Generics werden nicht mehr mehrfach hinzugefügt.
TPHS werden im Klassenname als LTPH angegeben, so entsteht keine Uneindeutigkeit zu Klassen mit Object (ohne TPH) im Klassenname.
This commit is contained in:
Etienne Zink 2022-03-29 19:19:31 +02:00
parent c65102d89a
commit eebb07ec8b
3 changed files with 19 additions and 18 deletions

View File

@ -39,13 +39,13 @@ public class FunNGenerator implements FunNUtilities{
private final String methodName = "apply"; private final String methodName = "apply";
private final int bytecodeVersion = V1_8; private final int bytecodeVersion = V1_8;
private static final String objectSuperType = Type.getInternalName(Object.class).replace('.','/'); private final String objectSuperType = Type.getInternalName(Object.class).replace('.','/');
private static final RefType objectRefType = new RefType(new JavaClassName(objectSuperType), null); private final RefType objectRefType = new RefType(new JavaClassName(objectSuperType), null);
private static final String objectSignature = applySignature(objectRefType); private final String objectSignature = applySignature(objectRefType);
private static String applyDescriptor(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToDescriptor(true)); } private String applyDescriptor(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToDescriptor(true)); }
private static String applySignature(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToSignature(true)); } private 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 applyNameDescriptor(RefTypeOrTPHOrWildcardOrGeneric a){ return a instanceof TypePlaceholder ? "LTPH;" : String.format("L%s;", applyDescriptor(a)); }
@Override @Override
public byte[] generateSuperBytecode(int numberArguments) { public byte[] generateSuperBytecode(int numberArguments) {
@ -86,14 +86,16 @@ public class FunNGenerator implements FunNUtilities{
boolean containsGeneric = false; boolean containsGeneric = false;
String genericSignature = "<"; String genericSignature = "<";
for (int currentTypArgumentIndex = 0; currentTypArgumentIndex < argumentTypes.size(); currentTypArgumentIndex++) { for (RefTypeOrTPHOrWildcardOrGeneric typeArgument : parameters) {
RefTypeOrTPHOrWildcardOrGeneric typArgument = argumentTypes.get(currentTypArgumentIndex); //ToDo Etienne: Refactor
if (typArgument instanceof GenericRefType){ if (typeArgument instanceof GenericRefType){
GenericRefType generic = (GenericRefType) typArgument; GenericRefType generic = (GenericRefType) typeArgument;
if(genericSignature.contains(generic.getParsedName())) continue;
genericSignature += String.format("%s:%s", generic.getParsedName(), applyDescriptor(generic)); genericSignature += String.format("%s:%s", generic.getParsedName(), applyDescriptor(generic));
containsGeneric = true; containsGeneric = true;
} else if(typArgument instanceof TypePlaceholder){ } else if(typeArgument instanceof TypePlaceholder){
TypePlaceholder placeholder = (TypePlaceholder) typArgument; TypePlaceholder placeholder = (TypePlaceholder) typeArgument;
if(genericSignature.contains(applySignature(placeholder).substring(1))) continue;
genericSignature += String.format("%s:%s", applySignature(placeholder).substring(1), objectSignature); genericSignature += String.format("%s:%s", applySignature(placeholder).substring(1), objectSignature);
containsGeneric = true; containsGeneric = true;
} }
@ -109,12 +111,11 @@ public class FunNGenerator implements FunNUtilities{
@Override @Override
public String getSpecializedClassName(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType) { public String getSpecializedClassName(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType) {
if (returnType instanceof TypePlaceholder) returnType = objectRefType;
return String.format("Fun%d$$%s%s", return String.format("Fun%d$$%s%s",
argumentTypes.size(), argumentTypes.size(),
argumentTypes argumentTypes
.stream() .stream()
.map(FunNGenerator::applyNameDescriptor) .map(this::applyNameDescriptor)
.collect(Collectors.joining()), .collect(Collectors.joining()),
applyNameDescriptor(returnType)) applyNameDescriptor(returnType))
.replace('/', '$') .replace('/', '$')

View File

@ -1,6 +1,6 @@
import java.lang.String; import java.lang.String;
//import java.lang.Integer; import java.lang.Integer;
//import java.lang.Double; import java.lang.Double;
import java.util.Vector; import java.util.Vector;
import java.lang.Boolean; import java.lang.Boolean;

View File

@ -1,6 +1,6 @@
import java.lang.String; import java.lang.String;
//import java.lang.Integer; import java.lang.Integer;
//import java.lang.Double; import java.lang.Double;
import java.util.Vector; import java.util.Vector;
import java.lang.Boolean; import java.lang.Boolean;