diff --git a/notizen/stan/lambdaBytecode/Lambda2.java b/notizen/stan/lambdaBytecode/Lambda2.java new file mode 100644 index 00000000..b2d6535b --- /dev/null +++ b/notizen/stan/lambdaBytecode/Lambda2.java @@ -0,0 +1,4 @@ + +class Lambda2 { + Fun1> op = (f) -> f.apply(new Integer(2)); +} diff --git a/src/de/dhbwstuttgart/bytecode/ClassGenerator.java b/src/de/dhbwstuttgart/bytecode/ClassGenerator.java index ef7138d7..0f8012ac 100644 --- a/src/de/dhbwstuttgart/bytecode/ClassGenerator.java +++ b/src/de/dhbwstuttgart/bytecode/ClassGenerator.java @@ -51,8 +51,8 @@ public class ClassGenerator extends ClassGen{ * @param toTPH * @return Es gilt dann "toTPH extends Type" */ - public org.apache.commons.bcel6.generic.Type getNearestNonTPHType(TypePlaceholder toTPH){ - Type t = resolveTPH(toTPH); + public org.apache.commons.bcel6.generic.Type getNearestUsedType(TypePlaceholder toTPH, Menge usedTypes){ + Type t = resolveTPH(toTPH, usedTypes); if(t == null){ return this.getInstructionFactory().createObjectType(); }else if(t instanceof TypePlaceholder){ //Es muss sich in diesem Fall um einen TPH handeln: @@ -62,9 +62,16 @@ public class ClassGenerator extends ClassGen{ return t.getBytecodeType(this); } } + public org.apache.commons.bcel6.generic.Type getNearestUsedType(TypePlaceholder toTPH){ + return this.getNearestUsedType(toTPH, null); + } public Type resolveTPH(TypePlaceholder typePlaceholder) { - return tiResult.getTypeOfPlaceholder(typePlaceholder); + return resolveTPH(typePlaceholder, null); + } + + public Type resolveTPH(TypePlaceholder typePlaceholder, Menge toOneOfTheseTypes) { + return tiResult.getTypeOfPlaceholder(typePlaceholder, toOneOfTheseTypes); } public String createLambdaMethodName() { diff --git a/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java b/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java index 831476ed..4b48e9c5 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java @@ -387,8 +387,7 @@ public class TypePlaceholder extends ObjectType // Ende Spezialfunktionen /////////////////////////////////////////////////////////////////// - @Override - public JavaCodeResult printJavaCode(ResultSet resultSet) { + private JavaCodeResult printJavaCodePr(ResultSet resultSet) { Type equalType = resultSet.getTypeEqualTo(this); if(equalType == null || equalType.equals(this)){ //Für den Fall das der TPH nicht aufgelöst werden konnte. @@ -400,9 +399,10 @@ public class TypePlaceholder extends ObjectType return equalType.printJavaCode(resultSet); } - + + @Override public JavaCodeResult printJavaCode(ResultSet resultSet, boolean resolveTPHs){ - if(resolveTPHs)return printJavaCode(resultSet); + if(resolveTPHs)return printJavaCodePr(resultSet); return new JavaCodeResult(this.get_Name()); } /** @@ -457,10 +457,14 @@ public class TypePlaceholder extends ObjectType @Override public String getClassSignature(ClassGenerator cg) { //Die Signaturen in der Klasse bauen sich für Generische Variabeln (also TPHs) folgendermaßen auf: "GVAR:SuperClass" - String ret = this.getBytecodeSignature(cg); - ret = ret.substring(1, ret.length()-1) + ":"; //";" mit ":" ersetzen - org.apache.commons.bcel6.generic.Type nearestType = cg.getNearestNonTPHType(this); - if(nearestType instanceof TypePlaceholderType){ //Handelt es sich um einen weiteren TPH als nächsten Typ, so ist es ein allgemeiner Typ und wir nehmen Object als Superklasse + String ret = this.get_Name();//this.getBytecodeSignature(cg); + //ret = ret.substring(1, ret.length()-1) + ":"; //";" mit ":" ersetzen + ret+=":"; + Menge possibleTPHs = cg.getUsedTPH(); + possibleTPHs.remove(this); + org.apache.commons.bcel6.generic.Type nearestType = cg.getNearestUsedType(this, possibleTPHs); + //if(nearestType instanceof TypePlaceholderType){ //Handelt es sich um einen weiteren TPH als nächsten Typ, so ist es ein allgemeiner Typ und wir nehmen Object als Superklasse + if(nearestType == null){ ret += cg.getInstructionFactory().createObjectType().getSignature(); }else{ ret += nearestType.getSignature(); diff --git a/src/de/dhbwstuttgart/typeinference/ResultSet.java b/src/de/dhbwstuttgart/typeinference/ResultSet.java index 5f1aeca2..59de1ba0 100755 --- a/src/de/dhbwstuttgart/typeinference/ResultSet.java +++ b/src/de/dhbwstuttgart/typeinference/ResultSet.java @@ -2,7 +2,7 @@ package de.dhbwstuttgart.typeinference; import java.util.Iterator; import de.dhbwstuttgart.typeinference.Menge; - +import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.Type; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.exceptions.DebugException; @@ -38,23 +38,48 @@ public class ResultSet implements Iterable { * @return */ public Type getTypeEqualTo(Type type) { + return getTypeEqualTo(type,null); + } + + /** + * Löst den übergebenen Typ auf. Rückgabetyp ist ein Reftype oder Void + * @param type + * @return + */ + public Type getTypeEqualTo(Type type, Menge ofType) { Type res = type; Menge modifiedResultPairs = (Menge) resultPairs.clone(); - int i = findPairWithTypeEqualTo(type, modifiedResultPairs); - while(i != -1){ - res = modifiedResultPairs.get(i).TA2; - modifiedResultPairs.remove(i); - i = findPairWithTypeEqualTo(res, modifiedResultPairs); + Ret r = findPairWithTypeEqualTo(type, modifiedResultPairs); + while(r.i != -1 && r.ret != null){ + res = r.ret; + if(ofType != null){ + if(res instanceof RefType)return res; + if(ofType.contains(res) && ! res.equals(type))return res; + } + modifiedResultPairs.remove(r.i); + r = findPairWithTypeEqualTo(res, modifiedResultPairs); } return res; } - private int findPairWithTypeEqualTo(Type type, Menge inResultPairs){ + class Ret{ + int i; + Type ret; + private Ret(int i, Type ret){ + this.i = i; + this.ret = ret; + } + } + + private Ret findPairWithTypeEqualTo(Type type, Menge inResultPairs){ for(int i = 0; i iterator() { diff --git a/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java b/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java index 8ceb950a..9a38d298 100755 --- a/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java +++ b/src/de/dhbwstuttgart/typeinference/TypeinferenceResultSet.java @@ -80,8 +80,11 @@ public class TypeinferenceResultSet * Ermittelt den in diesem ResultSet für den TypePlaceholder tph zugewiesenen Wert. * @author Andreas Stadelmeier, a10023 */ - public Type getTypeOfPlaceholder(TypePlaceholder tph){ - return this.getUnifiedConstraints().getTypeEqualTo(tph); + public Type getTypeOfPlaceholder(TypePlaceholder tph){ + return this.getTypeOfPlaceholder(tph,null); + } + public Type getTypeOfPlaceholder(TypePlaceholder tph, Menge ofType){ + return this.getUnifiedConstraints().getTypeEqualTo(tph, ofType); } /** diff --git a/test/bytecode/Matrix_lambda2.jav b/test/bytecode/Matrix_lambda2.jav index aad428d4..e67ce40c 100644 --- a/test/bytecode/Matrix_lambda2.jav +++ b/test/bytecode/Matrix_lambda2.jav @@ -1,4 +1,4 @@ -class Matrix { +class Matrix2 { op = (f) -> f.apply(2); } \ No newline at end of file diff --git a/test/bytecode/Matrix_lambda3.jav b/test/bytecode/Matrix_lambda3.jav new file mode 100644 index 00000000..7fc6dede --- /dev/null +++ b/test/bytecode/Matrix_lambda3.jav @@ -0,0 +1,4 @@ + +class Matrix3 { + op = (f) -> f.apply(); +} \ No newline at end of file diff --git a/test/bytecode/Matrix_lambdaTest2.java b/test/bytecode/Matrix_lambdaTest2.java index aa6ec4fd..3264bfa0 100644 --- a/test/bytecode/Matrix_lambdaTest2.java +++ b/test/bytecode/Matrix_lambdaTest2.java @@ -5,7 +5,7 @@ import org.junit.Test; public class Matrix_lambdaTest2 { public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; public final static String testFile = "Matrix_lambda2.jav"; - public final static String outputFile = "Matrix_lambda2.class"; + public final static String outputFile = "Matrix2.class"; @Test public void test() { diff --git a/test/bytecode/Matrix_lambdaTest3.java b/test/bytecode/Matrix_lambdaTest3.java new file mode 100644 index 00000000..a649ec93 --- /dev/null +++ b/test/bytecode/Matrix_lambdaTest3.java @@ -0,0 +1,14 @@ +package bytecode; + +import org.junit.Test; + +public class Matrix_lambdaTest3 { + public final static String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; + public final static String testFile = "Matrix_lambda3.jav"; + public final static String outputFile = "Matrix3.class"; + + @Test + public void test() { + SingleClassTester.compileToBytecode(rootDirectory+testFile, rootDirectory+outputFile); + } +} diff --git a/test/bytecode/Test.java b/test/bytecode/Test.java index 22e417af..cd86d699 100644 --- a/test/bytecode/Test.java +++ b/test/bytecode/Test.java @@ -21,7 +21,7 @@ class Test2{ return f; } public static void main(String[] args){ - Test2 t = new Test2<>(); - System.out.println(t.m().apply(1)); + Matrix2 m2 = new Matrix2<>(); + System.out.println(m2.op.apply((Integer x) -> x)); } } diff --git a/test/plugindevelopment/TypeInsertTests/LambdaTest29.jav b/test/plugindevelopment/TypeInsertTests/LambdaTest29.jav new file mode 100644 index 00000000..c297d4df --- /dev/null +++ b/test/plugindevelopment/TypeInsertTests/LambdaTest29.jav @@ -0,0 +1,3 @@ +class LambdaTest29{ + lambda = (f)->f.apply(); +} \ No newline at end of file diff --git a/test/plugindevelopment/TypeInsertTests/LambdaTest29.java b/test/plugindevelopment/TypeInsertTests/LambdaTest29.java new file mode 100644 index 00000000..d832d75d --- /dev/null +++ b/test/plugindevelopment/TypeInsertTests/LambdaTest29.java @@ -0,0 +1,18 @@ +package plugindevelopment.TypeInsertTests; + +import de.dhbwstuttgart.typeinference.Menge; + +import org.junit.Test; + +public class LambdaTest29 { + + private static final String TEST_FILE = "LambdaTest29.jav"; + + @Test + public void run(){ + Menge mustContain = new Menge(); + //mustContain.add("A a"); + MultipleTypesInsertTester.test(this.TEST_FILE, mustContain); + } + +}