From 01339ca7ec7ec368cf9e6a3fee5e481fc19459f9 Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Thu, 28 Dec 2017 10:20:28 +0100 Subject: [PATCH 001/208] Descriptor-Erzeugen neu mit Visitor-pattern implementiert --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 16 ++- .../bytecode/BytecodeGenMethod.java | 36 +++--- src/de/dhbwstuttgart/bytecode/Descriptor.java | 112 ------------------ .../bytecode/DescriptorToString.java | 85 +++++++++++++ .../bytecode/DescriptorVisitor.java | 9 ++ src/de/dhbwstuttgart/bytecode/Lambda.java | 25 ++++ .../bytecode/MethodFromMethodCall.java | 18 +++ .../bytecode/NormalConstructor.java | 20 ++++ .../dhbwstuttgart/bytecode/NormalMethod.java | 29 +++++ src/de/dhbwstuttgart/bytecode/SamMethod.java | 31 +++++ .../bytecode/TypeToDescriptor.java | 37 ++++++ test/bytecode/JavaTXCompilerTest.java | 2 +- 12 files changed, 285 insertions(+), 135 deletions(-) delete mode 100644 src/de/dhbwstuttgart/bytecode/Descriptor.java create mode 100644 src/de/dhbwstuttgart/bytecode/DescriptorToString.java create mode 100644 src/de/dhbwstuttgart/bytecode/DescriptorVisitor.java create mode 100644 src/de/dhbwstuttgart/bytecode/Lambda.java create mode 100644 src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java create mode 100644 src/de/dhbwstuttgart/bytecode/NormalConstructor.java create mode 100644 src/de/dhbwstuttgart/bytecode/NormalMethod.java create mode 100644 src/de/dhbwstuttgart/bytecode/SamMethod.java create mode 100644 src/de/dhbwstuttgart/bytecode/TypeToDescriptor.java diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 6f7acd64..1b6ef47b 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -88,11 +88,12 @@ public class BytecodeGen implements ASTVisitor { @Override public void visit(Constructor field) { - Descriptor desc = new Descriptor(field, resultSet); - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", desc.getDesc(), null, null); + NormalConstructor constructor = new NormalConstructor(field); + String desc = constructor.accept(new DescriptorToString(resultSet)); + MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", desc, null, null); mv.visitCode(); System.out.println("-----Constructor-----"); - BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,field, mv,paramsAndLocals,desc.getDesc(),cw,isInterface); + BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,field, mv,paramsAndLocals,desc,cw,isInterface); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(0, 0); @@ -104,12 +105,15 @@ public class BytecodeGen implements ASTVisitor { // TODO: check if the method is static => if static then the first param will be stored in pos 0 // else it will be stored in pos 1 and this will be stored in pos 0 method.getParameterList().accept(this); - Descriptor methDesc = new Descriptor(method,resultSet); + + NormalMethod meth = new NormalMethod(method); + String methDesc = meth.accept(new DescriptorToString(resultSet)); + System.out.println("-----Method-----"); - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), methDesc.getDesc(), null, null); + MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), methDesc, null, null); mv.visitCode(); - BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,method, mv,paramsAndLocals,methDesc.getDesc(),cw,isInterface); + BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,method, mv,paramsAndLocals,methDesc,cw,isInterface); mv.visitMaxs(0, 0); mv.visitEnd(); } diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index c2acaba0..ffdf1450 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -94,7 +94,8 @@ public class BytecodeGenMethod implements StatementVisitor{ } private String getResolvedType(RefTypeOrTPHOrWildcardOrGeneric type) { - return resultSet.resolveType(type).resolvedType.toString().replace(".", "/"); +// return resultSet.resolveType(type).resolvedType.toString().replace(".", "/"); + return resultSet.resolveType(type).resolvedType.acceptTV(new TypeToDescriptor()); } @@ -164,7 +165,9 @@ public class BytecodeGenMethod implements StatementVisitor{ public void visit(LambdaExpression lambdaExpression) { System.out.println("\n++ In Lambda: "); this.lamCounter++; - Descriptor lamDesc = new Descriptor(lambdaExpression, resultSet); + + 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 //the lambda is being converted MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, @@ -175,9 +178,9 @@ public class BytecodeGenMethod implements StatementVisitor{ "metafactory", mt.toMethodDescriptorString(), false); String methodName = "lambda$new$" + this.lamCounter; // Type erasure - Type arg1 = Type.getMethodType(lamDesc.getDesc()); + Type arg1 = Type.getMethodType(lamDesc); // real Type - Type arg3 = Type.getMethodType(lamDesc.getDesc()); + Type arg3 = Type.getMethodType(lamDesc); int staticOrSpecial=0; int staticOrInstance=0; @@ -197,12 +200,12 @@ public class BytecodeGenMethod implements StatementVisitor{ Handle arg2 = new Handle(staticOrSpecial, this.className, methodName, arg3.toString(),false); // Descriptor of functional interface methode - Descriptor fiMethodDesc = new Descriptor(kindOfLambda.getArgumentList(), lambdaExpression.getType(),resultSet); - + SamMethod samMethod = new SamMethod(kindOfLambda.getArgumentList(), lambdaExpression.getType()); // Desc: (this/nothing)TargetType - mv.visitInvokeDynamicInsn("apply", fiMethodDesc.getDesc(), bootstrap, + String fiMethodDesc = samMethod.accept(new DescriptorToString(resultSet)); + mv.visitInvokeDynamicInsn("apply", fiMethodDesc, bootstrap, arg1, arg2,arg3); - + MethodVisitor mvLambdaBody = cw.visitMethod(Opcodes.ACC_PRIVATE+ staticOrInstance + Opcodes.ACC_SYNTHETIC, methodName, arg3.toString(), null, null); @@ -274,17 +277,18 @@ public class BytecodeGenMethod implements StatementVisitor{ methodCall.receiver.accept(this); methodCall.arglist.accept(this); - Descriptor mDesc = new Descriptor(methodCall.arglist, methodCall.getType(),resultSet); + MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType()); + String mDesc = method.accept(new DescriptorToString(resultSet)); 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.getDesc(), false); + methodCall.name, mDesc, false); }else { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getResolvedType(methodCall.receiver.getType()), - methodCall.name, mDesc.getDesc(), isInterface); + methodCall.name, mDesc, isInterface); } // test // if(!methodCall.getType().toString().equals("V")) { @@ -303,7 +307,7 @@ public class BytecodeGenMethod implements StatementVisitor{ methodCall.arglist.accept(this); String d = "("; for(Expression e : methodCall.arglist.getArguments()) { - d = d + "L"+e.getType().toString().replace(".", "/") + ";"; + d = d + "L"+getResolvedType(e.getType()) + ";"; } d += ")V"; @@ -339,7 +343,7 @@ public class BytecodeGenMethod implements StatementVisitor{ System.out.println("In StaticClassName: "); // mv.visitMethodInsn(Opcodes.INVOKESTATIC, staticClassName.getType().toString().replace(".", "/"), // staticClassName.toString(), staticClassName.getType().toString(), false); - mv.visitFieldInsn(Opcodes.GETSTATIC, staticClassName.getType().toString().replace(".", "/"), + mv.visitFieldInsn(Opcodes.GETSTATIC, getResolvedType(staticClassName.getType()), fieldName, fieldDesc); } @@ -379,7 +383,7 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(Literal literal) { // value? - mv.visitLdcInsn(literal.getType().toString()); + mv.visitLdcInsn(getResolvedType(literal.getType())); } @Override @@ -399,8 +403,8 @@ public class BytecodeGenMethod implements StatementVisitor{ // array slot onto the top of the operand stack. assignLeftSide.field.receiver.accept(this); this.rightSideTemp.accept(this); - mv.visitFieldInsn(Opcodes.PUTFIELD, assignLeftSide.field.receiver.getType().toString(), - assignLeftSide.field.fieldVarName, assignLeftSide.field.getType().toString()); + mv.visitFieldInsn(Opcodes.PUTFIELD, getResolvedType(assignLeftSide.field.receiver.getType()), + assignLeftSide.field.fieldVarName, getResolvedType(assignLeftSide.field.getType())); } @Override diff --git a/src/de/dhbwstuttgart/bytecode/Descriptor.java b/src/de/dhbwstuttgart/bytecode/Descriptor.java deleted file mode 100644 index 3b024d71..00000000 --- a/src/de/dhbwstuttgart/bytecode/Descriptor.java +++ /dev/null @@ -1,112 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -import java.util.List; -import java.util.Iterator; - -import de.dhbwstuttgart.exceptions.NotImplementedException; -import de.dhbwstuttgart.syntaxtree.Constructor; -import de.dhbwstuttgart.syntaxtree.FormalParameter; -import de.dhbwstuttgart.syntaxtree.Method; -import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; -import de.dhbwstuttgart.syntaxtree.statement.Expression; -import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; -import de.dhbwstuttgart.syntaxtree.type.*; -import de.dhbwstuttgart.typeinference.result.ResultSet; - -public class Descriptor { - String desc; - - public Descriptor(Method method, ResultSet resultSet) { - desc = "("; - Iterator itr = method.getParameterList().iterator(); - while(itr.hasNext()) { - FormalParameter fp = itr.next(); - desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; - } - desc = addReturnType(desc,method.getReturnType(), resultSet); - - - } - - private class TypeToDescriptor implements TypeVisitor{ - - @Override - public String visit(RefType refType) { - return refType.getName().toString().replace(".", "/"); - } - - @Override - public String visit(SuperWildcardType superWildcardType) { - throw new NotImplementedException(); - } - - @Override - public String visit(TypePlaceholder typePlaceholder) { - return typePlaceholder.toString(); - } - - @Override - public String visit(ExtendsWildcardType extendsWildcardType) { - throw new NotImplementedException(); - } - - @Override - public String visit(GenericRefType genericRefType) { - return genericRefType.getParsedName(); - } - } - - private String addReturnType(String desc2, RefTypeOrTPHOrWildcardOrGeneric returnType, ResultSet resultSet) { - System.out.println("DescType = "+returnType.toString()); - if(resultSet.resolveType(returnType).resolvedType.toString().equals("void")){ - desc = desc + ")V"; - }else { - desc = desc + ")" + "L"+resultSet.resolveType(returnType).resolvedType.toString().replace(".", "/")+";"; - } - return desc; - } - - public Descriptor(Constructor constructor, ResultSet resultSet) { - desc = "("; - Iterator itr = constructor.getParameterList().iterator(); - while(itr.hasNext()) { - FormalParameter fp = itr.next(); - desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.toString().replace(".", "/") + ";"; - } - desc = desc + ")V"; - } - - public Descriptor(LambdaExpression lambdaExpr, ResultSet resultSet) { - desc = "("; - Iterator itr = lambdaExpr.params.iterator(); - while(itr.hasNext()) { - FormalParameter fp = itr.next(); - desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.toString().replace(".", "/") + ";"; - } - desc = addReturnType(desc, lambdaExpr.getReturnType(), resultSet); - } - - public Descriptor(ArgumentList argList, RefTypeOrTPHOrWildcardOrGeneric returnType, ResultSet resultSet) { - desc = "("; - for(Expression e : argList.getArguments()) { - desc = desc + "L"+resultSet.resolveType(e.getType()).resolvedType.toString().replace(".", "/") + ";"; - } - desc = addReturnType(desc, returnType, resultSet); - - } - - public Descriptor(List argumentList,RefTypeOrTPHOrWildcardOrGeneric returnType ,ResultSet resultSet) { - desc = "("; - Iterator itr = argumentList.iterator(); - while(itr.hasNext()) { - RefTypeOrTPHOrWildcardOrGeneric rt = itr.next(); - desc = desc + "L"+resultSet.resolveType(rt).resolvedType.toString().replace(".", "/")+";"; - } - desc = desc + ")"+"L"+resultSet.resolveType(returnType).resolvedType.toString().replace(".", "/")+";"; - } - - public String getDesc() { - return this.desc; - } - -} diff --git a/src/de/dhbwstuttgart/bytecode/DescriptorToString.java b/src/de/dhbwstuttgart/bytecode/DescriptorToString.java new file mode 100644 index 00000000..044218e4 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/DescriptorToString.java @@ -0,0 +1,85 @@ +package de.dhbwstuttgart.bytecode; + +import java.util.Iterator; + +import de.dhbwstuttgart.syntaxtree.FormalParameter; +import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.typeinference.result.ResultSet; + +public class DescriptorToString implements DescriptorVisitor{ + ResultSet resultSet; + + public DescriptorToString(ResultSet resultSet) { + this.resultSet = resultSet; + } + + private String addReturnType(String desc, RefTypeOrTPHOrWildcardOrGeneric returnType, ResultSet resultSet) { + if(resultSet.resolveType(returnType).resolvedType.toString().equals("void")){ + desc = desc + ")V"; + }else { + desc = desc + ")" + "L"+resultSet.resolveType(returnType).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + return desc; + } + + @Override + public String visit(NormalMethod method) { + + String desc = "("; + Iterator itr = method.getParameterList().iterator(); + while(itr.hasNext()) { + FormalParameter fp = itr.next(); + desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + desc = addReturnType(desc,method.getReturnType(), resultSet); + return desc; + } + + @Override + public String visit(NormalConstructor constructor) { + String desc = "("; + Iterator itr = constructor.getParameterList().iterator(); + while(itr.hasNext()) { + FormalParameter fp = itr.next(); + desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + desc = desc + ")V"; + return desc; + } + + @Override + public String visit(Lambda lambdaExpression) { + String desc = "("; + Iterator itr = lambdaExpression.getParams().iterator(); + while(itr.hasNext()) { + FormalParameter fp = itr.next(); + desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor()) + ";"; + } + desc = addReturnType(desc, lambdaExpression.getReturnType(), resultSet); + return desc; + } + + @Override + public String visit(SamMethod samMethod) { + String desc = "("; + Iterator itr = samMethod.getArgumentList().iterator(); + while(itr.hasNext()) { + RefTypeOrTPHOrWildcardOrGeneric rt = itr.next(); + desc = desc + "L"+resultSet.resolveType(rt).resolvedType.acceptTV(new TypeToDescriptor())+";"; + } + desc = desc + ")"+"L"+resultSet.resolveType(samMethod.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor())+";"; + return desc; + } + + @Override + public String visit(MethodFromMethodCall methodFromMethodCall) { + String desc = "("; + for(Expression e : methodFromMethodCall.argList.getArguments()) { + desc = desc + "L"+resultSet.resolveType(e.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + desc = addReturnType(desc, methodFromMethodCall.returnType, resultSet); + return desc; + } + +} diff --git a/src/de/dhbwstuttgart/bytecode/DescriptorVisitor.java b/src/de/dhbwstuttgart/bytecode/DescriptorVisitor.java new file mode 100644 index 00000000..63198828 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/DescriptorVisitor.java @@ -0,0 +1,9 @@ +package de.dhbwstuttgart.bytecode; + +public interface DescriptorVisitor { + public String visit(NormalMethod method); + public String visit(NormalConstructor constructor); + public String visit(Lambda lambdaExpression); + public String visit(SamMethod samMethod); + public String visit(MethodFromMethodCall methodFromMethodCall); +} diff --git a/src/de/dhbwstuttgart/bytecode/Lambda.java b/src/de/dhbwstuttgart/bytecode/Lambda.java new file mode 100644 index 00000000..283b7393 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/Lambda.java @@ -0,0 +1,25 @@ +package de.dhbwstuttgart.bytecode; + +import de.dhbwstuttgart.syntaxtree.ParameterList; +import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; + +public class Lambda { + private LambdaExpression lambdaExpression; + + public Lambda(LambdaExpression lambdaExpression) { + this.lambdaExpression = lambdaExpression; + } + + public ParameterList getParams() { + return lambdaExpression.params; + } + + public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { + return lambdaExpression.getReturnType(); + } + + public String accept(DescriptorVisitor descVisitor) { + return descVisitor.visit(this); + } +} diff --git a/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java b/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java new file mode 100644 index 00000000..034e2abf --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java @@ -0,0 +1,18 @@ +package de.dhbwstuttgart.bytecode; + +import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; + +public class MethodFromMethodCall { + ArgumentList argList; + RefTypeOrTPHOrWildcardOrGeneric returnType; + + public MethodFromMethodCall(ArgumentList argList,RefTypeOrTPHOrWildcardOrGeneric returnType) { + this.argList = argList; + this.returnType = returnType; + } + + public String accept(DescriptorVisitor descVisitor) { + return descVisitor.visit(this); + } +} diff --git a/src/de/dhbwstuttgart/bytecode/NormalConstructor.java b/src/de/dhbwstuttgart/bytecode/NormalConstructor.java new file mode 100644 index 00000000..d029d21e --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/NormalConstructor.java @@ -0,0 +1,20 @@ +package de.dhbwstuttgart.bytecode; + +import de.dhbwstuttgart.syntaxtree.Constructor; +import de.dhbwstuttgart.syntaxtree.ParameterList; + +public class NormalConstructor { + private Constructor constructor; + + public NormalConstructor(Constructor constructor) { + this.constructor = constructor; + } + + public ParameterList getParameterList() { + return constructor.getParameterList(); + } + + public String accept(DescriptorVisitor descVisitor) { + return descVisitor.visit(this); + } +} diff --git a/src/de/dhbwstuttgart/bytecode/NormalMethod.java b/src/de/dhbwstuttgart/bytecode/NormalMethod.java new file mode 100644 index 00000000..2dd81d78 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/NormalMethod.java @@ -0,0 +1,29 @@ +package de.dhbwstuttgart.bytecode; + +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.ParameterList; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; + +public class NormalMethod { + private Method method; + + public NormalMethod(Method method) { + this.method = method; + } + + public Method getMethod() { + return method; + } + + public ParameterList getParameterList() { + return method.getParameterList(); + } + + public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { + return method.getType(); + } + + public String accept(DescriptorVisitor descVisitor) { + return descVisitor.visit(this); + } +} diff --git a/src/de/dhbwstuttgart/bytecode/SamMethod.java b/src/de/dhbwstuttgart/bytecode/SamMethod.java new file mode 100644 index 00000000..9cf039a2 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/SamMethod.java @@ -0,0 +1,31 @@ +package de.dhbwstuttgart.bytecode; + +import java.util.List; + +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; + +public class SamMethod { + private List argumentList; + private RefTypeOrTPHOrWildcardOrGeneric returnType; + + public SamMethod(List argumentList, RefTypeOrTPHOrWildcardOrGeneric returnType) { + this.argumentList = argumentList; + this.returnType = returnType; + } + + public List getArgumentList() { + return argumentList; + } + + + + public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { + return returnType; + } + + + + public String accept(DescriptorVisitor descVisitor) { + return descVisitor.visit(this); + } +} diff --git a/src/de/dhbwstuttgart/bytecode/TypeToDescriptor.java b/src/de/dhbwstuttgart/bytecode/TypeToDescriptor.java new file mode 100644 index 00000000..5271e63d --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/TypeToDescriptor.java @@ -0,0 +1,37 @@ +package de.dhbwstuttgart.bytecode; + +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.SuperWildcardType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.type.TypeVisitor; + +public class TypeToDescriptor implements TypeVisitor{ + + @Override + public String visit(RefType refType) { + return refType.getName().toString().replace(".", "/"); + } + + @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(".", "/"); + } +} \ No newline at end of file diff --git a/test/bytecode/JavaTXCompilerTest.java b/test/bytecode/JavaTXCompilerTest.java index f43a0740..39ff5084 100644 --- a/test/bytecode/JavaTXCompilerTest.java +++ b/test/bytecode/JavaTXCompilerTest.java @@ -29,7 +29,7 @@ public class JavaTXCompilerTest { @Test public void test() throws IOException, java.lang.ClassNotFoundException { System.out.println(rootDirectory); - String fileName = "Generics"; + String fileName = "Faculty"; filesToTest.add(new File(rootDirectory+fileName+".jav")); System.out.println(rootDirectory+fileName+".jav"); JavaTXCompiler compiler = new JavaTXCompiler(filesToTest); From 3c732346d907ae5b59e7aece1975fa29ff40baeb Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Sat, 6 Jan 2018 09:38:53 +0100 Subject: [PATCH 002/208] ASPParameterlist erzeugt auch paramNum --- pom.xml | 5 +++++ src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/pom.xml b/pom.xml index 6b0f2fba..9cce2d99 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,11 @@ antlr4 4.7 + + commons-io + commons-io + 2.6 + com.google.guava guava diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java b/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java index f0c06744..46eed10a 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java @@ -11,11 +11,13 @@ import java.util.Set; public class ASPParameterList { private final static String ASP_PARAMLIST_NAME = "param"; + private final static String ASP_PARAMLISTNUMERATION_NAME = "paramNum"; private final static String ASP_PARAMLIST_END_POINTER = "null"; public final String name; private final List types; public ASPParameterList(List types, ASPWriter writer){ + int paramNum = 0; this.types = types; if(types.size() == 0){ name = ASP_PARAMLIST_END_POINTER; @@ -30,6 +32,8 @@ public class ASPParameterList { if(! it.hasNext())nextPointer = ASP_PARAMLIST_END_POINTER; param += nextPointer; writer.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); + writer.add(new ASPStatement(ASP_PARAMLISTNUMERATION_NAME + "(" + name + "," +t + "," + paramNum + ")")); + paramNum++; //paramDefinitions.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); } } From 19a1ef40244e76c117a7f0ad3bab406bb7f54733 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 10 Jan 2018 10:53:07 +0100 Subject: [PATCH 003/208] =?UTF-8?q?Erste=20lauff=C3=A4hige=20aber=20unvoll?= =?UTF-8?q?st=C3=A4ndige=20Version=20des=20UnifyWithoutWildcards?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/sat/asp/Clingo.java | 46 +++++++++++++++++++ .../dhbwstuttgart/sat/asp/model/ASPRule.java | 24 ++++++++++ .../dhbwstuttgart/sat/asp/model/ASPType.java | 4 -- .../sat/asp/parser/ASPParser.java | 17 +++++++ .../asp/parser/model/ParsedASPStatement.java | 7 +++ .../sat/asp/{ => writer}/ASPGenerator.java | 8 +--- .../sat/asp/{ => writer}/ASPWriter.java | 6 +-- .../{ => writer}/model/ASPGenericType.java | 7 +-- .../sat/asp/{ => writer}/model/ASPPair.java | 2 +- .../asp/{ => writer}/model/ASPPairEquals.java | 7 +-- .../{ => writer}/model/ASPPairSmaller.java | 11 +++-- .../{ => writer}/model/ASPPairSmallerDot.java | 7 +-- .../{ => writer}/model/ASPParameterList.java | 20 ++++---- .../asp/{ => writer}/model/ASPRefType.java | 7 +-- .../asp/{ => writer}/model/ASPStatement.java | 2 +- .../sat/asp/writer/model/ASPType.java | 4 ++ .../asp/{ => writer}/model/ASPTypeVar.java | 2 +- test/asp/ClingoTest.java | 46 +++++++++++++++++++ test/asp/typeinference/ASPTest.java | 2 +- test/asp/unifywithoutwildcards/ASPTests.java | 10 ++++ test/typeinference/JavaTXCompilerTest.java | 15 ------ 21 files changed, 193 insertions(+), 61 deletions(-) create mode 100644 src/de/dhbwstuttgart/sat/asp/Clingo.java create mode 100644 src/de/dhbwstuttgart/sat/asp/model/ASPRule.java delete mode 100644 src/de/dhbwstuttgart/sat/asp/model/ASPType.java create mode 100644 src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java create mode 100644 src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java rename src/de/dhbwstuttgart/sat/asp/{ => writer}/ASPGenerator.java (94%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/ASPWriter.java (67%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPGenericType.java (53%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPPair.java (90%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPPairEquals.java (55%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPPairSmaller.java (51%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPPairSmallerDot.java (55%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPParameterList.java (59%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPRefType.java (68%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPStatement.java (91%) create mode 100644 src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPTypeVar.java (83%) create mode 100644 test/asp/ClingoTest.java create mode 100644 test/asp/unifywithoutwildcards/ASPTests.java diff --git a/src/de/dhbwstuttgart/sat/asp/Clingo.java b/src/de/dhbwstuttgart/sat/asp/Clingo.java new file mode 100644 index 00000000..85f6dc56 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/Clingo.java @@ -0,0 +1,46 @@ +package de.dhbwstuttgart.sat.asp; + + +import org.apache.commons.io.IOUtils; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class Clingo { + private final List input; + private static final List programFiles = new ArrayList<>(); + static{ + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/basis.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/subst.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/reduce1.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/reduce2.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/unify.lp")); + } + + public Clingo(List inputFiles){ + this.input = inputFiles; + } + + public String runClingo() throws IOException, InterruptedException { + String pathToClingo = + "/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/clingo-5.2.1-linux-x86_64/clingo"; + List commands = new ArrayList<>(); + commands.add(pathToClingo); + commands.add("--outf=2"); //use JSON-Output + for(File file : input){ + commands.add(file.getPath()); + } + commands.addAll(programFiles.stream().map(f->f.getPath()).collect(Collectors.toList())); + Process clingo = new ProcessBuilder( commands.toArray(new String[0])).start(); + InputStream output = clingo.getInputStream(); + clingo.waitFor(); + String result = IOUtils.toString(output, StandardCharsets.UTF_8); + return result; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java b/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java new file mode 100644 index 00000000..41237547 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java @@ -0,0 +1,24 @@ +package de.dhbwstuttgart.sat.asp.model; + +public enum ASPRule { + ASP_GENERIC_TYPE_NAME("genericType"), + ASP_PAIR_EQUALS_NAME("equals"), + ASP_PAIR_SMALLER_NAME("smaller"), + ASP_PAIR_SMALLER_DOT_NAME("smallerDot"), + ASP_PARAMLIST_NAME("param"), + ASP_PARAMLISTNUMERATION_NAME("paramNum"), + ASP_PARAMLIST_END_POINTER("null"), + ASP_TYPE("type") + ; + + private final String text; + + private ASPRule(final String text) { + this.text = text; + } + + @Override + public String toString() { + return text; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPType.java b/src/de/dhbwstuttgart/sat/asp/model/ASPType.java deleted file mode 100644 index e703596c..00000000 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPType.java +++ /dev/null @@ -1,4 +0,0 @@ -package de.dhbwstuttgart.sat.asp.model; - -public interface ASPType { -} diff --git a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java new file mode 100644 index 00000000..c7be7dbc --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java @@ -0,0 +1,17 @@ +package de.dhbwstuttgart.sat.asp.parser; + +import de.dhbwstuttgart.typeinference.result.ResultPair; +import de.dhbwstuttgart.typeinference.result.ResultSet; + +import java.util.HashSet; +import java.util.Set; + +public class ASPParser { + ResultSet parse(String result){ + Set ret = new HashSet<>(); + for(String pair : result.split(",")){ + + } + return new ResultSet(ret); + } +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java new file mode 100644 index 00000000..614bddb0 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java @@ -0,0 +1,7 @@ +package de.dhbwstuttgart.sat.asp.parser.model; + +public class ParsedASPStatement { + public ParsedASPStatement(String statement){ + + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/ASPGenerator.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java similarity index 94% rename from src/de/dhbwstuttgart/sat/asp/ASPGenerator.java rename to src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java index 1b942ad0..002e3c7f 100644 --- a/src/de/dhbwstuttgart/sat/asp/ASPGenerator.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java @@ -1,9 +1,8 @@ -package de.dhbwstuttgart.sat.asp; +package de.dhbwstuttgart.sat.asp.writer; -import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.scope.JavaClassName; -import de.dhbwstuttgart.sat.asp.model.*; +import de.dhbwstuttgart.sat.asp.writer.model.*; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; @@ -11,13 +10,10 @@ import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; -import de.dhbwstuttgart.typeinference.unify.model.UnifyType; -import java.sql.Ref; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.Optional; public class ASPGenerator { ASPWriter writer = new ASPWriter(); diff --git a/src/de/dhbwstuttgart/sat/asp/ASPWriter.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java similarity index 67% rename from src/de/dhbwstuttgart/sat/asp/ASPWriter.java rename to src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java index 4d33cbbf..450950d9 100644 --- a/src/de/dhbwstuttgart/sat/asp/ASPWriter.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java @@ -1,8 +1,6 @@ -package de.dhbwstuttgart.sat.asp; +package de.dhbwstuttgart.sat.asp.writer; -import de.dhbwstuttgart.sat.asp.model.ASPRefType; -import de.dhbwstuttgart.sat.asp.model.ASPStatement; -import de.dhbwstuttgart.sat.asp.model.ASPType; +import de.dhbwstuttgart.sat.asp.writer.model.ASPStatement; import java.util.HashSet; diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java similarity index 53% rename from src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java index 26e6ea02..57619edf 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java @@ -1,7 +1,8 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; public class ASPGenericType implements ASPType{ - public static final String ASP_GENERIC_TYPE_NAME = "genericType"; private final String name; public ASPGenericType(String name){ @@ -9,6 +10,6 @@ public class ASPGenericType implements ASPType{ } public String toString(){ - return ASP_GENERIC_TYPE_NAME + "(" + name + ")"; + return ASPRule.ASP_GENERIC_TYPE_NAME + "(" + name + ")"; } } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPair.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java similarity index 90% rename from src/de/dhbwstuttgart/sat/asp/model/ASPPair.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java index bbd3ff3f..662f995c 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPPair.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; public abstract class ASPPair { public final ASPType leftSide; diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPairEquals.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java similarity index 55% rename from src/de/dhbwstuttgart/sat/asp/model/ASPPairEquals.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java index b67920bd..8806f2ce 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPPairEquals.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java @@ -1,13 +1,14 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; public class ASPPairEquals extends ASPPair{ - private final static String ASP_PAIR_EQUALS_NAME = "equals"; public ASPPairEquals(ASPType ls, ASPType rs){ super(ls, rs); } @Override protected String getRuleName() { - return ASP_PAIR_EQUALS_NAME; + return ASPRule.ASP_PAIR_EQUALS_NAME.toString(); } } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmaller.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java similarity index 51% rename from src/de/dhbwstuttgart/sat/asp/model/ASPPairSmaller.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java index 882054cc..f57170f6 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmaller.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java @@ -1,13 +1,16 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; + +import java.util.Map; public class ASPPairSmaller extends ASPPair{ - private final static String ASP_PAIR_SMALLER_NAME = "smaller"; public ASPPairSmaller(ASPType ls, ASPType rs){ super(ls, rs); } @Override protected String getRuleName() { - return ASP_PAIR_SMALLER_NAME; + return ASPRule.ASP_PAIR_SMALLER_NAME.toString(); } -} +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmallerDot.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java similarity index 55% rename from src/de/dhbwstuttgart/sat/asp/model/ASPPairSmallerDot.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java index 0e6598c1..e8482bfc 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmallerDot.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java @@ -1,13 +1,14 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; public class ASPPairSmallerDot extends ASPPair{ - private final static String ASP_PAIR_SMALLER_NAME = "smallerDot"; public ASPPairSmallerDot(ASPType ls, ASPType rs){ super(ls, rs); } @Override protected String getRuleName() { - return ASP_PAIR_SMALLER_NAME; + return ASPRule.ASP_PAIR_SMALLER_DOT_NAME.toString(); } } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java similarity index 59% rename from src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java index 46eed10a..3c176e49 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java @@ -1,18 +1,14 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; -import de.dhbwstuttgart.sat.asp.ASPGenerator; -import de.dhbwstuttgart.sat.asp.ASPWriter; +import de.dhbwstuttgart.sat.asp.model.ASPRule; +import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; +import de.dhbwstuttgart.sat.asp.writer.ASPWriter; import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; -import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Set; public class ASPParameterList { - private final static String ASP_PARAMLIST_NAME = "param"; - private final static String ASP_PARAMLISTNUMERATION_NAME = "paramNum"; - private final static String ASP_PARAMLIST_END_POINTER = "null"; public final String name; private final List types; @@ -20,7 +16,7 @@ public class ASPParameterList { int paramNum = 0; this.types = types; if(types.size() == 0){ - name = ASP_PARAMLIST_END_POINTER; + name = ASPRule.ASP_PARAMLIST_END_POINTER.toString(); }else{ name = newName(); String nextPointer = name; @@ -29,10 +25,10 @@ public class ASPParameterList { ASPType t = it.next(); String param = nextPointer + "," + t.toString() + ","; nextPointer = newName(); - if(! it.hasNext())nextPointer = ASP_PARAMLIST_END_POINTER; + if(! it.hasNext())nextPointer = ASPRule.ASP_PARAMLIST_END_POINTER.toString(); param += nextPointer; - writer.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); - writer.add(new ASPStatement(ASP_PARAMLISTNUMERATION_NAME + "(" + name + "," +t + "," + paramNum + ")")); + writer.add(new ASPStatement(ASPRule.ASP_PARAMLIST_NAME + "(" + param + ")")); + writer.add(new ASPStatement(ASPRule.ASP_PARAMLISTNUMERATION_NAME + "(" + name + "," +t + "," + paramNum + ")")); paramNum++; //paramDefinitions.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPRefType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java similarity index 68% rename from src/de/dhbwstuttgart/sat/asp/model/ASPRefType.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java index ba318876..6491ff33 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPRefType.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java @@ -1,7 +1,8 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; public class ASPRefType implements ASPType { - public static final String ASP_TYPE = "type"; private final ASPParameterList params; private final String name; @@ -15,6 +16,6 @@ public class ASPRefType implements ASPType { } public String toString(){ - return ASP_TYPE + "(" + name +"," + params.name + ")"; + return ASPRule.ASP_TYPE + "(" + name +"," + params.name + ")"; } } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPStatement.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java similarity index 91% rename from src/de/dhbwstuttgart/sat/asp/model/ASPStatement.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java index 8e4a1d35..76bb63e8 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPStatement.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; public class ASPStatement { private final String stmt; diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java new file mode 100644 index 00000000..da694218 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java @@ -0,0 +1,4 @@ +package de.dhbwstuttgart.sat.asp.writer.model; + +public interface ASPType { +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java similarity index 83% rename from src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java index e2f87636..4b07d30b 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; public class ASPTypeVar implements ASPType{ private final String name; diff --git a/test/asp/ClingoTest.java b/test/asp/ClingoTest.java new file mode 100644 index 00000000..dbfa70d8 --- /dev/null +++ b/test/asp/ClingoTest.java @@ -0,0 +1,46 @@ +package asp; + +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; +import de.dhbwstuttgart.sat.asp.Clingo; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import org.junit.Test; + +import java.io.*; +import java.util.*; + +public class ClingoTest { + public static final String rootDirectory = "~/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards"; + public static final String tempDirectory = "/tmp/"; + + @Test + public void test() throws IOException, InterruptedException { + String content = ""; + content = new ASPGenerator(this.getPairs(), this.getFC()).getASP(); + + PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8"); + writer.println(content); + writer.close(); + + Clingo clingo = new Clingo(Arrays.asList(new File(tempDirectory + "test.lp"))); + System.out.println(clingo.runClingo()); + } + + public Collection getFC() { + Set ret = new HashSet<>(); + ret.add(ASTFactory.createObjectClass()); + ret.add(ASTFactory.createClass(java.util.List.class)); + return ret; + } + + public ConstraintSet getPairs() { + ConstraintSet ret = new ConstraintSet<>(); + ret.addUndConstraint(new Pair(TypePlaceholder.fresh(new NullToken()), ASTFactory.createObjectType(), PairOperator.SMALLERDOT)); + return ret; + } +} diff --git a/test/asp/typeinference/ASPTest.java b/test/asp/typeinference/ASPTest.java index 5074e4f3..b41d9405 100644 --- a/test/asp/typeinference/ASPTest.java +++ b/test/asp/typeinference/ASPTest.java @@ -1,7 +1,7 @@ package asp.typeinference; import de.dhbwstuttgart.core.JavaTXCompiler; -import de.dhbwstuttgart.sat.asp.ASPGenerator; +import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; diff --git a/test/asp/unifywithoutwildcards/ASPTests.java b/test/asp/unifywithoutwildcards/ASPTests.java new file mode 100644 index 00000000..80e7bb78 --- /dev/null +++ b/test/asp/unifywithoutwildcards/ASPTests.java @@ -0,0 +1,10 @@ +package asp.unifywithoutwildcards; + +import org.junit.Test; + +public class ASPTests { + @Test + public void test(){ + + } +} diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index fbbed9af..f4622954 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -1,29 +1,15 @@ package typeinference; import de.dhbwstuttgart.core.JavaTXCompiler; -import de.dhbwstuttgart.parser.scope.JavaClassName; -import de.dhbwstuttgart.sat.asp.ASPGenerator; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; -import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; -import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter; import de.dhbwstuttgart.typedeployment.TypeInsert; import de.dhbwstuttgart.typedeployment.TypeInsertFactory; -import de.dhbwstuttgart.typeinference.constraints.Constraint; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.result.ResultSet; -import de.dhbwstuttgart.typeinference.typeAlgo.TYPE; -import de.dhbwstuttgart.typeinference.unify.TypeUnify; -import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; -import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import org.junit.Test; import java.io.File; import java.io.IOException; -import java.net.URL; -import java.net.URLClassLoader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -32,7 +18,6 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; -import java.util.stream.Collectors; public class JavaTXCompilerTest { From 268056542b7e3ac368cf9057ceecd679c3a0d62d Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 10 Jan 2018 11:36:29 +0100 Subject: [PATCH 004/208] =?UTF-8?q?erzeugt=20bytecode=20f=C3=BCr=20generic?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 122 ++++++++++++-- .../bytecode/BytecodeGenMethod.java | 47 ++++-- .../bytecode/DescriptorToString.java | 73 ++++++++- .../bytecode/MethodFromMethodCall.java | 29 +++- .../bytecode/NormalConstructor.java | 21 ++- .../dhbwstuttgart/bytecode/NormalMethod.java | 28 +++- src/de/dhbwstuttgart/bytecode/Signature.java | 155 ++++++++++++++++++ .../dhbwstuttgart/bytecode/TypeToString.java | 38 +++++ test/bytecode/Generics.jav | 2 + test/bytecode/Generics2.jav | 6 + test/bytecode/Generics2Test.java | 7 + test/bytecode/GenericsTest.java | 7 + test/bytecode/InterfaceTest.java | 7 + test/bytecode/JavaTXCompilerTest.java | 36 ++-- test/bytecode/LamAssign.jav | 4 + test/bytecode/LamAssignTest.java | 7 + 16 files changed, 526 insertions(+), 63 deletions(-) create mode 100644 src/de/dhbwstuttgart/bytecode/Signature.java create mode 100644 src/de/dhbwstuttgart/bytecode/TypeToString.java create mode 100644 test/bytecode/Generics2.jav create mode 100644 test/bytecode/Generics2Test.java create mode 100644 test/bytecode/GenericsTest.java create mode 100644 test/bytecode/InterfaceTest.java create mode 100644 test/bytecode/LamAssignTest.java diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 1b6ef47b..f1994c20 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.bytecode; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -8,6 +9,9 @@ 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.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.*; @@ -16,6 +20,7 @@ import de.dhbwstuttgart.syntaxtree.statement.literal.Null; 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.typeinference.result.ResultSet; @@ -23,7 +28,6 @@ import de.dhbwstuttgart.typeinference.result.ResultSet; public class BytecodeGen implements ASTVisitor { ClassWriter cw =new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); -// String methDesc; String type; @@ -34,6 +38,12 @@ public class BytecodeGen implements ASTVisitor { // stores parameter, local vars and the next index on the local variable table, which use for aload_i, astore_i,... HashMap paramsAndLocals = new HashMap<>(); + // stores generics and their bounds of class + HashMap genericsAndBounds = new HashMap<>(); + // stores generics and their bounds of method + HashMap genericsAndBoundsMethod = new HashMap<>(); + + HashMap methodParamsAndTypes = new HashMap<>(); byte[] bytecode; HashMap classFiles; @@ -45,14 +55,19 @@ public class BytecodeGen implements ASTVisitor { @Override public void visit(SourceFile sourceFile) { for(ClassOrInterface cl : sourceFile.getClasses()) { - isInterface = (cl.getModifiers()&512)==512; - System.out.println("IS Interface = "+"modifiers= "+cl.getModifiers()+" ->"+(cl.getModifiers()&512) + isInterface); BytecodeGen classGen = new BytecodeGen(classFiles, resultSet); cl.accept(classGen); + System.out.println("In CLASS: "+(cl.getClassName().toString())); classGen.writeClass(cl.getClassName().toString()); } } - + + /** + * Associates the bytecode of the class that was build with the classWriter {@link #cw} + * with the class name in the map {@link #classFiles} + * + * @param name name of the class with which the the bytecode is to be associated + */ private void writeClass(String name) { bytecode = cw.toByteArray(); classFiles.put(name, bytecode); @@ -62,12 +77,30 @@ public class BytecodeGen implements ASTVisitor { public HashMap getClassFiles() { return classFiles; } + + @Override public void visit(ClassOrInterface classOrInterface) { className = classOrInterface.getClassName().toString(); - // access flages?? - cw.visit(Opcodes.V1_8, classOrInterface.getModifiers()+Opcodes.ACC_SUPER, classOrInterface.getClassName().toString() - , null, classOrInterface.getSuperClass().toString().replace(".", "/"), 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 + * Signature looks like: + * Superclass + */ + if(classOrInterface.getGenerics().iterator().hasNext()) { + Signature signature = new Signature(classOrInterface, genericsAndBounds); + + System.out.println(signature.toString()); + sig = signature.toString(); + } + // needs implemented Interfaces? + cw.visit(Opcodes.V1_8, acc, classOrInterface.getClassName().toString() + , sig, classOrInterface.getSuperClass().acceptTV(new TypeToDescriptor()), null); // for each field in the class for(Field f : classOrInterface.getFieldDecl()) { @@ -85,17 +118,34 @@ public class BytecodeGen implements ASTVisitor { } cw.visitSource(classOrInterface.getClassName().toString()+".jav", null); } - + @Override public void visit(Constructor field) { - NormalConstructor constructor = new NormalConstructor(field); - String desc = constructor.accept(new DescriptorToString(resultSet)); - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", desc, null, null); + field.getParameterList().accept(this); + + String desc = null; + boolean hasGen = false; + for(String paramName : methodParamsAndTypes.keySet()) { + genericsAndBounds.containsKey(paramName); + hasGen = true; + } + 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,desc,cw,isInterface); - - mv.visitInsn(Opcodes.RETURN); + BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,field, mv,paramsAndLocals,cw, + genericsAndBoundsMethod,genericsAndBounds,isInterface); + if(!field.getParameterList().iterator().hasNext()) { + mv.visitInsn(Opcodes.RETURN); + } mv.visitMaxs(0, 0); mv.visitEnd(); } @@ -106,14 +156,48 @@ public class BytecodeGen implements ASTVisitor { // else it will be stored in pos 1 and this will be stored in pos 0 method.getParameterList().accept(this); - NormalMethod meth = new NormalMethod(method); - String methDesc = meth.accept(new DescriptorToString(resultSet)); + String methDesc = null; + + // Method getModifiers() ? + int acc = isInterface?Opcodes.ACC_ABSTRACT:0; System.out.println("-----Method-----"); - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), methDesc, null, null); + + boolean hasGenInParameterList = genericsAndBounds.containsKey(method.getReturnType().acceptTV(new TypeToDescriptor())); + if(!hasGenInParameterList) { + for(String paramName : methodParamsAndTypes.keySet()) { + if(genericsAndBounds.containsKey(paramName)) { + hasGenInParameterList = true; + break; + } + } + } + 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) { + // 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(); - BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,method, mv,paramsAndLocals,methDesc,cw,isInterface); + BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,method, mv,paramsAndLocals,cw,genericsAndBounds,genericsAndBounds,isInterface); mv.visitMaxs(0, 0); mv.visitEnd(); } @@ -121,11 +205,13 @@ public class BytecodeGen implements ASTVisitor { @Override public void visit(ParameterList formalParameters) { paramsAndLocals = new HashMap<>(); + methodParamsAndTypes = new HashMap<>(); Iterator itr = formalParameters.iterator(); int i = 1; while(itr.hasNext()) { FormalParameter fp = itr.next(); paramsAndLocals.put(fp.getName(), i); + methodParamsAndTypes.put(fp.getName(), fp.getType()); fp.accept(this); i++; } diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index ffdf1450..5368cf93 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -21,6 +21,7 @@ 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; @@ -29,12 +30,13 @@ public class BytecodeGenMethod implements StatementVisitor{ private Method m; private MethodVisitor mv; private HashMap paramsAndLocals = new HashMap<>(); - private String desc; private String className; private int lamCounter; private ClassWriter cw; private ResultSet resultSet; private boolean isInterface; + HashMap genericsAndBoundsMethod; + private HashMap genericsAndBounds; //for tests ** private String fieldName; @@ -46,8 +48,9 @@ public class BytecodeGenMethod implements StatementVisitor{ private ArrayList varsFunInterface; - public BytecodeGenMethod(String className,ResultSet resultSet, Method m, MethodVisitor mv, HashMap paramsAndLocals, - String desc, ClassWriter cw, boolean isInterface) { + public BytecodeGenMethod(String className,ResultSet resultSet, Method m, MethodVisitor mv, + HashMap paramsAndLocals, ClassWriter cw, + HashMap genericsAndBoundsMethod, HashMap genericsAndBounds, boolean isInterface) { this.where = "<<<<<< NORMAL METHOD >>>>>>"; @@ -56,29 +59,32 @@ public class BytecodeGenMethod implements StatementVisitor{ this.m = m; this.mv = mv; this.paramsAndLocals = paramsAndLocals; - this.desc = desc; this.cw = cw; + this.genericsAndBoundsMethod = genericsAndBoundsMethod; + this.genericsAndBounds = genericsAndBounds; this.isInterface = isInterface; this.lamCounter = -1; this.varsFunInterface = new ArrayList<>(); System.out.println("PARAMS = "+this.paramsAndLocals.size()); - this.m.block.accept(this); - System.out.println("PARAMS = "+this.paramsAndLocals.size()); - for(int i = 0; i(); @@ -114,7 +120,7 @@ public class BytecodeGenMethod implements StatementVisitor{ superCall.receiver.accept(this); superCall.arglist.accept(this); // mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", superCall.name, desc,false); - mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Object.class), superCall.name, desc,isInterface); + mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Object.class), superCall.name, "()V",isInterface); } // ?? @@ -151,9 +157,6 @@ public class BytecodeGenMethod implements StatementVisitor{ assign.rightSide.accept(this); assign.lefSide.accept(this); } - - - } @Override @@ -165,7 +168,9 @@ public class BytecodeGenMethod implements StatementVisitor{ 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 @@ -177,7 +182,9 @@ 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;"; // Type erasure +// Type arg1 = Type.getMethodType(typeErasure); Type arg1 = Type.getMethodType(lamDesc); // real Type Type arg3 = Type.getMethodType(lamDesc); @@ -209,7 +216,7 @@ 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,arg3.toString(),indexOfFirstParamLam,isInterface); + new BytecodeGenMethod(lambdaExpression,this.resultSet,mvLambdaBody,indexOfFirstParamLam,isInterface); mvLambdaBody.visitMaxs(0, 0); mvLambdaBody.visitEnd(); @@ -277,7 +284,8 @@ public class BytecodeGenMethod implements StatementVisitor{ methodCall.receiver.accept(this); methodCall.arglist.accept(this); - MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType()); + MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType(), + genericsAndBoundsMethod,genericsAndBounds); String mDesc = method.accept(new DescriptorToString(resultSet)); System.out.println("is Vars empty: "+varsFunInterface.isEmpty()); @@ -287,6 +295,7 @@ public class BytecodeGenMethod implements StatementVisitor{ 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); } @@ -397,6 +406,8 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(AssignToField assignLeftSide) { +// temporäre Lösung für testen, bis ich weiss wie man funktionale +// interfaces erkennt if(isRightSideALambda) varsFunInterface.add(assignLeftSide.field.getType()); // Loads the an object reference from the local variable diff --git a/src/de/dhbwstuttgart/bytecode/DescriptorToString.java b/src/de/dhbwstuttgart/bytecode/DescriptorToString.java index 044218e4..481dbc5d 100644 --- a/src/de/dhbwstuttgart/bytecode/DescriptorToString.java +++ b/src/de/dhbwstuttgart/bytecode/DescriptorToString.java @@ -30,9 +30,37 @@ public class DescriptorToString implements DescriptorVisitor{ Iterator itr = method.getParameterList().iterator(); while(itr.hasNext()) { FormalParameter fp = itr.next(); - desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + if(method.hasGen()) { + String fpDesc = fp.getType().acceptTV(new TypeToDescriptor()); + if(method.getGenericsAndBoundsMethod().containsKey(fpDesc)) { + desc += "L"+method.getGenericsAndBoundsMethod().get(fpDesc)+ ";"; + }else if(method.getGenericsAndBounds().containsKey(fpDesc)){ + desc += "L"+method.getGenericsAndBounds().get(fpDesc)+ ";"; + }else { + desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + }else { + desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } } - desc = addReturnType(desc,method.getReturnType(), resultSet); + + if(resultSet.resolveType(method.getReturnType()).resolvedType.toString().equals("void")) { + desc += ")V"; + }else { + if(method.hasGen()) { + String ret = method.getReturnType().acceptTV(new TypeToDescriptor()); + if(method.getGenericsAndBoundsMethod().containsKey(ret)) { + desc += ")L"+method.getGenericsAndBoundsMethod().get(ret)+ ";"; + }else if(method.getGenericsAndBounds().containsKey(ret)){ + desc += ")L"+method.getGenericsAndBounds().get(ret)+ ";"; + }else { + desc += ")" + "L"+resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + }else { + desc += ")" + "L"+resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + } +// desc = addReturnType(desc,method.getReturnType(), resultSet); return desc; } @@ -42,7 +70,19 @@ public class DescriptorToString implements DescriptorVisitor{ Iterator itr = constructor.getParameterList().iterator(); while(itr.hasNext()) { FormalParameter fp = itr.next(); - desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + if(constructor.hasGen()) { + System.out.println("Cons has Gens"); + String fpDesc = fp.getType().acceptTV(new TypeToDescriptor()); + 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"); + desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } } desc = desc + ")V"; return desc; @@ -56,6 +96,7 @@ 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; } @@ -75,10 +116,30 @@ public class DescriptorToString implements DescriptorVisitor{ @Override public String visit(MethodFromMethodCall methodFromMethodCall) { String desc = "("; - for(Expression e : methodFromMethodCall.argList.getArguments()) { - desc = desc + "L"+resultSet.resolveType(e.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + for(Expression e : methodFromMethodCall.getArgList().getArguments()) { + String d = e.getType().acceptTV(new TypeToDescriptor()); + if(methodFromMethodCall.getGenericsAndBoundsMethod().containsKey(d)) { + desc += "L"+methodFromMethodCall.getGenericsAndBoundsMethod().get(d)+ ";"; + }else if(methodFromMethodCall.getGenericsAndBounds().containsKey(d)) { + desc += "L"+methodFromMethodCall.getGenericsAndBounds().get(d)+ ";"; + }else { + desc += "L"+resultSet.resolveType(e.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } } - desc = addReturnType(desc, methodFromMethodCall.returnType, resultSet); + + if(resultSet.resolveType(methodFromMethodCall.getReturnType()).resolvedType.toString().equals("void")) { + desc += ")V"; + }else { + String ret = resultSet.resolveType(methodFromMethodCall.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor()); + if(methodFromMethodCall.getGenericsAndBoundsMethod().containsKey(ret)) { + desc += ")L"+methodFromMethodCall.getGenericsAndBoundsMethod().get(ret)+ ";"; + }else if(methodFromMethodCall.getGenericsAndBounds().containsKey(ret)){ + desc += ")L"+methodFromMethodCall.getGenericsAndBounds().get(ret)+ ";"; + }else { + desc += ")" + "L"+resultSet.resolveType(methodFromMethodCall.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + } +// desc = addReturnType(desc, methodFromMethodCall.getReturnType(), resultSet); return desc; } diff --git a/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java b/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java index 034e2abf..330b0666 100644 --- a/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java +++ b/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java @@ -1,15 +1,38 @@ package de.dhbwstuttgart.bytecode; +import java.util.HashMap; + import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; public class MethodFromMethodCall { - ArgumentList argList; - RefTypeOrTPHOrWildcardOrGeneric returnType; + private ArgumentList argList; + private RefTypeOrTPHOrWildcardOrGeneric returnType; + private HashMap genericsAndBoundsMethod; + private HashMap genericsAndBounds; - public MethodFromMethodCall(ArgumentList argList,RefTypeOrTPHOrWildcardOrGeneric returnType) { + public MethodFromMethodCall(ArgumentList argList,RefTypeOrTPHOrWildcardOrGeneric returnType, + HashMap genericsAndBoundsMethod,HashMap genericsAndBounds) { this.argList = argList; this.returnType = returnType; + this.genericsAndBoundsMethod = genericsAndBoundsMethod; + this.genericsAndBounds = genericsAndBounds; + } + + public ArgumentList getArgList() { + return argList; + } + + public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { + return returnType; + } + + public HashMap getGenericsAndBoundsMethod(){ + return genericsAndBoundsMethod; + } + + public HashMap getGenericsAndBounds(){ + return genericsAndBounds; } public String accept(DescriptorVisitor descVisitor) { diff --git a/src/de/dhbwstuttgart/bytecode/NormalConstructor.java b/src/de/dhbwstuttgart/bytecode/NormalConstructor.java index d029d21e..d2174fd4 100644 --- a/src/de/dhbwstuttgart/bytecode/NormalConstructor.java +++ b/src/de/dhbwstuttgart/bytecode/NormalConstructor.java @@ -1,15 +1,34 @@ package de.dhbwstuttgart.bytecode; +import java.util.HashMap; + import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.ParameterList; public class NormalConstructor { private Constructor constructor; + private HashMap genericsAndBounds; + private boolean hasGenerics; - public NormalConstructor(Constructor constructor) { + public NormalConstructor(Constructor constructor, boolean hasGenerics) { this.constructor = constructor; + this.hasGenerics = hasGenerics; } + public NormalConstructor(Constructor constructor, HashMap genericsAndBounds, boolean hasGenerics) { + this.constructor = constructor; + this.genericsAndBounds = genericsAndBounds; + this.hasGenerics = hasGenerics; + } + + public HashMap getGenericsAndBounds() { + return genericsAndBounds; + } + + public boolean hasGen() { + return hasGenerics; + } + public ParameterList getParameterList() { return constructor.getParameterList(); } diff --git a/src/de/dhbwstuttgart/bytecode/NormalMethod.java b/src/de/dhbwstuttgart/bytecode/NormalMethod.java index 2dd81d78..3f6bf2b4 100644 --- a/src/de/dhbwstuttgart/bytecode/NormalMethod.java +++ b/src/de/dhbwstuttgart/bytecode/NormalMethod.java @@ -1,14 +1,28 @@ package de.dhbwstuttgart.bytecode; +import java.util.HashMap; + import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.ParameterList; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; public class NormalMethod { private Method method; + private HashMap genericsAndBounds; + private HashMap genericsAndBoundsMethod; + private boolean hasGenerics; - public NormalMethod(Method method) { + public NormalMethod(Method method, boolean hasGenerics) { this.method = method; + this.hasGenerics = hasGenerics; + } + + public NormalMethod(Method method, HashMap genericsAndBounds, + HashMap genericsAndBoundsMethod,boolean hasGenerics) { + this.method = method; + this.genericsAndBounds = genericsAndBounds; + this.genericsAndBoundsMethod = genericsAndBoundsMethod; + this.hasGenerics = hasGenerics; } public Method getMethod() { @@ -19,10 +33,22 @@ public class NormalMethod { return method.getParameterList(); } + public HashMap getGenericsAndBounds(){ + return genericsAndBounds; + } + + public HashMap getGenericsAndBoundsMethod(){ + return genericsAndBoundsMethod; + } + public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { return method.getType(); } + public boolean hasGen() { + return this.hasGenerics; + } + public String accept(DescriptorVisitor descVisitor) { return descVisitor.visit(this); } diff --git a/src/de/dhbwstuttgart/bytecode/Signature.java b/src/de/dhbwstuttgart/bytecode/Signature.java new file mode 100644 index 00000000..362429a3 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/Signature.java @@ -0,0 +1,155 @@ +package de.dhbwstuttgart.bytecode; + +import java.util.HashMap; +import java.util.Iterator; + +import org.objectweb.asm.signature.SignatureVisitor; +import org.objectweb.asm.signature.SignatureWriter; + +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +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.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.typeinference.result.ResultSet; + +public class Signature { + private ClassOrInterface classOrInterface; + private HashMap genericsAndBounds; + private HashMap genericsAndBoundsMethod; + private SignatureWriter sw; + private Constructor constructor; + private Method method; + private HashMap methodParamsAndTypes; + private ResultSet resultSet; + + public Signature(ClassOrInterface classOrInterface, HashMap genericsAndBounds) { + this.classOrInterface = classOrInterface; + this.genericsAndBounds = genericsAndBounds; + sw = new SignatureWriter(); + createSignatureForClassOrInterface(); + } + + public Signature(Constructor constructor, HashMap genericsAndBounds, HashMap methodParamsAndTypes) { + this.constructor = constructor; + this.genericsAndBounds = genericsAndBounds; + this.methodParamsAndTypes = methodParamsAndTypes; + sw = new SignatureWriter(); + createSignatureForConsOrMethod(this.constructor,true); + } + + public Signature(Method method, HashMap genericsAndBoundsMethod, + HashMap methodParamsAndTypes, ResultSet resultSet) { + this.method = method; + this.genericsAndBoundsMethod = genericsAndBoundsMethod; + this.methodParamsAndTypes = methodParamsAndTypes; + this.resultSet = resultSet; + sw = new SignatureWriter(); + createSignatureForConsOrMethod(this.method,false); + } + + /** + * Creates signature for a method or constructor with @see {@link SignatureWriter} + * Signature looks like: + * (params L.. OR T.. Or basistape)ReturnType + * + * @param method method or constructor + * @param isConstructor true if constructor + */ + private void createSignatureForConsOrMethod(Method method, boolean isConstructor) { + Iterator itr = method.getGenerics().iterator(); + // visits all formal type parameter and visits their bounds + while(itr.hasNext()) { + GenericTypeVar g = itr.next(); + getBoundsOfTypeVar(g,genericsAndBoundsMethod); + } + // visits each method-parameter to create the signature + for(String paramName : methodParamsAndTypes.keySet()) { + RefTypeOrTPHOrWildcardOrGeneric t = methodParamsAndTypes.get(paramName); + // parameter type deswegen ist true + doVisitParamsOrReturn(t,true); + } + if(isConstructor) { + sw.visitReturnType().visitBaseType('V'); + }else { + RefTypeOrTPHOrWildcardOrGeneric returnType = method.getReturnType(); + // return type deswegen ist false + doVisitParamsOrReturn(returnType, false); + } +// sw.visitEnd(); + } + /** + * Visits parameter type or return type with {@link SignatureVisitor} to create + * the method signature + * @param t type of parameter or return type + * @param isParameterType true if t is type of parameter + */ + private void doVisitParamsOrReturn(RefTypeOrTPHOrWildcardOrGeneric t, boolean isParameterType) { + String type = t.acceptTV(new TypeToString()); + SignatureVisitor sv; + if(isParameterType) { + sv = sw.visitParameterType(); + } + else { + sv = sw.visitReturnType(); + } + switch (type) { + case "RT": + sv.visitClassType(t.acceptTV(new TypeToDescriptor())); + break; + case "GRT": + GenericRefType g = (GenericRefType) t; + sv.visitTypeVariable(g.getParsedName()); + break; + case "TPH": + System.out.println(resultSet.resolveType(t).resolvedType.acceptTV(new TypeToDescriptor())); +// sv.visitInterface().visitClassType(resultSet.resolveType(t).resolvedType.acceptTV(new TypeToDescriptor())+";"); + + break; + default: + if(!isParameterType) + sv.visitBaseType('V'); + break; + } + } + /** + * Creates signature for class or interface with {@link SignatureWriter} + * Signature looks like: + * superclass + */ + private void createSignatureForClassOrInterface() { + Iterator itr = classOrInterface.getGenerics().iterator(); + + while(itr.hasNext()) { + GenericTypeVar g = itr.next(); + getBoundsOfTypeVar(g,genericsAndBounds); + } + + sw.visitSuperclass().visitClassType(classOrInterface.getSuperClass().acceptTV(new TypeToDescriptor()));; + sw.visitEnd(); + } + /** + * Get bounds of type variable + * @param g type variable + * @param genAndBounds + */ + private void getBoundsOfTypeVar(GenericTypeVar g, HashMap genAndBounds) { + sw.visitFormalTypeParameter(g.getParsedName()); + + Iterator bItr = g.getBounds().iterator(); + while(bItr.hasNext()) { + RefTypeOrTPHOrWildcardOrGeneric b =bItr.next(); + String boundDesc = b.acceptTV(new TypeToDescriptor()); + // Ensure that <...> extends java.lang.Object OR ... + sw.visitClassBound().visitClassType(boundDesc); + genAndBounds.put(g.getParsedName(), boundDesc); + } + sw.visitClassBound().visitEnd(); + } + + public String toString() { + return sw.toString(); + } + +} diff --git a/src/de/dhbwstuttgart/bytecode/TypeToString.java b/src/de/dhbwstuttgart/bytecode/TypeToString.java new file mode 100644 index 00000000..86d4124a --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/TypeToString.java @@ -0,0 +1,38 @@ +package de.dhbwstuttgart.bytecode; + +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.SuperWildcardType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.type.TypeVisitor; + +public class TypeToString implements TypeVisitor{ + + @Override + public String visit(RefType refType) { + return "RT"; + } + + @Override + public String visit(SuperWildcardType superWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(TypePlaceholder typePlaceholder) { + return "TPH"; + } + + @Override + public String visit(ExtendsWildcardType extendsWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(GenericRefType genericRefType) { + return "GRT"; + } + +} diff --git a/test/bytecode/Generics.jav b/test/bytecode/Generics.jav index 2c7a67b6..bb7b2af5 100644 --- a/test/bytecode/Generics.jav +++ b/test/bytecode/Generics.jav @@ -1,5 +1,7 @@ class Generics { + Generics(B b){ + } B mt1(B b){ return mt1(b); } diff --git a/test/bytecode/Generics2.jav b/test/bytecode/Generics2.jav new file mode 100644 index 00000000..52d5caa2 --- /dev/null +++ b/test/bytecode/Generics2.jav @@ -0,0 +1,6 @@ +class Generics2{ + B m1(B b){ + return b; + } + +} \ No newline at end of file diff --git a/test/bytecode/Generics2Test.java b/test/bytecode/Generics2Test.java new file mode 100644 index 00000000..26e52665 --- /dev/null +++ b/test/bytecode/Generics2Test.java @@ -0,0 +1,7 @@ +package bytecode; + +public class Generics2Test extends JavaTXCompilerTest{ + public Generics2Test() { + this.fileName = "Generics2"; + } +} diff --git a/test/bytecode/GenericsTest.java b/test/bytecode/GenericsTest.java new file mode 100644 index 00000000..cca16129 --- /dev/null +++ b/test/bytecode/GenericsTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class GenericsTest extends JavaTXCompilerTest { + public GenericsTest() { + this.fileName = "Generics"; + } +} diff --git a/test/bytecode/InterfaceTest.java b/test/bytecode/InterfaceTest.java new file mode 100644 index 00000000..ed378127 --- /dev/null +++ b/test/bytecode/InterfaceTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class InterfaceTest extends JavaTXCompilerTest{ + public InterfaceTest() { + this.fileName = "Interface1"; + } +} diff --git a/test/bytecode/JavaTXCompilerTest.java b/test/bytecode/JavaTXCompilerTest.java index 39ff5084..f017e23e 100644 --- a/test/bytecode/JavaTXCompilerTest.java +++ b/test/bytecode/JavaTXCompilerTest.java @@ -25,11 +25,12 @@ public class JavaTXCompilerTest { private static final String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; private static final List filesToTest = new ArrayList<>(); - + + protected String fileName = ""; + @Test public void test() throws IOException, java.lang.ClassNotFoundException { System.out.println(rootDirectory); - String fileName = "Faculty"; filesToTest.add(new File(rootDirectory+fileName+".jav")); System.out.println(rootDirectory+fileName+".jav"); JavaTXCompiler compiler = new JavaTXCompiler(filesToTest); @@ -48,7 +49,7 @@ public class JavaTXCompilerTest { if(pos != -1) { name = f.getName().substring(0, pos); } - this.writeClassFile(bytecode, name); + this.writeClassFile(bytecode); } } @@ -61,20 +62,23 @@ public class JavaTXCompilerTest { return bytecodeGen.getClassFiles(); } - public void writeClassFile(HashMap classFiles, String name) { + public void writeClassFile(HashMap classFiles) { FileOutputStream output; - byte[] bytecode = classFiles.get(name); - try { - System.out.println("generating .class file"); - output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" +name+".class")); - output.write(bytecode); - output.close(); - System.out.println(".class file generated"); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } + 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")); + output.write(bytecode); + output.close(); + System.out.println(name+".class file generated"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } static String readFile(String path, Charset encoding) diff --git a/test/bytecode/LamAssign.jav b/test/bytecode/LamAssign.jav index e522bd3b..3df81780 100644 --- a/test/bytecode/LamAssign.jav +++ b/test/bytecode/LamAssign.jav @@ -7,3 +7,7 @@ class LamAssign { return lam1; } } + +interface Fun1{ + A apply(B b); +} diff --git a/test/bytecode/LamAssignTest.java b/test/bytecode/LamAssignTest.java new file mode 100644 index 00000000..3442c1d1 --- /dev/null +++ b/test/bytecode/LamAssignTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class LamAssignTest extends JavaTXCompilerTest{ + public LamAssignTest() { + this.fileName = "LamAssign"; + } +} From ab86cc32295c46ad2bae473aeaae4c836405d411 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 10 Jan 2018 12:10:01 +0100 Subject: [PATCH 005/208] RefType.toString(): Ausgabe von Parameterliste implementieren --- src/de/dhbwstuttgart/syntaxtree/type/RefType.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 002e792d..4cf85297 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -6,6 +6,7 @@ import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; import org.antlr.v4.runtime.Token; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; @@ -28,7 +29,15 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric @Override public String toString(){ - return this.name.toString(); + String params = "<"; + Iterator it = parameter.iterator(); + while(it.hasNext()){ + RefTypeOrTPHOrWildcardOrGeneric param = it.next(); + params += param.toString(); + if(it.hasNext())params += ", "; + } + params += ">"; + return this.name.toString() + params; } @Override From 672373fc8d5d0cf1179b5c2ba37853d9ed1e8de2 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 10 Jan 2018 12:11:51 +0100 Subject: [PATCH 006/208] --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 007/208] --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 008/208] =?UTF-8?q?bytecode=20f=C3=BCr=20Generics:=20LamAs?= =?UTF-8?q?signTest=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 009/208] =?UTF-8?q?erzeugt=20Klassendatei=20f=C3=BCr=20Fun?= =?UTF-8?q?N?= 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 1f5071d4e4d2ac7deaa81fd77e8dd0387db1bb6a Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Fri, 19 Jan 2018 15:24:49 +0100 Subject: [PATCH 010/208] =?UTF-8?q?Methode=20soll=20kein=20Feld=20mehr=20s?= =?UTF-8?q?ein.=20Nicht=20lauff=C3=A4hig!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/syntaxtree/Field.java | 2 +- src/de/dhbwstuttgart/syntaxtree/Method.java | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/Field.java b/src/de/dhbwstuttgart/syntaxtree/Field.java index 421f0ff3..4ee1675e 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Field.java +++ b/src/de/dhbwstuttgart/syntaxtree/Field.java @@ -5,7 +5,7 @@ import org.antlr.v4.runtime.Token; import java.util.ArrayList; -public class Field extends SyntaxTreeNode implements TypeScope{ +public class Field extends SyntaxTreeNode { private String name; private RefTypeOrTPHOrWildcardOrGeneric type; diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index 3686fb4e..fd4ad180 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -22,7 +22,7 @@ import de.dhbwstuttgart.syntaxtree.statement.Block; * @author janulrich * */ -public class Method extends Field implements IItemWithOffset +public class Method implements IItemWithOffset, TypeScope { public final Block block; private ParameterList parameterlist = new ParameterList(new ArrayList<>(), new NullToken()); @@ -54,4 +54,9 @@ public class Method extends Field implements IItemWithOffset public void accept(ASTVisitor visitor) { visitor.visit(this); } + + @Override + public Token getOffset() { + return null; + } } From b7aca993012539d2a6c55376f8430ca9a66fa932 Mon Sep 17 00:00:00 2001 From: Andreas Stadelmeier Date: Fri, 19 Jan 2018 16:36:08 +0100 Subject: [PATCH 011/208] local maven repository --- pom.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pom.xml b/pom.xml index af770407..3f5d4ae9 100644 --- a/pom.xml +++ b/pom.xml @@ -105,4 +105,11 @@ 1.8 1.8 + + + maven-repository + MyCo Internal Repository + file:///${project.basedir}/maven-repository/ + + From 47689f2fc6ac1faf9fef40cd66a0d24f1f9b0b9f Mon Sep 17 00:00:00 2001 From: Andreas Stadelmeier Date: Fri, 19 Jan 2018 16:55:23 +0100 Subject: [PATCH 012/208] ZIP-FIle erstellen --- README.md | 14 ++++++++++++++ pom.xml | 15 +++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 00000000..9ba3499d --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ + +# plugin site erstellen +* die JAvaTXCOmpiler DAtei in ein plugin umwandeln und deployen. +* siehe: http://www.vogella.com/tutorials/EclipseJarToPlugin/article.html#convert-jar-files-to-osgi-bundles-with-the-p2-maven-plugin +* AUsführung: + * mvn deploy #erstellt die JAR-Datei und steckt sie in ein lokales Repo (maven-repository) + * mvn p2:site + * mvn package # hier wird die ZIP-Filf zum Einbinden in Eclipse erstellt + +# Einbinden in Eclipse +* In Eclipse kann die Zip-FIle wie ein Plugin installiert werden +* Hier tritt FEhler auf. Reflections-Library kann nicht installiert werden. Möglicherweise wird sie auch nicht gebraucht +* Nach dem installieren de.dhbwstuttagrt.JavaTXcompiler zu den DEpendencies des plugins hinzufügen +* Anschließend unter "Overview" auf "Updata Classpath" klicken diff --git a/pom.xml b/pom.xml index 3f5d4ae9..b30c80c0 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,7 @@ de.dhbwstuttgart JavaTXcompiler jar + 0.1 JavaTXcompiler http://maven.apache.org @@ -86,6 +87,19 @@ + + + org.eclipse.tycho + tycho-p2-repository-plugin + ${tycho.version} + + + package + + archive-repository + + + @@ -104,6 +118,7 @@ 1.8 1.8 + 0.23.0 From a08e2e88158d40aefff717b7dbdc6fa78350b35c Mon Sep 17 00:00:00 2001 From: Andreas Stadelmeier Date: Fri, 19 Jan 2018 17:48:25 +0100 Subject: [PATCH 013/208] Versuche google und reflections auch noch als plugin zu verpacken. WIll nicht funktionieren --- pom.xml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index b30c80c0..ce7d7ded 100644 --- a/pom.xml +++ b/pom.xml @@ -83,6 +83,8 @@ de.dhbwstuttgart:JavaTXcompiler:0.1 + org.reflections:reflections:0.9.11 + com.google.guava:guava:22:0 @@ -110,10 +112,10 @@ - + maven-repository file:///${project.basedir}/target - + 1.8 From 8d12821c681fde205636d83f75b57c013eaa7f29 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Sun, 21 Jan 2018 11:38:55 +0100 Subject: [PATCH 014/208] Methode erbt nicht mehr von Feld --- .../dhbwstuttgart/bytecode/NormalMethod.java | 2 +- .../SyntaxTreeGenerator.java | 7 +++---- .../syntaxtree/AbstractASTWalker.java | 2 +- .../dhbwstuttgart/syntaxtree/Constructor.java | 4 ++-- src/de/dhbwstuttgart/syntaxtree/Field.java | 6 ++++-- src/de/dhbwstuttgart/syntaxtree/Method.java | 18 ++++++++++++++---- .../syntaxtree/factory/ASTFactory.java | 5 ++--- .../syntaxtree/visual/OutputGenerator.java | 2 +- .../typeinference/typeAlgo/TYPEStmt.java | 2 +- 9 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/de/dhbwstuttgart/bytecode/NormalMethod.java b/src/de/dhbwstuttgart/bytecode/NormalMethod.java index 3f6bf2b4..16e84cc5 100644 --- a/src/de/dhbwstuttgart/bytecode/NormalMethod.java +++ b/src/de/dhbwstuttgart/bytecode/NormalMethod.java @@ -42,7 +42,7 @@ public class NormalMethod { } public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { - return method.getType(); + return method.getReturnType(); } public boolean hasGen() { diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index c5b135e6..3d9d7aca 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -139,9 +139,9 @@ public class SyntaxTreeGenerator{ block = stmtGen.convert(body.block()); } if(parentClass.equals(new JavaClassName(name))){ - return new Constructor(modifiers, name, retType, modifiers, parameterList, block, gtvDeclarations, header.getStart(), fieldInitializations); + return new Constructor(modifiers, name, retType, parameterList, block, gtvDeclarations, header.getStart(), fieldInitializations); }else{ - return new Method(modifiers, name, retType, modifiers, parameterList,block, gtvDeclarations, header.getStart()); + return new Method(modifiers, name, retType, parameterList,block, gtvDeclarations, header.getStart()); } } @@ -221,10 +221,9 @@ public class SyntaxTreeGenerator{ */ private Constructor generateStandardConstructor(String className, JavaClassName parentClass, RefType superClass, GenericDeclarationList classGenerics, Token offset){ RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset); - int modifiers = 0; ParameterList params = new ParameterList(new ArrayList<>(), offset); Block block = new Block(new ArrayList<>(), offset); - return new Constructor(Modifier.PUBLIC, className, classType, modifiers, params, block, classGenerics, offset, fieldInitializations); + return new Constructor(Modifier.PUBLIC, className, classType, params, block, classGenerics, offset, fieldInitializations); } private RefType convert(Java8Parser.SuperclassContext superclass) { diff --git a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index d771bd93..8a66629b 100644 --- a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -61,7 +61,7 @@ public abstract class AbstractASTWalker implements ASTVisitor{ } private void visitMethod(Method method){ - method.getType().accept(this); + method.getReturnType().accept(this); method.getParameterList().accept(this); if(method.block != null) method.block.accept(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index bb48be40..a99692bb 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -14,9 +14,9 @@ public class Constructor extends Method { //TODO: Constructor braucht ein super-Statement - public Constructor(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block codeInsideConstructor, + public Constructor(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block codeInsideConstructor, GenericDeclarationList gtvDeclarations, Token offset, List fieldInitializations) { - super(modifier, name, returnType, modifiers, parameterList, prepareBlock(codeInsideConstructor,fieldInitializations), gtvDeclarations, offset); + super(modifier, name, returnType, parameterList, prepareBlock(codeInsideConstructor,fieldInitializations), gtvDeclarations, offset); } diff --git a/src/de/dhbwstuttgart/syntaxtree/Field.java b/src/de/dhbwstuttgart/syntaxtree/Field.java index 4ee1675e..8a4230e3 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Field.java +++ b/src/de/dhbwstuttgart/syntaxtree/Field.java @@ -5,8 +5,9 @@ import org.antlr.v4.runtime.Token; import java.util.ArrayList; -public class Field extends SyntaxTreeNode { - +public class Field extends SyntaxTreeNode implements TypeScope{ + + public final int modifier; private String name; private RefTypeOrTPHOrWildcardOrGeneric type; @@ -14,6 +15,7 @@ public class Field extends SyntaxTreeNode { super(offset); this.name = name; this.type = type; + this.modifier = modifier; } public String getName(){ diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index fd4ad180..99cd3f91 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -22,16 +22,22 @@ import de.dhbwstuttgart.syntaxtree.statement.Block; * @author janulrich * */ -public class Method implements IItemWithOffset, TypeScope +public class Method extends SyntaxTreeNode implements IItemWithOffset, TypeScope { public final Block block; + public final int modifier; + public final String name; private ParameterList parameterlist = new ParameterList(new ArrayList<>(), new NullToken()); private ExceptionList exceptionlist; private GenericDeclarationList generics; + private final RefTypeOrTPHOrWildcardOrGeneric returnType; - public Method(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block block, + public Method(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block block, GenericDeclarationList gtvDeclarations, Token offset) { - super(name, returnType, modifiers, offset); + super(offset); + this.name = name; + this.modifier = modifier; + this.returnType = returnType; this.parameterlist = parameterList; this.block = block; this.generics = gtvDeclarations; @@ -47,7 +53,7 @@ public class Method implements IItemWithOffset, TypeScope @Override public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { - return this.getType(); + return this.returnType; } @Override @@ -59,4 +65,8 @@ public class Method implements IItemWithOffset, TypeScope public Token getOffset() { return null; } + + public String getName() { + return name; + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 046b97b8..59e54385 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -88,7 +88,7 @@ public class ASTFactory { return null; } - return new de.dhbwstuttgart.syntaxtree.Constructor(constructor.getModifiers(), name,returnType, modifier, parameterList, block, gtvDeclarations, offset, new ArrayList<>()); + return new de.dhbwstuttgart.syntaxtree.Constructor(modifier, name,returnType, parameterList, block, gtvDeclarations, offset, new ArrayList<>()); } //private static RefType createType(Class classType) { @@ -109,9 +109,8 @@ public class ASTFactory { Block block = new Block(new ArrayList(), new NullToken()); GenericDeclarationList gtvDeclarations = createGenerics(jreMethod.getTypeParameters(), inClass, jreMethod.getName()); Token offset = new NullToken(); - int modifier = jreMethod.getModifiers(); - return new Method(jreMethod.getModifiers(), name,returnType, modifier, parameterList, block, gtvDeclarations, offset); + return new Method(jreMethod.getModifiers(), name,returnType, parameterList, block, gtvDeclarations, offset); } public static GenericDeclarationList createGenerics(TypeVariable[] typeParameters, Class context, String methodName){ diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index e760e458..d56c39d8 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -81,7 +81,7 @@ public class OutputGenerator implements ASTVisitor{ @Override public void visit(Method method) { - method.getType().accept(this); + method.getReturnType().accept(this); out.append(" " + method.getName()); method.getParameterList().accept(this); if(method.block != null) diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 2152fc65..3448e8e3 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -345,7 +345,7 @@ public class TYPEStmt implements StatementVisitor{ for(Method m : cl.getMethods()){ if(m.getName().equals(name) && m.getParameterList().getFormalparalist().size() == numArgs){ - RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getType()); + RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getReturnType()); ret.add(new MethodAssumption(cl, retType, convertParams(m.getParameterList(),info), createTypeScope(cl, m))); From 5c9b40e0b7bc4970d782f22eae8740a233882945 Mon Sep 17 00:00:00 2001 From: Andreas Stadelmeier Date: Wed, 24 Jan 2018 16:37:01 +0100 Subject: [PATCH 015/208] =?UTF-8?q?Rumprobieren.=20Nicht=20lauff=C3=A4hig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++++ pom.xml | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9ba3499d..8b1e7ddd 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ + # plugin site erstellen * die JAvaTXCOmpiler DAtei in ein plugin umwandeln und deployen. * siehe: http://www.vogella.com/tutorials/EclipseJarToPlugin/article.html#convert-jar-files-to-osgi-bundles-with-the-p2-maven-plugin @@ -12,3 +13,7 @@ * Hier tritt FEhler auf. Reflections-Library kann nicht installiert werden. Möglicherweise wird sie auch nicht gebraucht * Nach dem installieren de.dhbwstuttagrt.JavaTXcompiler zu den DEpendencies des plugins hinzufügen * Anschließend unter "Overview" auf "Updata Classpath" klicken + +# Windows +* JAVA_HOME setzen: + * export JAVA_HOME=/c/Program\ Files/Java/jdk1.8.0_102/ diff --git a/pom.xml b/pom.xml index ce7d7ded..138b0633 100644 --- a/pom.xml +++ b/pom.xml @@ -79,12 +79,14 @@ - + de.dhbwstuttgart:JavaTXcompiler:0.1 org.reflections:reflections:0.9.11 - com.google.guava:guava:22:0 + com.google.guava:guava:22.0 + javax.annotation:javax.annotation-api:1.3.1 + org.glassfish:javax.annotation:3.1.1 From 9186f5dff5df15ad5077552d7b84e956966f406f Mon Sep 17 00:00:00 2001 From: Andreas Stadelmeier Date: Wed, 24 Jan 2018 19:05:09 +0100 Subject: [PATCH 016/208] Alle Dependencies werden in eine Jar gepackt. Diese in Eclipseplugin umwandeln und einbinden funktioniert \o/ --- pom.xml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pom.xml b/pom.xml index 138b0633..86266e05 100644 --- a/pom.xml +++ b/pom.xml @@ -69,6 +69,22 @@ + + maven-assembly-plugin + + + package + + single + + + + + + jar-with-dependencies + + + org.reficio p2-maven-plugin From 0610ea8e63c02f2cfda8004e6b5bd95f54527175 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Mon, 29 Jan 2018 17:32:25 +0100 Subject: [PATCH 017/208] FC Generator --- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 1 + .../SyntaxTreeGenerator/FCGenerator.java | 148 ++++++++++++++++++ .../syntaxtree/factory/UnifyTypeFactory.java | 53 +------ 3 files changed, 151 insertions(+), 51 deletions(-) create mode 100644 src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 2abd8b9d..86666a49 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -89,6 +89,7 @@ public class JavaTXCompiler { final ConstraintSet cons = getConstraints(); FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses); + System.out.println(finiteClosure); ConstraintSet unifyCons = UnifyTypeFactory.convert(cons); TypeUnify unify = new TypeUnify(); diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java new file mode 100644 index 00000000..cbca13ce --- /dev/null +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -0,0 +1,148 @@ +package de.dhbwstuttgart.parser.SyntaxTreeGenerator; + +import de.dhbwstuttgart.exceptions.DebugException; +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; +import de.dhbwstuttgart.syntaxtree.type.*; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.model.*; + +import java.util.*; + +public class FCGenerator { + /** + * Baut die FiniteClosure aus availableClasses. + * Klassen welche nicht in availableClasses vorkommen werden im Java Classpath nachgeschlagen. + * + * @param availableClasses - Alle geparsten Klassen + */ + public static Set toFC(Collection availableClasses) throws ClassNotFoundException { + HashSet pairs = new HashSet<>(); + for(ClassOrInterface cly : availableClasses){ + pairs.addAll(getSuperTypes(cly, availableClasses)); + } + System.out.println(pairs); + return pairs; + } + + /** + * Bildet eine Kette vom übergebenen Typ bis hin zum höchsten bekannten Typ + * Als Generics werden TPHs benutzt, welche der Unifikationsalgorithmus korrekt interpretieren muss. + * Die verwendeten TPHs werden in der Kette nach oben gereicht, so erhält der selbe GTV immer den selben TPH + * @param forType + * @return + */ + private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses) throws ClassNotFoundException { + return getSuperTypes(forType, availableClasses, new HashMap<>()); + } + + //TODO: implements Interface auch als superklassen beachten + private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses, HashMap gtvs) throws ClassNotFoundException { + List params = new ArrayList<>(); + //Die GTVs, die in forType hinzukommen: + HashMap newGTVs = new HashMap<>(); + //Generics mit gleichem Namen müssen den selben TPH bekommen + for(GenericTypeVar gtv : forType.getGenerics()){ + if(!gtvs.containsKey(gtv.getParsedName())){ + gtvs.put(gtv.getParsedName(), PlaceholderType.freshPlaceholder()); + newGTVs.put(gtv.getParsedName(), PlaceholderType.freshPlaceholder()); + } + params.add(gtvs.get(gtv.getParsedName())); + } + + Optional hasSuperclass = availableClasses.stream().filter(cl -> forType.getSuperClass().getName().equals(cl.getClassName())).findAny(); + ClassOrInterface superClass; + if(!hasSuperclass.isPresent()) //Wenn es die Klasse in den available Klasses nicht gibt wird sie im Classpath gesucht. Ansonsten Exception + { + superClass = ASTFactory.createClass(ClassLoader.getSystemClassLoader().loadClass(forType.getSuperClass().getName().toString())); + }else{ + superClass = hasSuperclass.get(); + } + /* + Die Parameter der superklasse müssen jetzt nach den Angaben in der Subklasse + modifiziert werden + Beispie: Matrix extends Vector> + Den ersten Parameter mit Vector austauschen und dort alle Generics zu den Typplaceholdern in gtvs austauschen + */ + //Hier vermerken, welche Typen im der Superklasse ausgetauscht werden müssen + Iterator itGenParams = superClass.getGenerics().iterator(); + Iterator itSetParams = forType.getSuperClass().getParaList().iterator(); + while(itGenParams.hasNext()){ + RefTypeOrTPHOrWildcardOrGeneric setType = itSetParams.next(); + //In diesem Typ die GTVs durch TPHs und Einsetzungen austauschen: + UnifyType setSetType = setType.acceptTV(new TypeExchanger(gtvs)); + newGTVs.put(itGenParams.next().getParsedName(), setSetType); + } + + UnifyType superType = forType.getSuperClass().acceptTV(new TypeExchanger(newGTVs)); + + TypeParams paramList = new TypeParams(params); + UnifyType t1 = new ReferenceType(forType.getClassName().toString(), paramList); + UnifyType t2 = superType; + + UnifyPair ret = UnifyTypeFactory.generateSmallerPair(t1, t2); + + List superTypes; + //Rekursiver Aufruf. Abbruchbedingung ist Object als Superklasse: + if(superClass.getClassName().equals(ASTFactory.createObjectClass().getClassName())){ + superTypes = Arrays.asList(UnifyTypeFactory.generateSmallerPair(UnifyTypeFactory.convert(ASTFactory.createObjectType()), UnifyTypeFactory.convert(ASTFactory.createObjectType()))); + }else{ + superTypes = getSuperTypes(superClass, availableClasses, newGTVs); + } + + List retList = new ArrayList<>(); + retList.add(ret); + retList.addAll(superTypes); + + return retList; + } + + /** + * Tauscht die GTVs in einem Typ gegen die entsprechenden Typen in der übergebenen Map aus. + */ + private static class TypeExchanger implements TypeVisitor{ + + private final HashMap gtvs; + + TypeExchanger(HashMap gtvs){ + this.gtvs = gtvs; + } + + @Override + public UnifyType visit(RefType refType) { + List params = new ArrayList<>(); + for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){ + params.add(param.acceptTV(this)); + } + TypeParams paramList = new TypeParams(params); + UnifyType ret = new ReferenceType(refType.getName().toString(), paramList); + return ret; + } + + @Override + public UnifyType visit(SuperWildcardType superWildcardType) { + throw new DebugException("Dieser Fall darf nicht auftreten"); + } + + @Override + public UnifyType visit(TypePlaceholder typePlaceholder) { + throw new DebugException("Dieser Fall darf nicht auftreten"); + } + + @Override + public UnifyType visit(ExtendsWildcardType extendsWildcardType) { + throw new DebugException("Dieser Fall darf nicht auftreten"); + } + + @Override + public UnifyType visit(GenericRefType genericRefType) { + if(! gtvs.containsKey(genericRefType.getParsedName())) + throw new DebugException("Dieser Fall darf nicht auftreten"); + return gtvs.get(genericRefType.getParsedName()); + } + + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index 6e48c797..aef8068c 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -6,6 +6,7 @@ import java.util.stream.Collectors; import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; @@ -35,57 +36,7 @@ public class UnifyTypeFactory { Generell dürfen sie immer die gleichen Namen haben. TODO: die transitive Hülle bilden */ - HashSet pairs = new HashSet<>(); - for(ClassOrInterface cly : fromClasses){ - pairs.addAll(getSuperTypes(cly, fromClasses)); - } - return new FiniteClosure(pairs); - } - - /** - * Bildet eine Kette vom übergebenen Typ bis hin zum höchsten bekannten Typ - * Als Generics werden TPHs benutzt, welche der Unifikationsalgorithmus korrekt interpretieren muss. - * Die verwendeten TPHs werden in der Kette nach oben gereicht, so erhält der selbe GTV immer den selben TPH - * @param forType - * @return - */ - private static List getSuperTypes(ClassOrInterface forType, List availableClasses) throws ClassNotFoundException { - return getSuperTypes(forType, availableClasses, new HashMap<>()); - } - - private static List getSuperTypes(ClassOrInterface forType, List availableClasses, HashMap gtvs) throws ClassNotFoundException { - List params = new ArrayList<>(); - //Generics mit gleichem Namen müssen den selben TPH bekommen - for(GenericTypeVar gtv : forType.getGenerics()){ - if(!gtvs.containsKey(gtv.getParsedName())) - gtvs.put(gtv.getParsedName(), PlaceholderType.freshPlaceholder()); - params.add(gtvs.get(gtv.getParsedName())); - } - Optional hasSuperclass = availableClasses.stream().filter(cl -> forType.getSuperClass().getName().equals(cl.getClassName())).findAny(); - ClassOrInterface superClass; - if(!hasSuperclass.isPresent()) //TODO: Wenn es die Object-Klasse ist, dann ist es in Ordnung, ansonsten Fehler ausgeben: - { - superClass = ASTFactory.createClass(ClassLoader.getSystemClassLoader().loadClass(forType.getSuperClass().getName().toString())); - }else{ - superClass = hasSuperclass.get(); - } - List superTypes; - if(superClass.getClassName().equals(ASTFactory.createObjectClass().getClassName())){ - superTypes = Arrays.asList(generateSmallerPair(convert(ASTFactory.createObjectType()), convert(ASTFactory.createObjectType()))); - }else{ - superTypes = getSuperTypes(superClass, availableClasses, gtvs); - } - - TypeParams paramList = new TypeParams(params); - UnifyType t1 = new ReferenceType(forType.getClassName().toString(), paramList); - UnifyType t2 = superTypes.get(0).getLhsType(); - - UnifyPair ret = generateSmallerPair(t1, t2); - List retList = new ArrayList<>(); - retList.add(ret); - retList.addAll(superTypes); - - return retList; + return new FiniteClosure(FCGenerator.toFC(fromClasses)); } public static UnifyPair generateSmallerPair(UnifyType tl, UnifyType tr){ From ede55d228e23eeaf7fd054357e1b7b51ada0f6f8 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Mon, 29 Jan 2018 17:35:49 +0100 Subject: [PATCH 018/208] Fehler in ASTFactory beheben. GenericRefType statt RefType in den Parameterlisten --- src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 59e54385..6acb4214 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -48,7 +48,7 @@ public class ASTFactory { if(superjreClass != null){ List params = new ArrayList<>(); for(TypeVariable tv : superjreClass.getTypeParameters()){ - params.add(new RefType(new GenericTypeName(new GenericContext( name, null),tv.getName()), new NullToken())); + params.add(new GenericRefType(new GenericTypeName(new GenericContext( name, null),tv.getName()), new NullToken())); } superClass = new RefType(new JavaClassName(superjreClass.getName()), params, new NullToken()); }else{//Jede Klasse und jedes Interface erbt von Object: (auch Object selbst!) From c7dea4c86bfa9c22567dd9452537bca5518c2b30 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Mon, 29 Jan 2018 18:26:14 +0100 Subject: [PATCH 019/208] Matrix Test --- .../syntaxtree/factory/UnifyTypeFactory.java | 1 - .../typeinference/unify/model/UnifyType.java | 10 ++++++++++ test/typeinference/MatrixTest.java | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/typeinference/MatrixTest.java diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index aef8068c..a962c2bc 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -26,7 +26,6 @@ public class UnifyTypeFactory { public static FiniteClosure generateFC(List fromClasses) throws ClassNotFoundException { /* - TODO: Generics werden zu TPHs Die transitive Hülle muss funktionieren. Man darf schreiben List extends AL und Vector extends List diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java index 56718198..1080f03b 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java @@ -96,4 +96,14 @@ public abstract class UnifyType { ret.addAll(typeParams.getInvolvedPlaceholderTypes()); return ret; } + + @Override + public int hashCode() { + return this.toString().hashCode(); + } + + @Override + public boolean equals(Object obj) { + return this.toString().equals(obj.toString()); + } } \ No newline at end of file diff --git a/test/typeinference/MatrixTest.java b/test/typeinference/MatrixTest.java new file mode 100644 index 00000000..8cc587c9 --- /dev/null +++ b/test/typeinference/MatrixTest.java @@ -0,0 +1,9 @@ +package typeinference; + +import java.io.File; + +public class MatrixTest extends JavaTXCompilerTest{ + public MatrixTest() { + this.fileToTest = new File(rootDirectory+"Matrix.jav"); + } +} \ No newline at end of file From 5d94820fed8e2681e1d1848febad2bd57d38d0ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Mon, 29 Jan 2018 21:30:45 +0100 Subject: [PATCH 020/208] modified: src/de/dhbwstuttgart/typeinference/unify/RuleSet.java modified: src/de/dhbwstuttgart/typeinference/unify/model/Node.java Genauere Fehlermeldung --- src/de/dhbwstuttgart/typeinference/unify/RuleSet.java | 2 +- src/de/dhbwstuttgart/typeinference/unify/model/Node.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java index 39ea0eff..8c41660b 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -305,7 +305,7 @@ public class RuleSet implements IRuleSet{ if(!rhsSType.getName().equals(lhsSType.getName())) return Optional.empty(); - if(!(lhsSType.getTypeParams().size()==rhsSType.getTypeParams().size()))throw new DebugException("Fehler in Unifizierung"); + if(!(lhsSType.getTypeParams().size()==rhsSType.getTypeParams().size()))throw new DebugException("Fehler in Unifizierung"+ " " + lhsSType.toString() + " " + rhsSType.toString()); //if(rhsSType.getTypeParams().size() != lhsSType.getTypeParams().size()) // return Optional.empty(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Node.java b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java index d24b6825..c6aa9b3e 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Node.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java @@ -94,6 +94,6 @@ class Node { @Override public String toString() { - return "Node(" + content.toString() + ")"; + return "Node(" + content.toString() + ")\n"; } } From 39793b461e934819280ef55d8e35deea1c82e3ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Mon, 29 Jan 2018 22:35:48 +0100 Subject: [PATCH 021/208] modified: test/javFiles/Matrix.jav Falsche Typen in methode korrigiert. --- test/javFiles/Matrix.jav | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/javFiles/Matrix.jav b/test/javFiles/Matrix.jav index e6aa84cd..14b58cde 100644 --- a/test/javFiles/Matrix.jav +++ b/test/javFiles/Matrix.jav @@ -2,8 +2,8 @@ import java.util.Vector; class Matrix extends Vector> { - methode(Matrix m) { - Vector> i; + methode(Vector> m) { + Matrix i; methode(i); } } From 027538a0829f02d4d20103075ac735a702314f6a Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Tue, 30 Jan 2018 01:08:21 +0100 Subject: [PATCH 022/208] =?UTF-8?q?Unary=20Expression=20und=20while=20stat?= =?UTF-8?q?ement=20anf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../syntaxtree/statement/NegativeExpr.html | 2 +- .../syntaxtree/statement/NotExpr.html | 2 +- .../syntaxtree/statement/PositivExpr.html | 2 +- .../syntaxtree/statement/PostDecExpr.html | 2 +- .../syntaxtree/statement/PostIncExpr.html | 2 +- .../syntaxtree/statement/PreDecExpr.html | 2 +- .../syntaxtree/statement/PreIncExpr.html | 2 +- .../statement/class-use/NegativeExpr.html | 6 +- .../statement/class-use/NotExpr.html | 6 +- .../statement/class-use/PositivExpr.html | 6 +- .../statement/class-use/PostDecExpr.html | 6 +- .../statement/class-use/PostIncExpr.html | 6 +- .../statement/class-use/PreDecExpr.html | 6 +- .../statement/class-use/PreIncExpr.html | 6 +- .../dhbwstuttgart/bytecode/BytecodeGen.java | 12 ++-- .../bytecode/BytecodeGenMethod.java | 11 +-- .../dhbwstuttgart/bytecode/KindOfLambda.java | 12 ++-- .../StatementGenerator.java | 27 ++++--- .../SyntaxTreeGenerator/SyntacticSugar.java | 30 +++++--- .../SyntaxTreeGenerator/TypeGenerator.java | 7 +- .../syntaxtree/AbstractASTWalker.java | 11 +-- .../syntaxtree/StatementVisitor.java | 4 +- .../syntaxtree/factory/ASTFactory.java | 2 +- .../syntaxtree/statement/IfStmt.java | 6 +- .../statement/JavaInternalExpression.java | 12 ++++ .../syntaxtree/statement/NegativeExpr.java | 18 ----- .../syntaxtree/statement/NotExpr.java | 14 ---- .../syntaxtree/statement/PositivExpr.java | 15 ---- .../syntaxtree/statement/PostDecExpr.java | 17 ----- .../syntaxtree/statement/PostIncExpr.java | 15 ---- .../syntaxtree/statement/PreDecExpr.java | 17 ----- .../syntaxtree/statement/PreIncExpr.java | 12 ---- .../syntaxtree/statement/UnaryExpr.java | 29 +++++++- .../syntaxtree/statement/UnaryMinus.java | 12 ---- .../syntaxtree/statement/UnaryNot.java | 7 -- .../syntaxtree/statement/UnaryPlus.java | 20 ------ .../syntaxtree/visual/OutputGenerator.java | 10 +-- .../visual/TypeOutputGenerator.java | 5 -- .../typedeployment/TypeInsertPlacer.java | 2 + .../typeinference/typeAlgo/TYPEStmt.java | 28 ++++++-- test/javFiles/Matrix.jav | 70 ++++++------------- 41 files changed, 187 insertions(+), 294 deletions(-) create mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/JavaInternalExpression.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/NotExpr.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/UnaryMinus.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/UnaryNot.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.html index 274fb17d..7ddcc746 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
    • -
    • de.dhbwstuttgart.syntaxtree.statement.NegativeExpr
    • +
    • de.dhbwstuttgart.syntaxtree.statement.javaInternal.NegativeExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NotExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NotExpr.html index d59ab82b..1eb88e1d 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NotExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NotExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
    • -
    • de.dhbwstuttgart.syntaxtree.statement.NotExpr
    • +
    • de.dhbwstuttgart.syntaxtree.statement.javaInternal.NotExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.html index 6dfdb37b..e02874de 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
    • -
    • de.dhbwstuttgart.syntaxtree.statement.PositivExpr
    • +
    • de.dhbwstuttgart.syntaxtree.statement.javaInternal.PositivExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.html index 86c5a8fa..9c293c66 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
    • -
    • de.dhbwstuttgart.syntaxtree.statement.PostDecExpr
    • +
    • de.dhbwstuttgart.syntaxtree.statement.javaInternal.PostDecExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.html index 94b06b12..4d52f399 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
    • -
    • de.dhbwstuttgart.syntaxtree.statement.PostIncExpr
    • +
    • de.dhbwstuttgart.syntaxtree.statement.javaInternal.PostIncExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.html index 47989bf2..3027535e 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
    • -
    • de.dhbwstuttgart.syntaxtree.statement.PreDecExpr
    • +
    • de.dhbwstuttgart.syntaxtree.statement.javaInternal.PreDecExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.html index 437a143f..e2caa4bd 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
    • -
    • de.dhbwstuttgart.syntaxtree.statement.PreIncExpr
    • +
    • de.dhbwstuttgart.syntaxtree.statement.javaInternal.PreIncExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NegativeExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NegativeExpr.html index beb55c1f..6e30f09e 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NegativeExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NegativeExpr.html @@ -3,7 +3,7 @@ -Uses of Class de.dhbwstuttgart.syntaxtree.statement.NegativeExpr +Uses of Class de.dhbwstuttgart.syntaxtree.statement.javaInternal.NegativeExpr @@ -70,9 +70,9 @@
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.NegativeExpr

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.NegativeExpr

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.NegativeExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.NegativeExpr
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.NotExpr

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.NotExpr

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.NotExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.NotExpr
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PositivExpr

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PositivExpr

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PositivExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PositivExpr
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PostDecExpr

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PostDecExpr

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PostDecExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PostDecExpr
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PostIncExpr

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PostIncExpr

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PostIncExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PostIncExpr
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PreDecExpr

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PreDecExpr

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PreDecExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PreDecExpr
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PreIncExpr

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PreIncExpr

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PreIncExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PreIncExpr
    diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index f1994c20..6fff6729 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.statement.*; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.FieldVisitor; @@ -382,12 +383,6 @@ public class BytecodeGen implements ASTVisitor { } - @Override - public void visit(UnaryPlus unaryPlus) { - // TODO Auto-generated method stub - - } - @Override public void visit(WhileStmt whileStmt) { // TODO Auto-generated method stub @@ -453,4 +448,9 @@ public class BytecodeGen implements ASTVisitor { } + @Override + public void visit(UnaryExpr unaryExpr) { + throw new NotImplementedException(); + } + } diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 5368cf93..0d0f8cea 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.statement.*; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Handle; @@ -336,6 +337,11 @@ public class BytecodeGenMethod implements StatementVisitor{ receiver.expr.accept(this); } + @Override + public void visit(UnaryExpr unaryExpr) { + throw new NotImplementedException(); + } + @Override public void visit(Return aReturn) { aReturn.retexpr.accept(this); @@ -367,11 +373,6 @@ public class BytecodeGenMethod implements StatementVisitor{ mv.visitVarInsn(Opcodes.ALOAD, 0); } - @Override - public void visit(UnaryPlus unaryPlus) { - System.out.println("++ In UnaryPlus: "); - } - @Override public void visit(WhileStmt whileStmt) { // TODO Auto-generated method stub diff --git a/src/de/dhbwstuttgart/bytecode/KindOfLambda.java b/src/de/dhbwstuttgart/bytecode/KindOfLambda.java index 36a28ce6..5264888c 100644 --- a/src/de/dhbwstuttgart/bytecode/KindOfLambda.java +++ b/src/de/dhbwstuttgart/bytecode/KindOfLambda.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.bytecode; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.statement.*; import java.util.ArrayList; @@ -126,6 +127,11 @@ public class KindOfLambda implements StatementVisitor{ receiver.expr.accept(this); } + @Override + public void visit(UnaryExpr unaryExpr) { + throw new NotImplementedException(); + } + @Override public void visit(Return aReturn) { aReturn.retexpr.accept(this); @@ -155,12 +161,6 @@ public class KindOfLambda implements StatementVisitor{ this.argumentList.add(aThis.getType()); } - @Override - public void visit(UnaryPlus unaryPlus) { - // TODO Auto-generated method stub - - } - @Override public void visit(WhileStmt whileStmt) { // TODO Auto-generated method stub diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index f4ada6b2..a1d17e8a 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -16,6 +16,7 @@ import de.dhbwstuttgart.syntaxtree.type.Void; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.TerminalNode; +import java.lang.reflect.Type; import java.util.*; public class StatementGenerator { @@ -289,8 +290,8 @@ public class StatementGenerator { } private Statement convert(Java8Parser.PostIncrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); + return new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, convert(stmt.postfixExpression()), + TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); } private Statement convert(Java8Parser.PostDecrementExpressionContext stmt) { @@ -316,8 +317,10 @@ public class StatementGenerator { } private Statement convert(Java8Parser.IfThenElseStatementContext stmt){ - //TODO - throw new NotImplementedException(); + Expression expr = convert(stmt.expression()); + Statement thenBlock = convert(stmt.statementNoShortIf()); + Statement elseBlock = convert(stmt.statement()); + return new IfStmt(TypePlaceholder.fresh(stmt.getStart()), expr, thenBlock, elseBlock, stmt.getStart()); } private Statement convert(Java8Parser.IfThenElseStatementNoShortIfContext stmt){ @@ -591,7 +594,7 @@ public class StatementGenerator { if(expression.relationalExpression() == null){ return convert(expression.shiftExpression()); }else{ - throw new NotImplementedException(); + return convert(expression.relationalExpression()); } } @@ -604,7 +607,9 @@ public class StatementGenerator { } private Expression convert(Java8Parser.AdditiveExpressionContext expression) { - if(expression.additiveExpression() == null){ + if(expression.additiveExpression() != null){ + return convert(expression.additiveExpression()); + }else if(expression.multiplicativeExpression() != null){ return convert(expression.multiplicativeExpression()); }else{ throw new NotImplementedException(); @@ -627,9 +632,11 @@ public class StatementGenerator { }else if(expression.unaryExpressionNotPlusMinus() != null){ return convert(expression.unaryExpressionNotPlusMinus()); }else if(expression.getText().startsWith("+")){ - return new UnaryPlus(convert(expression.unaryExpression())); + return new UnaryExpr(UnaryExpr.Operation.PLUS, convert(expression.unaryExpression()), + TypePlaceholder.fresh(expression.getStart()), expression.getStart()); }else if(expression.getText().startsWith("-")){ - return new UnaryMinus(convert(expression.unaryExpression())); + return new UnaryExpr(UnaryExpr.Operation.MINUS, convert(expression.unaryExpression()), + TypePlaceholder.fresh(expression.getStart()), expression.getStart()); }else{ //Diese Exceptions sollte nie geworfen werden. //Der Code wurde nur noch nicht getestet. Sollte zur Sicherheit drin bleiben. @@ -672,10 +679,10 @@ public class StatementGenerator { } for(Java8Parser.PostIncrementExpression_lf_postfixExpressionContext inc : expression.postIncrementExpression_lf_postfixExpression()){ - expr = new PostIncExpr(expr); + expr = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, expr, TypePlaceholder.fresh(inc.getStart()), inc.getStart()); } for(Java8Parser.PostDecrementExpression_lf_postfixExpressionContext dec : expression.postDecrementExpression_lf_postfixExpression()){ - expr = new PostDecExpr(expr); + expr = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, expr, TypePlaceholder.fresh(dec.getStart()), dec.getStart()); } return expr; diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java index d758f52b..d7926004 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java @@ -1,6 +1,9 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; +import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.statement.*; import java.util.List; @@ -10,22 +13,27 @@ public class SyntacticSugar { public static List addTrailingReturn(List statements){ if(statements.size()!=0) { Statement lastStmt = statements.get(statements.size() - 1); - if (lastStmt instanceof Return) return statements; - if (lastStmt instanceof WhileStmt) { - //TODO - //if (hasReturn(((WhileStmt) lastStmt).loopBlock)) return statements; - } else if (lastStmt instanceof IfStmt) { - if (hasReturn(((IfStmt) lastStmt).then_block) - && hasReturn(((IfStmt) lastStmt).else_block)) return statements; - } else if (lastStmt instanceof ForStmt) { - if (hasReturn(((ForStmt) lastStmt).body_Loop_block)) return statements; - } else { - } + ReturnFinder hasReturn = new ReturnFinder(); + lastStmt.accept(hasReturn); + if(hasReturn.hasReturn)return statements; } statements.add(new ReturnVoid(new NullToken())); return statements; } + private static class ReturnFinder extends AbstractASTWalker{ + public boolean hasReturn = false; + @Override + public void visit(Return aReturn) { + hasReturn = true; + } + + @Override + public void visit(ReturnVoid aReturn) { + hasReturn = true; + } + } + private static boolean hasReturn(Block block){ for(Statement s : block.getStatements()) if(s instanceof Return)return true; diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index d54b682a..1dd259d0 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -40,7 +40,12 @@ public class TypeGenerator { public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannTypeContext unannTypeContext, JavaClassRegistry reg, GenericsRegistry genericsRegistry) { if(unannTypeContext.unannPrimitiveType()!=null){ - throw new NotImplementedException(); + if(unannTypeContext.unannPrimitiveType().getText().equals("boolean")){ + return new RefType(ASTFactory.createClass(Boolean.class).getClassName(), unannTypeContext.getStart()); + }else{ + Java8Parser.NumericTypeContext numericType = unannTypeContext.unannPrimitiveType().numericType(); + throw new NotImplementedException(); + } }else if(unannTypeContext.unannReferenceType().unannArrayType()!=null){ //System.out.println(unannTypeContext.getText()); diff --git a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index 8a66629b..054c3915 100644 --- a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -7,6 +7,7 @@ import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; import de.dhbwstuttgart.syntaxtree.type.*; +import javax.management.Notification; import java.lang.reflect.Modifier; import java.util.Iterator; @@ -202,6 +203,11 @@ public abstract class AbstractASTWalker implements ASTVisitor{ receiver.expr.accept(this); } + @Override + public void visit(UnaryExpr unaryExpr) { + unaryExpr.expr.accept(this); + } + @Override public void visit(Return aReturn) { aReturn.retexpr.accept(this); @@ -227,11 +233,6 @@ public abstract class AbstractASTWalker implements ASTVisitor{ } - @Override - public void visit(UnaryPlus unaryPlus) { - - } - @Override public void visit(WhileStmt whileStmt) { diff --git a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java index 195db230..1c4e149c 100644 --- a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java +++ b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java @@ -50,8 +50,6 @@ public interface StatementVisitor { void visit(This aThis); - void visit(UnaryPlus unaryPlus); - void visit(WhileStmt whileStmt); void visit(DoStmt whileStmt); @@ -67,4 +65,6 @@ public interface StatementVisitor { void visit(SuperCall superCall); void visit(ExpressionReceiver expressionReceiver); + + void visit(UnaryExpr unaryExpr); } diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 6acb4214..b0683569 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -133,7 +133,7 @@ public class ASTFactory { } */ - public static RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type, JavaClassName parentClass, String parentMethod){ + private static RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type, JavaClassName parentClass, String parentMethod){ if(type.getTypeName().equals("void")){ return new Void(new NullToken()); }else if(type.getTypeName().equals("int")){ diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java index 5d6a4658..4af1e770 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java @@ -11,11 +11,11 @@ import org.antlr.v4.runtime.Token; public class IfStmt extends Statement { public final Expression expr; - public final Block then_block; - public final Block else_block; + public final Statement then_block; + public final Statement else_block; public IfStmt(RefTypeOrTPHOrWildcardOrGeneric type, - Expression expr, Block thenBlock, Block elseBlock, Token offset) + Expression expr, Statement thenBlock, Statement elseBlock, Token offset) { super(type,offset); this.expr = expr; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/JavaInternalExpression.java b/src/de/dhbwstuttgart/syntaxtree/statement/JavaInternalExpression.java new file mode 100644 index 00000000..366c0602 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/statement/JavaInternalExpression.java @@ -0,0 +1,12 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import org.antlr.v4.runtime.Token; + +import java.nio.charset.StandardCharsets; + +public abstract class JavaInternalExpression extends Statement{ + public JavaInternalExpression(RefTypeOrTPHOrWildcardOrGeneric retType, Token offset){ + super(retType, offset); + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.java deleted file mode 100644 index 67e71e95..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; -import java.util.Hashtable; -import java.util.Iterator; - -import org.antlr.v4.runtime.Token; - - - -public class NegativeExpr extends UnaryExpr -{ - public NegativeExpr(Token offset) - { - super(offset); - } - private UnaryMinus neg; - public Expression expr; - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NotExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/NotExpr.java deleted file mode 100644 index 0fcfd333..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NotExpr.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -import org.antlr.v4.runtime.Token; - -public class NotExpr extends UnaryExpr -{ - public NotExpr(Token offset) - { - super(offset); - } - private UnaryNot not; - public Expression expr; -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.java deleted file mode 100644 index bec6ffbe..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.java +++ /dev/null @@ -1,15 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -import org.antlr.v4.runtime.Token; - -public class PositivExpr extends UnaryExpr -{ - public PositivExpr(Token offset) - { - super(offset); - } - protected UnaryPlus plus; - public Expression expr; - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.java deleted file mode 100644 index 18b56f96..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; -import java.util.Hashtable; -import java.util.Iterator; - -import de.dhbwstuttgart.parser.antlr.Java8Parser; - - - - -public class PostDecExpr extends PostIncExpr -{ - - public PostDecExpr(Expression expr) { - super(expr); - } - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java deleted file mode 100644 index 1292192b..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java +++ /dev/null @@ -1,15 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; -import de.dhbwstuttgart.exceptions.NotImplementedException; - - -public class PostIncExpr extends UnaryExpr -{ - public PostIncExpr(Expression expr) { - super(null); - } - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java deleted file mode 100644 index de0fb0b1..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; -import java.util.Hashtable; -import java.util.Iterator; - -import org.antlr.v4.runtime.Token; - - - - - -public class PreDecExpr extends UnaryExpr -{ - public PreDecExpr(Token offset) - { - super(offset); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java deleted file mode 100644 index 736dc311..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java +++ /dev/null @@ -1,12 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -import org.antlr.v4.runtime.Token; - -public class PreIncExpr extends UnaryExpr -{ - public PreIncExpr(Token offset) - { - super(offset); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java index 2157560c..ca06146a 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java @@ -1,14 +1,37 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.syntaxtree.statement.JavaInternalExpression; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; -public abstract class UnaryExpr extends MethodCall +public class UnaryExpr extends JavaInternalExpression { + public enum Operation{ + NOT, + MINUS, + PREINCREMENT, + PREDECREMENT, + POSTINCREMENT, + PLUS, POSTDECREMENT + } + + + public final Operation operation; public Expression expr; - public UnaryExpr(Token offset) + public UnaryExpr(Operation operation, Expression argument, RefTypeOrTPHOrWildcardOrGeneric retType, Token offset) { - super(null,null,null,null,null); + super(retType, offset); + this.expr = argument; + this.operation = operation; } + @Override + public void accept(StatementVisitor visitor) { + visitor.visit(this); + } + + } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryMinus.java b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryMinus.java deleted file mode 100644 index 6c100225..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryMinus.java +++ /dev/null @@ -1,12 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; - -public class UnaryMinus extends UnaryPlus -{ - - public UnaryMinus(Expression expression) { - super(expression); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryNot.java b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryNot.java deleted file mode 100644 index 2dce91bd..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryNot.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -public class UnaryNot -{ - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java deleted file mode 100644 index b374166f..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java +++ /dev/null @@ -1,20 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - -import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; -import de.dhbwstuttgart.exceptions.NotImplementedException; - -public class UnaryPlus extends Expression -{ - public UnaryPlus(Expression expression){ - super(expression.getType(),expression.getOffset()); - - } - - @Override - public void accept(StatementVisitor visitor) { - visitor.visit(this); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index d56c39d8..191d58a0 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -296,11 +296,6 @@ public class OutputGenerator implements ASTVisitor{ out.append("this"); } - @Override - public void visit(UnaryPlus unaryPlus) { - - } - @Override public void visit(WhileStmt whileStmt) { out.append("while("); @@ -349,4 +344,9 @@ public class OutputGenerator implements ASTVisitor{ public void visit(ExpressionReceiver receiver) { receiver.expr.accept(this); } + + @Override + public void visit(UnaryExpr unaryExpr) { + throw new NotImplementedException(); + } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java index dcefafa4..6f90051a 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java @@ -214,11 +214,6 @@ public class TypeOutputGenerator extends OutputGenerator { aThis.getType().accept(this); } - @Override - public void visit(UnaryPlus unaryPlus) { - super.visit(unaryPlus); - } - @Override public void visit(WhileStmt whileStmt) { super.visit(whileStmt); diff --git a/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java b/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java index 8dea5748..ad591ebe 100644 --- a/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java +++ b/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java @@ -1,6 +1,8 @@ package de.dhbwstuttgart.typedeployment; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.statement.JavaInternalExpression; import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.result.ResultSet; diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 3448e8e3..ed5837ad 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -2,12 +2,14 @@ package de.dhbwstuttgart.typeinference.typeAlgo; //import com.sun.org.apache.xpath.internal.Arg; +import com.sun.org.apache.xpath.internal.operations.Bool; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.parser.antlr.Java8Parser; import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; @@ -193,6 +195,23 @@ public class TYPEStmt implements StatementVisitor{ receiver.expr.accept(this); } + @Override + public void visit(UnaryExpr unaryExpr) { + if(unaryExpr.operation == UnaryExpr.Operation.POSTDECREMENT || + unaryExpr.operation == UnaryExpr.Operation.POSTINCREMENT || + unaryExpr.operation == UnaryExpr.Operation.PREDECREMENT || + unaryExpr.operation == UnaryExpr.Operation.PREINCREMENT){ + //@see: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.14.2 + //Expression muss zu Numeric Convertierbar sein. also von Numeric erben + RefType number = new RefType(ASTFactory.createClass(Number.class).getClassName(), unaryExpr.getOffset()); + constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), number, PairOperator.EQUALSDOT)); + //The type of the postfix increment expression is the type of the variable + constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), unaryExpr.getType(), PairOperator.EQUALSDOT)); + }else{ + throw new NotImplementedException(); + } + } + @Override public void visit(Return returnExpr) { returnExpr.retexpr.accept(this); @@ -241,14 +260,11 @@ public class TYPEStmt implements StatementVisitor{ }; } - @Override - public void visit(UnaryPlus unaryPlus) { - throw new NotImplementedException(); - } - @Override public void visit(WhileStmt whileStmt) { - throw new NotImplementedException(); + RefType booleanType = new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken()); + constraintsSet.addUndConstraint(new Pair(whileStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT)); + whileStmt.loopBlock.accept(this); } @Override diff --git a/test/javFiles/Matrix.jav b/test/javFiles/Matrix.jav index e6aa84cd..66abd5e0 100644 --- a/test/javFiles/Matrix.jav +++ b/test/javFiles/Matrix.jav @@ -1,54 +1,24 @@ import java.util.Vector; class Matrix extends Vector> { - - methode(Matrix m) { - Vector> i; - methode(i); - } - } - -/* -import java.util.Vector; - -class Matrix extends Vector> { - - Matrix mul_rec(Matrix m) { - auto v1 = new Vector(); - auto v2 = new Vector(); - auto i = 0; - while(i < m.size()) { - auto v; - v = m.elementAt(i); - v2.addElement(v.remove(v.size()-1)); - i++; - } - Matrix ret; - if (m.elementAt(0).size() > 0) { - ret = this.mul_rec(m); - } - else { - ret = new Matrix(); - i = 0; - while (i < this.size()) { - ret.addElement(new Vector()); - i++; - } - } - i = 0; - while (i < this.size()) { - int erg = 0; - auto j; - j = 0; - while (j < v2.size()) { - erg = erg + this.elementAt(i).elementAt(j).intValue() - * v2.elementAt(j).intValue(); - j++; - } - ret.elementAt(i).addElement(erg); - i++; - } - return ret; - } + mul(m) { + var ret = new Matrix(); + var i = 0; + while(i (); + var j = 0; + while(j < v1.size()) { + var erg = 0; + var k = 0; + while(k < v1.size()) { + erg = erg + v1.elementAt(k) + * m.elementAt(k).elementAt(j); + k++; } + v2.addElement(new Integer(erg)); + j++; } + ret.addElement(v2); + i++; } + return ret; + } } -*/ \ No newline at end of file From 3ce4937bc1961b097ffb1e9ef6102649168dbe7e Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Tue, 30 Jan 2018 02:23:58 +0100 Subject: [PATCH 023/208] =?UTF-8?q?Fehler=20im=20Trailing=20Return=20anh?= =?UTF-8?q?=C3=A4ngen=20beheben?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SyntaxTreeGenerator/FCGenerator.java | 2 ++ .../StatementGenerator.java | 8 ++++---- .../SyntaxTreeGenerator.java | 2 +- .../syntaxtree/AbstractASTWalker.java | 9 +++++---- .../syntaxtree/visual/OutputGenerator.java | 20 ++++++++++++++++++- test/javFiles/Matrix.jav | 2 +- test/typeinference/JavaTXCompilerTest.java | 2 ++ 7 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java index cbca13ce..7863c8ed 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -141,6 +141,8 @@ public class FCGenerator { public UnifyType visit(GenericRefType genericRefType) { if(! gtvs.containsKey(genericRefType.getParsedName())) throw new DebugException("Dieser Fall darf nicht auftreten"); + //TODO: Diesen Dirty-Hack beseitigen. Fehler tritt bei java.lang.invoke.LambdaFormEditor$Transform$Kind auf. + //return UnifyTypeFactory.convert(ASTFactory.createObjectType()); return gtvs.get(genericRefType.getParsedName()); } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index a1d17e8a..069f49dc 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -96,7 +96,7 @@ public class StatementGenerator { private Statement convert(Java8Parser.StatementWithoutTrailingSubstatementContext stmt) { if(stmt.block() != null){ - return convert(stmt.block()); + return convert(stmt.block(), false); }else if(stmt.emptyStatement() != null){ return new EmptyStmt(stmt.getStart()); }else if(stmt.expressionStatement() != null){ @@ -122,14 +122,14 @@ public class StatementGenerator { }else throw new NotImplementedException(); } - public Block convert(Java8Parser.BlockContext block) { + public Block convert(Java8Parser.BlockContext block, boolean addTrailingReturn) { List statements = new ArrayList<>(); if(block.blockStatements() != null) for(Java8Parser.BlockStatementContext statementContext : block.blockStatements().blockStatement()){ List stmt = convert(statementContext); statements.addAll(stmt); } - statements = SyntacticSugar.addTrailingReturn(statements); + if(addTrailingReturn)statements = SyntacticSugar.addTrailingReturn(statements); return new Block(statements, block.getStart()); } @@ -857,7 +857,7 @@ public class StatementGenerator { expression.lambdaBody().expression().getStart())); block = new Block(statements, expression.lambdaBody().getStart()); }else{ - block = lambdaGenerator.convert(expression.lambdaBody().block()); + block = lambdaGenerator.convert(expression.lambdaBody().block(), true); } List funNParams = new ArrayList<>(); funNParams.add(TypePlaceholder.fresh(expression.getStart()));//ret-Type diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 3d9d7aca..b681d9c3 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -136,7 +136,7 @@ public class SyntaxTreeGenerator{ //TODO: Error! Abstrakte Methode ohne abstrakt Keyword } }else{ - block = stmtGen.convert(body.block()); + block = stmtGen.convert(body.block(),true); } if(parentClass.equals(new JavaClassName(name))){ return new Constructor(modifiers, name, retType, parameterList, block, gtvDeclarations, header.getStart(), fieldInitializations); diff --git a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index 054c3915..dfb8438c 100644 --- a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -158,12 +158,13 @@ public abstract class AbstractASTWalker implements ASTVisitor{ @Override public void visit(ForStmt forStmt) { - + forStmt.body_Loop_block.accept(this); } @Override public void visit(IfStmt ifStmt) { - + ifStmt.then_block.accept(this); + ifStmt.else_block.accept(this); } @Override @@ -235,12 +236,12 @@ public abstract class AbstractASTWalker implements ASTVisitor{ @Override public void visit(WhileStmt whileStmt) { - + whileStmt.loopBlock.accept(this); } @Override public void visit(DoStmt whileStmt) { - + whileStmt.loopBlock.accept(this); } @Override diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 191d58a0..879a4ba2 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -347,6 +347,24 @@ public class OutputGenerator implements ASTVisitor{ @Override public void visit(UnaryExpr unaryExpr) { - throw new NotImplementedException(); + if(unaryExpr.operation == UnaryExpr.Operation.MINUS){ + out.append("-"); + } + if(unaryExpr.operation == UnaryExpr.Operation.PLUS){ + out.append("+"); + } + if(unaryExpr.operation == UnaryExpr.Operation.PREDECREMENT){ + out.append("--"); + } + if(unaryExpr.operation == UnaryExpr.Operation.PREINCREMENT){ + out.append("++"); + } + unaryExpr.expr.accept(this); + if(unaryExpr.operation == UnaryExpr.Operation.POSTDECREMENT){ + out.append("--"); + } + if(unaryExpr.operation == UnaryExpr.Operation.POSTINCREMENT){ + out.append("++"); + } } } \ No newline at end of file diff --git a/test/javFiles/Matrix.jav b/test/javFiles/Matrix.jav index 66abd5e0..476027b3 100644 --- a/test/javFiles/Matrix.jav +++ b/test/javFiles/Matrix.jav @@ -4,7 +4,7 @@ class Matrix extends Vector> { mul(m) { var ret = new Matrix(); var i = 0; - while(i (); var j = 0; diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index f4622954..03452018 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -2,6 +2,7 @@ package typeinference; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter; import de.dhbwstuttgart.typedeployment.TypeInsert; import de.dhbwstuttgart.typedeployment.TypeInsertFactory; @@ -51,6 +52,7 @@ public class JavaTXCompilerTest { for(File f : compiler.sourceFiles.keySet()){ SourceFile sf = compiler.sourceFiles.get(f); System.out.println(ASTTypePrinter.print(sf)); + System.out.println(ASTPrinter.print(sf)); //List results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen assert results.size()>0; Set insertedTypes = new HashSet<>(); From 8e4791cca6beff87e588959c73df93cf98cf41a6 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 31 Jan 2018 13:34:54 +0100 Subject: [PATCH 024/208] Anpassungen im Matrix Test --- test/javFiles/Matrix.jav | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/javFiles/Matrix.jav b/test/javFiles/Matrix.jav index 476027b3..e1183535 100644 --- a/test/javFiles/Matrix.jav +++ b/test/javFiles/Matrix.jav @@ -1,4 +1,6 @@ import java.util.Vector; +import java.lang.Integer; +import java.lang.Boolean; class Matrix extends Vector> { mul(m) { From 9a94ba2114e2bfa369c91fddf09b8a143ac4a521 Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 31 Jan 2018 14:16:20 +0100 Subject: [PATCH 025/208] =?UTF-8?q?unn=C3=B6tige=20Kommentare=20wurden=20e?= =?UTF-8?q?ntfernt?= 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 e556a470e7dc586360ae68b9d9c2af948b52bbd1 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 31 Jan 2018 15:39:19 +0100 Subject: [PATCH 026/208] =?UTF-8?q?Literal=20l=C3=B6schen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../syntaxtree/statement/BoolLiteral.html | 4 +-- .../syntaxtree/statement/CharLiteral.html | 4 +-- .../syntaxtree/statement/DoubleLiteral.html | 4 +-- .../syntaxtree/statement/FloatLiteral.html | 4 +-- .../syntaxtree/statement/IntLiteral.html | 4 +-- .../syntaxtree/statement/Literal.html | 2 +- .../syntaxtree/statement/LongLiteral.html | 4 +-- .../syntaxtree/statement/Null.html | 4 +-- .../syntaxtree/statement/StringLiteral.html | 4 +-- .../statement/class-use/Literal.html | 4 +-- .../dhbwstuttgart/bytecode/BytecodeGen.java | 17 ++-------- .../bytecode/BytecodeGenMethod.java | 11 ++----- .../dhbwstuttgart/bytecode/KindOfLambda.java | 11 ++----- .../environment/CompilationEnvironment.java | 16 +++++---- src/de/dhbwstuttgart/parser/JavaTXParser.java | 2 +- .../StatementGenerator.java | 33 +++++++++++-------- .../dhbwstuttgart/syntaxtree/ASTVisitor.java | 6 ---- .../syntaxtree/AbstractASTWalker.java | 13 ++------ .../syntaxtree/StatementVisitor.java | 11 ++----- .../syntaxtree/statement/Binary.java | 27 --------------- .../syntaxtree/statement/BinaryExpr.java | 33 +++++++++++++++++-- .../syntaxtree/statement/InstanceOf.java | 5 ++- .../statement/{literal => }/Literal.java | 12 ++++--- .../syntaxtree/statement/MethodCall.java | 1 - .../statement/literal/BoolLiteral.java | 26 --------------- .../statement/literal/CharLiteral.java | 21 ------------ .../syntaxtree/statement/literal/Null.java | 21 ------------ .../statement/literal/NumberLiteral.java | 22 ------------- .../statement/literal/StringLiteral.java | 23 ------------- .../syntaxtree/visual/ASTTypePrinter.java | 7 ---- .../syntaxtree/visual/OutputGenerator.java | 23 +++++-------- .../visual/TypeOutputGenerator.java | 17 +++------- .../typeinference/assumptions/FunNClass.java | 1 - .../typeinference/typeAlgo/TYPEStmt.java | 27 ++++++--------- 34 files changed, 123 insertions(+), 301 deletions(-) delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/Binary.java rename src/de/dhbwstuttgart/syntaxtree/statement/{literal => }/Literal.java (52%) delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/literal/BoolLiteral.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/literal/CharLiteral.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/literal/Null.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/literal/NumberLiteral.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/literal/StringLiteral.java diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/BoolLiteral.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/BoolLiteral.html index 35ca0132..be892a1e 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/BoolLiteral.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/BoolLiteral.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.Expr
    • -
    • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
    • +
    • de.dhbwstuttgart.syntaxtree.statement.Literal
      • de.dhbwstuttgart.syntaxtree.statement.literal.BoolLiteral
      • @@ -253,7 +253,7 @@ extends
          -
        • +
        • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

          diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/CharLiteral.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/CharLiteral.html index b13baef9..7d0bcafb 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/CharLiteral.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/CharLiteral.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
        • de.dhbwstuttgart.syntaxtree.statement.Expr
          • -
          • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
          • +
          • de.dhbwstuttgart.syntaxtree.statement.Literal
            • de.dhbwstuttgart.syntaxtree.statement.literal.CharLiteral
            • @@ -253,7 +253,7 @@ extends
                -
              • +
              • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

                diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/DoubleLiteral.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/DoubleLiteral.html index 3f3f7c69..d367cd8a 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/DoubleLiteral.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/DoubleLiteral.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
              • de.dhbwstuttgart.syntaxtree.statement.Expr
                • -
                • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
                • +
                • de.dhbwstuttgart.syntaxtree.statement.Literal
                  • de.dhbwstuttgart.syntaxtree.statement.literal.DoubleLiteral
                  • @@ -263,7 +263,7 @@ extends
                      -
                    • +
                    • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

                      diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/FloatLiteral.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/FloatLiteral.html index 4491e9b7..2c88f19d 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/FloatLiteral.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/FloatLiteral.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
                    • de.dhbwstuttgart.syntaxtree.statement.Expr
                      • -
                      • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
                      • +
                      • de.dhbwstuttgart.syntaxtree.statement.Literal
                        • de.dhbwstuttgart.syntaxtree.statement.literal.FloatLiteral
                        • @@ -263,7 +263,7 @@ extends
                            -
                          • +
                          • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

                            diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/IntLiteral.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/IntLiteral.html index 664dd208..7d7ef3eb 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/IntLiteral.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/IntLiteral.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
                          • de.dhbwstuttgart.syntaxtree.statement.Expr
                            • -
                            • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
                            • +
                            • de.dhbwstuttgart.syntaxtree.statement.Literal
                              • de.dhbwstuttgart.syntaxtree.statement.literal.NumberLiteral
                              • @@ -263,7 +263,7 @@ extends
                                  -
                                • +
                                • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

                                  diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Literal.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Literal.html index 63ee66ac..8fdde516 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Literal.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Literal.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
                                • de.dhbwstuttgart.syntaxtree.statement.Expr
                                  • -
                                  • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
                                  • +
                                  • de.dhbwstuttgart.syntaxtree.statement.Literal
                                diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/LongLiteral.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/LongLiteral.html index 9604e4c8..ba044487 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/LongLiteral.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/LongLiteral.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
                              • de.dhbwstuttgart.syntaxtree.statement.Expr
                                • -
                                • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
                                • +
                                • de.dhbwstuttgart.syntaxtree.statement.Literal
                                  • de.dhbwstuttgart.syntaxtree.statement.LongLiteral
                                  • @@ -263,7 +263,7 @@ extends
                                      -
                                    • +
                                    • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

                                      diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Null.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Null.html index d7812a4f..23f741de 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Null.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Null.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
                                    • de.dhbwstuttgart.syntaxtree.statement.Expr
                                      • -
                                      • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
                                      • +
                                      • de.dhbwstuttgart.syntaxtree.statement.Literal
                                        • de.dhbwstuttgart.syntaxtree.statement.literal.Null
                                        • @@ -245,7 +245,7 @@ extends
                                            -
                                          • +
                                          • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

                                            diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/StringLiteral.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/StringLiteral.html index 4d1c4988..89acbb3b 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/StringLiteral.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/StringLiteral.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
                                          • de.dhbwstuttgart.syntaxtree.statement.Expr
                                            • -
                                            • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
                                            • +
                                            • de.dhbwstuttgart.syntaxtree.statement.Literal
                                              • de.dhbwstuttgart.syntaxtree.statement.literal.StringLiteral
                                              • @@ -253,7 +253,7 @@ extends
                                                  -
                                                • +
                                                • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

                                                  diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/Literal.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/Literal.html index 5c5ba6a3..33eabfbd 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/Literal.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/Literal.html @@ -3,7 +3,7 @@ -Uses of Class de.dhbwstuttgart.syntaxtree.statement.literal.Literal +Uses of Class de.dhbwstuttgart.syntaxtree.statement.Literal @@ -70,7 +70,7 @@
  • -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.Literal

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.Literal

  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr