From 8706882388bbd6140c54c28cf1fe4168c58a88d3 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Fri, 29 Jan 2021 10:55:04 +0100 Subject: [PATCH] modified: src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java new file: src/test/java/insertGenerics/TestGGFinder.java --- .../dhbwstuttgart/bytecode/TPHExtractor.java | 26 ++++--- .../java/insertGenerics/TestGGFinder.java | 72 +++++++++++++++++++ 2 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 src/test/java/insertGenerics/TestGGFinder.java diff --git a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java index de5e29c1..9b895199 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java @@ -78,15 +78,25 @@ public class TPHExtractor extends AbstractASTWalker { public void visit(TypePlaceholder tph) { if (resultSet.resolveType(tph).resolvedType instanceof TypePlaceholder) { TypePlaceholder resolvedTPH = (TypePlaceholder) resultSet.resolveType(tph).resolvedType; - if (inMethod && !tphsClass.contains(resolvedTPH.getName())) { - methodAndTph.getTphs().add(resolvedTPH.getName()); - if (inLocalOrParamOrReturn) - methodAndTph.getLocalTphs().add(resolvedTPH.getName()); - }else { - tphsClass.add(resolvedTPH.getName()); + String tphName = resolvedTPH.getName(); + if (inMethod && !tphsClass.contains(tphName)) { + ArrayList tphs = null; + if (!(tphs = methodAndTph.getTphs()).contains(tphName)) { + tphs.add(tphName); + } + if (inLocalOrParamOrReturn) { + if (!(tphs = methodAndTph.getLocalTphs()).contains(tphName)) { + tphs.add(tphName); + } + } + } else { + if (!tphsClass.contains(tphName)) { + tphsClass.add(tphName); + } + } + if (!allTPHS.containsKey(tphName)) { + allTPHS.put(tphName, inMethod); } - - allTPHS.put(resolvedTPH.getName(), inMethod); // final List cons = new ArrayList<>(); // resultSet.resolveType(tph).additionalGenerics.forEach(ag -> { // TPHConstraint con = new ExtendsConstraint(ag.TA1.getName(), ag.TA2.getName(), Relation.EXTENDS); diff --git a/src/test/java/insertGenerics/TestGGFinder.java b/src/test/java/insertGenerics/TestGGFinder.java new file mode 100644 index 00000000..c517136c --- /dev/null +++ b/src/test/java/insertGenerics/TestGGFinder.java @@ -0,0 +1,72 @@ +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 TestGGFinder { + + 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 { + 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); + //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); + } +}