From eb270035152a2162de76d9352c6ba333e4c92aaa Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Thu, 7 May 2020 16:29:00 +0200 Subject: [PATCH] modified: ../../../../main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java modified: ../../../../main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java modified: ../../../../main/java/de/dhbwstuttgart/typeinference/unify/model/Node.java Fehler in der Transitivaet der Finit Closure gefixt modified: ../../../java/bytecode/InheritTest.java modified: ../../bytecode/javFiles/AA.jav modified: ../../bytecode/javFiles/CC.jav modified: ../../bytecode/javFiles/Inherit.jav Noch Probl;em in der Reflections --- .../typeinference/unify/TypeUnifyTask.java | 4 +- .../unify/model/FiniteClosure.java | 15 +++- .../typeinference/unify/model/Node.java | 18 +++++ src/test/java/bytecode/InheritTest.java | 76 ++++++++++++++----- src/test/resources/bytecode/javFiles/AA.jav | 3 +- src/test/resources/bytecode/javFiles/CC.jav | 4 +- .../resources/bytecode/javFiles/Inherit.jav | 5 +- 7 files changed, 96 insertions(+), 29 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 3a8d5e3e..9be3c74d 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -2041,7 +2041,7 @@ public class TypeUnifyTask extends RecursiveTask>> { //Set retFlat = new HashSet<>(); //ret.stream().forEach(x -> retFlat.addAll(x)); - //Alle wildcard Faelle rausfiltern bei not wildable + //Alle wildcard Faelle rausfiltern bei not wildcardable ret = ret.stream().filter(x -> { Optional optElem; return !((optElem=x.stream().filter(y -> (y.getLhsType()) instanceof PlaceholderType && !((PlaceholderType)y.getLhsType()).isWildcardable() @@ -2079,7 +2079,7 @@ public class TypeUnifyTask extends RecursiveTask>> { //Set retFlat = new HashSet<>(); //ret.stream().forEach(x -> retFlat.addAll(x)); - //Alle wildcard Faelle rausfiltern bei not wildable + //Alle wildcard Faelle rausfiltern bei not wildcardable ret = ret.stream().filter(x -> { Optional optElem; return !((optElem=x.stream().filter(y -> (y.getLhsType()) instanceof PlaceholderType && !((PlaceholderType)y.getLhsType()).isWildcardable() diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index 3a86af14..038f308e 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -75,6 +75,15 @@ implements IFiniteClosure { // Build the transitive closure of the inheritance tree for(UnifyPair pair : pairs) { + + try { + logFile.write("Pair: " + pair + "\n"); + logFile.flush(); + } + catch (IOException e) { + System.err.println("no LogFile"); + } + if(pair.getPairOp() != PairOperator.SMALLER) continue; @@ -93,13 +102,17 @@ implements IFiniteClosure { Node childNode = inheritanceGraph.get(pair.getLhsType()); Node parentNode = inheritanceGraph.get(pair.getRhsType()); - + // Add edge parentNode.addDescendant(childNode); // Add edges to build the transitive closure parentNode.getPredecessors().stream().forEach(x -> x.addDescendant(childNode)); childNode.getDescendants().stream().forEach(x -> x.addPredecessor(parentNode)); + + //PL eingefuegt 2020-05-07 File UnitTest InheritTest.java + this.inheritanceGraph.forEach((x,y) -> { if (y.getDescendants().contains(parentNode)) { y.addDescendant(childNode); y.addAllDescendant(childNode.getDescendants());}; + if (y.getPredecessors().contains(childNode)) { y.addPredecessor(parentNode); y.addAllPredecessor(parentNode.getPredecessors());};} ); } // Build the alternative representation with strings as keys diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/Node.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/Node.java index d108e91b..031130cf 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/Node.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/Node.java @@ -45,6 +45,15 @@ class Node { descendant.addPredecessor(this); } + /** + * Adds some directed edges from this node to the descendant (this -> descendant) + */ + public void addAllDescendant(Set> allDescendants) { + for(Node descendant: allDescendants) { + addDescendant(descendant); + } + } + /** * Adds a directed edge from the predecessor to this node (predecessor -> this) */ @@ -56,6 +65,15 @@ class Node { predecessor.addDescendant(this); } + /** + * Adds some directed edges from the predecessor to this node (predecessor -> this) + */ + public void addAllPredecessor(Set> allPredecessors) { + for(Node predecessor: allPredecessors) { + addPredecessor(predecessor); + } + } + /** * The content of this node. */ diff --git a/src/test/java/bytecode/InheritTest.java b/src/test/java/bytecode/InheritTest.java index 7166c54e..5d9bb231 100644 --- a/src/test/java/bytecode/InheritTest.java +++ b/src/test/java/bytecode/InheritTest.java @@ -7,13 +7,19 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Stack; import java.util.Vector; +import java.util.stream.Collectors; import org.junit.BeforeClass; import org.junit.Test; +import com.google.common.collect.Lists; + import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -24,17 +30,17 @@ public class InheritTest { private static JavaTXCompiler compiler; private static ClassLoader loader; private static Class classToTest; - private static Class classToTest1; - private static String pathToClassFile; + private static Class classToTest1, classToTest2, classToTest3, classToTest4; + private static String pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/";; private static Object instanceOfClass; - private static Object instanceOfClass1; + private static Object instanceOfClass1, instanceOfClass2, instanceOfClass3, instanceOfClass4; + private static HashMap, Method> hm = new HashMap<>(); @BeforeClass public static void setUpBeforeClass() throws Exception { path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/AA.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/"; List typeinferenceResult = compiler.typeInference(); List simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); @@ -45,45 +51,53 @@ public class InheritTest { path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/BB.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/"; typeinferenceResult = compiler.typeInference(); simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); - classToTest1 = loader.loadClass("AA"); - instanceOfClass1 = classToTest1.getDeclaredConstructor().newInstance(); + classToTest2 = loader.loadClass("BB"); + instanceOfClass2 = classToTest1.getDeclaredConstructor().newInstance(); path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/CC.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/"; typeinferenceResult = compiler.typeInference(); simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); - classToTest1 = loader.loadClass("AA"); - instanceOfClass1 = classToTest1.getDeclaredConstructor().newInstance(); + classToTest3 = loader.loadClass("CC"); + instanceOfClass3 = classToTest1.getDeclaredConstructor().newInstance(); path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/DD.jav"; fileToTest = new File(path); compiler = new JavaTXCompiler(fileToTest); - pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/"; typeinferenceResult = compiler.typeInference(); simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); - classToTest1 = loader.loadClass("AA"); - instanceOfClass1 = classToTest1.getDeclaredConstructor().newInstance(); + classToTest4 = loader.loadClass("DD"); + instanceOfClass4 = classToTest1.getDeclaredConstructor().newInstance(); + path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/Inherit.jav"; fileToTest = new File(path); - compiler = new JavaTXCompiler(fileToTest); - pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/"; + compiler = new JavaTXCompiler( + Lists.newArrayList(fileToTest), + Lists.newArrayList(new File(pathToClassFile))); + //compiler = new JavaTXCompiler(fileToTest); typeinferenceResult = compiler.typeInference(); simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass("Inherit"); instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); + + for (Method m: classToTest.getMethods()) { + ArrayList param = Arrays.stream(m.getParameterTypes()).map(x -> x.getName()).collect(Collectors.toCollection(ArrayList::new)); + ArrayList nameParam = new ArrayList<>(); + nameParam.add(m.getName()); + nameParam.addAll(param); + hm.put(nameParam, m); + } } @Test @@ -92,15 +106,35 @@ public class InheritTest { } @Test - public void testAName() { + public void testAAName() { assertEquals("AA", classToTest1.getName()); } - /* + @Test - public void testPutElementVector() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Method m = classToTest.getDeclaredMethod("putElement", Object.class, Vector.class); - Vector v_invoke = new Vector<>(); - m.invoke(instanceOfClass, 5, v_invoke); + public void testBBName() { + assertEquals("BB", classToTest2.getName()); + } + + @Test + public void testCCName() { + assertEquals("CC", classToTest3.getName()); + } + + @Test + public void testDDName() { + assertEquals("DD", classToTest4.getName()); + } + + + @Test + public void testmainAA() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException { + String[] x = {"main", "AA", "java.lang.Integer"}; + ArrayList nameParam = new ArrayList<>(Arrays.asList(x)); + Method m = hm.get(nameParam); + assertEquals(m.invoke(classToTest1.getConstructor().newInstance(), 5), "AA"); + } + /* + Vector v_invoke = new Vector<>(); Vector v = new Vector<>(); v.add(5); assertEquals(v, v_invoke); diff --git a/src/test/resources/bytecode/javFiles/AA.jav b/src/test/resources/bytecode/javFiles/AA.jav index aee2d5de..4593fdfb 100644 --- a/src/test/resources/bytecode/javFiles/AA.jav +++ b/src/test/resources/bytecode/javFiles/AA.jav @@ -1,5 +1,6 @@ import java.lang.Integer; +import java.lang.String; public class AA { - m(Integer i) { } + m(Integer i) { return "AA"; } } \ No newline at end of file diff --git a/src/test/resources/bytecode/javFiles/CC.jav b/src/test/resources/bytecode/javFiles/CC.jav index af5a4000..4523dfda 100644 --- a/src/test/resources/bytecode/javFiles/CC.jav +++ b/src/test/resources/bytecode/javFiles/CC.jav @@ -1,7 +1,7 @@ import java.lang.Integer; - +import java.lang.String; public class CC extends BB { - m(Integer i) { } + m(Integer i) { return "CC"; } } diff --git a/src/test/resources/bytecode/javFiles/Inherit.jav b/src/test/resources/bytecode/javFiles/Inherit.jav index 4f13c94e..bf677af9 100644 --- a/src/test/resources/bytecode/javFiles/Inherit.jav +++ b/src/test/resources/bytecode/javFiles/Inherit.jav @@ -1,17 +1,18 @@ import java.util.Vector; import java.lang.Integer; +import java.lang.String; public class Inherit { main(d, i) { - d.m(i); + return d.m(i); } main(v, i) { var aa = v.elementAt(0); - aa.m(i); + return aa.m(i); } } \ No newline at end of file