From e229093017ce45fc0680b6b98ed8533de0b5cb31 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Mon, 29 Mar 2021 09:57:45 +0200 Subject: [PATCH] new file: src/test/java/insertGenerics/TestContraVariant.java modified: src/test/java/insertGenerics/TestTPHsAndGenerics.java new file: src/test/java/insertGenerics/TestThreeArgs.java --- .../FamilyOfGeneratedGenerics.java | 3 +- .../insertGenerics/TestContraVariant.java | 104 ++++++++++++++++++ .../insertGenerics/TestTPHsAndGenerics.java | 2 + .../java/insertGenerics/TestThreeArgs.java | 90 +++++++++++++++ 4 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 src/test/java/insertGenerics/TestContraVariant.java create mode 100644 src/test/java/insertGenerics/TestThreeArgs.java diff --git a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java index 5fbea9bc..cd479b20 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java @@ -597,7 +597,8 @@ public class FamilyOfGeneratedGenerics { while(itEqual.hasNext()) { Pair pairEqual = itEqual.next(); // tphR = (TypePlaceholder) (pairEqual.TA2); - Pair newPair = new Pair(resSet.resolveType((TypePlaceholder)(pairExtends.TA2)).resolvedType, resSet.resolveType((TypePlaceholder)(pairEqual.TA1)).resolvedType, PairOperator.SMALLERDOT); + Pair newPair = new Pair(resSet.resolveType((TypePlaceholder)(pairExtends.TA2)).resolvedType, + resSet.resolveType((TypePlaceholder)(pairEqual.TA1)).resolvedType, PairOperator.SMALLERDOT); Iterator itTC = tcOfoldConsSet.iterator(); while(itTC.hasNext()) { Pair pairTC = itTC.next(); diff --git a/src/test/java/insertGenerics/TestContraVariant.java b/src/test/java/insertGenerics/TestContraVariant.java new file mode 100644 index 00000000..0be39f5c --- /dev/null +++ b/src/test/java/insertGenerics/TestContraVariant.java @@ -0,0 +1,104 @@ +package insertGenerics; + +import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; +import de.dhbwstuttgart.bytecode.insertGenerics.ClassConstraint; +import de.dhbwstuttgart.bytecode.insertGenerics.FamilyOfGeneratedGenerics; +import de.dhbwstuttgart.bytecode.insertGenerics.MethodConstraint; +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; +import de.dhbwstuttgart.typeinference.result.ResultSet; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class TestContraVariant { + + public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/insertGenericsJav/"; + + @Test + public void ggFinder() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"TestContraVariant.jav")); + } + + private static class TestResultSet{ + + } + + public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException { + 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); + + List classConstraintsTest = new ArrayList<>(); + HashMap> methodConstraintsWithPositionTest = new HashMap<>(); + Set lmc; + lmc = new HashSet<>(); + lmc.add(new MethodConstraint("R", "O", Relation.EXTENDS)); + lmc.add(new MethodConstraint("O", "java/lang/Object", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH Rm(TPH O)", lmc); + lmc = new HashSet<>(); + lmc.add(new MethodConstraint("S", "java/lang/Object", Relation.EXTENDS)); + lmc.add(new MethodConstraint("R", "java/lang/Object", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH Rmain(TPH S)", lmc); + + FamilyOfGeneratedGenerics fogg = compiler.fogg; + Set computedClassCons = new HashSet<>(fogg.classConstraints); + Set expectedClassCons = new HashSet<>(classConstraintsTest); + + assertEquals(expectedClassCons, computedClassCons); + + HashMap> methodConstraintsWithPositionComputed = new HashMap<>(); + fogg.methodConstraintsWithPosition.forEach((s, l) -> methodConstraintsWithPositionComputed.put(s,new HashSet<>(l))); + + assertEquals(methodConstraintsWithPositionTest, methodConstraintsWithPositionComputed); + + //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); + } + } + 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/TestTPHsAndGenerics.java b/src/test/java/insertGenerics/TestTPHsAndGenerics.java index dd47ca52..ff99b2cb 100644 --- a/src/test/java/insertGenerics/TestTPHsAndGenerics.java +++ b/src/test/java/insertGenerics/TestTPHsAndGenerics.java @@ -79,6 +79,7 @@ public class TestTPHsAndGenerics { methodConstraintsWithPositionTest.put("TPH ETWid2(TPH V)", lmc); lmc = new HashSet<>(); lmc.add(new MethodConstraint("AM", "java/lang/Object", Relation.EXTENDS)); + lmc.add(new MethodConstraint("AI", "java/lang/Object", Relation.EXTENDS)); methodConstraintsWithPositionTest.put("TPH AIm2(TPH AMTPH AI)", lmc); } else { if (fogg.allConstraints.contains((new MethodConstraint("DZP", "U", Relation.EXTENDS)))) { @@ -97,6 +98,7 @@ public class TestTPHsAndGenerics { methodConstraintsWithPositionTest.put("TPH Uid2(TPH V)", lmc); lmc = new HashSet<>(); lmc.add(new MethodConstraint("AM", "java/lang/Object", Relation.EXTENDS)); + lmc.add(new MethodConstraint("AI", "java/lang/Object", Relation.EXTENDS)); methodConstraintsWithPositionTest.put("TPH AIm2(TPH AMTPH AI)", lmc); } else { if (fogg.allConstraints.contains((new MethodConstraint("EIM", "FEA", Relation.EXTENDS)))) { diff --git a/src/test/java/insertGenerics/TestThreeArgs.java b/src/test/java/insertGenerics/TestThreeArgs.java new file mode 100644 index 00000000..75e50580 --- /dev/null +++ b/src/test/java/insertGenerics/TestThreeArgs.java @@ -0,0 +1,90 @@ +package insertGenerics; + +import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; +import de.dhbwstuttgart.bytecode.insertGenerics.ClassConstraint; +import de.dhbwstuttgart.bytecode.insertGenerics.FamilyOfGeneratedGenerics; +import de.dhbwstuttgart.bytecode.insertGenerics.MethodConstraint; +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; +import de.dhbwstuttgart.typeinference.result.ResultSet; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +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; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class TestThreeArgs { + + 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 = "TestThreeArgs"; + + @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, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); + List results = compiler.typeInference(); + List simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results); + + List classConstraintsTest = new ArrayList<>(); + classConstraintsTest.add(new ClassConstraint("N", "java/lang/Object", Relation.EXTENDS)); + HashMap> methodConstraintsWithPositionTest = new HashMap<>(); + Set lmc; + lmc = new HashSet<>(); + lmc.add(new MethodConstraint("P", "R", Relation.EXTENDS)); + lmc.add(new MethodConstraint("R", "java/lang/Object", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH AFid(TPH P)", lmc); + lmc = new HashSet<>(); + lmc.add(new MethodConstraint("AF", "W", Relation.EXTENDS)); + lmc.add(new MethodConstraint("AB", "P", Relation.EXTENDS)); + lmc.add(new MethodConstraint("P", "AF", Relation.EXTENDS)); + lmc.add(new MethodConstraint("AC", "java/lang/Object", Relation.EXTENDS)); + lmc.add(new MethodConstraint("W", "java/lang/Object", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH Wm(TPH WTPH ABTPH AC)", lmc); + + FamilyOfGeneratedGenerics fogg = compiler.fogg; + Set computedClassCons = new HashSet<>(fogg.classConstraints); + Set expectedClassCons = new HashSet<>(classConstraintsTest); + + assertEquals(expectedClassCons, computedClassCons); + + HashMap> methodConstraintsWithPositionComputed = new HashMap<>(); + fogg.methodConstraintsWithPosition.forEach((s, l) -> methodConstraintsWithPositionComputed.put(s,new HashSet<>(l))); + + assertEquals(methodConstraintsWithPositionTest, methodConstraintsWithPositionComputed); + + 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(); + } +}