diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 2c68ad43..57aeecc3 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -10,6 +10,10 @@ import org.objectweb.asm.FieldVisitor; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; +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; @@ -53,7 +57,6 @@ public class BytecodeGen implements ASTVisitor { for(ClassOrInterface cl : sourceFile.getClasses()) { BytecodeGen classGen = new BytecodeGen(classFiles, resultSet); cl.accept(classGen); - System.out.println("In CLASS: "+(cl.getClassName().toString())); classGen.writeClass(cl.getClassName().toString()); } } @@ -79,9 +82,10 @@ public class BytecodeGen implements ASTVisitor { public void visit(ClassOrInterface classOrInterface) { className = classOrInterface.getClassName().toString(); - isInterface = (classOrInterface.getModifiers()&512)==512; - System.out.println("IS Interface = "+"modifiers= "+classOrInterface.getModifiers()+" ->"+(classOrInterface.getModifiers()&512) + isInterface); + cw.visitSource(className +".jav", null); + isInterface = (classOrInterface.getModifiers()&512)==512; + int acc = isInterface?classOrInterface.getModifiers()+Opcodes.ACC_ABSTRACT:classOrInterface.getModifiers()+Opcodes.ACC_SUPER; String sig = null; /* if class has generics then creates signature @@ -90,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? @@ -100,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); } @@ -112,7 +112,6 @@ public class BytecodeGen implements ASTVisitor { for(Method m : classOrInterface.getMethods()) { m.accept(this); } - cw.visitSource(classOrInterface.getClassName().toString()+".jav", null); } @Override @@ -121,24 +120,25 @@ 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) { - 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); + genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles); if(!field.getParameterList().iterator().hasNext()) { mv.visitInsn(Opcodes.RETURN); } @@ -155,45 +155,40 @@ 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; - 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()) { - if(genericsAndBounds.containsKey(paramName)) { + String typeOfParam = methodParamsAndTypes.get(paramName).acceptTV(new TypeToDescriptor()); + if(genericsAndBounds.containsKey(typeOfParam)) { hasGenInParameterList = true; break; } } } + + //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; - // 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(); - System.out.println(sig); - } - + 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); + MethodVisitor mv = cw.visitMethod(acc, method.getName(), methDesc, sig, null); 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(); } @@ -207,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++; } @@ -250,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(); @@ -259,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 8cbb64fa..c5480ac0 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; @@ -15,12 +19,21 @@ 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; import de.dhbwstuttgart.syntaxtree.StatementVisitor; + import de.dhbwstuttgart.syntaxtree.statement.Literal; + import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -41,17 +54,18 @@ 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; 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 >>>>>>"; +// this.where = "<<<<<< NORMAL METHOD >>>>>>"; this.className = className; this.resultSet = resultSet; @@ -62,29 +76,24 @@ public class BytecodeGenMethod implements StatementVisitor{ this.genericsAndBoundsMethod = genericsAndBoundsMethod; this.genericsAndBounds = genericsAndBounds; this.isInterface = isInterface; + this.classFiles = classFiles; 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) { - this.where = "<<<<<< LAMBDA METHOD >>>>>>"; +// this.where = "<<<<<< LAMBDA METHOD >>>>>>"; this.resultSet = resultSet; this.mv = mv; this.isInterface = isInterface; + this.classFiles = classFiles; this.lamCounter = -1; this.varsFunInterface = new ArrayList<>(); @@ -99,7 +108,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()); } @@ -107,10 +115,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"); } } @@ -125,7 +131,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)); } // ?? @@ -133,12 +139,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)) { @@ -147,7 +153,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; @@ -159,17 +164,16 @@ public class BytecodeGenMethod implements StatementVisitor{ } @Override + public void visit(BinaryExpr 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: " + lambdaExpression.getReturnType().acceptTV(new TypeToString())); 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 @@ -181,10 +185,20 @@ 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;"; + + // 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()) { + itr.next(); + typeErasure += "L"+Type.getInternalName(Object.class) + ";"; + } + + 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); @@ -209,20 +223,64 @@ 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); - 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,typeErasure); } + private void generateBCForFunN(LambdaExpression lambdaExpression, String methDesc) { + ClassWriter classWriter =new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); + + SignatureWriter methSig = new SignatureWriter(); + + int numberOfParams = 0; + SignatureVisitor paramVisitor = methSig.visitParameterType(); + Iterator itr = lambdaExpression.params.iterator(); + while(itr.hasNext()) { + numberOfParams++; + // getBounds + paramVisitor.visitTypeVariable("T"+numberOfParams); + itr.next(); + } + methSig.visitReturnType().visitTypeVariable("R"); + // ")"+lam.getReturn.getBounds + 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/examples/" +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 @@ -237,7 +295,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())+";"; @@ -261,7 +318,7 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(IfStmt ifStmt) { - System.out.println("++ IF-Statment: "); + } @Override @@ -272,13 +329,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); @@ -287,14 +342,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); } @@ -306,8 +360,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); @@ -330,8 +384,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); } @@ -353,7 +405,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()), @@ -362,19 +413,18 @@ 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(WhileStmt whileStmt) { - // TODO Auto-generated method stub - + whileStmt.expr.accept(this); + whileStmt.loopBlock.accept(this); } @Override @@ -385,17 +435,14 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(Literal literal) { - // value? mv.visitLdcInsn(getResolvedType(literal.getType())); } @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 @@ -414,7 +461,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/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 16e84cc5..5afc31fb 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 85% rename from src/de/dhbwstuttgart/bytecode/DescriptorToString.java rename to src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java index 481dbc5d..c9725a82 100644 --- a/src/de/dhbwstuttgart/bytecode/DescriptorToString.java +++ b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java @@ -1,9 +1,16 @@ -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.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; @@ -30,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)) { @@ -39,7 +48,11 @@ public class DescriptorToString implements DescriptorVisitor{ }else { desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } - }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())+ ";"; } } @@ -71,16 +84,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())+ ";"; } } @@ -96,7 +109,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 76% rename from src/de/dhbwstuttgart/bytecode/Signature.java rename to src/de/dhbwstuttgart/bytecode/signature/Signature.java index 362429a3..fc9e2191 100644 --- a/src/de/dhbwstuttgart/bytecode/Signature.java +++ b/src/de/dhbwstuttgart/bytecode/signature/Signature.java @@ -1,16 +1,21 @@ -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; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -49,6 +54,29 @@ 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.visitFormalTypeParameter("R"); + // getBounds vom Return-Type + sw.visitClassBound().visitClassType(Type.getInternalName(Object.class)); + sw.visitClassBound().visitEnd(); + for(int i = 0;i;"); - + 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())); break; default: if(!isParameterType) @@ -141,6 +170,7 @@ public class Signature { while(bItr.hasNext()) { RefTypeOrTPHOrWildcardOrGeneric b =bItr.next(); String boundDesc = b.acceptTV(new TypeToDescriptor()); +// System.out.println("GetBounds: " + boundDesc); // Ensure that <...> extends java.lang.Object OR ... sw.visitClassBound().visitClassType(boundDesc); genAndBounds.put(g.getParsedName(), boundDesc); diff --git a/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java b/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java new file mode 100644 index 00000000..78b2fb22 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java @@ -0,0 +1,53 @@ +package de.dhbwstuttgart.bytecode.signature; + +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/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/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 4cf85297..3c9a5907 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -29,14 +29,17 @@ 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){ + params += "<"; + 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; } diff --git a/test/bytecode/ATest.java b/test/bytecode/ATest.java new file mode 100644 index 00000000..14dbacbf --- /dev/null +++ b/test/bytecode/ATest.java @@ -0,0 +1,8 @@ +package bytecode; + +public class ATest extends JavaTXCompilerTest { + public ATest() { + fileName = "Example"; + } + +} 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/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/Example.jav b/test/bytecode/Example.jav new file mode 100644 index 00000000..8fc7a0a7 --- /dev/null +++ b/test/bytecode/Example.jav @@ -0,0 +1,7 @@ +public class Example { + + public m(Integer x) { +// String x = "X"; + return x; + } +} \ No newline at end of file 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/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/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 1e0ee3d8..4a4f4f73 100644 --- a/test/bytecode/LamAssign.jav +++ b/test/bytecode/LamAssign.jav @@ -9,7 +9,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"; + } +} 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"; + } +}