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.BeforeClass; 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 TestAny { public static final String rootDirectory = System.getProperty("user.dir")+"/resources/insertGenericsJav/"; @BeforeClass public static void resetNamesOfTypePlaceholder() { de.dhbwstuttgart.syntaxtree.factory.NameGenerator.reset(); } @Test public void ggFinder() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"TestAny.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<>(); classConstraintsTest.add(new ClassConstraint("N", "O", Relation.EXTENDS)); classConstraintsTest.add(new ClassConstraint("N", "U", Relation.EXTENDS)); classConstraintsTest.add(new ClassConstraint("U", "O", Relation.EXTENDS)); classConstraintsTest.add(new ClassConstraint("O", "java/lang/Object", Relation.EXTENDS)); HashMap> methodConstraintsWithPositionTest = new HashMap<>(); Set lmc; lmc = new HashSet<>(); lmc.add(new MethodConstraint("R", "java/lang/Object", Relation.EXTENDS)); methodConstraintsWithPositionTest.put("TPH RanyMethod()", lmc); lmc = new HashSet<>(); methodConstraintsWithPositionTest.put("TPH UotherMethod(TPH U)", 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); } }