From e147a8ce85b2167b5bc0951adf92177ee409ea66 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Tue, 23 Mar 2021 12:02:58 +0100 Subject: [PATCH] toString geaendert, um Tests generieren zu koennen: modified: src/main/java/de/dhbwstuttgart/bytecode/constraint/TPHConstraint.java modified: src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/ClassConstraint.java modified: src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java modified: src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/MethodConstraint.java geaendert, um GenerateGenerics direkt teste zu koennen, sollte ggf. wieder rueckgaenig gemacht werden modified: src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java modified: src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java angepasset Tests: modified: src/test/java/insertGenerics/TestLocalVarLambda.java modified: src/test/java/insertGenerics/TestTPHsAndGenerics.java modified: src/test/java/insertGenerics/TestTwoArgs.java --- .../bytecode/constraint/TPHConstraint.java | 26 +++++++++++ .../GeneratedGenericsFinder.java | 10 +++++ .../insertGenerics/ClassConstraint.java | 9 ++++ .../FamilyOfGeneratedGenerics.java | 19 ++++++++ .../insertGenerics/MethodConstraint.java | 10 +++++ .../de/dhbwstuttgart/core/JavaTXCompiler.java | 7 +++ .../insertGenerics/TestLocalVarLambda.java | 26 +++++++++++ .../insertGenerics/TestTPHsAndGenerics.java | 45 +++++++++++++++++-- src/test/java/insertGenerics/TestTwoArgs.java | 43 ++++++++++++++++++ 9 files changed, 191 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/constraint/TPHConstraint.java b/src/main/java/de/dhbwstuttgart/bytecode/constraint/TPHConstraint.java index 0e3c9ed8..ae51c18e 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/constraint/TPHConstraint.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/constraint/TPHConstraint.java @@ -49,6 +49,23 @@ public class TPHConstraint { return rel == constraint.getRel() && left.equals(constraint.getLeft()) && right.equals(constraint.getRight()); } + @Override + public int hashCode() { + return (left+right).hashCode(); + } + + @Override + public boolean equals (Object o) { + if (o instanceof TPHConstraint) { + TPHConstraint o_tphcons = (TPHConstraint)o; + return (left+right).equals(o_tphcons.getLeft()+o_tphcons.getRight()); + } + else { + return false; + } + } + + /* @Override public String toString() { if(rel == Relation.EXTENDS) { @@ -57,4 +74,13 @@ public class TPHConstraint { return left + " = " + right; } } + */ + public String toString() { + if(rel == Relation.EXTENDS) { + return "new TPHConstraint(\"" + left + "\", \"" + right + "\", Relation.EXTENDS)"; + } + else { + return "new TPHConstraint(\"" + left + "\", \"" + right + "\", Relation.EQUAL)"; + } + } } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java index 595f874e..e25958d1 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java @@ -79,6 +79,7 @@ public class GeneratedGenericsFinder implements ASTVisitor { private JavaClassName className; private Resolver resolver; + /** * @param sf * @param listOfResultSets @@ -93,6 +94,14 @@ public class GeneratedGenericsFinder implements ASTVisitor { return generatedGenericsForSF; } + /** + * returns the family of Generated Generics + * insbesondere fuer Testzwecke + */ + public FamilyOfGeneratedGenerics getFogg() { + return fogg; + } + /* * (non-Javadoc) * @@ -140,6 +149,7 @@ public class GeneratedGenericsFinder implements ASTVisitor { System.out.println("fogg.posOfTPHs: "+ fogg.posOfTPHs); System.out.println("fogg.classConstraints: "+ fogg.classConstraints); System.out.println("fogg.methodConstraintsWithPosition: "+ fogg.methodConstraintsWithPosition); + System.out.println(fogg); ///* //Fayez Ansatz Anfang diff --git a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/ClassConstraint.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/ClassConstraint.java index 3f1d19d2..afbae005 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/ClassConstraint.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/ClassConstraint.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.bytecode.insertGenerics; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; +import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; public class ClassConstraint extends TPHConstraint { //private TPHConstraint constraint; @@ -12,4 +13,12 @@ public class ClassConstraint extends TPHConstraint { /*public ClassConstraint(TPHConstraint constraint) { this.constraint = constraint; }*/ + public String toString() { + if(rel == Relation.EXTENDS) { + return "new ClassConstraint(\"" + left + "\", \"" + right + "\", Relation.EXTENDS)"; + } + else { + return "new ClassConstraint(\"" + left + "\", \"" + right + "\", Relation.EQUAL)"; + } + } } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java index 5aafb37a..a5d01d03 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java @@ -760,6 +760,25 @@ public class FamilyOfGeneratedGenerics { return tempMethodConstraintsWithPosition; } + + public String toString() { + String ret = + "List classConstraintsTest = new ArrayList<>();\n"; + for(ClassConstraint cC: classConstraints) { + ret = ret + "classConstraintsTest.add(" + cC.toString() + ");\n"; + } + ret = ret + "HashMap> methodConstraintsWithPositionTest = new HashMap<>();\n"; + ret = ret + "List lmc;\n"; + for (String methName : methodConstraintsWithPosition.keySet()) { + String consSet = ""; + ret = ret + "lmc = new ArrayList<>();\n"; + for (MethodConstraint methConstrs : methodConstraintsWithPosition.get(methName)) { + consSet = consSet + "lmc.add(" + methConstrs + ");\n"; + } + ret = ret + consSet + "methodConstraintsWithPositionTest.put(\""+ methName + "\", lmc);\n"; + } + return ret; + } } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/MethodConstraint.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/MethodConstraint.java index d68f7d3f..c0000f20 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/MethodConstraint.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/MethodConstraint.java @@ -1,9 +1,19 @@ package de.dhbwstuttgart.bytecode.insertGenerics; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; +import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; public class MethodConstraint extends TPHConstraint { public MethodConstraint(String left, String right, Relation rel) { super(left, right, rel); } + + public String toString() { + if(rel == Relation.EXTENDS) { + return "new MethodConstraint(\"" + left + "\", \"" + right + "\", Relation.EXTENDS)"; + } + else { + return "new MethodConstraint(\"" + left + "\", \"" + right + "\", Relation.EQUAL)"; + } + } } diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 8425d180..eb220f4f 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -6,6 +6,7 @@ import de.dhbwstuttgart.bytecode.BytecodeGen; import de.dhbwstuttgart.bytecode.Exception.BytecodeGeneratorError; import de.dhbwstuttgart.bytecode.genericsGenerator.GeneratedGenericsFinder; import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; +import de.dhbwstuttgart.bytecode.insertGenerics.FamilyOfGeneratedGenerics; import de.dhbwstuttgart.environment.CompilationEnvironment; import de.dhbwstuttgart.environment.DirectoryClassLoader; import de.dhbwstuttgart.exceptions.DebugException; @@ -78,7 +79,12 @@ public class JavaTXCompiler { Boolean log = true; //gibt an ob ein Log-File nach System.getProperty("user.dir")+"src/test/java/logFiles" geschrieben werden soll? public volatile UnifyTaskModel usedTasks = new UnifyTaskModel(); private final ClassLoader classLoader; + + //nur fuer Testzwecke of Generated Generics + //wieder loeschen PL 2021-03-22 + public FamilyOfGeneratedGenerics fogg; + public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException { this(Arrays.asList(sourceFile), null); } @@ -872,6 +878,7 @@ public class JavaTXCompiler { SourceFile sf = sourceFiles.get(f); GeneratedGenericsFinder sResFinder = new GeneratedGenericsFinder(sf, typeinferenceResult); GenericGenratorResultForSourceFile simplifyResOfSF = sResFinder.findGeneratedGenerics(); + this.fogg = sResFinder.getFogg(); result.add(simplifyResOfSF); } return result; diff --git a/src/test/java/insertGenerics/TestLocalVarLambda.java b/src/test/java/insertGenerics/TestLocalVarLambda.java index bc830b49..043a9797 100644 --- a/src/test/java/insertGenerics/TestLocalVarLambda.java +++ b/src/test/java/insertGenerics/TestLocalVarLambda.java @@ -1,6 +1,10 @@ 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; @@ -10,6 +14,8 @@ 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; @@ -19,6 +25,8 @@ 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; @@ -45,6 +53,24 @@ public class TestLocalVarLambda { JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); List results = compiler.typeInference(); List simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results); + + List classConstraintsTest = new ArrayList<>(); + HashMap> methodConstraintsWithPositionTest = new HashMap<>(); + List lmc; + lmc = new ArrayList<>(); + lmc.add(new MethodConstraint("O", "ALU", Relation.EXTENDS)); + lmc.add(new MethodConstraint("DIU", "java/lang/Object", Relation.EXTENDS)); + lmc.add(new MethodConstraint("ALU", "DIU", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH DIUm(TPH O)", lmc); + + FamilyOfGeneratedGenerics fogg = compiler.fogg; + Set computedClassCons = new HashSet<>(fogg.classConstraints); + Set expectedClassCons = new HashSet<>(classConstraintsTest); + + assertEquals(computedClassCons, expectedClassCons); + + assertEquals(fogg.methodConstraintsWithPosition, methodConstraintsWithPositionTest); + compiler.generateBytecode(new File(pathToClassFile), results, simplifyResultsForAllSourceFiles); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass(className); diff --git a/src/test/java/insertGenerics/TestTPHsAndGenerics.java b/src/test/java/insertGenerics/TestTPHsAndGenerics.java index e49ed344..6aebd04f 100644 --- a/src/test/java/insertGenerics/TestTPHsAndGenerics.java +++ b/src/test/java/insertGenerics/TestTPHsAndGenerics.java @@ -1,7 +1,11 @@ package insertGenerics; - import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; - import de.dhbwstuttgart.core.JavaTXCompiler; + 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; @@ -10,7 +14,9 @@ package insertGenerics; import de.dhbwstuttgart.typeinference.result.ResultSet; import org.junit.Test; - import java.io.File; +import static org.junit.Assert.assertEquals; + +import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.net.URL; @@ -19,7 +25,9 @@ package insertGenerics; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; - import java.util.HashSet; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Set; @@ -46,6 +54,35 @@ public class TestTPHsAndGenerics { JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); List results = compiler.typeInference(); List simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results); + + List classConstraintsTest = new ArrayList<>(); + classConstraintsTest.add(new ClassConstraint("UD", "DZP", Relation.EXTENDS)); + classConstraintsTest.add(new ClassConstraint("DZP", "ETW", Relation.EXTENDS)); + classConstraintsTest.add(new ClassConstraint("ETW", "java/lang/Object", Relation.EXTENDS)); + HashMap> methodConstraintsWithPositionTest = new HashMap<>(); + List lmc; + lmc = new ArrayList<>(); + lmc.add(new MethodConstraint("AI", "AE", Relation.EXTENDS)); + lmc.add(new MethodConstraint("AD", "AI", Relation.EXTENDS)); + lmc.add(new MethodConstraint("AB", "AM", Relation.EXTENDS)); + lmc.add(new MethodConstraint("AE", "java/lang/Object", Relation.EXTENDS)); + lmc.add(new MethodConstraint("AM", "java/lang/Object", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH ABm(TPH ABTPH AD)", lmc); + lmc = new ArrayList<>(); + lmc.add(new MethodConstraint("V", "UD", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH ETWid2(TPH V)", lmc); + lmc = new ArrayList<>(); + lmc.add(new MethodConstraint("AM", "java/lang/Object", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH AIm2(TPH AMTPH AI)", lmc); + + FamilyOfGeneratedGenerics fogg = compiler.fogg; + Set computedClassCons = new HashSet<>(fogg.classConstraints); + Set expectedClassCons = new HashSet<>(classConstraintsTest); + + assertEquals(computedClassCons, expectedClassCons); + + assertEquals(fogg.methodConstraintsWithPosition, methodConstraintsWithPositionTest); + /* compiler.generateBytecode(new File(pathToClassFile), results, simplifyResultsForAllSourceFiles); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); diff --git a/src/test/java/insertGenerics/TestTwoArgs.java b/src/test/java/insertGenerics/TestTwoArgs.java index 0b2b87e3..d106e5c7 100644 --- a/src/test/java/insertGenerics/TestTwoArgs.java +++ b/src/test/java/insertGenerics/TestTwoArgs.java @@ -1,6 +1,13 @@ package insertGenerics; +import static org.junit.Assert.*; + +import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; +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; @@ -19,6 +26,8 @@ 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; @@ -45,6 +54,40 @@ public class TestTwoArgs { JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); List results = compiler.typeInference(); List simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results); + + List classConstraintsTest = new ArrayList<>(); + classConstraintsTest.add(new ClassConstraint("AP", "Z", Relation.EXTENDS)); + classConstraintsTest.add(new ClassConstraint("Z", "P", Relation.EXTENDS)); + classConstraintsTest.add(new ClassConstraint("P", "AL", Relation.EXTENDS)); + classConstraintsTest.add(new ClassConstraint("AL", "AF", Relation.EXTENDS)); + classConstraintsTest.add(new ClassConstraint("AF", "java/lang/Object", Relation.EXTENDS)); + HashMap> methodConstraintsWithPositionTest = new HashMap<>(); + List lmc; + lmc = new ArrayList<>(); + lmc.add(new MethodConstraint("P", "AL", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH ALid(TPH P)", lmc); + lmc = new ArrayList<>(); + lmc.add(new MethodConstraint("AL", "AF", Relation.EXTENDS)); + lmc.add(new MethodConstraint("Z", "P", Relation.EXTENDS)); + lmc.add(new MethodConstraint("P", "AL", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH AFm(TPH AFTPH Z)", lmc); + lmc = new ArrayList<>(); + lmc.add(new MethodConstraint("T", "AP", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH APsetA(TPH T)", lmc); + lmc = new ArrayList<>(); + lmc.add(new MethodConstraint("AG", "P", Relation.EXTENDS)); + lmc.add(new MethodConstraint("AH", "T", Relation.EXTENDS)); + lmc.add(new MethodConstraint("T", "AP", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH AFmain(TPH AGTPH AH)", lmc); + + FamilyOfGeneratedGenerics fogg = compiler.fogg; + Set computedClassCons = new HashSet<>(fogg.classConstraints); + Set expectedClassCons = new HashSet<>(classConstraintsTest); + + assertEquals(computedClassCons, expectedClassCons); + + assertEquals(fogg.methodConstraintsWithPosition, methodConstraintsWithPositionTest); + compiler.generateBytecode(new File(pathToClassFile), results, simplifyResultsForAllSourceFiles); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); classToTest = loader.loadClass(className);