diff --git a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java index 9b8951997..e30299328 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java @@ -24,8 +24,10 @@ import de.dhbwstuttgart.syntaxtree.Field; import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.ParameterList; +import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.statement.LocalVar; import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl; +import de.dhbwstuttgart.syntaxtree.statement.MethodCall; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; @@ -41,6 +43,8 @@ import de.dhbwstuttgart.typeinference.result.ResultSet; public class TPHExtractor extends AbstractASTWalker { // Alle TPHs der Felder werden iKopf der Klasse definiert // alle TPHs der Klasse: (TPH, is in Method?) + public final SourceFile sf; + public final HashMap nameToMeth; public final HashMap allTPHS = new HashMap<>(); public final List tphsClass = new ArrayList<>(); MethodAndTPH methodAndTph; @@ -54,8 +58,12 @@ public class TPHExtractor extends AbstractASTWalker { private ResultSet resultSet; private Resolver resolver; - public TPHExtractor() { - + public TPHExtractor(SourceFile sf) { + this.sf = sf; + this.nameToMeth = new HashMap<>(); + for(Method m : sf.getAllMethods()) { + this.nameToMeth.put(m.getName(), m); + } } public void setResultSet(ResultSet resultSet) { @@ -216,6 +224,24 @@ public class TPHExtractor extends AbstractASTWalker { // inLocalOrParamOrReturn = false; } + + @Override + public void visit(MethodCall methodCall) { + super.visit(methodCall); + Iterator paraIt = nameToMeth.get(methodCall.name).getParameterList().iterator(); + methodCall.getArgumentList() + .getArguments() + .forEach(x -> { + RefTypeOrTPHOrWildcardOrGeneric left = null; + RefTypeOrTPHOrWildcardOrGeneric right = null; + if ((left = x.getType()) instanceof TypePlaceholder) { + if ((right = paraIt.next().getType()) instanceof TypePlaceholder) { + methodAndTph.addPair(((TypePlaceholder)left).getName(), + ((TypePlaceholder)left).getName()); + } + } + }); + } @Override public void visit(LocalVar localVar) { // inLocalOrParamOrReturn = inMethod; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java index 493bf1ed0..71c44f83b 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java @@ -66,7 +66,7 @@ import de.dhbwstuttgart.typeinference.result.ResultSet; * */ public class GeneratedGenericsFinder implements ASTVisitor { - private final TPHExtractor tphExtractor = new TPHExtractor(); + private final TPHExtractor tphExtractor; private Collection listOfResultSets; private SourceFile sf; private List tphsClass; @@ -86,6 +86,7 @@ public class GeneratedGenericsFinder implements ASTVisitor { public GeneratedGenericsFinder(SourceFile sf, Collection listOfResultSets) { this.sf = sf; this.listOfResultSets = listOfResultSets; + this.tphExtractor = new TPHExtractor(sf); } public GenericGenratorResultForSourceFile findGeneratedGenerics() { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java index 4a22147be..f9ec340b1 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -45,6 +45,12 @@ public class SourceFile extends SyntaxTreeNode{ public List getClasses() { return KlassenVektor; } + + public List getAllMethods() { + List ret = new ArrayList<>(); + getClasses().forEach(cl -> ret.addAll(cl.getMethods())); + return ret; + } @Override public void accept(ASTVisitor visitor) { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index b60270b38..a698ce3bf 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -258,6 +258,7 @@ public class UnifyTypeFactory { ret = TypePlaceholder.fresh(new NullToken()); tphs.put(t.getName(), ret); } + ret.setVariance(t.getVariance()); return ret; } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java b/src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java index 66435b68a..38932247a 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java @@ -20,10 +20,9 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric private final String name; /** - * wird bisher nicht genutzt - * setVariance muss ggf. auskommentiert werden. + * wird im Generate Generics Teil nach der Rueckumwandlung nach dem Unify genutzt */ - int variance = 0; + private int variance = 0; /** @@ -78,11 +77,9 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric return name; } - /* wird bisher nicht genutzt public void setVariance(int variance) { this.variance= variance; } - */ public int getVariance() { return this.variance; diff --git a/src/test/java/insertGenerics/TestTwoArgs.java b/src/test/java/insertGenerics/TestTwoArgs.java index 7513b65f9..0b2b87e34 100644 --- a/src/test/java/insertGenerics/TestTwoArgs.java +++ b/src/test/java/insertGenerics/TestTwoArgs.java @@ -12,6 +12,9 @@ import org.junit.Test; import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.net.URL; +import java.net.URLClassLoader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -22,51 +25,30 @@ import java.util.Set; public class TestTwoArgs { - public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/insertGenericsJav/"; - - @Test - public void ggFinder() throws IOException, ClassNotFoundException { - execute(new File(rootDirectory+"TestTwoArgs.jav")); + private static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/insertGenericsJav/"; + private String pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/"; + private static ClassLoader loader; + private static Class classToTest; + private static Object instanceOfClass; + private static String className = "TestTwoArgs"; + + @Test + public void ggFinder() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + execute(new File(rootDirectory+className+".jav")); } private static class TestResultSet{ } - public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException { + public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); - for(File f : compiler.sourceFiles.keySet()){ - SourceFile sf = compiler.sourceFiles.get(f); - } List results = compiler.typeInference(); List simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results); - //compiler.generateBytecode(rootDirectory+"xxx.class", results, simplifyResultsForAllSourceFiles); - 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<>(); - for(ResultSet resultSet : results){ - Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet, results, simplifyResultsForAllSourceFiles); - assert result.size()>0; - String content = readFile(f.getPath(), StandardCharsets.UTF_8); - for(TypeInsert tip : result){ - insertedTypes.add(tip.insert(content)); - } - } - for(String s : insertedTypes){ - System.out.println(s); - } - } + compiler.generateBytecode(new File(pathToClassFile), results, simplifyResultsForAllSourceFiles); + loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); + classToTest = loader.loadClass(className); + instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); return new TestResultSet(); } - - static String readFile(String path, Charset encoding) - throws IOException - { - byte[] encoded = Files.readAllBytes(Paths.get(path)); - return new String(encoded, encoding); - } } diff --git a/src/test/java/insertGenerics/TestTwoArgs2.java b/src/test/java/insertGenerics/TestTwoArgs2.java index 084eeb1c9..6522187a7 100644 --- a/src/test/java/insertGenerics/TestTwoArgs2.java +++ b/src/test/java/insertGenerics/TestTwoArgs2.java @@ -12,6 +12,9 @@ import org.junit.Test; import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.net.URL; +import java.net.URLClassLoader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -22,51 +25,30 @@ import java.util.Set; public class TestTwoArgs2 { - public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/insertGenericsJav/"; - - @Test - public void ggFinder() throws IOException, ClassNotFoundException { - execute(new File(rootDirectory+"TestTwoArgs2.jav")); + private static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/insertGenericsJav/"; + private String pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/"; + private static ClassLoader loader; + private static Class classToTest; + private static Object instanceOfClass; + private static String className = "TestTwoArgs2"; + + @Test + public void ggFinder() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + execute(new File(rootDirectory+className+".jav")); } private static class TestResultSet{ } - public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException { + public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); - for(File f : compiler.sourceFiles.keySet()){ - SourceFile sf = compiler.sourceFiles.get(f); - } List results = compiler.typeInference(); List simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results); - //compiler.generateBytecode(rootDirectory+"xxx.class", results, simplifyResultsForAllSourceFiles); - 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<>(); - for(ResultSet resultSet : results){ - Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet, results, simplifyResultsForAllSourceFiles); - assert result.size()>0; - String content = readFile(f.getPath(), StandardCharsets.UTF_8); - for(TypeInsert tip : result){ - insertedTypes.add(tip.insert(content)); - } - } - for(String s : insertedTypes){ - System.out.println(s); - } - } + compiler.generateBytecode(new File(pathToClassFile), results, simplifyResultsForAllSourceFiles); + loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); + classToTest = loader.loadClass(className); + instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); return new TestResultSet(); } - - static String readFile(String path, Charset encoding) - throws IOException - { - byte[] encoded = Files.readAllBytes(Paths.get(path)); - return new String(encoded, encoding); - } } diff --git a/src/test/resources/insertGenericsJav/TestTwoArgs.jav b/src/test/resources/insertGenericsJav/TestTwoArgs.jav index 78b274e46..846311510 100644 --- a/src/test/resources/insertGenericsJav/TestTwoArgs.jav +++ b/src/test/resources/insertGenericsJav/TestTwoArgs.jav @@ -1,4 +1,4 @@ -class TestTwoArgs { +public class TestTwoArgs { a; id(b) { @@ -13,6 +13,7 @@ class TestTwoArgs { m(x,y) { x = id(y); + return x; } main(x,y) { diff --git a/src/test/resources/insertGenericsJav/TestTwoArgs2.jav b/src/test/resources/insertGenericsJav/TestTwoArgs2.jav index a1913bfcf..f8c270a71 100644 --- a/src/test/resources/insertGenericsJav/TestTwoArgs2.jav +++ b/src/test/resources/insertGenericsJav/TestTwoArgs2.jav @@ -1,4 +1,4 @@ -class TestTwoArgs { +public class TestTwoArgs2 { a; id(b) { @@ -6,17 +6,12 @@ class TestTwoArgs { return c; } - setA(x) { - a = x; - return a; - } - m(x,y) { x = id(y); return x; } main(x,y) { - return m(id(x),setA(y)); + return m(id(x),m(x,y)); } } \ No newline at end of file