From 672373fc8d5d0cf1179b5c2ba37853d9ed1e8de2 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 10 Jan 2018 12:11:51 +0100 Subject: [PATCH 1/8] --amend --- .../dhbwstuttgart/syntaxtree/type/RefType.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 4cf85297..edd0c49f 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -29,14 +29,16 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric @Override public String toString(){ - String params = "<"; - Iterator it = parameter.iterator(); - while(it.hasNext()){ - RefTypeOrTPHOrWildcardOrGeneric param = it.next(); - params += param.toString(); - if(it.hasNext())params += ", "; + String params = ""; + if(parameter.size()>0){ + Iterator it = parameter.iterator(); + while(it.hasNext()){ + RefTypeOrTPHOrWildcardOrGeneric param = it.next(); + params += param.toString(); + if(it.hasNext())params += ", "; + } + params += ">"; } - params += ">"; return this.name.toString() + params; } From b1b1cfdc54f259d987865f0f9663a88313333323 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 10 Jan 2018 12:14:20 +0100 Subject: [PATCH 2/8] --amend --- src/de/dhbwstuttgart/syntaxtree/type/RefType.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index edd0c49f..3c9a5907 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -31,6 +31,7 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric public String toString(){ String params = ""; if(parameter.size()>0){ + params += "<"; Iterator it = parameter.iterator(); while(it.hasNext()){ RefTypeOrTPHOrWildcardOrGeneric param = it.next(); From 7b24e2d83f0664d71aa0c3bd2067d6b302db783c Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 10 Jan 2018 15:37:50 +0100 Subject: [PATCH 3/8] =?UTF-8?q?bytecode=20f=C3=BCr=20Generics:=20LamAssign?= =?UTF-8?q?Test=20funktioniert=20richtig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 23 ++++---- .../bytecode/BytecodeGenMethod.java | 17 ++++-- src/de/dhbwstuttgart/bytecode/Signature.java | 12 +++-- .../bytecode/TypeToSignature.java | 53 +++++++++++++++++++ test/bytecode/DuMethodTest.java | 7 +++ test/bytecode/Exceptions.jav | 3 ++ test/bytecode/TestIfTest.java | 7 +++ 7 files changed, 101 insertions(+), 21 deletions(-) create mode 100644 src/de/dhbwstuttgart/bytecode/TypeToSignature.java create mode 100644 test/bytecode/DuMethodTest.java create mode 100644 test/bytecode/Exceptions.jav create mode 100644 test/bytecode/TestIfTest.java diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index f1994c20..88bf36c4 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -125,9 +125,13 @@ public class BytecodeGen implements ASTVisitor { String desc = null; boolean hasGen = false; + for(String paramName : methodParamsAndTypes.keySet()) { - genericsAndBounds.containsKey(paramName); - hasGen = true; + String typeOfParam = methodParamsAndTypes.get(paramName).acceptTV(new TypeToDescriptor()); + if(genericsAndBounds.containsKey(typeOfParam)) { + hasGen = true; + break; + } } String sig = null; if(hasGen) { @@ -166,7 +170,8 @@ public class BytecodeGen implements ASTVisitor { boolean hasGenInParameterList = genericsAndBounds.containsKey(method.getReturnType().acceptTV(new TypeToDescriptor())); if(!hasGenInParameterList) { for(String paramName : methodParamsAndTypes.keySet()) { - if(genericsAndBounds.containsKey(paramName)) { + String typeOfParam = methodParamsAndTypes.get(paramName).acceptTV(new TypeToDescriptor()); + if(genericsAndBounds.containsKey(typeOfParam)) { hasGenInParameterList = true; break; } @@ -174,15 +179,9 @@ public class BytecodeGen implements ASTVisitor { } String sig = null; boolean hasGen = method.getGenerics().iterator().hasNext() || hasGenInParameterList; - // Wenn ReturnType has Generics?? Fun1<...> wie testet man das generic hat?? - System.out.println(method.getReturnType().acceptTV(new TypeToString())); -// if(method.getReturnType().acceptTV(new TypeToString()).equals("TPH")) { -// Signature signature = new Signature(method, genericsAndBoundsMethod, methodParamsAndTypes,resultSet); -// sig = signature.toString(); -// System.out.println(sig); -// } - /* if method has generics, create signature */ - if(hasGen) { + + /* if method has generics or return type is TPH, create signature */ + if(hasGen||method.getReturnType().acceptTV(new TypeToString()).equals("TPH")) { // resultset hier zum testen Signature signature = new Signature(method, genericsAndBoundsMethod, methodParamsAndTypes,resultSet); sig = signature.toString(); diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 5368cf93..ae1bf8b1 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -170,7 +170,7 @@ public class BytecodeGenMethod implements StatementVisitor{ this.lamCounter++; System.out.println("Lam Hs Gens: " + lambdaExpression.getGenerics().iterator().hasNext()); - System.out.println("Lam Hs Gens: " + lambdaExpression.getReturnType().acceptTV(new TypeToString())); + System.out.println("Lam Hs Gens: " + resultSet.resolveType(lambdaExpression.getReturnType()).resolvedType.acceptTV(new TypeToSignature())); Lambda lam = new Lambda(lambdaExpression); String lamDesc = lam.accept(new DescriptorToString(resultSet)); //Call site, which, when invoked, returns an instance of the functional interface to which @@ -182,10 +182,19 @@ public class BytecodeGenMethod implements StatementVisitor{ Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory", mt.toMethodDescriptorString(), false); String methodName = "lambda$new$" + this.lamCounter; -// String typeErasure = "(Ljava/lang/Object;)Ljava/lang/Object;"; + + String typeErasure = "("; + Iterator itr = lambdaExpression.params.iterator(); + while(itr.hasNext()) { + itr.next(); + typeErasure += "L"+Type.getInternalName(Object.class) + ";"; + } + + System.out.println("LamReturnType: "+lambdaExpression.getReturnType().acceptTV(new TypeToString())); + typeErasure += ")L"+Type.getInternalName(Object.class) + ";"; // Type erasure -// Type arg1 = Type.getMethodType(typeErasure); - Type arg1 = Type.getMethodType(lamDesc); + Type arg1 = Type.getMethodType(typeErasure); +// Type arg1 = Type.getMethodType(lamDesc); // real Type Type arg3 = Type.getMethodType(lamDesc); diff --git a/src/de/dhbwstuttgart/bytecode/Signature.java b/src/de/dhbwstuttgart/bytecode/Signature.java index 362429a3..1a764afa 100644 --- a/src/de/dhbwstuttgart/bytecode/Signature.java +++ b/src/de/dhbwstuttgart/bytecode/Signature.java @@ -11,6 +11,7 @@ import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; +import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -96,16 +97,17 @@ public class Signature { } switch (type) { case "RT": - sv.visitClassType(t.acceptTV(new TypeToDescriptor())); +// sv.visitClassType(t.acceptTV(new TypeToDescriptor())); + sv.visitClassType(t.acceptTV(new TypeToSignature())); break; case "GRT": GenericRefType g = (GenericRefType) t; - sv.visitTypeVariable(g.getParsedName()); +// sv.visitTypeVariable(g.getParsedName()); + sv.visitTypeVariable(g.acceptTV(new TypeToSignature())); break; case "TPH": - System.out.println(resultSet.resolveType(t).resolvedType.acceptTV(new TypeToDescriptor())); -// sv.visitInterface().visitClassType(resultSet.resolveType(t).resolvedType.acceptTV(new TypeToDescriptor())+";"); - + RefType r = (RefType) resultSet.resolveType(t).resolvedType; + sv.visitInterface().visitClassType(r.acceptTV(new TypeToSignature())); break; default: if(!isParameterType) diff --git a/src/de/dhbwstuttgart/bytecode/TypeToSignature.java b/src/de/dhbwstuttgart/bytecode/TypeToSignature.java new file mode 100644 index 00000000..f40515a1 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/TypeToSignature.java @@ -0,0 +1,53 @@ +package de.dhbwstuttgart.bytecode; + +import java.util.Iterator; + +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; +import de.dhbwstuttgart.syntaxtree.type.GenericRefType; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.type.TypeVisitor; + +public class TypeToSignature implements TypeVisitor { + + @Override + public String visit(RefType refType) { +// return refType.toString().replace(".", "/"); + String params = ""; + if(refType.getParaList().size()>0){ + params += "<"; + Iterator it = refType.getParaList().iterator(); + while(it.hasNext()){ + RefTypeOrTPHOrWildcardOrGeneric param = it.next(); + params += "L"+param.toString().replace(".", "/"); + if(it.hasNext())params += ";"; + } + params += ";>;"; + } + return refType.getName().toString().replace(".", "/") + params; + } + + @Override + public String visit(SuperWildcardType superWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(TypePlaceholder typePlaceholder) { + return typePlaceholder.toString().replace(".", "/"); + } + + @Override + public String visit(ExtendsWildcardType extendsWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(GenericRefType genericRefType) { + return genericRefType.getParsedName().replace(".", "/"); + } + +} diff --git a/test/bytecode/DuMethodTest.java b/test/bytecode/DuMethodTest.java new file mode 100644 index 00000000..a4c8a226 --- /dev/null +++ b/test/bytecode/DuMethodTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class DuMethodTest extends JavaTXCompilerTest{ + public DuMethodTest() { + this.fileName = "DuMethod"; + } +} diff --git a/test/bytecode/Exceptions.jav b/test/bytecode/Exceptions.jav new file mode 100644 index 00000000..9903511c --- /dev/null +++ b/test/bytecode/Exceptions.jav @@ -0,0 +1,3 @@ +public class Exceptions { +// m(Integer i) throws +} \ No newline at end of file diff --git a/test/bytecode/TestIfTest.java b/test/bytecode/TestIfTest.java new file mode 100644 index 00000000..804a0616 --- /dev/null +++ b/test/bytecode/TestIfTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class TestIfTest extends JavaTXCompilerTest{ + public TestIfTest() { + this.fileName = "IfTest"; + } +} From 542f87e8a3c673102f58bb8e1e1b509c1301cac4 Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 17 Jan 2018 13:49:18 +0100 Subject: [PATCH 4/8] =?UTF-8?q?erzeugt=20Klassendatei=20f=C3=BCr=20FunN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 18 ++- .../bytecode/BytecodeGenMethod.java | 70 +++++++- src/de/dhbwstuttgart/bytecode/ClassFile.java | 14 -- src/de/dhbwstuttgart/bytecode/Lambda.java | 1 + .../bytecode/MethodFromMethodCall.java | 1 + .../bytecode/NormalConstructor.java | 1 + .../dhbwstuttgart/bytecode/NormalMethod.java | 1 + src/de/dhbwstuttgart/bytecode/SamMethod.java | 1 + src/de/dhbwstuttgart/bytecode/Test.java | 150 ------------------ src/de/dhbwstuttgart/bytecode/TestClass.java | 8 - src/de/dhbwstuttgart/bytecode/TestFields.java | 59 ------- src/de/dhbwstuttgart/bytecode/TestIf.java | 11 -- src/de/dhbwstuttgart/bytecode/TestMeth.java | 18 --- src/de/dhbwstuttgart/bytecode/TetsF.java | 6 - .../{ => descriptor}/DescriptorToString.java | 8 +- .../{ => descriptor}/DescriptorVisitor.java | 8 +- .../{ => descriptor}/TypeToDescriptor.java | 2 +- .../bytecode/{ => signature}/Signature.java | 38 ++++- .../{ => signature}/TypeToSignature.java | 2 +- .../{ => signature}/TypeToString.java | 2 +- test/bytecode/LamAssign.jav | 6 +- 21 files changed, 134 insertions(+), 291 deletions(-) delete mode 100644 src/de/dhbwstuttgart/bytecode/ClassFile.java delete mode 100644 src/de/dhbwstuttgart/bytecode/Test.java delete mode 100644 src/de/dhbwstuttgart/bytecode/TestClass.java delete mode 100644 src/de/dhbwstuttgart/bytecode/TestFields.java delete mode 100644 src/de/dhbwstuttgart/bytecode/TestIf.java delete mode 100644 src/de/dhbwstuttgart/bytecode/TestMeth.java delete mode 100644 src/de/dhbwstuttgart/bytecode/TetsF.java rename src/de/dhbwstuttgart/bytecode/{ => descriptor}/DescriptorToString.java (95%) rename src/de/dhbwstuttgart/bytecode/{ => descriptor}/DescriptorVisitor.java (50%) rename src/de/dhbwstuttgart/bytecode/{ => descriptor}/TypeToDescriptor.java (95%) rename src/de/dhbwstuttgart/bytecode/{ => signature}/Signature.java (78%) rename src/de/dhbwstuttgart/bytecode/{ => signature}/TypeToSignature.java (97%) rename src/de/dhbwstuttgart/bytecode/{ => signature}/TypeToString.java (95%) diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 88bf36c4..f6f14b4c 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -1,6 +1,5 @@ package de.dhbwstuttgart.bytecode; -import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -9,10 +8,11 @@ import org.objectweb.asm.ClassWriter; import org.objectweb.asm.FieldVisitor; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; -import org.objectweb.asm.Type; -import org.objectweb.asm.signature.SignatureVisitor; -import org.objectweb.asm.signature.SignatureWriter; +import de.dhbwstuttgart.bytecode.descriptor.DescriptorToString; +import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor; +import de.dhbwstuttgart.bytecode.signature.Signature; +import de.dhbwstuttgart.bytecode.signature.TypeToString; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; @@ -56,8 +56,8 @@ public class BytecodeGen implements ASTVisitor { public void visit(SourceFile sourceFile) { for(ClassOrInterface cl : sourceFile.getClasses()) { BytecodeGen classGen = new BytecodeGen(classFiles, resultSet); - cl.accept(classGen); System.out.println("In CLASS: "+(cl.getClassName().toString())); + cl.accept(classGen); classGen.writeClass(cl.getClassName().toString()); } } @@ -83,6 +83,8 @@ public class BytecodeGen implements ASTVisitor { public void visit(ClassOrInterface classOrInterface) { className = classOrInterface.getClassName().toString(); + cw.visitSource(className +".jav", null); + isInterface = (classOrInterface.getModifiers()&512)==512; System.out.println("IS Interface = "+"modifiers= "+classOrInterface.getModifiers()+" ->"+(classOrInterface.getModifiers()&512) + isInterface); @@ -116,7 +118,6 @@ public class BytecodeGen implements ASTVisitor { for(Method m : classOrInterface.getMethods()) { m.accept(this); } - cw.visitSource(classOrInterface.getClassName().toString()+".jav", null); } @Override @@ -146,7 +147,7 @@ public class BytecodeGen implements ASTVisitor { mv.visitCode(); System.out.println("-----Constructor-----"); BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,field, mv,paramsAndLocals,cw, - genericsAndBoundsMethod,genericsAndBounds,isInterface); + genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles); if(!field.getParameterList().iterator().hasNext()) { mv.visitInsn(Opcodes.RETURN); } @@ -196,7 +197,8 @@ public class BytecodeGen implements ASTVisitor { mv.visitCode(); - BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,method, mv,paramsAndLocals,cw,genericsAndBounds,genericsAndBounds,isInterface); + BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,method, mv,paramsAndLocals,cw, + genericsAndBounds,genericsAndBounds,isInterface,classFiles); mv.visitMaxs(0, 0); mv.visitEnd(); } diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index ae1bf8b1..756837b0 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -1,5 +1,9 @@ package de.dhbwstuttgart.bytecode; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import java.lang.invoke.CallSite; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; @@ -14,7 +18,14 @@ import org.objectweb.asm.Handle; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; +import org.objectweb.asm.signature.SignatureVisitor; +import org.objectweb.asm.signature.SignatureWriter; +import de.dhbwstuttgart.bytecode.descriptor.DescriptorToString; +import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor; +import de.dhbwstuttgart.bytecode.signature.Signature; +import de.dhbwstuttgart.bytecode.signature.TypeToSignature; +import de.dhbwstuttgart.bytecode.signature.TypeToString; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.Method; @@ -45,12 +56,13 @@ public class BytecodeGenMethod implements StatementVisitor{ private String where; private boolean isRightSideALambda = false; private KindOfLambda kindOfLambda; + private HashMap classFiles; private ArrayList varsFunInterface; public BytecodeGenMethod(String className,ResultSet resultSet, Method m, MethodVisitor mv, - HashMap paramsAndLocals, ClassWriter cw, - HashMap genericsAndBoundsMethod, HashMap genericsAndBounds, boolean isInterface) { + HashMap paramsAndLocals, ClassWriter cw, HashMap genericsAndBoundsMethod, + HashMap genericsAndBounds, boolean isInterface, HashMap classFiles) { this.where = "<<<<<< NORMAL METHOD >>>>>>"; @@ -63,6 +75,7 @@ public class BytecodeGenMethod implements StatementVisitor{ this.genericsAndBoundsMethod = genericsAndBoundsMethod; this.genericsAndBounds = genericsAndBounds; this.isInterface = isInterface; + this.classFiles = classFiles; this.lamCounter = -1; this.varsFunInterface = new ArrayList<>(); @@ -79,13 +92,14 @@ public class BytecodeGenMethod implements StatementVisitor{ } public BytecodeGenMethod(LambdaExpression lambdaExpression,ResultSet resultSet ,MethodVisitor mv, - int indexOfFirstParamLam, boolean isInterface) { + int indexOfFirstParamLam, boolean isInterface, HashMap classFiles) { System.out.println("\t\t++++++IN LAMBDA -------"); this.where = "<<<<<< LAMBDA METHOD >>>>>>"; this.resultSet = resultSet; this.mv = mv; this.isInterface = isInterface; + this.classFiles = classFiles; this.lamCounter = -1; this.varsFunInterface = new ArrayList<>(); @@ -183,6 +197,8 @@ public class BytecodeGenMethod implements StatementVisitor{ "metafactory", mt.toMethodDescriptorString(), false); String methodName = "lambda$new$" + this.lamCounter; + // Für die Parameter-Typen und Return-Typ braucht man die Bounds (für die Typlöschung) + String typeErasure = "("; Iterator itr = lambdaExpression.params.iterator(); while(itr.hasNext()) { @@ -225,14 +241,60 @@ public class BytecodeGenMethod implements StatementVisitor{ MethodVisitor mvLambdaBody = cw.visitMethod(Opcodes.ACC_PRIVATE+ staticOrInstance + Opcodes.ACC_SYNTHETIC, methodName, arg3.toString(), null, null); - new BytecodeGenMethod(lambdaExpression,this.resultSet,mvLambdaBody,indexOfFirstParamLam,isInterface); + new BytecodeGenMethod(lambdaExpression,this.resultSet,mvLambdaBody,indexOfFirstParamLam,isInterface, + classFiles); mvLambdaBody.visitMaxs(0, 0); mvLambdaBody.visitEnd(); cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL); + + generateBCForFunN(lambdaExpression); } + private void generateBCForFunN(LambdaExpression lambdaExpression) { + ClassWriter classWriter =new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); + + SignatureWriter methSig = new SignatureWriter(); + String methDesc = "("; + int numberOfParams = 0; + SignatureVisitor paramVisitor = methSig.visitParameterType(); + Iterator itr = lambdaExpression.params.iterator(); + while(itr.hasNext()) { + numberOfParams++; + // getBounds + methDesc += "L"+Type.getInternalName(Object.class)+";"; + paramVisitor.visitTypeVariable("T"+numberOfParams); + itr.next(); + } + methSig.visitReturnType().visitTypeVariable("R"); + // ")"+lam.getReturn.getBounds + methDesc += ")L"+Type.getInternalName(Object.class)+";"; + Signature sig = new Signature(lambdaExpression,numberOfParams); + String name = "Fun"+numberOfParams; + classWriter.visit(Opcodes.V1_8, Opcodes.ACC_INTERFACE+Opcodes.ACC_ABSTRACT, name, + sig.toString(), Type.getInternalName(Object.class), null); + MethodVisitor mvApply = classWriter.visitMethod(Opcodes.ACC_PUBLIC+Opcodes.ACC_ABSTRACT, "apply", methDesc, methSig.toString(), null); + mvApply.visitEnd(); + writeClassFile(classWriter.toByteArray(),name); + } + + public void writeClassFile(byte[] bytecode, String name) { + FileOutputStream output; + try { + System.out.println("generating"+name+ ".class file"); + output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" +name+".class")); + output.write(bytecode); + output.close(); + System.out.println(name+".class file generated"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + +} + @Override public void visit(CastExpr castExpr) { // TODO Auto-generated method stub diff --git a/src/de/dhbwstuttgart/bytecode/ClassFile.java b/src/de/dhbwstuttgart/bytecode/ClassFile.java deleted file mode 100644 index fcbad90b..00000000 --- a/src/de/dhbwstuttgart/bytecode/ClassFile.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -public class ClassFile { - - String name; - byte[] bytecode; - - public ClassFile(String name, byte[] bytecode) { - this.name = name; - this.bytecode = bytecode; - } - - -} diff --git a/src/de/dhbwstuttgart/bytecode/Lambda.java b/src/de/dhbwstuttgart/bytecode/Lambda.java index 283b7393..c3b4e8f5 100644 --- a/src/de/dhbwstuttgart/bytecode/Lambda.java +++ b/src/de/dhbwstuttgart/bytecode/Lambda.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.bytecode; +import de.dhbwstuttgart.bytecode.descriptor.DescriptorVisitor; import de.dhbwstuttgart.syntaxtree.ParameterList; import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; diff --git a/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java b/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java index 330b0666..0f781b46 100644 --- a/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java +++ b/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.bytecode; import java.util.HashMap; +import de.dhbwstuttgart.bytecode.descriptor.DescriptorVisitor; import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; diff --git a/src/de/dhbwstuttgart/bytecode/NormalConstructor.java b/src/de/dhbwstuttgart/bytecode/NormalConstructor.java index d2174fd4..a9aa482a 100644 --- a/src/de/dhbwstuttgart/bytecode/NormalConstructor.java +++ b/src/de/dhbwstuttgart/bytecode/NormalConstructor.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.bytecode; import java.util.HashMap; +import de.dhbwstuttgart.bytecode.descriptor.DescriptorVisitor; import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.ParameterList; diff --git a/src/de/dhbwstuttgart/bytecode/NormalMethod.java b/src/de/dhbwstuttgart/bytecode/NormalMethod.java index 3f6bf2b4..b7320479 100644 --- a/src/de/dhbwstuttgart/bytecode/NormalMethod.java +++ b/src/de/dhbwstuttgart/bytecode/NormalMethod.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.bytecode; import java.util.HashMap; +import de.dhbwstuttgart.bytecode.descriptor.DescriptorVisitor; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.ParameterList; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; diff --git a/src/de/dhbwstuttgart/bytecode/SamMethod.java b/src/de/dhbwstuttgart/bytecode/SamMethod.java index 9cf039a2..f14d5407 100644 --- a/src/de/dhbwstuttgart/bytecode/SamMethod.java +++ b/src/de/dhbwstuttgart/bytecode/SamMethod.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.bytecode; import java.util.List; +import de.dhbwstuttgart.bytecode.descriptor.DescriptorVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; public class SamMethod { diff --git a/src/de/dhbwstuttgart/bytecode/Test.java b/src/de/dhbwstuttgart/bytecode/Test.java deleted file mode 100644 index 333ec41f..00000000 --- a/src/de/dhbwstuttgart/bytecode/Test.java +++ /dev/null @@ -1,150 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.lang.invoke.CallSite; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodType; -import java.lang.reflect.Method; -import java.net.URL; -import java.net.URLClassLoader; - -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.Handle; -import org.objectweb.asm.Label; -import org.objectweb.asm.MethodVisitor; -import org.objectweb.asm.Opcodes; -import org.objectweb.asm.Type; - - -public class Test { - private static final String rootDirectory = System.getProperty("user.dir") + "/bin/de/dhbwstuttgart/bytecode/"; - - protected static ClassLoader getClassLoader() throws Exception { - File file = new File(rootDirectory); - URL url = file.toURI().toURL(); - URL[] urls = new URL[] { url }; - System.out.println(urls[0]); - return new URLClassLoader(urls); - } - - public static void main(String[] args) { - // Test Lambda - ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); - cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "TestClass", null, "java/lang/Object", null); - - cw.visitSource("TestClass.java", null); - - // Create Constructor - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", "()V", null, null); - mv.visitVarInsn(Opcodes.ALOAD, 0); - - mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", "()V"); - - // mv.visitMethodInsn(INVOKEDYNAMIC, "#0", "run", "()Ljava/lang/Runnable"); - //Call site, which, when invoked, returns an instance of the functional interface to which - //the lambda is being converted - MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, - MethodType.class, MethodType.class, MethodHandle.class, MethodType.class); - - Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory", - mt.toMethodDescriptorString()); - Handle arg2 = new Handle(Opcodes.H_INVOKESTATIC, "TestClass", "lambda$0", "()V"); - - mv.visitInvokeDynamicInsn("run", "()Ljava/lang/Runnable;", bootstrap, - Type.getMethodType("()V"), arg2, - Type.getMethodType("()V")); - - mv.visitVarInsn(Opcodes.ASTORE, 1); - mv.visitVarInsn(Opcodes.ALOAD, 1); - mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Runnable", "run", "()V"); - mv.visitInsn(Opcodes.RETURN); - - // creates bridge method, contains lambdas body - MethodVisitor mvl = cw.visitMethod(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_SYNTHETIC, "lambda$0", - "()V", null, null); - mvl.visitCode(); - mvl.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); - mvl.visitLdcInsn("lambda"); - mvl.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); - mvl.visitInsn(Opcodes.RETURN); - mvl.visitMaxs(2, 0); - mvl.visitEnd(); - - mv.visitMaxs(1, 2); - mv.visitEnd(); - - - cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", - Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL); - cw.visitEnd(); - - byte[] b = cw.toByteArray(); - - // Test if statement - /* - * ClassWriter cw = new - * ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); - * - * cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC+Opcodes.ACC_SUPER, "TestIf", null, - * "java/lang/Object", null); MethodVisitor mv = - * cw.visitMethod(Opcodes.ACC_PUBLIC, "", "(Ljava/lang/Boolean;)V", null, - * null); mv.visitCode(); - * - * // Label l0 = new Label(); // mv.visitLabel(l0); - * - * mv.visitVarInsn(Opcodes.ALOAD, 0); - * - * mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", - * "()V"); - * - * // Label l1 = new Label(); // mv.visitLabel(l1); - * mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, - * "java/lang/Boolean", "booleanValue", "()Z"); - * - * Label label = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, label); - * - * mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", - * "Ljava/io/PrintStream;"); mv.visitLdcInsn("1"); - * mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", - * "(Ljava/lang/String;)V"); - * - * Label endLabel = new Label(); mv.visitJumpInsn(Opcodes.GOTO, endLabel); - * - * mv.visitLabel(label); mv.visitFieldInsn(Opcodes.GETSTATIC, - * "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("0"); - * mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", - * "(Ljava/lang/String;)V"); - * - * - * - * mv.visitLabel(endLabel); mv.visitInsn(Opcodes.RETURN); - * - * // Label l2 = new Label(); // mv.visitLabel(l2); - * - * // mv.visitLocalVariable("this", "LTestIf;", null, l0, l2, 0); // - * mv.visitLocalVariable("b", "Ljava/lang/Boolean;", null, l0, l2, 1); - * mv.visitMaxs(2, 2); mv.visitEnd(); - * - * cw.visitEnd(); byte[] b = cw.toByteArray(); - */ - FileOutputStream output; - - try { - output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/TestClass.class")); - output.write(b); - output.close(); - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } -} diff --git a/src/de/dhbwstuttgart/bytecode/TestClass.java b/src/de/dhbwstuttgart/bytecode/TestClass.java deleted file mode 100644 index 168447af..00000000 --- a/src/de/dhbwstuttgart/bytecode/TestClass.java +++ /dev/null @@ -1,8 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -public class TestClass { - public TestClass() { - Runnable lam = () -> System.out.println("lambda"); - lam.run(); - } -} diff --git a/src/de/dhbwstuttgart/bytecode/TestFields.java b/src/de/dhbwstuttgart/bytecode/TestFields.java deleted file mode 100644 index 652b8d19..00000000 --- a/src/de/dhbwstuttgart/bytecode/TestFields.java +++ /dev/null @@ -1,59 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; - -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.FieldVisitor; -import org.objectweb.asm.MethodVisitor; -import org.objectweb.asm.Opcodes; -import org.objectweb.asm.Type; - -public class TestFields { - private static final String rootDirectory = System.getProperty("user.dir") + "/bin/de/dhbwstuttgart/bytecode/"; - - public static void main(String[] args) { - // TODO Auto-generated method stub - ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); - cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "TetsF", null, "java/lang/Object", null); - - cw.visitSource("TetsF.java", null); - - FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, "z", Type.INT_TYPE.getDescriptor(), null, null); - fv.visitEnd(); - - FieldVisitor fvS = cw.visitField(Opcodes.ACC_PUBLIC, "s", "Ljava/lang/String;", null, null); - fvS.visitEnd(); - // Create Constructor - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", "()V", null, null); - mv.visitCode(); - - mv.visitVarInsn(Opcodes.ALOAD, 0); - mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", "()V"); - mv.visitVarInsn(Opcodes.ALOAD, 0); - mv.visitLdcInsn(""); - mv.visitFieldInsn(Opcodes.PUTFIELD, "TetsF", "s", "Ljava/lang/String;"); - mv.visitInsn(Opcodes.RETURN); - mv.visitMaxs(2, 1); - mv.visitEnd(); - - byte[] b = cw.toByteArray(); - - FileOutputStream output; - - try { - output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/TetsF.class")); - output.write(b); - output.close(); - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - -} diff --git a/src/de/dhbwstuttgart/bytecode/TestIf.java b/src/de/dhbwstuttgart/bytecode/TestIf.java deleted file mode 100644 index 36e3c706..00000000 --- a/src/de/dhbwstuttgart/bytecode/TestIf.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -public class TestIf { - public TestIf(Boolean b) { - if(b) { - System.out.println("1"); - }else { - System.out.println("0"); - } - } -} diff --git a/src/de/dhbwstuttgart/bytecode/TestMeth.java b/src/de/dhbwstuttgart/bytecode/TestMeth.java deleted file mode 100644 index 51223ea1..00000000 --- a/src/de/dhbwstuttgart/bytecode/TestMeth.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -public class TestMeth { - private int z; - public String s; - public TestMeth(int temp) { - this.z = temp; - } - public void m1(int a, int b) { - int y = m2(1,2,3,4); - } - - public int m2(int a, int b, int x, int y) { - Integer c = 55; - Integer g; - return a+b+y+c; - } -} diff --git a/src/de/dhbwstuttgart/bytecode/TetsF.java b/src/de/dhbwstuttgart/bytecode/TetsF.java deleted file mode 100644 index af72a8d3..00000000 --- a/src/de/dhbwstuttgart/bytecode/TetsF.java +++ /dev/null @@ -1,6 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -public class TetsF { - private int z; - public String s = ""; -} diff --git a/src/de/dhbwstuttgart/bytecode/DescriptorToString.java b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java similarity index 95% rename from src/de/dhbwstuttgart/bytecode/DescriptorToString.java rename to src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java index 481dbc5d..2ed48867 100644 --- a/src/de/dhbwstuttgart/bytecode/DescriptorToString.java +++ b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java @@ -1,7 +1,12 @@ -package de.dhbwstuttgart.bytecode; +package de.dhbwstuttgart.bytecode.descriptor; import java.util.Iterator; +import de.dhbwstuttgart.bytecode.Lambda; +import de.dhbwstuttgart.bytecode.MethodFromMethodCall; +import de.dhbwstuttgart.bytecode.NormalConstructor; +import de.dhbwstuttgart.bytecode.NormalMethod; +import de.dhbwstuttgart.bytecode.SamMethod; import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.statement.Expression; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; @@ -96,7 +101,6 @@ public class DescriptorToString implements DescriptorVisitor{ FormalParameter fp = itr.next(); desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor()) + ";"; } - System.out.println("LamReturnType: "+lambdaExpression.getReturnType().acceptTV(new TypeToString())); desc = addReturnType(desc, lambdaExpression.getReturnType(), resultSet); return desc; } diff --git a/src/de/dhbwstuttgart/bytecode/DescriptorVisitor.java b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorVisitor.java similarity index 50% rename from src/de/dhbwstuttgart/bytecode/DescriptorVisitor.java rename to src/de/dhbwstuttgart/bytecode/descriptor/DescriptorVisitor.java index 63198828..6bc57f21 100644 --- a/src/de/dhbwstuttgart/bytecode/DescriptorVisitor.java +++ b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorVisitor.java @@ -1,4 +1,10 @@ -package de.dhbwstuttgart.bytecode; +package de.dhbwstuttgart.bytecode.descriptor; + +import de.dhbwstuttgart.bytecode.Lambda; +import de.dhbwstuttgart.bytecode.MethodFromMethodCall; +import de.dhbwstuttgart.bytecode.NormalConstructor; +import de.dhbwstuttgart.bytecode.NormalMethod; +import de.dhbwstuttgart.bytecode.SamMethod; public interface DescriptorVisitor { public String visit(NormalMethod method); diff --git a/src/de/dhbwstuttgart/bytecode/TypeToDescriptor.java b/src/de/dhbwstuttgart/bytecode/descriptor/TypeToDescriptor.java similarity index 95% rename from src/de/dhbwstuttgart/bytecode/TypeToDescriptor.java rename to src/de/dhbwstuttgart/bytecode/descriptor/TypeToDescriptor.java index 5271e63d..e374bb93 100644 --- a/src/de/dhbwstuttgart/bytecode/TypeToDescriptor.java +++ b/src/de/dhbwstuttgart/bytecode/descriptor/TypeToDescriptor.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.bytecode; +package de.dhbwstuttgart.bytecode.descriptor; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; diff --git a/src/de/dhbwstuttgart/bytecode/Signature.java b/src/de/dhbwstuttgart/bytecode/signature/Signature.java similarity index 78% rename from src/de/dhbwstuttgart/bytecode/Signature.java rename to src/de/dhbwstuttgart/bytecode/signature/Signature.java index 1a764afa..1c208b85 100644 --- a/src/de/dhbwstuttgart/bytecode/Signature.java +++ b/src/de/dhbwstuttgart/bytecode/signature/Signature.java @@ -1,15 +1,19 @@ -package de.dhbwstuttgart.bytecode; +package de.dhbwstuttgart.bytecode.signature; import java.util.HashMap; import java.util.Iterator; +import org.objectweb.asm.Type; import org.objectweb.asm.signature.SignatureVisitor; import org.objectweb.asm.signature.SignatureWriter; +import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.Constructor; +import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; @@ -50,6 +54,31 @@ public class Signature { createSignatureForConsOrMethod(this.method,false); } + public Signature(LambdaExpression lambdaExpression,int numberOfParams) { + sw = new SignatureWriter(); + createSignatureForFunN(lambdaExpression, numberOfParams); + } + + private void createSignatureForFunN(LambdaExpression lambdaExpression, int numberOfParams) { + +// sw.visitReturnType().visitTypeVariable("R"); + sw.visitFormalTypeParameter("R"); + // getBounds vom Return-Type + sw.visitClassBound().visitClassType(Type.getInternalName(Object.class)); + sw.visitClassBound().visitEnd(); + for(int i = 0;i extends java.lang.Object OR ... sw.visitClassBound().visitClassType(boundDesc); genAndBounds.put(g.getParsedName(), boundDesc); diff --git a/src/de/dhbwstuttgart/bytecode/TypeToSignature.java b/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java similarity index 97% rename from src/de/dhbwstuttgart/bytecode/TypeToSignature.java rename to src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java index f40515a1..bbc19c37 100644 --- a/src/de/dhbwstuttgart/bytecode/TypeToSignature.java +++ b/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.bytecode; +package de.dhbwstuttgart.bytecode.signature; import java.util.Iterator; diff --git a/src/de/dhbwstuttgart/bytecode/TypeToString.java b/src/de/dhbwstuttgart/bytecode/signature/TypeToString.java similarity index 95% rename from src/de/dhbwstuttgart/bytecode/TypeToString.java rename to src/de/dhbwstuttgart/bytecode/signature/TypeToString.java index 86d4124a..94314afe 100644 --- a/src/de/dhbwstuttgart/bytecode/TypeToString.java +++ b/src/de/dhbwstuttgart/bytecode/signature/TypeToString.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.bytecode; +package de.dhbwstuttgart.bytecode.signature; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; diff --git a/test/bytecode/LamAssign.jav b/test/bytecode/LamAssign.jav index 3df81780..8f42d5ea 100644 --- a/test/bytecode/LamAssign.jav +++ b/test/bytecode/LamAssign.jav @@ -8,6 +8,6 @@ class LamAssign { } } -interface Fun1{ - A apply(B b); -} +//interface Fun1{ +// A apply(B b); +//} From 9a94ba2114e2bfa369c91fddf09b8a143ac4a521 Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 31 Jan 2018 14:16:20 +0100 Subject: [PATCH 5/8] =?UTF-8?q?unn=C3=B6tige=20Kommentare=20wurden=20entfe?= =?UTF-8?q?rnt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 26 ++---- .../bytecode/BytecodeGenMethod.java | 83 ++++++------------- .../descriptor/DescriptorToString.java | 12 ++- .../bytecode/signature/Signature.java | 6 +- test/bytecode/ATest.java | 8 ++ test/bytecode/DuMethod.jav | 2 +- test/bytecode/Example.jav | 7 ++ test/bytecode/Import.jav | 8 ++ test/bytecode/ImportTest.java | 7 ++ test/bytecode/JavaTXCompilerTest.java | 13 ++- test/bytecode/LamAssign.jav | 4 - test/bytecode/OverlaodGen.jav | 11 +++ test/bytecode/OverlaodGenTest.java | 7 ++ 13 files changed, 100 insertions(+), 94 deletions(-) create mode 100644 test/bytecode/ATest.java create mode 100644 test/bytecode/Example.jav create mode 100644 test/bytecode/Import.jav create mode 100644 test/bytecode/ImportTest.java create mode 100644 test/bytecode/OverlaodGen.jav create mode 100644 test/bytecode/OverlaodGenTest.java diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index f6f14b4c..25c06024 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -56,7 +56,6 @@ public class BytecodeGen implements ASTVisitor { public void visit(SourceFile sourceFile) { for(ClassOrInterface cl : sourceFile.getClasses()) { BytecodeGen classGen = new BytecodeGen(classFiles, resultSet); - System.out.println("In CLASS: "+(cl.getClassName().toString())); cl.accept(classGen); classGen.writeClass(cl.getClassName().toString()); } @@ -86,8 +85,7 @@ public class BytecodeGen implements ASTVisitor { cw.visitSource(className +".jav", null); isInterface = (classOrInterface.getModifiers()&512)==512; - System.out.println("IS Interface = "+"modifiers= "+classOrInterface.getModifiers()+" ->"+(classOrInterface.getModifiers()&512) + isInterface); - + int acc = isInterface?classOrInterface.getModifiers()+Opcodes.ACC_ABSTRACT:classOrInterface.getModifiers()+Opcodes.ACC_SUPER; String sig = null; /* if class has generics then creates signature @@ -96,8 +94,6 @@ public class BytecodeGen implements ASTVisitor { */ if(classOrInterface.getGenerics().iterator().hasNext()) { Signature signature = new Signature(classOrInterface, genericsAndBounds); - - System.out.println(signature.toString()); sig = signature.toString(); } // needs implemented Interfaces? @@ -106,8 +102,6 @@ public class BytecodeGen implements ASTVisitor { // for each field in the class for(Field f : classOrInterface.getFieldDecl()) { - System.out.println("get Fields"); - System.out.println(f.getName()); f.accept(this); } @@ -136,16 +130,13 @@ public class BytecodeGen implements ASTVisitor { } String sig = null; if(hasGen) { - System.out.println("IM IN CONST HAS Gens"); Signature signature = new Signature(field, genericsAndBounds,methodParamsAndTypes); sig = signature.toString(); - System.out.println(sig); } NormalConstructor constructor = new NormalConstructor(field,genericsAndBounds,hasGen); desc = constructor.accept(new DescriptorToString(resultSet)); MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", desc, sig, null); mv.visitCode(); - System.out.println("-----Constructor-----"); BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,field, mv,paramsAndLocals,cw, genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles); if(!field.getParameterList().iterator().hasNext()) { @@ -166,9 +157,7 @@ public class BytecodeGen implements ASTVisitor { // Method getModifiers() ? int acc = isInterface?Opcodes.ACC_ABSTRACT:0; - System.out.println("-----Method-----"); - - boolean hasGenInParameterList = genericsAndBounds.containsKey(method.getReturnType().acceptTV(new TypeToDescriptor())); + boolean hasGenInParameterList = genericsAndBounds.containsKey(resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor())); if(!hasGenInParameterList) { for(String paramName : methodParamsAndTypes.keySet()) { String typeOfParam = methodParamsAndTypes.get(paramName).acceptTV(new TypeToDescriptor()); @@ -178,6 +167,10 @@ public class BytecodeGen implements ASTVisitor { } } } + + //TODO: Test if the return-type or any of the parameter is a parameterized type. (VP) + //than create the descriptor with the new syntax. + String sig = null; boolean hasGen = method.getGenerics().iterator().hasNext() || hasGenInParameterList; @@ -186,13 +179,10 @@ public class BytecodeGen implements ASTVisitor { // resultset hier zum testen Signature signature = new Signature(method, genericsAndBoundsMethod, methodParamsAndTypes,resultSet); sig = signature.toString(); - System.out.println(sig); - } NormalMethod meth = new NormalMethod(method,genericsAndBounds,genericsAndBoundsMethod,hasGen); methDesc = meth.accept(new DescriptorToString(resultSet)); - System.out.println("methDesc" + methDesc); MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC+acc, method.getName(), methDesc, sig, null); mv.visitCode(); @@ -212,7 +202,7 @@ public class BytecodeGen implements ASTVisitor { while(itr.hasNext()) { FormalParameter fp = itr.next(); paramsAndLocals.put(fp.getName(), i); - methodParamsAndTypes.put(fp.getName(), fp.getType()); + methodParamsAndTypes.put(fp.getName(), resultSet.resolveType(fp.getType()).resolvedType); fp.accept(this); i++; } @@ -255,7 +245,6 @@ public class BytecodeGen implements ASTVisitor { // ?? @Override public void visit(FieldVar fieldVar) { - System.out.println("in fieldvar"); // cw.newField(fieldVar.receiver.toString(), fieldVar.fieldVarName.toString(), fieldVar.getType().toString()); FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, fieldVar.fieldVarName, "L"+fieldVar.getType()+";", null, null); fv.visitEnd(); @@ -264,7 +253,6 @@ public class BytecodeGen implements ASTVisitor { // access flages?? modifiers @Override public void visit(Field field) { - System.out.println("in field"); FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, field.getName(), "L"+field.getType().toString().replace(".", "/")+";", null, null); fv.visitEnd(); } diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 756837b0..c19315e8 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -32,7 +32,6 @@ import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; -import de.dhbwstuttgart.syntaxtree.type.FunN; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -53,7 +52,7 @@ public class BytecodeGenMethod implements StatementVisitor{ private String fieldName; private String fieldDesc; private Expression rightSideTemp; - private String where; +// private String where; private boolean isRightSideALambda = false; private KindOfLambda kindOfLambda; private HashMap classFiles; @@ -64,7 +63,7 @@ public class BytecodeGenMethod implements StatementVisitor{ HashMap paramsAndLocals, ClassWriter cw, HashMap genericsAndBoundsMethod, HashMap genericsAndBounds, boolean isInterface, HashMap classFiles) { - this.where = "<<<<<< NORMAL METHOD >>>>>>"; +// this.where = "<<<<<< NORMAL METHOD >>>>>>"; this.className = className; this.resultSet = resultSet; @@ -79,23 +78,16 @@ public class BytecodeGenMethod implements StatementVisitor{ this.lamCounter = -1; this.varsFunInterface = new ArrayList<>(); - System.out.println("PARAMS = "+this.paramsAndLocals.size()); if(!isInterface) this.m.block.accept(this); -// System.out.println("PARAMS = "+this.paramsAndLocals.size()); -// for(int i = 0; i classFiles) { - System.out.println("\t\t++++++IN LAMBDA -------"); - this.where = "<<<<<< LAMBDA METHOD >>>>>>"; +// this.where = "<<<<<< LAMBDA METHOD >>>>>>"; this.resultSet = resultSet; this.mv = mv; this.isInterface = isInterface; @@ -114,7 +106,6 @@ public class BytecodeGenMethod implements StatementVisitor{ } private String getResolvedType(RefTypeOrTPHOrWildcardOrGeneric type) { -// return resultSet.resolveType(type).resolvedType.toString().replace(".", "/"); return resultSet.resolveType(type).resolvedType.acceptTV(new TypeToDescriptor()); } @@ -122,10 +113,8 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(Block block) { for(Statement stmt : block.getStatements()) { - System.out.println(where); - System.out.println("Stmt : " + stmt.toString()); +// System.out.println(where); stmt.accept(this); - System.out.println("--------------------------\n"); } } @@ -140,7 +129,7 @@ public class BytecodeGenMethod implements StatementVisitor{ // ?? @Override public void visit(LocalVar localVar) { - System.out.println("in Local Var: " + localVar.name); +// System.out.println("in Local Var: " + localVar.name); mv.visitVarInsn(Opcodes.ALOAD, paramsAndLocals.get(localVar.name)); } // ?? @@ -148,12 +137,12 @@ public class BytecodeGenMethod implements StatementVisitor{ public void visit(LocalVarDecl localVarDecl) { // Integer i; // paramsAndLocals.put(localVarDecl.getName(), paramsAndLocals.size()+1); - System.out.println("In localVarDecl :: "+localVarDecl.getName()); +// System.out.println("In localVarDecl :: "+localVarDecl.getName()); } @Override public void visit(Assign assign) { - System.out.println("Assign : \nright = "+assign.rightSide + "\nLeft = " + assign.lefSide); +// System.out.println("Assign : \nright = "+assign.rightSide + "\nLeft = " + assign.lefSide); // if the right side is a lambda => the left side must be a functional interface if(assign.rightSide.getClass().equals(LambdaExpression.class)) { @@ -162,7 +151,6 @@ public class BytecodeGenMethod implements StatementVisitor{ isRightSideALambda = false; } - System.out.println("\t isRight Side lambda: " + isRightSideALambda); if(assign.lefSide.getClass().equals(AssignToField.class)) { // load_0, ldc or .. then putfield this.rightSideTemp = assign.rightSide; @@ -175,16 +163,13 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(Binary binary) { - System.out.println("\t++ In Binary: "); + } @Override public void visit(LambdaExpression lambdaExpression) { - System.out.println("\n++ In Lambda: "); this.lamCounter++; - System.out.println("Lam Hs Gens: " + lambdaExpression.getGenerics().iterator().hasNext()); - System.out.println("Lam Hs Gens: " + resultSet.resolveType(lambdaExpression.getReturnType()).resolvedType.acceptTV(new TypeToSignature())); Lambda lam = new Lambda(lambdaExpression); String lamDesc = lam.accept(new DescriptorToString(resultSet)); //Call site, which, when invoked, returns an instance of the functional interface to which @@ -206,7 +191,6 @@ public class BytecodeGenMethod implements StatementVisitor{ typeErasure += "L"+Type.getInternalName(Object.class) + ";"; } - System.out.println("LamReturnType: "+lambdaExpression.getReturnType().acceptTV(new TypeToString())); typeErasure += ")L"+Type.getInternalName(Object.class) + ";"; // Type erasure Type arg1 = Type.getMethodType(typeErasure); @@ -235,8 +219,7 @@ public class BytecodeGenMethod implements StatementVisitor{ SamMethod samMethod = new SamMethod(kindOfLambda.getArgumentList(), lambdaExpression.getType()); // Desc: (this/nothing)TargetType String fiMethodDesc = samMethod.accept(new DescriptorToString(resultSet)); - mv.visitInvokeDynamicInsn("apply", fiMethodDesc, bootstrap, - arg1, arg2,arg3); + mv.visitInvokeDynamicInsn("apply", fiMethodDesc, bootstrap, arg1, arg2,arg3); MethodVisitor mvLambdaBody = cw.visitMethod(Opcodes.ACC_PRIVATE+ staticOrInstance + Opcodes.ACC_SYNTHETIC, methodName, arg3.toString(), null, null); @@ -249,32 +232,31 @@ public class BytecodeGenMethod implements StatementVisitor{ cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL); - generateBCForFunN(lambdaExpression); + generateBCForFunN(lambdaExpression,typeErasure); } - private void generateBCForFunN(LambdaExpression lambdaExpression) { + private void generateBCForFunN(LambdaExpression lambdaExpression, String methDesc) { ClassWriter classWriter =new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); SignatureWriter methSig = new SignatureWriter(); - String methDesc = "("; + int numberOfParams = 0; SignatureVisitor paramVisitor = methSig.visitParameterType(); Iterator itr = lambdaExpression.params.iterator(); while(itr.hasNext()) { numberOfParams++; // getBounds - methDesc += "L"+Type.getInternalName(Object.class)+";"; paramVisitor.visitTypeVariable("T"+numberOfParams); itr.next(); } methSig.visitReturnType().visitTypeVariable("R"); // ")"+lam.getReturn.getBounds - methDesc += ")L"+Type.getInternalName(Object.class)+";"; Signature sig = new Signature(lambdaExpression,numberOfParams); String name = "Fun"+numberOfParams; classWriter.visit(Opcodes.V1_8, Opcodes.ACC_INTERFACE+Opcodes.ACC_ABSTRACT, name, sig.toString(), Type.getInternalName(Object.class), null); - MethodVisitor mvApply = classWriter.visitMethod(Opcodes.ACC_PUBLIC+Opcodes.ACC_ABSTRACT, "apply", methDesc, methSig.toString(), null); + MethodVisitor mvApply = classWriter.visitMethod(Opcodes.ACC_PUBLIC+Opcodes.ACC_ABSTRACT, "apply", + methDesc, methSig.toString(), null); mvApply.visitEnd(); writeClassFile(classWriter.toByteArray(),name); } @@ -282,8 +264,8 @@ public class BytecodeGenMethod implements StatementVisitor{ public void writeClassFile(byte[] bytecode, String name) { FileOutputStream output; try { - System.out.println("generating"+name+ ".class file"); - output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" +name+".class")); + System.out.println("generating "+name+ ".class file..."); + output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/examples/" +name+".class")); output.write(bytecode); output.close(); System.out.println(name+".class file generated"); @@ -309,7 +291,6 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(FieldVar fieldVar) { - System.out.println("in fieldVar " + fieldVar.fieldVarName + " ** receiver: "+fieldVar.receiver); fieldName = fieldVar.fieldVarName; fieldDesc = "L"+getResolvedType(fieldVar.getType())+";"; @@ -333,7 +314,7 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(IfStmt ifStmt) { - System.out.println("++ IF-Statment: "); + } @Override @@ -344,13 +325,11 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(MethodCall methodCall) { - System.out.println(" In Methodcall: (" +methodCall.name+")" ); - System.out.print("\t\tMethod-Receiver: "); - if(methodCall.receiver instanceof ExpressionReceiver){ - System.out.print(((ExpressionReceiver) methodCall.receiver).expr + "\n"); - }else{ - System.out.print(((StaticClassName) methodCall.receiver).getType().toString() + "\n"); - } +// if(methodCall.receiver instanceof ExpressionReceiver){ +// System.out.print(((ExpressionReceiver) methodCall.receiver).expr + "\n"); +// }else{ +// System.out.print(((StaticClassName) methodCall.receiver).getType().toString() + "\n"); +// } methodCall.receiver.accept(this); methodCall.arglist.accept(this); @@ -359,14 +338,13 @@ public class BytecodeGenMethod implements StatementVisitor{ genericsAndBoundsMethod,genericsAndBounds); String mDesc = method.accept(new DescriptorToString(resultSet)); - System.out.println("is Vars empty: "+varsFunInterface.isEmpty()); +// System.out.println("is Vars empty: "+varsFunInterface.isEmpty()); // is methodCall.receiver functional Interface)? if(varsFunInterface.contains(methodCall.receiver.getType())) { mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, getResolvedType(methodCall.receiver.getType()), methodCall.name, mDesc, false); }else { - System.out.println("mDesc = " + mDesc); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getResolvedType(methodCall.receiver.getType()), methodCall.name, mDesc, isInterface); } @@ -378,8 +356,8 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(NewClass methodCall) { - System.out.println("In NewClass: "); - System.out.println("\t\tname: " + methodCall.name + " *** " + "Receiver: " + methodCall.receiver); +// System.out.println("In NewClass: "); +// System.out.println("\t\tname: " + methodCall.name + " *** " + "Receiver: " + methodCall.receiver); mv.visitTypeInsn(Opcodes.NEW, methodCall.name.replace(".", "/")); mv.visitInsn(Opcodes.DUP); @@ -402,8 +380,6 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(ExpressionReceiver receiver) { - System.out.println(" in Receiver"); - System.out.println(" expr : " + receiver.expr); receiver.expr.accept(this); } @@ -420,7 +396,6 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(StaticClassName staticClassName) { - System.out.println("In StaticClassName: "); // mv.visitMethodInsn(Opcodes.INVOKESTATIC, staticClassName.getType().toString().replace(".", "/"), // staticClassName.toString(), staticClassName.getType().toString(), false); mv.visitFieldInsn(Opcodes.GETSTATIC, getResolvedType(staticClassName.getType()), @@ -429,18 +404,17 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(Super aSuper) { - System.out.println(">> In Super: "); + } @Override public void visit(This aThis) { - System.out.println("-> IN This"); mv.visitVarInsn(Opcodes.ALOAD, 0); } @Override public void visit(UnaryPlus unaryPlus) { - System.out.println("++ In UnaryPlus: "); + } @Override @@ -468,11 +442,9 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(ArgumentList argumentList) { - System.out.println("in ArgumentList: "); for(Expression al : argumentList.getArguments()) { al.accept(this); } - System.out.println("out from Argumentlist"); } @Override @@ -491,7 +463,6 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(AssignToLocal assignLeftSide) { - System.out.println("In Assign To Local: "); if(isRightSideALambda) varsFunInterface.add(assignLeftSide.localVar.getType()); paramsAndLocals.put(assignLeftSide.localVar.name, paramsAndLocals.size()+1); diff --git a/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java index 2ed48867..c7c623b6 100644 --- a/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java +++ b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java @@ -7,8 +7,10 @@ import de.dhbwstuttgart.bytecode.MethodFromMethodCall; import de.dhbwstuttgart.bytecode.NormalConstructor; import de.dhbwstuttgart.bytecode.NormalMethod; import de.dhbwstuttgart.bytecode.SamMethod; +import de.dhbwstuttgart.bytecode.signature.TypeToSignature; import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -35,6 +37,8 @@ public class DescriptorToString implements DescriptorVisitor{ Iterator itr = method.getParameterList().iterator(); while(itr.hasNext()) { FormalParameter fp = itr.next(); +// System.out.println(resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToSignature())); +// System.out.println("Parmetrisierte typ ? "+ ((RefType) fp.getType()).getParaList().size()); if(method.hasGen()) { String fpDesc = fp.getType().acceptTV(new TypeToDescriptor()); if(method.getGenericsAndBoundsMethod().containsKey(fpDesc)) { @@ -44,6 +48,8 @@ public class DescriptorToString implements DescriptorVisitor{ }else { desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } + }else if(((RefType) fp.getType()).getParaList().size() > 0){ + desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.toString().replace(".", "%").replace("<", "%%").replace(">", "%%")+ ";"; }else { desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } @@ -76,16 +82,16 @@ public class DescriptorToString implements DescriptorVisitor{ while(itr.hasNext()) { FormalParameter fp = itr.next(); if(constructor.hasGen()) { - System.out.println("Cons has Gens"); +// System.out.println("Cons has Gens"); String fpDesc = fp.getType().acceptTV(new TypeToDescriptor()); - System.out.println(fpDesc); +// System.out.println(fpDesc); if(constructor.getGenericsAndBounds().containsKey(fpDesc)){ desc += "L"+constructor.getGenericsAndBounds().get(fpDesc)+ ";"; }else { desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } }else { - System.out.println("Cons has NOT Gens"); +// System.out.println("Cons has NOT Gens"); desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } } diff --git a/src/de/dhbwstuttgart/bytecode/signature/Signature.java b/src/de/dhbwstuttgart/bytecode/signature/Signature.java index 1c208b85..fc9e2191 100644 --- a/src/de/dhbwstuttgart/bytecode/signature/Signature.java +++ b/src/de/dhbwstuttgart/bytecode/signature/Signature.java @@ -61,7 +61,6 @@ public class Signature { private void createSignatureForFunN(LambdaExpression lambdaExpression, int numberOfParams) { -// sw.visitReturnType().visitTypeVariable("R"); sw.visitFormalTypeParameter("R"); // getBounds vom Return-Type sw.visitClassBound().visitClassType(Type.getInternalName(Object.class)); @@ -73,7 +72,6 @@ public class Signature { sw.visitClassBound().visitClassType(Type.getInternalName(Object.class)); sw.visitClassBound().visitEnd(); } -// sw.visitClassBound().visitEnd(); // TODO: prüfe ob Return-Type = void, sw.visitSuperclass().visitClassType(Type.getInternalName(Object.class));; sw.visitEnd(); @@ -136,7 +134,7 @@ public class Signature { RefTypeOrTPHOrWildcardOrGeneric r = resultSet.resolveType(t).resolvedType; sv.visitInterface().visitClassType(r.acceptTV(new TypeToSignature())); // sv.visitClassType(r.acceptTV(new TypeToSignature())); - System.out.println(r.getClass()+" Signature TPH: "+r.acceptTV(new TypeToSignature())); +// System.out.println(r.getClass()+" Signature TPH: "+r.acceptTV(new TypeToSignature())); break; default: if(!isParameterType) @@ -172,7 +170,7 @@ public class Signature { while(bItr.hasNext()) { RefTypeOrTPHOrWildcardOrGeneric b =bItr.next(); String boundDesc = b.acceptTV(new TypeToDescriptor()); - System.out.println("GetBounds: " + boundDesc); +// System.out.println("GetBounds: " + boundDesc); // Ensure that <...> extends java.lang.Object OR ... sw.visitClassBound().visitClassType(boundDesc); genAndBounds.put(g.getParsedName(), boundDesc); diff --git a/test/bytecode/ATest.java b/test/bytecode/ATest.java new file mode 100644 index 00000000..9865825e --- /dev/null +++ b/test/bytecode/ATest.java @@ -0,0 +1,8 @@ +package bytecode; + +public class ATest extends JavaTXCompilerTest { + public ATest() { + fileName = "A"; + } + +} diff --git a/test/bytecode/DuMethod.jav b/test/bytecode/DuMethod.jav index b56f6c55..3898a7ba 100644 --- a/test/bytecode/DuMethod.jav +++ b/test/bytecode/DuMethod.jav @@ -1,7 +1,7 @@ public class DuMethod{ method(a){ - return a; + return a+a; } method(a){ diff --git a/test/bytecode/Example.jav b/test/bytecode/Example.jav new file mode 100644 index 00000000..451803ea --- /dev/null +++ b/test/bytecode/Example.jav @@ -0,0 +1,7 @@ +public class Example { + + public m() { + String x = "X"; + return x; + } +} \ No newline at end of file diff --git a/test/bytecode/Import.jav b/test/bytecode/Import.jav new file mode 100644 index 00000000..c658568f --- /dev/null +++ b/test/bytecode/Import.jav @@ -0,0 +1,8 @@ +import java.util.Vector; + +class Import { + void methode(){ + Vector v = new Vector<>(); + v.add("X"); + } +} \ No newline at end of file diff --git a/test/bytecode/ImportTest.java b/test/bytecode/ImportTest.java new file mode 100644 index 00000000..b7244e15 --- /dev/null +++ b/test/bytecode/ImportTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class ImportTest extends JavaTXCompilerTest{ + public ImportTest() { + this.fileName = "Import"; + } +} diff --git a/test/bytecode/JavaTXCompilerTest.java b/test/bytecode/JavaTXCompilerTest.java index f017e23e..2f832962 100644 --- a/test/bytecode/JavaTXCompilerTest.java +++ b/test/bytecode/JavaTXCompilerTest.java @@ -34,17 +34,16 @@ public class JavaTXCompilerTest { filesToTest.add(new File(rootDirectory+fileName+".jav")); System.out.println(rootDirectory+fileName+".jav"); JavaTXCompiler compiler = new JavaTXCompiler(filesToTest); - System.out.println("test"); for(File f : filesToTest){ String content = readFile(f.getPath(), StandardCharsets.UTF_8); List typeinferenceResult = compiler.typeInference(); HashMap bytecode = this.getBytecode(compiler.sourceFiles.get(f), typeinferenceResult.get(0)); - for(ResultPair ep : typeinferenceResult.get(0).results) { - System.out.println(ep.getLeft() + " ->" + ep.getRight()); - } +// for(ResultPair ep : typeinferenceResult.get(0).results) { +// System.out.println(ep.getLeft() + " ->" + ep.getRight()); +// } - String name = ""; + String name; int pos = f.getName().lastIndexOf("."); if(pos != -1) { name = f.getName().substring(0, pos); @@ -67,8 +66,8 @@ public class JavaTXCompilerTest { for(String name : classFiles.keySet()) { byte[] bytecode = classFiles.get(name); try { - System.out.println("generating"+name+ ".class file"); - output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" +name+".class")); + System.out.println("generating "+name+ ".class file ..."); + output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/examples/" +name+".class")); output.write(bytecode); output.close(); System.out.println(name+".class file generated"); diff --git a/test/bytecode/LamAssign.jav b/test/bytecode/LamAssign.jav index 8f42d5ea..e522bd3b 100644 --- a/test/bytecode/LamAssign.jav +++ b/test/bytecode/LamAssign.jav @@ -7,7 +7,3 @@ class LamAssign { return lam1; } } - -//interface Fun1{ -// A apply(B b); -//} diff --git a/test/bytecode/OverlaodGen.jav b/test/bytecode/OverlaodGen.jav new file mode 100644 index 00000000..d5946496 --- /dev/null +++ b/test/bytecode/OverlaodGen.jav @@ -0,0 +1,11 @@ +import java.util.Vector; + +class OverlaodGen { + void method(Vector v) { +// Integer i = v.get(0); + } + + void method(Vector v) { +// String s = v.get(0); + } +} \ No newline at end of file diff --git a/test/bytecode/OverlaodGenTest.java b/test/bytecode/OverlaodGenTest.java new file mode 100644 index 00000000..72bde4f9 --- /dev/null +++ b/test/bytecode/OverlaodGenTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class OverlaodGenTest extends JavaTXCompilerTest { + public OverlaodGenTest() { + this.fileName = "OverlaodGen"; + } +} From 56d919f446216cd19ec8a5c55d9adf50d474a3af Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 31 Jan 2018 15:55:02 +0100 Subject: [PATCH 6/8] bug fix --- src/de/dhbwstuttgart/bytecode/BytecodeGen.java | 2 +- src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java | 6 +++--- .../bytecode/descriptor/DescriptorToString.java | 8 +++++--- .../dhbwstuttgart/bytecode/signature/TypeToSignature.java | 4 ++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index d4fee65f..8aa4c9e2 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -181,7 +181,7 @@ public class BytecodeGen implements ASTVisitor { Signature signature = new Signature(method, genericsAndBoundsMethod, methodParamsAndTypes,resultSet); sig = signature.toString(); } - + System.out.println(sig); NormalMethod meth = new NormalMethod(method,genericsAndBounds,genericsAndBoundsMethod,hasGen); methDesc = meth.accept(new DescriptorToString(resultSet)); MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC+acc, method.getName(), methDesc, sig, null); diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 65834125..b2c38397 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -31,6 +31,7 @@ import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.statement.literal.BoolLiteral; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; @@ -420,8 +421,8 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(WhileStmt whileStmt) { - // TODO Auto-generated method stub - + whileStmt.expr.accept(this); + whileStmt.loopBlock.accept(this); } @Override @@ -437,7 +438,6 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(Literal literal) { - // value? mv.visitLdcInsn(getResolvedType(literal.getType())); } diff --git a/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java index c7c623b6..c9725a82 100644 --- a/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java +++ b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java @@ -48,9 +48,11 @@ public class DescriptorToString implements DescriptorVisitor{ }else { desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } - }else if(((RefType) fp.getType()).getParaList().size() > 0){ - desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.toString().replace(".", "%").replace("<", "%%").replace(">", "%%")+ ";"; - }else { + } +// else if(((RefType) fp.getType()).getParaList().size() > 0){ +// desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.toString().replace(".", "%").replace("<", "%%").replace(">", "%%")+ ";"; +// } + else { desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } } diff --git a/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java b/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java index bbc19c37..78b2fb22 100644 --- a/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java +++ b/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java @@ -25,9 +25,9 @@ public class TypeToSignature implements TypeVisitor { params += "L"+param.toString().replace(".", "/"); if(it.hasNext())params += ";"; } - params += ";>;"; + params += ";>"; } - return refType.getName().toString().replace(".", "/") + params; + return refType.getName().toString().replace(".", "/") + params+";"; } @Override From b32e437a05fffc852dd303c6df68753cdadfab3d Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 31 Jan 2018 16:16:05 +0100 Subject: [PATCH 7/8] Fehler --- src/de/dhbwstuttgart/bytecode/BytecodeGen.java | 4 ++-- test/bytecode/ATest.java | 2 +- test/bytecode/Example.jav | 4 ++-- test/bytecode/For.jav | 13 +++++++++++++ test/bytecode/ForTest.java | 9 +++++++++ test/bytecode/LamAssign.jav | 1 + 6 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 test/bytecode/For.jav create mode 100644 test/bytecode/ForTest.java diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 8ec49b8b..57aeecc3 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -155,7 +155,7 @@ public class BytecodeGen implements ASTVisitor { String methDesc = null; // Method getModifiers() ? - int acc = isInterface?Opcodes.ACC_ABSTRACT:0; + int acc = isInterface?Opcodes.ACC_ABSTRACT:method.modifier; boolean hasGenInParameterList = genericsAndBounds.containsKey(resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor())); if(!hasGenInParameterList) { @@ -183,7 +183,7 @@ public class BytecodeGen implements ASTVisitor { System.out.println(sig); NormalMethod meth = new NormalMethod(method,genericsAndBounds,genericsAndBoundsMethod,hasGen); methDesc = meth.accept(new DescriptorToString(resultSet)); - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC+acc, method.getName(), methDesc, sig, null); + MethodVisitor mv = cw.visitMethod(acc, method.getName(), methDesc, sig, null); mv.visitCode(); diff --git a/test/bytecode/ATest.java b/test/bytecode/ATest.java index 9865825e..14dbacbf 100644 --- a/test/bytecode/ATest.java +++ b/test/bytecode/ATest.java @@ -2,7 +2,7 @@ package bytecode; public class ATest extends JavaTXCompilerTest { public ATest() { - fileName = "A"; + fileName = "Example"; } } diff --git a/test/bytecode/Example.jav b/test/bytecode/Example.jav index 451803ea..8fc7a0a7 100644 --- a/test/bytecode/Example.jav +++ b/test/bytecode/Example.jav @@ -1,7 +1,7 @@ public class Example { - public m() { - String x = "X"; + public m(Integer x) { +// String x = "X"; return x; } } \ No newline at end of file diff --git a/test/bytecode/For.jav b/test/bytecode/For.jav new file mode 100644 index 00000000..c93b34c4 --- /dev/null +++ b/test/bytecode/For.jav @@ -0,0 +1,13 @@ +class For{ + m(Integer x){ + Boolean b = true; + while(x<2){ + x = x +1; + b = false; + } + return x; +// for(int i = 0;i<10;i++) { +// x = x + 5; +// } + } +} \ No newline at end of file diff --git a/test/bytecode/ForTest.java b/test/bytecode/ForTest.java new file mode 100644 index 00000000..7df68665 --- /dev/null +++ b/test/bytecode/ForTest.java @@ -0,0 +1,9 @@ +package bytecode; + +public class ForTest extends JavaTXCompilerTest { + + public ForTest() { + this.fileName = "For"; + } + +} diff --git a/test/bytecode/LamAssign.jav b/test/bytecode/LamAssign.jav index e522bd3b..7fd5bc64 100644 --- a/test/bytecode/LamAssign.jav +++ b/test/bytecode/LamAssign.jav @@ -1,3 +1,4 @@ + class LamAssign { m () { From 9c4f763c9af94761a682e7148d5540fb47abfd49 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 31 Jan 2018 16:42:25 +0100 Subject: [PATCH 8/8] =?UTF-8?q?java.lang.=20nicht=20standardm=C3=A4=C3=9Fi?= =?UTF-8?q?g=20importieren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/parser/scope/GatherNames.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/dhbwstuttgart/parser/scope/GatherNames.java b/src/de/dhbwstuttgart/parser/scope/GatherNames.java index cd508cde..69481508 100644 --- a/src/de/dhbwstuttgart/parser/scope/GatherNames.java +++ b/src/de/dhbwstuttgart/parser/scope/GatherNames.java @@ -72,7 +72,7 @@ public class GatherNames { public static Map getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException { Map ret = new HashMap<>(); ClassLoader classLoader = ClassLoader.getSystemClassLoader(); - ret.putAll(packages.getClassNames("java.lang")); + //ret.putAll(packages.getClassNames("java.lang")); for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){ if(importDeclCtx.singleTypeImportDeclaration() != null){ Class cl = classLoader.loadClass(importDeclCtx.singleTypeImportDeclaration().typeName().getText());