diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 0fdcbb77a..15430da87 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -63,6 +63,7 @@ public class JavaTXCompiler { System.out.println("RESULT: " + result); results.addAll(result); } + //TODO: Hier läuft irgendwas gewaltig schief: return new ResultSet(UnifyTypeFactory.convert(results, generateTPHMap(cons))); } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 0f2f8e510..c16dcc8ad 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -233,6 +233,8 @@ public class SyntaxTreeGenerator{ block = stmtGen.convert(body.block()); } if(parentClass.equals(new JavaClassName(name))){ + //TODO: Constructor darf nicht Rückgabetyp void bekommen: Hier als Rückgabetyp die Klasse inklusive generische Variablen + //retType = TypeGenerator.convertTypeName(name, gtvDeclarations, header.getStart(), reg, localGenerics); return new Constructor(name, retType, modifiers, parameterList, block, gtvDeclarations, header.getStart()); }else{ return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, header.getStart()); diff --git a/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java b/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java index 3af59dc11..e07d299a9 100644 --- a/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java +++ b/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java @@ -11,6 +11,11 @@ import de.dhbwstuttgart.typeinference.ResultSet; import java.util.ArrayList; import java.util.List; +/** + * TODO: + * Falls in Feldern Generics entstehen, dann werden diese als Klassenparameter eingesetzt + * Für die Instanzierung von Klassen kann man dann beispielsweise nur noch den Diamond-Operator verwenden + */ public class TypeInsertFactory { public static List createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults){ List ret = new ArrayList<>(); diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index 7a0cad925..ae1c69529 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.typeinference.typeAlgo; +import com.sun.org.apache.xpath.internal.Arg; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.syntaxtree.*; @@ -34,6 +35,19 @@ public class TYPE implements StatementVisitor{ @Override public void visit(LambdaExpression lambdaExpression) { + List methodAssumptionss = getMethods("apply", lambdaExpression.params.getFormalparalist().size(), info); + //TODO: Nur FunN-Interface als mögliche Typen verwenden + //methodAssumptionss.stream().filter((methodAssumption -> methodAssumption.getReceiverType().getName().toString())) + Set possibleLambdaTypes = new HashSet<>(); + for(MethodAssumption mAss : methodAssumptionss){ + Constraint cons = new Constraint(); + cons.add( + ConstraintsFactory.createPair(lambdaExpression.methodBody.getType(),mAss.getReturnType(),info)); + cons.add( + ConstraintsFactory.createPair(lambdaExpression.getType(),mAss.getReceiverType(),PairOperator.EQUALSDOT, info)); + } + constraintsSet.addOderConstraint(possibleLambdaTypes); + //Constraints des Bodys generieren: TYPE lambdaScope = new TYPE(new TypeInferenceBlockInformation(info, lambdaExpression)); lambdaExpression.methodBody.accept(lambdaScope); constraintsSet.addAll(lambdaScope.getConstraints()); @@ -224,12 +238,12 @@ public class TYPE implements StatementVisitor{ } - public static List getMethods(String name, ArgumentList arglist, TypeInferenceBlockInformation info) { + public static List getMethods(String name, int numArgs, TypeInferenceBlockInformation info) { List ret = new ArrayList<>(); for(ClassOrInterface cl : info.getAvailableClasses()){ for(Method m : cl.getMethods()){ if(m.getName().equals(name) && - m.getParameterList().getFormalparalist().size() == arglist.getArguments().size()){ + m.getParameterList().getFormalparalist().size() == numArgs){ RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getType()); ret.add(new MethodAssumption(cl.getType(), retType, convertParams(m.getParameterList(),info))); @@ -239,7 +253,11 @@ public class TYPE implements StatementVisitor{ return ret; } - protected static List convertParams(ParameterList parameterList, TypeInferenceBlockInformation info){ + public static List getMethods(String name, ArgumentList arglist, TypeInferenceBlockInformation info) { + return getMethods(name, arglist.getArguments().size(), info); + } + + protected static List convertParams(ParameterList parameterList, TypeInferenceBlockInformation info){ List params = new ArrayList<>(); for(FormalParameter fp : parameterList.getFormalparalist()){ params.add(info.checkGTV(fp.getType())); diff --git a/test/javFiles/Lambda2.jav b/test/javFiles/Lambda2.jav index 8326dcd56..b0ef82219 100644 --- a/test/javFiles/Lambda2.jav +++ b/test/javFiles/Lambda2.jav @@ -4,8 +4,14 @@ public class Lambda2 public static void main(List args){ auto listOfStrings = new List(); auto listOfObjects; - //listOfObjects = map(listOfStrings, (a) -> a); + listOfObjects = map(listOfStrings, (a) -> a); } + +public map(a , b){ + b.apply(a); + return a; +} + /* public static List map(List input, Function func) { List output; diff --git a/test/javFiles/Lambda3.jav b/test/javFiles/Lambda3.jav new file mode 100644 index 000000000..9c4e960cd --- /dev/null +++ b/test/javFiles/Lambda3.jav @@ -0,0 +1,23 @@ + +public class Lambda2 +{ + /* + public static List map(List input, + Function func){ + input.add(func.apply(input.get())); + } + */ + public map(input,func){ + input.add(func.apply(input.get())); + return map(new List(), func); + } +} + +class List{ + A get(); + void add(A); +} + +class Function{ + B apply(A a); +} \ No newline at end of file diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index d710174b1..4bd02ba62 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -25,11 +25,12 @@ public class JavaTXCompilerTest { @Test public void test() throws IOException, ClassNotFoundException { - filesToTest.add(new File(rootDirectory+"Lambda2.jav")); + filesToTest.add(new File(rootDirectory+"Lambda.jav")); + //filesToTest.add(new File(rootDirectory+"Lambda2.jav")); + //filesToTest.add(new File(rootDirectory+"Lambda3.jav")); //filesToTest.add(new File(rootDirectory+"Vector.jav")); //filesToTest.add(new File(rootDirectory+"Generics.jav")); //filesToTest.add(new File(rootDirectory+"MethodsEasy.jav")); - //filesToTest.add(new File(rootDirectory+"Lambda.jav")); //filesToTest.add(new File(rootDirectory+"Matrix.jav")); JavaTXCompiler compiler = new JavaTXCompiler(); for(File f : filesToTest){