From 7343ea1701c3313d5fe7543eff24bba6c3f93fa7 Mon Sep 17 00:00:00 2001 From: Michael Uhl Date: Wed, 13 Nov 2019 10:14:07 +0100 Subject: [PATCH] Vor Fertigstellung Ersetzung Generics. --- .../GenericGenratorResultForSourceFile.java | 5 ++-- .../GenericsGeneratorResultForClass.java | 19 ++++++++---- .../typedeployment/TypeInsert.java | 27 +++++++++++++---- .../typedeployment/TypeInsertFactory.java | 29 ++++++++++--------- .../typedeployment/TypeInsertPoint.java | 23 ++++++++++++++- 5 files changed, 74 insertions(+), 29 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGenratorResultForSourceFile.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGenratorResultForSourceFile.java index 85d0bcbf..90eaffa9 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGenratorResultForSourceFile.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGenratorResultForSourceFile.java @@ -43,9 +43,8 @@ public class GenericGenratorResultForSourceFile { return genericGeneratorResultForAllClasses.stream() .filter(sr -> sr.getClassName().equals(name)) .findAny() - .orElseThrow(() -> new NoSuchElementException( - "Simplify results for the class " + pkgName + "." + name + " are not found")); + .orElse(new GenericsGeneratorResultForClass(name)); } - 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/genericsGeneratorTypes/GenericsGeneratorResultForClass.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResultForClass.java index d6532ecb..75d1aaa7 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResultForClass.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResultForClass.java @@ -5,6 +5,9 @@ package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; import java.util.Collections; import java.util.List; +import java.util.Optional; + +import com.google.common.collect.Lists; /** * @author fayez @@ -56,12 +59,16 @@ public class GenericsGeneratorResultForClass { .anyMatch(i -> i.equals(id)); } - - - public List getMethodConstraintsByID(String id) { - return methodsAndTheirConstraints.getMethodsAndConstraints().stream().filter(mc -> mc.getMethodID().equals(id)) - .findFirst().get().getConstraints(); + Optional methodConstraints = methodsAndTheirConstraints.getMethodsAndConstraints() + .stream() + .filter(mc -> mc.getMethodID().equals(id)) + .findFirst(); + + if (methodConstraints.isPresent()) { + return methodConstraints.get().getConstraints(); + } else { + return Collections.emptyList(); + } } - } diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsert.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsert.java index d57cc03f..131c9f9b 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsert.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsert.java @@ -1,8 +1,13 @@ package de.dhbwstuttgart.typedeployment; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Set; +import java.util.TreeSet; + +import org.antlr.v4.parse.BlockSetTransformer.setAlt_return; import de.dhbwstuttgart.syntaxtree.statement.This; import de.dhbwstuttgart.typeinference.result.ResultPair; @@ -23,10 +28,15 @@ public class TypeInsert { public String insert(String intoSource){ List offsets = new ArrayList<>(); - String ret = point.insert(intoSource, offsets); - offsets.add(point); - for(TypeInsertPoint insertPoint : inserts){ - ret = insertPoint.insert(ret, offsets); + String ret = intoSource; + + List insertsSorted = new ArrayList<>(); + insertsSorted.add(point); + insertsSorted.addAll(inserts); + Collections.sort(insertsSorted, new TypeInsertPoint.TypeInsertPointPositionComparator().reversed()); + + for(TypeInsertPoint insertPoint : insertsSorted) { + ret = insertPoint.insert(ret, new ArrayList<>()); offsets.add(insertPoint); } return ret; @@ -56,7 +66,14 @@ public class TypeInsert { } public Set getAdditionalPoints() { - return this.inserts; + TypeInsertPoint.TypeInsertPointPositionComparator comparator = new TypeInsertPoint.TypeInsertPointPositionComparator(); + TreeSet result = new TreeSet<>(comparator.reversed()); + result.addAll(inserts); + return result; + } + + public Set getAdditionalPointsUnsorted() { + return inserts; } public String toString() { diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java index 44bfd2ff..d1a42342 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java @@ -44,7 +44,7 @@ public class TypeInsertFactory { try { ResolvedType resolvedType = resultSet.resolveType(type); TypeInsertPoint insertPoint = new TypeInsertPoint(offset, - new TypeToInsertString(resolvedType.resolvedType).insert); + new TypeToInsertString(resolvedType.resolvedType).insert, KindOfTypeInsertPoint.NORMAL_INSERT); List simplifyResults = JavaTXCompiler.INSTANCE.getGeneratedGenericResultsForAllSourceFiles(newResults); for (GenericGenratorResultForSourceFile simplifyResultsEntries : simplifyResults) { GenericsGeneratorResultForClass genericResultsForClass = simplifyResultsEntries.getSimplifyResultsByName("", cl.getClassName().toString()); @@ -60,22 +60,22 @@ public class TypeInsertFactory { 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)); - result.addAll(createMethodConstraints(method, methodConstraints, mOffset)); - } + + Resolver resolver = new Resolver(resultSet); + + if (m != null) { + List methodConstraints = genericResult.getMethodConstraintsByID(MethodUtility.createID(resolver, m)); + result.addAll(createMethodConstraints(methodConstraints, mOffset)); + } return result; } - private static Set createMethodConstraints(Method method, List constraints, Token mOffset) { + private static Set createMethodConstraints(List constraints, Token mOffset) { Set result = new HashSet<>(); Token offset = mOffset; if (constraints.size() == 0) { - result.add(new TypeInsertPoint(offset, "")); return result; } @@ -85,14 +85,15 @@ public class TypeInsertFactory { if (genericInsertConstraint.getConstraint().getRight().equals(Type.getInternalName(Object.class))) { insert += genericInsertConstraint.getConstraint().getLeft(); } else { - insert += genericInsertConstraint.getConstraint().getLeft() + " extends " + genericInsertConstraint.getConstraint().getRight() + ", "; + insert += genericInsertConstraint.getConstraint().getLeft() + " extends " + genericInsertConstraint.getConstraint().getRight(); } + insert += ", "; } insert = insert.substring(0, insert.length() -2); insert += ">"; - result.add(new TypeInsertPoint(offset, insert)); + result.add(new TypeInsertPoint(offset, insert, KindOfTypeInsertPoint.GENERERIC_METHOD_INSERT)); return result; } @@ -103,7 +104,6 @@ public class TypeInsertFactory { List classConstraints = genericResult.getClassConstraints(); if (classConstraints.size() == 0) { - result.add(new TypeInsertPoint(offset, "")); return result; } @@ -113,14 +113,15 @@ public class TypeInsertFactory { if (genericInsertConstraint.getConstraint().getRight().equals(Type.getInternalName(Object.class))) { insert += genericInsertConstraint.getConstraint().getLeft(); } else { - insert += genericInsertConstraint.getConstraint().getLeft() + " extends " + genericInsertConstraint.getConstraint().getRight() + ", "; + insert += genericInsertConstraint.getConstraint().getLeft() + " extends " + genericInsertConstraint.getConstraint().getRight(); } + insert += ", "; } insert = insert.substring(0, insert.length() -2); insert += ">"; - result.add(new TypeInsertPoint(offset, insert)); + result.add(new TypeInsertPoint(offset, insert, KindOfTypeInsertPoint.GENERIC_CLASS_INSERT)); return result; } diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPoint.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPoint.java index 9736ef70..0e42258b 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPoint.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPoint.java @@ -5,6 +5,7 @@ import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.typeinference.unify.model.ReferenceType; import java.time.OffsetDateTime; +import java.util.Comparator; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -13,11 +14,17 @@ public class TypeInsertPoint { public Token point; private String insertString; private int extraOffset = 0; + private KindOfTypeInsertPoint kind; - public TypeInsertPoint(Token point, String toInsert){ + public TypeInsertPoint(Token point, String toInsert, KindOfTypeInsertPoint kind){ this.point = point; + this.kind = kind; this.insertString = (toInsert.endsWith(" ")) ? toInsert : toInsert + " " ; } + + public boolean isGenericClassInsertPoint() { + return kind == KindOfTypeInsertPoint.GENERIC_CLASS_INSERT; + } public String insert(String intoSource, List additionalOffset){ int offset = additionalOffset.stream().filter((token -> @@ -61,4 +68,18 @@ public class TypeInsertPoint { public String toString() { return point.toString() + " " + insertString.toString(); } + + public static final class TypeInsertPointPositionComparator implements Comparator { + + @Override + public int compare(TypeInsertPoint o1, TypeInsertPoint o2) { + if (o1.getPositionInCode() > o2.getPositionInCode()) { + return 1; + } else if (o1.getPositionInCode() < o2.getPositionInCode()) { + return -1; + } + return 0; + } + + } }