From 6a2a8313e6cc4c5bd6cddf9a892213b1d73e64f0 Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Thu, 28 Mar 2019 14:07:04 +0100 Subject: [PATCH] Generics werden in Signature richtig dargestellt und die passende Deskriptoren werden richtig erzeugt --- .../descriptor/DescriptorToString.java | 43 ++++++++++++++++--- .../bytecode/signature/Signature.java | 14 +++++- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java b/src/main/java/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java index 07471ae1..d4d6b430 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.bytecode.descriptor; +import java.util.HashMap; import java.util.Iterator; import org.objectweb.asm.Type; @@ -44,17 +45,28 @@ public class DescriptorToString implements DescriptorVisitor{ if(method.hasGen()) { String fpDesc = fp.getType().acceptTV(new TypeToDescriptor()); if(method.getGenericsAndBoundsMethod().containsKey(fpDesc)) { - desc += "L"+method.getGenericsAndBoundsMethod().get(fpDesc)+ ";"; + String bound = getBound(fpDesc, method.getGenericsAndBoundsMethod()); + desc += "L"+bound+ ";"; }else if(method.getGenericsAndBounds().containsKey(fpDesc)){ - desc += "L"+method.getGenericsAndBounds().get(fpDesc)+ ";"; + String bound = getBound(fpDesc, method.getGenericsAndBounds()); + desc += "L"+bound+ ";"; }else { // desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; String resType = resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor()); - if(resType.subSequence(0, 4).equals("TPH ")) { + if(resType.contains("TPH ")/*resType.subSequence(0, 4).equals("TPH ")*/) { // Bound ist immer Object desc += "L"+Type.getInternalName(Object.class)+ ";"; } else { - desc += "L"+resType+ ";"; + // TODO:: + if(method.getGenericsAndBounds().containsKey(resType)) { + String bound = getBound(resType, method.getGenericsAndBounds()); + desc += "L"+bound+ ";"; + }else if(method.getGenericsAndBoundsMethod().containsKey(resType)) { + String bound = getBound(resType, method.getGenericsAndBoundsMethod()); + desc += "L"+bound+ ";"; + } else { + desc += "L"+resType+ ";"; + } } } } @@ -78,11 +90,21 @@ public class DescriptorToString implements DescriptorVisitor{ desc += ")L"+method.getGenericsAndBounds().get(ret)+ ";"; }else { String resType = resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor()); - if(resType.subSequence(0, 4).equals("TPH ")) { + if(resType.contains("TPH ")/*resType.subSequence(0, 4).equals("TPH ")*/) { // desc += ")" + "L"+method.getGenericsAndBoundsMethod().get(resType.substring(4)+"$")+ ";"; desc += ")" + "L"+Type.getInternalName(Object.class)+ ";"; } else { - desc += ")" + "L"+resType+ ";"; + // TODO:: + if(method.getGenericsAndBounds().containsKey(resType)) { + String bound = getBound(resType, method.getGenericsAndBounds()); + desc += ")L"+bound+ ";"; + }else if(method.getGenericsAndBoundsMethod().containsKey(resType)) { + String bound = getBound(resType, method.getGenericsAndBoundsMethod()); + desc += ")L"+bound+ ";"; + } else { + desc += ")L"+resType+ ";"; + } +// desc += ")" + "L"+resType+ ";"; } } }else { @@ -93,6 +115,15 @@ public class DescriptorToString implements DescriptorVisitor{ return desc; } + private String getBound(String fpDesc, HashMap genericsAndBounds) { + String start = genericsAndBounds.get(fpDesc); + while(genericsAndBounds.containsKey(start)) { + start = genericsAndBounds.get(start); + } + + return start; + } + @Override public String visit(NormalConstructor constructor) { String desc = "("; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java b/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java index 4f0fee60..8493e9bb 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java @@ -290,7 +290,8 @@ public class Signature { break; case "GRT": GenericRefType g = (GenericRefType) t; - sv.visitTypeVariable(g.acceptTV(new TypeToSignature()).substring(1)); +// sv.visitTypeVariable(g.acceptTV(new TypeToSignature()).substring(1)); + sv.visitTypeVariable(g.acceptTV(new TypeToSignature())); break; case "TPH": RefTypeOrTPHOrWildcardOrGeneric r = resultSet.resolveType(t).resolvedType; @@ -307,7 +308,16 @@ public class Signature { System.out.println(" Signature FUN$$: "+r); sv.visitInterface().visitClassType(sig2.substring(1, sig2.length())); } else { - sv.visitClassType(sig2.substring(1, sig2.length())); + // Kann zwischen GenericRefType und RefType nicht unterscheiden + // Deswegen wird immer geprüft, ob der Name in Generic Maps liegt + String n = sig2.substring(1, sig2.length()-1); + if(genericsAndBoundsMethod.containsKey(n) || genericsAndBounds.containsKey(n)) { + sv.visitTypeVariable(n); + } else { + sv.visitClassType(n); + sv.visitEnd(); + } +// sv.visitClassType(n); } } else {