From 5060cca6dbcaea64ddc8f1f6e3e8ecbb5564c133 Mon Sep 17 00:00:00 2001 From: AluAli Date: Fri, 23 Oct 2020 10:42:24 +0200 Subject: [PATCH] modified: src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java modified: src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java modified: src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/MethodConstraint.java modified: src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java new file: src/test/java/insertGenerics/MethodsTest.java new file: src/test/java/insertGenerics/TryTest.java new file: src/test/resources/insertGenericsJav/TestGGFinder.jav --- .../gGenericsAli/ClassConstraint.java | 7 +- .../FamilyOfGeneratedGenerics.java | 30 +++--- .../gGenericsAli/MethodConstraint.java | 7 +- .../GeneratedGenericsFinder.java | 4 + src/test/java/insertGenerics/MethodsTest.java | 30 ++++++ src/test/java/insertGenerics/TryTest.java | 92 +++++++++++++++++++ .../insertGenericsJav/TestGGFinder.jav | 17 ++++ 7 files changed, 174 insertions(+), 13 deletions(-) create mode 100644 src/test/java/insertGenerics/MethodsTest.java create mode 100644 src/test/java/insertGenerics/TryTest.java create mode 100644 src/test/resources/insertGenericsJav/TestGGFinder.jav diff --git a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java index 113a797e..13387548 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/ClassConstraint.java @@ -1,4 +1,9 @@ package de.dhbwstuttgart.bytecode.gGenericsAli; -public class ClassConstraint { +import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; + +public class ClassConstraint extends TPHConstraint { + public ClassConstraint(String left, String right, Relation rel) { + super(left, right, rel); + } } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java index cf5b88e2..8a363983 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/FamilyOfGeneratedGenerics.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.bytecode.gGenericsAli; +import de.dhbwstuttgart.bytecode.TPHExtractor; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; import de.dhbwstuttgart.bytecode.genericsGenerator.ConstraintsSimplierResult; @@ -10,14 +11,14 @@ import de.dhbwstuttgart.syntaxtree.Method; import java.util.List; public class FamilyOfGeneratedGenerics { // extends TPHConstraint? - // private final TPHExtractor tphExtractor = new TPHExtractor(); - private final List cs; + private final TPHExtractor tphExtractor = new TPHExtractor(); + private final List cs = tphExtractor.allCons; //alle Constraints bekommen wie? public FamilyOfGeneratedGenerics(List cs) { this.cs = cs; } - List cs_cl; - List cs_m; + List cs_cl = null; + List cs_m = null; /*public Type getLeftSideOfConstraint(TPHConstraint constraint) { Type leftSideType = null; @@ -85,6 +86,12 @@ public class FamilyOfGeneratedGenerics { // extends TPHConstraint? */ public List typeOfANodeOfAField(List cs) { //TODO: + for(TPHConstraint cons: cs){ + if(cons.getRight()!=null && cons.getRel()==Relation.EXTENDS) { + cs_cl.add(new ClassConstraint(cons.getLeft(), cons.getRight(),cons.getRel())); + } + } + return cs_cl; } /** @@ -93,6 +100,12 @@ public class FamilyOfGeneratedGenerics { // extends TPHConstraint? */ public List transitiveSubtypeForClassTypes(List allConstraints, List classConstraints) { //TODO: + for(ClassConstraint cCons: classConstraints) { +// if(tphExtractor.containsConstraint(allConstraints, cCons)) { +// } + if(cCons.getRight() == ) + } + } /** @@ -148,12 +161,7 @@ public class FamilyOfGeneratedGenerics { // extends TPHConstraint? - - - - - - GeneratedGenericsFinder genGenFinder; +/* GeneratedGenericsFinder genGenFinder; ConstraintsSimplierResult simplifiedConstraints = null; GenericsGeneratorResultForClass ggResult = null; Method m; @@ -162,7 +170,7 @@ public class FamilyOfGeneratedGenerics { // extends TPHConstraint? genGenFinder.addMethodConstraints(simplifiedConstraints, ggResult, m); cs_m.add(); } - +*/ } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/MethodConstraint.java b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/MethodConstraint.java index 79e7edab..9b228258 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/MethodConstraint.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/gGenericsAli/MethodConstraint.java @@ -1,4 +1,9 @@ package de.dhbwstuttgart.bytecode.gGenericsAli; -public class MethodConstraint { +import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; + +public class MethodConstraint extends TPHConstraint { + public MethodConstraint(String left, String right, Relation rel) { + super(left, right, rel); + } } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java index d90718ce..dbda63b3 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GeneratedGenericsFinder.java @@ -9,6 +9,7 @@ import java.util.List; import java.util.Optional; import de.dhbwstuttgart.bytecode.TPHExtractor; +import de.dhbwstuttgart.bytecode.gGenericsAli.FamilyOfGeneratedGenerics; import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResultForClass; import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.MethodAndConstraints; @@ -74,6 +75,7 @@ public class GeneratedGenericsFinder implements ASTVisitor { private GenericGenratorResultForSourceFile generatedGenericsForSF;//Ergebnis des GGenerics private ResultSet resultSet; private final List methodNameAndParamsT = new ArrayList<>(); + private FamilyOfGeneratedGenerics fogg; private String pkgName; private JavaClassName className; @@ -133,6 +135,8 @@ public class GeneratedGenericsFinder implements ASTVisitor { classOrInterface.accept(tphExtractor); //PL 2020-10-16: Ab hier GGenerics implementieren durch Ali //Rueckgabe an generatedGenericsForSF + fogg = new FamilyOfGeneratedGenerics(tphExtractor); + tphsClass = tphExtractor.tphsClass; simplifiedConstraints = GenericsGenerator.simplifyConstraints(tphExtractor, tphsClass); diff --git a/src/test/java/insertGenerics/MethodsTest.java b/src/test/java/insertGenerics/MethodsTest.java new file mode 100644 index 00000000..91e67a1d --- /dev/null +++ b/src/test/java/insertGenerics/MethodsTest.java @@ -0,0 +1,30 @@ +package insertGenerics; + +import de.dhbwstuttgart.bytecode.genericsGenerator.GeneratedGenericsFinder; +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.typeinference.result.ResultSet; +import junit.framework.TestResult; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +public class MethodsTest { + public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/insertGenericsJav/"; + public static final String fileDirectory = rootDirectory + "TestGGFinder.jav"; + + @Test + public void testVisit(ClassOrInterface coi) throws IOException, ClassNotFoundException { + JavaTXCompiler compiler = new JavaTXCompiler(new File(fileDirectory)); + SourceFile sf = compiler.sourceFiles.get(compiler.sourceFiles.keySet()); + List results = compiler.typeInference(); + + GeneratedGenericsFinder ggf = new GeneratedGenericsFinder(sf, results); + ClassOrInterface coiHere = coi; + ggf.visit(coiHere); + //assert results.size()>0; + } +} diff --git a/src/test/java/insertGenerics/TryTest.java b/src/test/java/insertGenerics/TryTest.java new file mode 100644 index 00000000..d0b1ae2f --- /dev/null +++ b/src/test/java/insertGenerics/TryTest.java @@ -0,0 +1,92 @@ +package insertGenerics; + +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; +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 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.HashSet; +import java.util.List; +import java.util.Set; + +public class TryTest { + + public static final String rootDirectory = System.getProperty("user.dir")+"/src/test/resources/insertGenericsJav/"; + + @Test + public void ggFinder() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"TestGGFinder.jav")); + } + + private static class TestResultSet{ + + } + + public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException { + //filesToTest.add(new File(rootDirectory+"fc.jav")); + //filesToTest.add(new File(rootDirectory+"Lambda.jav")); + //filesToTest.add(new File(rootDirectory+"Lambda2.jav")); + //filesToTest.add(new File(rootDirectory+"Lambda3.jav")); + //filesToTest.add(new File(rootDirectory+"Vector.jav")); + //filesToTest.add(new File(rootDirectory+"Generics.jav")); + //filesToTest.add(new File(rootDirectory+"MethodsEasy.jav")); + //filesToTest.add(new File(rootDirectory+"Matrix.jav")); + //filesToTest.add(new File(rootDirectory+"Import.jav")); + // //filesToTest.add(new File(rootDirectory+"Faculty.jav")); + // //filesToTest.add(new File(rootDirectory+"mathStruc.jav")); + // //filesToTest.add(new File(rootDirectory+"test.jav")); + JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); + for(File f : compiler.sourceFiles.keySet()){ + SourceFile sf = compiler.sourceFiles.get(f); +// System.out.println(ASTTypePrinter.print(sf)); +// System.out.println("---------------------------1"); +// System.out.println(ASTPrinter.print(sf)); +// System.out.println("---------------------------2"); + } + 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("---------------------------3"); + System.out.println(ASTPrinter.print(sf)); +// System.out.println("---------------------------4"); + //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("---------------------------51"); + System.out.println(s); + System.out.println("---------------------------52"); + } + } + 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/TestGGFinder.jav b/src/test/resources/insertGenericsJav/TestGGFinder.jav new file mode 100644 index 00000000..d9309646 --- /dev/null +++ b/src/test/resources/insertGenericsJav/TestGGFinder.jav @@ -0,0 +1,17 @@ +class Generics{ + a; + + id(b) { + var c = b; + return c; + } + + setA(x) { + a = x; + return a; + } + + m(x,y) { + x = id(y); + } +} \ No newline at end of file