diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 7e3bcac5..da114c2c 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -131,7 +131,8 @@ public class BytecodeGen implements ASTVisitor { * Signature looks like: * Superclass */ - if(classOrInterface.getGenerics().iterator().hasNext() || !commonPairs.isEmpty()) { + if(classOrInterface.getGenerics().iterator().hasNext() || !commonPairs.isEmpty() || + classOrInterface.getSuperClass().acceptTV(new TypeToSignature()).contains("<")) { Signature signature = new Signature(classOrInterface, genericsAndBounds,commonPairs); sig = signature.toString(); System.out.println("Signature: => " + sig); @@ -232,13 +233,15 @@ public class BytecodeGen implements ASTVisitor { System.out.println(acc); /*Prüfe, ob die Rückgabe-Type der Methode eine Type-Variable ist*/ - boolean hasGenInParameterList = genericsAndBounds.containsKey(retType) || retType.subSequence(0, 4).equals("TPH "); + boolean hasGenInParameterList = genericsAndBounds.containsKey(retType) || retType.subSequence(0, 4).equals("TPH ") || + resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToSignature()).contains("<"); /*Wenn die Rückgabe-Type eine Typ-variable ist, erzeuge direkt die Signature, wenn nicht, * prüfe, ob einer der Parameter Typ-Variable als Typ hat*/ if(!hasGenInParameterList) { for(String paramName : methodParamsAndTypes.keySet()) { String typeOfParam = methodParamsAndTypes.get(paramName).acceptTV(new TypeToDescriptor()); - if(genericsAndBounds.containsKey(typeOfParam)||typeOfParam.substring(0, 4).equals("TPH ")) { + String sigOfParam = methodParamsAndTypes.get(paramName).acceptTV(new TypeToSignature()); + if(genericsAndBounds.containsKey(typeOfParam)||typeOfParam.substring(0, 4).equals("TPH ")||sigOfParam.contains("<")) { hasGenInParameterList = true; break; } @@ -252,11 +255,9 @@ public class BytecodeGen implements ASTVisitor { /* method.getGenerics: <....> RT method(..) * */ boolean hasGen = method.getGenerics().iterator().hasNext() || hasGenInParameterList; - /* if method has generics or return type is TPH, create signature */ // zwite operand muss weggelassen werden - if(hasGen||method.getReturnType().acceptTV(new TypeToString()).equals("TPH")) { - + if(hasGen||resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToString()).equals("TPH")) { ArrayList pairs = simplifyPairs(method.name,tphExtractor.allPairs); System.out.println(method.name + " => Simplified Pairs: "); pairs.forEach(p->System.out.println(p.TA1.getName() + " -> "+p.TA2.getName())); diff --git a/src/de/dhbwstuttgart/bytecode/signature/Signature.java b/src/de/dhbwstuttgart/bytecode/signature/Signature.java index 8c3fb466..c049cac3 100644 --- a/src/de/dhbwstuttgart/bytecode/signature/Signature.java +++ b/src/de/dhbwstuttgart/bytecode/signature/Signature.java @@ -113,24 +113,28 @@ public class Signature { if(!isConstructor) { String ret = resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToSignature()); System.out.println("RET:::: " + ret); -// if(ret.substring(0,4).equals("TPH ")) { -// String g = ret.substring(4,ret.length())+"$"; - if(genericsAndBounds.containsKey(ret)) { - genericsAndBoundsMethod.put(ret, genericsAndBounds.get(ret)); - }else { - sw.visitFormalTypeParameter(ret); - sw.visitClassBound().visitClassType(Type.getInternalName(Object.class)); - genericsAndBoundsMethod.put(ret, Type.getInternalName(Object.class)); - sw.visitClassBound().visitEnd(); - } -// } - - if(ret.contains("<")) { - RefType ref = (RefType) resultSet.resolveType(method.getReturnType()).resolvedType; - if(hasTPHs(ref)) { - createSignatureForParameterizedType(ref); + if(!ret.equals("V")) { + // TODO TypeToSignature nochmal kontrollieren und schauen ob man dort wirklich + // T... braucht und L ... + if(ret.contains("$") && !ret.contains("$$")) { +// String g = ret.substring(4,ret.length())+"$"; + if(genericsAndBounds.containsKey(ret)) { + genericsAndBoundsMethod.put(ret.substring(1), genericsAndBounds.get(ret.substring(1))); + }else { + sw.visitFormalTypeParameter(ret.substring(1)); + sw.visitClassBound().visitClassType(Type.getInternalName(Object.class)); + genericsAndBoundsMethod.put(ret.substring(1), Type.getInternalName(Object.class)); + sw.visitClassBound().visitEnd(); + } } + if(ret.contains("<")) { + RefType ref = (RefType) resultSet.resolveType(method.getReturnType()).resolvedType; + if(hasTPHs(ref)) { + createSignatureForParameterizedType(ref); + } + + } } } @@ -143,12 +147,12 @@ public class Signature { if(t instanceof TypePlaceholder) { // String gP = pT.substring(4,pT.length())+"$"; String gP = t.acceptTV(new TypeToSignature()); - if(!genericsAndBounds.containsKey(gP) && !genericsAndBoundsMethod.containsKey(gP)) { - sw.visitFormalTypeParameter(gP); + if(!genericsAndBounds.containsKey(gP.substring(1)) && !genericsAndBoundsMethod.containsKey(gP.substring(1))) { + sw.visitFormalTypeParameter(gP.substring(1)); String bound = Type.getInternalName(Object.class); boolean isTypeVar = false; for(GenericInsertPair pair : methodPairs) { - if(pT.substring(0,pT.length()).equals(pair.TA1.getName())) { + if(pT.substring(1,pT.length()-1).equals(pair.TA1.getName())) { bound = pair.TA2.getName()+"$"; isTypeVar = true; break; @@ -162,7 +166,7 @@ public class Signature { sw.visitClassBound().visitEnd(); } - genericsAndBoundsMethod.put(gP, bound); + genericsAndBoundsMethod.put(gP.substring(1), bound); } } @@ -207,7 +211,8 @@ public class Signature { // parameter type deswegen ist true doVisitParamsOrReturn(t,true); } - if(isConstructor) { + if(isConstructor || + resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToSignature()).equals("V")) { sw.visitReturnType().visitBaseType('V'); }else { RefTypeOrTPHOrWildcardOrGeneric returnType = method.getReturnType(); @@ -378,7 +383,7 @@ public class Signature { break; case "GRT": GenericRefType g = (GenericRefType) t; - sv.visitTypeVariable(g.acceptTV(new TypeToSignature())); + sv.visitTypeVariable(g.acceptTV(new TypeToSignature()).substring(1)); break; case "TPH": RefTypeOrTPHOrWildcardOrGeneric r = resultSet.resolveType(t).resolvedType; @@ -448,7 +453,8 @@ public class Signature { } } } - sw.visitSuperclass().visitClassType(classOrInterface.getSuperClass().acceptTV(new TypeToDescriptor()));; + String sClass = classOrInterface.getSuperClass().acceptTV(new TypeToSignature()); + sw.visitSuperclass().visitClassType(sClass.substring(1, sClass.length()-1)); sw.visitEnd(); } /** diff --git a/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java b/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java index 02e2a565..785bfabe 100644 --- a/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java +++ b/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java @@ -15,6 +15,8 @@ public class TypeToSignature implements TypeVisitor { @Override public String visit(RefType refType) { + if(refType.getName().toString().equals("void")) + return "V"; // return refType.toString().replace(".", "/"); String params = ""; if(refType.getParaList().size()>0){ @@ -32,7 +34,7 @@ public class TypeToSignature implements TypeVisitor { // params += "L"+param.toString().replace(".", "/"); // } params += param.acceptTV(new TypeToSignature()); - if(it.hasNext())params += ";"; +// if(it.hasNext())params += ";"; } params += ">"; } diff --git a/test/bytecode/SubMatTest.java b/test/bytecode/SubMatTest.java new file mode 100644 index 00000000..8818bd40 --- /dev/null +++ b/test/bytecode/SubMatTest.java @@ -0,0 +1,27 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; + +import org.junit.Test; + +import de.dhbwstuttgart.core.JavaTXCompiler; + +public class SubMatTest { + private static String path; + private static File fileToTest; + private static JavaTXCompiler compiler; + private static String pathToClassFile; + + @Test + public void test() throws ClassNotFoundException, IOException { + path = System.getProperty("user.dir")+"/test/bytecode/javFiles/SubMatrix.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + compiler.generateBytecode(pathToClassFile); + } + +} diff --git a/test/bytecode/javFiles/OL.jav b/test/bytecode/javFiles/OL.jav index bd02deb4..ea73c2d1 100644 --- a/test/bytecode/javFiles/OL.jav +++ b/test/bytecode/javFiles/OL.jav @@ -2,9 +2,9 @@ import java.lang.Integer; import java.lang.Double; -class OL { +public class OL { - m(java.lang.Integer x) { return x + x; } + m(x) { return x + x; } //m(x) { return x || x; } diff --git a/test/bytecode/javFiles/SubMatrix.jav b/test/bytecode/javFiles/SubMatrix.jav index 4358bbfe..f761321c 100644 --- a/test/bytecode/javFiles/SubMatrix.jav +++ b/test/bytecode/javFiles/SubMatrix.jav @@ -1,3 +1,13 @@ -public class SubMatrix extends Matrix { +import java.util.Vector; +import java.lang.Integer; + +public class Matrix2 extends Vector { +} + +public class SubMatrix extends Matrix2 { + m(){ + Vector v = new Vector(); + v.add(1); + } } \ No newline at end of file