diff --git a/PluginBau.docx b/PluginBau.docx new file mode 100644 index 00000000..6b387c71 Binary files /dev/null and b/PluginBau.docx differ diff --git a/pom.xml b/pom.xml index 98ea3933..603338b9 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ http://maven.apache.org/maven-v4_0_0.xsd"> JavaTXcompiler jar - 0.2 + 0.1 JavaTXcompiler http://maven.apache.org @@ -20,7 +20,7 @@ http://maven.apache.org/maven-v4_0_0.xsd"> org.antlr antlr4 - 4.7 + 4.8-1 commons-io @@ -63,6 +63,19 @@ http://maven.apache.org/maven-v4_0_0.xsd"> true + org.antlr antlr4-maven-plugin @@ -161,6 +174,8 @@ http://maven.apache.org/maven-v4_0_0.xsd"> + UTF-8 + UTF-8 1.8 1.8 0.23.0 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/bytecode/simplifyRes/GenericGenratorResultForSourceFile.java b/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericGenratorResultForSourceFile.java new file mode 100644 index 00000000..a36f6e07 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericGenratorResultForSourceFile.java @@ -0,0 +1,50 @@ +/** + * + */ +package de.dhbwstuttgart.bytecode.simplifyRes; + +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; + +/** + * The simplify results of a source file (package) + * + * @author fayez + * + */ +public class GenericGenratorResultForSourceFile { + private String pkgName; + private final List genericGeneratorResultForAllClasses = new ArrayList<>(); + + /** + * @param pkgName + */ + public GenericGenratorResultForSourceFile(String pkgName) { + this.pkgName = pkgName; + } + + public List getGenericGeneratorResultForAllClasses() { + return genericGeneratorResultForAllClasses; + } + + /** + * Appends the simplify results of a class to simplifyResForSF + * + * @param sResClass simplify results of a class to added + */ + public void addGenericGeneratorResultClass(GenericsGeneratorResultForClass sResClass) { + genericGeneratorResultForAllClasses.add(sResClass); + } + + public GenericsGeneratorResultForClass getSimplifyResultsByName(String pkgName, String name) { + for (int i = 0; i < genericGeneratorResultForAllClasses.size(); i++) { + GenericsGeneratorResultForClass genericsGeneratorResult = genericGeneratorResultForAllClasses.get(i); + if (genericsGeneratorResult.getClassName().equals(name)) { + return genericsGeneratorResult; + } + } + + 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 new file mode 100644 index 00000000..ff575b67 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericsGeneratorResultForClass.java @@ -0,0 +1,75 @@ +/** + * + */ +package de.dhbwstuttgart.bytecode.simplifyRes; + +import java.util.Collections; +import java.util.List; + +import com.google.common.base.Optional; + +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGeneratorResultsForAllMethods; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.MethodAndConstraints; + +/** + * @author fayez + * + */ +public class GenericsGeneratorResultForClass { + private final String className; + private final List classConstraints; + private final GenericGeneratorResultsForAllMethods methodsAndTheirConstraints; + + public GenericsGeneratorResultForClass(String className) { + this(className, Collections.emptyList(), new GenericGeneratorResultsForAllMethods()); + } + /** + * @param className + * @param classConstraints + * @param methodsAndTheirConstraints + */ + public GenericsGeneratorResultForClass(String className, List classConstraints, + GenericGeneratorResultsForAllMethods methodsAndTheirConstraints) { + this.className = className; + this.classConstraints = classConstraints; + this.methodsAndTheirConstraints = methodsAndTheirConstraints; + } + + /** + * @return the className + */ + public String getClassName() { + return className; + } + + /** + * @return the classConstraints + */ + public List getClassConstraints() { + return classConstraints; + } + + /** + * @return the methodsAndTheirConstraints + */ + public GenericGeneratorResultsForAllMethods getMethodsAndTheirConstraints() { + return methodsAndTheirConstraints; + } + + public boolean contains(String id) { + return methodsAndTheirConstraints.getMethodsAndConstraints().stream().map(mc -> mc.getMethodID()) + .anyMatch(i -> i.equals(id)); + } + + + + + public List getMethodConstraintsByID(String id) { + 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/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index d44ac77f..e1cd3063 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -48,7 +48,7 @@ import java.util.stream.Collectors; public class JavaTXCompiler { - public static JavaTXCompiler INSTANCE; + //public static JavaTXCompiler INSTANCE; final CompilationEnvironment environment; Boolean resultmodel = true; public final Map sourceFiles = new HashMap<>(); @@ -57,13 +57,13 @@ public class JavaTXCompiler { public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException { this(Arrays.asList(sourceFile)); - INSTANCE = this; + //INSTANCE = this; } public JavaTXCompiler(File sourceFile, Boolean log) throws IOException, ClassNotFoundException { this(sourceFile); this.log = log; - INSTANCE = this; + //INSTANCE = this; } public JavaTXCompiler(List sources) throws IOException, ClassNotFoundException { @@ -71,7 +71,7 @@ public class JavaTXCompiler { for (File s : sources) { sourceFiles.put(s, parse(s)); } - INSTANCE = this; + //INSTANCE = this; } public ConstraintSet getConstraints() throws ClassNotFoundException { @@ -714,10 +714,14 @@ public class JavaTXCompiler { return ret; } - public void generateBytecodForFile(String path, HashMap classFiles, SourceFile sf, - List typeinferenceResult) throws IOException { + public synchronized void generateBytecodForFile(String path, /* HashMap classFiles,*/ SourceFile sf, + List typeinferenceResult, List genericResultsRet) throws IOException { + //genericResultsRet is empty; fuer die Rückgabe try { + HashMap classFiles = new HashMap<>(); + List genericResults = getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); + genericResultsRet.addAll(genericResults);//fuer Rueckgabe BytecodeGen bytecodeGen = new BytecodeGen(classFiles,typeinferenceResult, genericResults, sf,path); bytecodeGen.visit(sf); this.writeClassFile(bytecodeGen.getClassFiles(), path); diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 140b3432..6f1300dc 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -359,7 +359,7 @@ public class SyntaxTreeGenerator{ && fieldDeclarationContext.unannTypeOrAuto().unannType() != null){ fieldType = TypeGenerator.convert(fieldDeclarationContext.unannTypeOrAuto().unannType(), reg, generics); }else{ - fieldType = TypePlaceholder.fresh(fieldDeclarationContext.getStart()); + fieldType = TypePlaceholder.fresh(fieldDeclarationContext.variableDeclaratorList().getStart()); //PL 2019-12-06: variableDeclaratorList() eingefuegt, um als Token nicht die Modifier zu bekommen } for(Java8Parser.VariableDeclaratorContext varCtx : fieldDeclarationContext.variableDeclaratorList().variableDeclarator()){ String fieldName = convert(varCtx.variableDeclaratorId()); diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java b/src/main/java/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java index 56b6e3fb..a0c420e8 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java @@ -6,7 +6,7 @@ import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; /** - * Eine Feldinitialisation steht für eine Felddeklaration mit gleichzeitiger Wertzuweisung + * Eine Feldinitialisation steht für eine Felddeklaration mit gleichzeitiger Wertzuweisung * Beispiel: 'public Feld FeldVar = FeldWert;' * @author janulrich * @@ -17,7 +17,7 @@ public class FieldDeclaration extends Field{ /** * Dieser Konstruktor der FieldDeclaration erstellt den Syntaxknoten vollständig. - * Kein nachträgliches hinzfügen von Informationen oder aufrufen von parserPostProcessing ist notwendig. + * Kein nachträgliches hinzfügen von Informationen oder aufrufen von parserPostProcessing ist notwendig. */ public FieldDeclaration(String name, RefTypeOrTPHOrWildcardOrGeneric typ, int modifier, Expression value, Token offset){ super(name, typ, modifier, offset);//Dieser Deklarator wird nicht vom Parser aufgerufen. Dadurch gibt es auch keinen Offset diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java b/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java index 99cd3f91..33722c09 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java @@ -63,7 +63,7 @@ public class Method extends SyntaxTreeNode implements IItemWithOffset, TypeScope @Override public Token getOffset() { - return null; + return super.getOffset(); } public String getName() { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java b/src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java index 6f0fa3ff..2edf6f22 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java @@ -36,7 +36,7 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric /** * @author Andreas Stadelmeier, a10023 * Ruft die TypePlaceholder.fresh()-Methode auf. - * Fügt zusätzlich einen Replacementlistener hinzu. + * Fügt zusätzlich einen Replacementlistener hinzu. * @return */ public static TypePlaceholder fresh(Token position){ diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/KindOfTypeInsertPoint.java b/src/main/java/de/dhbwstuttgart/typedeployment/KindOfTypeInsertPoint.java new file mode 100644 index 00000000..25b118e7 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/typedeployment/KindOfTypeInsertPoint.java @@ -0,0 +1,7 @@ +package de.dhbwstuttgart.typedeployment; + +public enum KindOfTypeInsertPoint { + NORMAL_INSERT, + GENERIC_CLASS_INSERT, + GENERERIC_METHOD_INSERT +} diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsert.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsert.java index d7e53552..131c9f9b 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsert.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsert.java @@ -1,9 +1,15 @@ 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; public class TypeInsert { @@ -22,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; @@ -53,6 +64,18 @@ public class TypeInsert { } } + + public Set getAdditionalPoints() { + 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() { return point.toString(); } diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java index 65e753be..4959585f 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java @@ -1,20 +1,35 @@ package de.dhbwstuttgart.typedeployment; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import org.antlr.v4.runtime.Token; +import org.objectweb.asm.Type; + import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult; import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResultForClass; import de.dhbwstuttgart.bytecode.utilities.MethodUtility; import de.dhbwstuttgart.bytecode.utilities.Resolver; import de.dhbwstuttgart.core.JavaTXCompiler; -import de.dhbwstuttgart.parser.NullToken; -import de.dhbwstuttgart.syntaxtree.*; -import de.dhbwstuttgart.syntaxtree.type.*; -import de.dhbwstuttgart.typeinference.result.*; - -import org.antlr.v4.runtime.Token; -import org.objectweb.asm.Type; - -import java.util.*; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; +import de.dhbwstuttgart.syntaxtree.type.GenericRefType; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.result.PairTPHEqualTPH; +import de.dhbwstuttgart.typeinference.result.PairTPHequalRefTypeOrWildcardType; +import de.dhbwstuttgart.typeinference.result.PairTPHsmallerTPH; +import de.dhbwstuttgart.typeinference.result.ResolvedType; +import de.dhbwstuttgart.typeinference.result.ResultSet; +import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; /** * TODO: @@ -31,49 +46,60 @@ import java.util.*; */ 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, List simplifyResultsForAllSourceFiles){ + TypeInsertFactory.newResults = newResults; + return new TypeInsertPlacer().getTypeInserts(forSourcefile, withResults, simplifyResultsForAllSourceFiles); } public static TypeInsert createInsertPoints(RefTypeOrTPHOrWildcardOrGeneric type, Token offset, ClassOrInterface cl, Method m, - ResultSet resultSet) { + ResultSet resultSet, List constraints, List classConstraints) { - try { + /* PL 2020-04-11 auskommentiert + * try { + */ ResolvedType resolvedType = resultSet.resolveType(type); TypeInsertPoint insertPoint = new TypeInsertPoint(offset, - new TypeToInsertString(resolvedType.resolvedType).insert); - List simplifyResults = JavaTXCompiler.INSTANCE.getGeneratedGenericResultsForAllSourceFiles(); + new TypeToInsertString(resolvedType.resolvedType, constraints, classConstraints).insert, KindOfTypeInsertPoint.NORMAL_INSERT); + /* PL 2020-04-11 auskommentiert + 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()); + */ + //GenericsGeneratorResultForClass genericResultsForClass = genericResult.getSimplifyResultsByName("", cl.getClassName().toString()); + return new TypeInsert(insertPoint, createGenericInsert(constraints, classConstraints, cl, m, resultSet, offset), resolvedType.getResultPair()); + + /* PL 2020-04-11 auskommentiert } catch (ClassNotFoundException e) { e.printStackTrace(); return null; } + */ } - private static synchronized Set createGenericInsert(GenericsGeneratorResultForClass genericResult, ClassOrInterface cl, Method m, ResultSet resultSet){ - 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); - } + private static synchronized Set createGenericInsert(List methodConstraints, List classConstraints,ClassOrInterface cl, Method m, ResultSet resultSet, Token mOffset){ + Set result = createGenericClassInserts(classConstraints, cl); + + Resolver resolver = new Resolver(resultSet); + + if (m != null) { + //List methodConstraints = genericResult.getMethodConstraintsByID(MethodUtility.createID(resolver, m)); + result.addAll(createMethodConstraints(methodConstraints, m.getOffset() != null ? m.getOffset() : mOffset)); + } return result; } - private static Set createMethodConstraints(Method method, List constraints) { + private static Set createMethodConstraints(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, "")); return result; } @@ -83,25 +109,25 @@ 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; } - private static Set createGenericClassInserts(GenericsGeneratorResultForClass genericResult, ClassOrInterface cl) { + private static Set createGenericClassInserts(List classConstraints, ClassOrInterface cl) { Set result = new HashSet<>(); Token offset = cl.getGenerics().getOffset(); - List classConstraints = genericResult.getClassConstraints(); + //List classConstraints = genericResult.getClassConstraints(); - if (classConstraints.size() == 0) { - result.add(new TypeInsertPoint(offset, "")); + if (classConstraints == null || classConstraints.size() == 0) { return result; } @@ -111,14 +137,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; } @@ -126,9 +153,14 @@ public class TypeInsertFactory { class TypeToInsertString implements ResultSetVisitor{ String insert = ""; + private List constraints; + private List classConstraints; + - TypeToInsertString(RefTypeOrTPHOrWildcardOrGeneric type){ - type.accept(this); + TypeToInsertString(RefTypeOrTPHOrWildcardOrGeneric type, List constraints, List classConstraints){ + this.constraints = constraints; + this.classConstraints = classConstraints; + type.accept(this); } @Override @@ -154,7 +186,7 @@ class TypeToInsertString implements ResultSetVisitor{ Iterator iterator = resolved.getParaList().iterator(); while(iterator.hasNext()){ RefTypeOrTPHOrWildcardOrGeneric typeParam = iterator.next(); - insert += new TypeToInsertString(typeParam).insert; + insert += new TypeToInsertString(typeParam, constraints, classConstraints).insert; if(iterator.hasNext())insert += ", "; } insert += ">"; @@ -168,16 +200,37 @@ class TypeToInsertString implements ResultSetVisitor{ @Override public void visit(SuperWildcardType superWildcardType) { - insert += "? super " + new TypeToInsertString(superWildcardType.getInnerType()).insert; + insert += "? super " + new TypeToInsertString(superWildcardType.getInnerType(), constraints, classConstraints).insert; } @Override public void visit(TypePlaceholder typePlaceholder) { - insert += typePlaceholder.getName(); + String realName = typePlaceholder.getName(); + //String realName = sig2.substring(1, sig2.length() - 1); + //String toVisit = realName+SPECIAL_CHAR; + //if(!genericsAndBounds.containsKey(toVisit)) {//PL 202-04-1 vielleicht braucht man das, vgl. Signature.java + Optional equalTPH = getEqualTPHFromClassConstraints(classConstraints, realName); + if(equalTPH.isPresent()){ + insert += equalTPH.get().getConstraint().getLeft();// + SPECIAL_CHAR; + } else { + insert += getEqualTPH(constraints, realName);// + SPECIAL_CHAR; + } } @Override public void visit(ExtendsWildcardType extendsWildcardType) { - insert += "? extends " + new TypeToInsertString(extendsWildcardType.getInnerType()).insert; + insert += "? extends " + new TypeToInsertString(extendsWildcardType.getInnerType(), constraints, classConstraints).insert; } + + private Optional getEqualTPHFromClassConstraints(List listOfConstraints, String tph) { + return listOfConstraints.stream() + .filter(c -> c.getConstraint().getLeft().equals(tph) || c.getEqualsTPHs().contains(tph)) + .findFirst(); + } + + private String getEqualTPH(List constraints2, String tph) { + return constraints2.stream() + .filter(c -> c.getConstraint().getLeft().equals(tph) || c.getEqualsTPHs().contains(tph)) + .findFirst().get().getConstraint().getLeft(); + } } \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java index 9e5ba545..ef65a445 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java @@ -1,59 +1,85 @@ package de.dhbwstuttgart.typedeployment; -import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResultForClass; +import de.dhbwstuttgart.bytecode.utilities.MethodUtility; +import de.dhbwstuttgart.bytecode.utilities.Resolver; 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.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; public class TypeInsertPlacer extends AbstractASTWalker{ Set inserts = new HashSet<>(); private ResultSet withResults; + String pkgName; + private List simplifyResultsForAllSourceFiles; - public Set getTypeInserts(SourceFile forSourceFile, ResultSet withResults){ + public Set getTypeInserts(SourceFile forSourceFile, ResultSet withResults, List simplifyResultsForAllSourceFiles){ this.withResults = withResults; + this.simplifyResultsForAllSourceFiles = simplifyResultsForAllSourceFiles; + pkgName = forSourceFile.getPkgName(); forSourceFile.accept(this); return inserts; } @Override public void visit(ClassOrInterface classOrInterface) { - TypeInsertPlacerClass cl = new TypeInsertPlacerClass(classOrInterface, withResults); + GenericsGeneratorResultForClass generatedGenerics = simplifyResultsForAllSourceFiles + .stream() + .map(sr->sr.getSimplifyResultsByName(pkgName, classOrInterface.getClassName().toString())) + .findFirst() + .get(); + TypeInsertPlacerClass cl = new TypeInsertPlacerClass(classOrInterface, withResults, generatedGenerics); this.inserts.addAll(cl.inserts); } } class TypeInsertPlacerClass extends AbstractASTWalker{ protected final ResultSet results; + private GenericsGeneratorResultForClass generatedGenerics; protected final ClassOrInterface cl; public final Set inserts = new HashSet<>(); private Method method; + + private Resolver resolver; + + List constraints; + List classConstraints; - TypeInsertPlacerClass(ClassOrInterface forClass, ResultSet withResults){ + TypeInsertPlacerClass(ClassOrInterface forClass, ResultSet withResults, GenericsGeneratorResultForClass generatedGenerics){ this.cl = forClass; this.method = null; this.results = withResults; + this.generatedGenerics = generatedGenerics; + resolver = new Resolver(withResults); //PL 2020-04-12 Ob das stimmt weiss ich nicht forClass.accept(this); } @Override public void visit(Method method) { this.method = method; + String id = MethodUtility.createID(resolver, method); + constraints = generatedGenerics.getMethodConstraintsByID(id); + classConstraints = generatedGenerics.getClassConstraints(); if(method.getReturnType() instanceof TypePlaceholder) inserts.add(TypeInsertFactory.createInsertPoints( - method.getReturnType(), method.getReturnType().getOffset(), cl, method, results)); + method.getReturnType(), method.getReturnType().getOffset(), cl, method, results, constraints, classConstraints)); super.visit(method); } @Override public void visit(Field field) { if(field.getType() instanceof TypePlaceholder){ + classConstraints = generatedGenerics.getClassConstraints(); inserts.add(TypeInsertFactory.createInsertPoints( - field.getType(), field.getType().getOffset(), cl, method, results)); + field.getType(), field.getType().getOffset(), cl, method, results, new ArrayList<>(), classConstraints)); } super.visit(field); } @@ -62,7 +88,7 @@ class TypeInsertPlacerClass extends AbstractASTWalker{ public void visit(FormalParameter param) { if(param.getType() instanceof TypePlaceholder) inserts.add(TypeInsertFactory.createInsertPoints( - param.getType(), param.getType().getOffset(), cl, method, results)); + param.getType(), param.getType().getOffset(), cl, method, results, constraints, classConstraints)); super.visit(param); } diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPoint.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPoint.java index bde45df1..0efb66e4 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPoint.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPoint.java @@ -1,29 +1,29 @@ package de.dhbwstuttgart.typedeployment; -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.stream.Collectors; +import java.util.Set; + +import org.antlr.v4.runtime.Token; 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 -> - //token.point.getLine() != point.getLine() && token.point.getCharPositionInLine() <= point.getCharPositionInLine())) - token.point.getStartIndex() <= point.getStartIndex())) - .mapToInt((typeInsertPoint -> typeInsertPoint.insertString.length())).sum(); - return new StringBuilder(intoSource).insert(point.getStartIndex()+offset+extraOffset, insertString).toString(); + return new StringBuilder(intoSource).insert(point.getStartIndex()+extraOffset, insertString).toString(); } public String getInsertString() { @@ -44,16 +44,50 @@ public class TypeInsertPoint { * @see java.lang.Object#equals(java.lang.Object) */ public boolean equals(Object obj) { + return this == obj; + /* if(!(obj instanceof TypeInsertPoint)) { return false; } else { - return ((TypeInsertPoint)obj).point.equals(this.point) && - ((TypeInsertPoint)obj).insertString.equals(this.insertString); - } + return + ((TypeInsertPoint)obj).getPositionInCode() == this.getPositionInCode() && + ((TypeInsertPoint)obj).insertString.equals(this.insertString); + } + */ + } + + public int hashCode() { + return getPositionInCode() * 11 * insertString.hashCode(); + } + + public Set getAdditionalPoints() { + return this.getAdditionalPoints(); } public String toString() { - return point.toString() + " " + insertString.toString(); + return point.getLine() + ":" + point.getCharPositionInLine() + ":" + insertString; + } + + public static final class TypeInsertPointPositionComparator implements Comparator { + + @Override + public int compare(TypeInsertPoint o1, TypeInsertPoint o2) { + if (o1.point == null && o2.point == null) { + return 0; + } else if (o2.point == null) { + return 1; + } else if (o1.point == null) { + return -1; + } + + if (o1.getPositionInCode() > o2.getPositionInCode()) { + return 1; + } else if (o1.getPositionInCode() < o2.getPositionInCode()) { + return -1; + } + return 0; + } + } } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 8e940d70..d3ebe2c6 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -186,6 +186,12 @@ public class TypeUnifyTask extends RecursiveTask>> { usedTasks.cancel(); writeLog(nOfUnify.toString() + "cancel"); System.out.println("cancel"); + try { + logFile.write("Abbruch"); + } + catch (IOException e) { + System.err.println("log-File nicht vorhanden"); + } } */ rules = new RuleSet(logFile); @@ -303,6 +309,11 @@ public class TypeUnifyTask extends RecursiveTask>> { /* * Step 1: Repeated application of reduce, adapt, erase, swap */ + synchronized (usedTasks) { + if (this.myIsCancelled()) { + return new HashSet<>(); + } + } rekTiefe++; nOfUnify++; @@ -463,7 +474,11 @@ public class TypeUnifyTask extends RecursiveTask>> { // .collect(Collectors.toCollection(HashSet::new)); //Muss auskommentiert werden, wenn computeCartesianRecursive ENDE - + synchronized (usedTasks) { + if (this.myIsCancelled()) { + return new HashSet<>(); + } + } Set> eqPrimePrimeSet = new HashSet<>(); @@ -907,6 +922,11 @@ public class TypeUnifyTask extends RecursiveTask>> { writeLog("wait "+ forkOrig.thNo); noOfThread--; res = forkOrig.join(); + synchronized (usedTasks) { + if (this.myIsCancelled()) { + return new HashSet<>(); + } + } //noOfThread++; forkOrig.writeLog("final Orig 1"); forkOrig.closeLogFile(); @@ -922,6 +942,11 @@ public class TypeUnifyTask extends RecursiveTask>> { synchronized (this) { noOfThread--; Set> fork_res = fork.join(); + synchronized (usedTasks) { + if (this.myIsCancelled()) { + return new HashSet<>(); + } + } //noOfThread++; writeLog("Join " + new Integer(fork.thNo).toString()); //noOfThread--; an das Ende von compute verschoben @@ -1013,6 +1038,11 @@ public class TypeUnifyTask extends RecursiveTask>> { writeLog("wait "+ forkOrig.thNo); noOfThread--; res = forkOrig.join(); + synchronized (usedTasks) { + if (this.myIsCancelled()) { + return new HashSet<>(); + } + } //noOfThread++; forkOrig.writeLog("final Orig -1"); forkOrig.closeLogFile(); @@ -1028,6 +1058,11 @@ public class TypeUnifyTask extends RecursiveTask>> { synchronized (this) { noOfThread--; Set> fork_res = fork.join(); + synchronized (usedTasks) { + if (this.myIsCancelled()) { + return new HashSet<>(); + } + } //noOfThread++; writeLog("Join " + new Integer(fork.thNo).toString()); //noOfThread--; an das Ende von compute verschoben @@ -1089,6 +1124,11 @@ public class TypeUnifyTask extends RecursiveTask>> { writeLog("wait "+ forkOrig.thNo); noOfThread--; res = forkOrig.join(); + synchronized (usedTasks) { + if (this.myIsCancelled()) { + return new HashSet<>(); + } + } //noOfThread++; forkOrig.writeLog("final Orig 2"); forkOrig.closeLogFile(); @@ -1103,6 +1143,11 @@ public class TypeUnifyTask extends RecursiveTask>> { synchronized (this) { noOfThread--; Set> fork_res = fork.join(); + synchronized (usedTasks) { + if (this.myIsCancelled()) { + return new HashSet<>(); + } + } //noOfThread++; writeLog("Join " + new Integer(fork.thNo).toString()); //noOfThread--; an das Ende von compute verschoben @@ -1224,7 +1269,7 @@ public class TypeUnifyTask extends RecursiveTask>> { if (!isUndefinedPairSetSet(par_res) && isUndefinedPairSetSet(result)) { //wenn korrektes Ergebnis gefunden alle Fehlerfaelle loeschen result = par_res; - if (par_res.iterator().next() instanceof WildcardType) { + if (!par_res.isEmpty() && par_res.iterator().next() instanceof WildcardType) { System.out.println(""); } } diff --git a/src/test/java/typeinference/JavaTXCompilerTest.java b/src/test/java/typeinference/JavaTXCompilerTest.java index 26f5db09..91446744 100644 --- a/src/test/java/typeinference/JavaTXCompilerTest.java +++ b/src/test/java/typeinference/JavaTXCompilerTest.java @@ -1,5 +1,6 @@ package typeinference; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; @@ -133,7 +134,8 @@ public class JavaTXCompilerTest { System.out.println(ASTPrinter.print(sf)); } 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)); @@ -142,7 +144,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, simplifyResultsForAllSourceFiles); 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 d879408c..1fa39d9a 100644 --- a/src/test/java/typeinference/Meth_GenTest.java +++ b/src/test/java/typeinference/Meth_GenTest.java @@ -1,5 +1,6 @@ package typeinference; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; @@ -105,6 +106,7 @@ public class Meth_GenTest { System.out.println(ASTPrinter.print(sf)); } List results = compiler.typeInference(); + List simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results); for(File f : compiler.sourceFiles.keySet()){ SourceFile sf = compiler.sourceFiles.get(f); @@ -114,7 +116,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, simplifyResultsForAllSourceFiles); 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 38af248e..5a0492b5 100644 --- a/src/test/java/typeinference/UnifyTest.java +++ b/src/test/java/typeinference/UnifyTest.java @@ -1,5 +1,6 @@ package typeinference; +import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; @@ -131,6 +132,7 @@ public class UnifyTest { System.out.println(ASTPrinter.print(sf)); } List results = compiler.typeInference(); + List simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(results); for(File f : compiler.sourceFiles.keySet()){ SourceFile sf = compiler.sourceFiles.get(f); @@ -140,7 +142,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, simplifyResultsForAllSourceFiles); assert result.size()>0; String content = readFile(f.getPath(), StandardCharsets.UTF_8); for(TypeInsert tip : result){ diff --git a/src/test/resources/bytecode/javFiles/Fac.jav b/src/test/resources/bytecode/javFiles/Fac.jav index 28650b45..bde593d4 100644 --- a/src/test/resources/bytecode/javFiles/Fac.jav +++ b/src/test/resources/bytecode/javFiles/Fac.jav @@ -3,7 +3,7 @@ import java.lang.Integer; public class Fac { - getFac(n){ + getFac(java.lang.Integer n){ var res = 1; var i = 1; while(i<=n) { diff --git a/src/test/resources/bytecode/javFiles/Field.jav b/src/test/resources/bytecode/javFiles/Field.jav index b19b2308..0fd1133f 100644 --- a/src/test/resources/bytecode/javFiles/Field.jav +++ b/src/test/resources/bytecode/javFiles/Field.jav @@ -1,7 +1,7 @@ import java.lang.Integer; public class Field { - public Integer x = 5; + public x = 5; m(){ return x; diff --git a/src/test/resources/javFiles/IfTest.jav b/src/test/resources/javFiles/IfTest.jav index 1e1141df..88885561 100644 --- a/src/test/resources/javFiles/IfTest.jav +++ b/src/test/resources/javFiles/IfTest.jav @@ -2,7 +2,7 @@ import java.lang.Integer; import java.lang.Boolean; import java.lang.Object; -public class IfTest{ +public class IfTest { Object m1(b) { Integer i; if(b) { diff --git a/src/test/resources/javFiles/ListenerOverload.jav b/src/test/resources/javFiles/ListenerOverload.jav index e1a8d65d..00a5e605 100644 --- a/src/test/resources/javFiles/ListenerOverload.jav +++ b/src/test/resources/javFiles/ListenerOverload.jav @@ -14,7 +14,7 @@ call(String s){} } -class Pair{ - A left; - B right; +class Pair{ + left; + right; } \ No newline at end of file diff --git a/src/test/resources/javFiles/fc.jav b/src/test/resources/javFiles/fc.jav index a3278cbc..b387cb91 100644 --- a/src/test/resources/javFiles/fc.jav +++ b/src/test/resources/javFiles/fc.jav @@ -8,7 +8,7 @@ class Test{ } interface Klasse1{ - Klasse1 meth(List p); + Klasse1 meth(p); Klasse1 meth(Klasse2 p); }