From 25fb7a2e289b0a9eb0f6609b3568bc6d0b5f161e Mon Sep 17 00:00:00 2001 From: Michael Uhl Date: Mon, 16 Sep 2019 11:52:34 +0200 Subject: [PATCH] Plugin: Additional generic insert points. --- .../GenericGenratorResultForSourceFile.java | 15 +++++++-------- .../GenericsGeneratorResultForClass.java | 8 ++++++-- .../typedeployment/TypeInsert.java | 6 ++++++ .../typedeployment/TypeInsertFactory.java | 18 ++++++++++-------- .../typedeployment/TypeInsertPlacer.java | 3 +-- .../typedeployment/TypeInsertPoint.java | 5 +++++ .../java/typeinference/JavaTXCompilerTest.java | 2 +- src/test/java/typeinference/Meth_GenTest.java | 2 +- src/test/java/typeinference/UnifyTest.java | 2 +- 9 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericGenratorResultForSourceFile.java b/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericGenratorResultForSourceFile.java index 76713ce0f..a36f6e07c 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericGenratorResultForSourceFile.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericGenratorResultForSourceFile.java @@ -38,14 +38,13 @@ public class GenericGenratorResultForSourceFile { } public GenericsGeneratorResultForClass getSimplifyResultsByName(String pkgName, String name) { - - if (this.pkgName.equals(pkgName)) { - return genericGeneratorResultForAllClasses.stream() - .filter(sr -> sr.getClassName().equals(name)) - .findAny() - .orElseThrow(() -> new NoSuchElementException( - "Simplify results for the class " + pkgName + "." + name + " are not found")); + for (int i = 0; i < genericGeneratorResultForAllClasses.size(); i++) { + GenericsGeneratorResultForClass genericsGeneratorResult = genericGeneratorResultForAllClasses.get(i); + if (genericsGeneratorResult.getClassName().equals(name)) { + return genericsGeneratorResult; + } } - throw new NoSuchElementException("Simplify results for the class " + pkgName + "." + name + " are not found"); + + return new GenericsGeneratorResultForClass(name); } } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericsGeneratorResultForClass.java b/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericsGeneratorResultForClass.java index a02cd1625..1e8db6889 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericsGeneratorResultForClass.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericsGeneratorResultForClass.java @@ -6,6 +6,8 @@ package de.dhbwstuttgart.bytecode.simplifyRes; import java.util.Collections; import java.util.List; +import com.google.common.base.Optional; + /** * @author fayez * @@ -60,8 +62,10 @@ public class GenericsGeneratorResultForClass { public List getMethodConstraintsByID(String id) { - return methodsAndTheirConstraints.getMethodsAndConstraints().stream().filter(mc -> mc.getMethodID().equals(id)) - .findFirst().get().getConstraints(); + java.util.Optional methodAndConstraints = methodsAndTheirConstraints.getMethodsAndConstraints().stream().filter(mc -> mc.getMethodID().equals(id)) + .findFirst(); + + return methodAndConstraints.isPresent() ? methodAndConstraints.get().getConstraints() : Collections.emptyList(); } } diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsert.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsert.java index d7e535522..d57cc03fd 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsert.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsert.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; +import de.dhbwstuttgart.syntaxtree.statement.This; import de.dhbwstuttgart.typeinference.result.ResultPair; public class TypeInsert { @@ -53,6 +54,11 @@ public class TypeInsert { } } + + public Set getAdditionalPoints() { + return this.inserts; + } + public String toString() { return point.toString(); } diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java index 7f039bdc6..188bb2f12 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java @@ -48,9 +48,11 @@ import javax.security.auth.kerberos.KerberosKey; */ public class TypeInsertFactory { + private static List newResults; - public static Set createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults){ - return new TypeInsertPlacer().getTypeInserts(forSourcefile, withResults); + public static Set createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults, List newResults){ + TypeInsertFactory.newResults = newResults; + return new TypeInsertPlacer().getTypeInserts(forSourcefile, withResults); } public static TypeInsert createInsertPoints(RefTypeOrTPHOrWildcardOrGeneric type, Token offset, ClassOrInterface cl, Method m, @@ -60,10 +62,10 @@ public class TypeInsertFactory { ResolvedType resolvedType = resultSet.resolveType(type); TypeInsertPoint insertPoint = new TypeInsertPoint(offset, new TypeToInsertString(resolvedType.resolvedType).insert); - List simplifyResults = JavaTXCompiler.INSTANCE.getGeneratedGenericResultsForAllSourceFiles(); + List simplifyResults = JavaTXCompiler.INSTANCE.getGeneratedGenericResultsForAllSourceFiles(newResults); for (GenericGenratorResultForSourceFile simplifyResultsEntries : simplifyResults) { GenericsGeneratorResultForClass genericResultsForClass = simplifyResultsEntries.getSimplifyResultsByName("", cl.getClassName().toString()); - return new TypeInsert(insertPoint, createGenericInsert(genericResultsForClass, cl, m, resultSet), resolvedType.getResultPair()); + return new TypeInsert(insertPoint, createGenericInsert(genericResultsForClass, cl, m, resultSet, offset), resolvedType.getResultPair()); } return new TypeInsert(insertPoint, new HashSet<>(), resolvedType.getResultPair()); @@ -73,21 +75,21 @@ public class TypeInsertFactory { } } - private static synchronized Set createGenericInsert(GenericsGeneratorResultForClass genericResult, ClassOrInterface cl, Method m, ResultSet resultSet){ + private static synchronized Set createGenericInsert(GenericsGeneratorResultForClass genericResult, ClassOrInterface cl, Method m, ResultSet resultSet, Token mOffset){ Set result = createGenericClassInserts(genericResult, cl); for (Method method : cl.getMethods()) { Resolver resolver = new Resolver(resultSet); List methodConstraints = genericResult.getMethodConstraintsByID(MethodUtility.createID(resolver, method)); - createMethodConstraints(method, methodConstraints); + result.addAll(createMethodConstraints(method, methodConstraints, mOffset)); } return result; } - private static Set createMethodConstraints(Method method, List constraints) { + private static Set createMethodConstraints(Method method, List constraints, Token mOffset) { Set result = new HashSet<>(); - Token offset = new GenericDeclarationList(method.getGenerics(), new NullToken()).getOffset(); + Token offset = mOffset; if (constraints.size() == 0) { result.add(new TypeInsertPoint(offset, "")); diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java index 9e5ba545a..e0627e5e8 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java @@ -1,13 +1,12 @@ package de.dhbwstuttgart.typedeployment; -import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.*; -import de.dhbwstuttgart.syntaxtree.statement.JavaInternalExpression; import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.result.ResultSet; import java.util.HashSet; +import java.util.List; import java.util.Set; public class TypeInsertPlacer extends AbstractASTWalker{ diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPoint.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPoint.java index bde45df12..9736ef701 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPoint.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPoint.java @@ -6,6 +6,7 @@ import de.dhbwstuttgart.typeinference.unify.model.ReferenceType; import java.time.OffsetDateTime; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; public class TypeInsertPoint { @@ -53,6 +54,10 @@ public class TypeInsertPoint { } } + public Set getAdditionalPoints() { + return this.getAdditionalPoints(); + } + public String toString() { return point.toString() + " " + insertString.toString(); } diff --git a/src/test/java/typeinference/JavaTXCompilerTest.java b/src/test/java/typeinference/JavaTXCompilerTest.java index 26f5db093..87ae477da 100644 --- a/src/test/java/typeinference/JavaTXCompilerTest.java +++ b/src/test/java/typeinference/JavaTXCompilerTest.java @@ -142,7 +142,7 @@ public class JavaTXCompilerTest { assert results.size()>0; Set insertedTypes = new HashSet<>(); for(ResultSet resultSet : results){ - Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet); + Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet, results); assert result.size()>0; String content = readFile(f.getPath(), StandardCharsets.UTF_8); for(TypeInsert tip : result){ diff --git a/src/test/java/typeinference/Meth_GenTest.java b/src/test/java/typeinference/Meth_GenTest.java index d879408c4..3846f15e2 100644 --- a/src/test/java/typeinference/Meth_GenTest.java +++ b/src/test/java/typeinference/Meth_GenTest.java @@ -114,7 +114,7 @@ public class Meth_GenTest { assert results.size()>0; Set insertedTypes = new HashSet<>(); for(ResultSet resultSet : results){ - Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet); + Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet, results); assert result.size()>0; String content = readFile(f.getPath(), StandardCharsets.UTF_8); for(TypeInsert tip : result){ diff --git a/src/test/java/typeinference/UnifyTest.java b/src/test/java/typeinference/UnifyTest.java index 38af248eb..41f7b06d5 100644 --- a/src/test/java/typeinference/UnifyTest.java +++ b/src/test/java/typeinference/UnifyTest.java @@ -140,7 +140,7 @@ public class UnifyTest { assert results.size()>0; Set insertedTypes = new HashSet<>(); for(ResultSet resultSet : results){ - Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet); + Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet, results); assert result.size()>0; String content = readFile(f.getPath(), StandardCharsets.UTF_8); for(TypeInsert tip : result){