diff --git a/doc/PluginBau.pdf b/doc/PluginBau.pdf deleted file mode 100644 index f6bab5cc..00000000 Binary files a/doc/PluginBau.pdf and /dev/null differ diff --git a/doc/pluginBau.md b/doc/pluginBau.md new file mode 100644 index 00000000..d01c72c9 --- /dev/null +++ b/doc/pluginBau.md @@ -0,0 +1,24 @@ +## Bau und installation des Eclipse plugins + +1. Zunächst muss der JavaTX Compiler gebaut werden. Dafür wird im Rootverzeichnis des JavaTX Compilers +`mvn install` ausgeführt. Falls die Tests nicht laufen kann das mit `-DskipTests` beseitigt werden. + +2. Dann muss das Plugin gebaut werden. Dafür muss ebenfalls `mvn install` ausgeführt werden, im Rootverzeichnis des Plugins. +Falls es dabei zu Fehlern mit der Java-Version kommen sollte muss man in ~\.mavenrc oder \~.mavenrc_pre.bat die Zeile +`set JAVA_HOME=` (Windows) oder `JAVA_HOME=""` (Linux) einfügen. Damit sollte maven korrekt konfiguriert sein. + +3. Als letzter Schritt muss nun das Plugin in Eclipse geladen werden. Wichtig dabei ist, dass Eclipse selber mit Java 19 startet. +Dafür kann die Datei `eclipse.ini` angepasst werden die im Installationsverzeichnis von Eclipse vorhanden ist. Hier müssen zwei Zeilen +eingetragen werden: +``` +-vm +/javaw.exe +``` +Ganz am Anfang der Datei +und unter -vmargs die Option `--enable-preview` + +4. Das gebaute Plugin befindet sich unter `JavaCompilerPlugin\releng\JavaCompilerPlugin.Update\target` +Um dieses Plugin in Eclipse laden zu können muss den Dialog `Help > Install new Software` öffnen. Oben rechts steht die Option `Add...` +Im sich öffnenden Dialog muss man `Archive` anklicken und dort das Zip Archiv aus dem Oben genannten Pfad einfügen. Nun mit Add den Dialog schließen. +Als letztes muss in der Liste oben der Eintrag `JavaCompilerPlugin.Update` ausgewählt werden. Hier sollte nun unten das Plugin angezeigt werden. +Einfach auswählen und unten Next drücken. Damit sollte der Vorgang abgeschlossen sein. \ No newline at end of file diff --git a/pom.xml b/pom.xml index 63e8985a..8fe3f636 100644 --- a/pom.xml +++ b/pom.xml @@ -54,8 +54,8 @@ http://maven.apache.org/maven-v4_0_0.xsd"> 3.8.0 --enable-preview - 20 - 20 + 19 + 19 @@ -112,8 +112,8 @@ http://maven.apache.org/maven-v4_0_0.xsd"> - 20 - 20 + 19 + 19 de.dhbwstuttgart.core.ConsoleInterface diff --git a/src/main/java/de/dhbwstuttgart/bytecode/constraint/EqualConstraint.java b/src/main/java/de/dhbwstuttgart/bytecode/constraint/EqualConstraint.java deleted file mode 100644 index 69fb0b02..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/constraint/EqualConstraint.java +++ /dev/null @@ -1,9 +0,0 @@ -package de.dhbwstuttgart.bytecode.constraint; - -public class EqualConstraint extends TPHConstraint { - - public EqualConstraint(String left, String right) { - super(left, right, Relation.EQUAL); - } - -} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/constraint/ExtendsConstraint.java b/src/main/java/de/dhbwstuttgart/bytecode/constraint/ExtendsConstraint.java deleted file mode 100644 index 77007a3b..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/constraint/ExtendsConstraint.java +++ /dev/null @@ -1,9 +0,0 @@ -package de.dhbwstuttgart.bytecode.constraint; - -public class ExtendsConstraint extends TPHConstraint { - - public ExtendsConstraint(String left, String right) { - super(left, right, Relation.EXTENDS); - } - -} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/constraint/TPHConstraint.java b/src/main/java/de/dhbwstuttgart/bytecode/constraint/TPHConstraint.java deleted file mode 100644 index 0533a028..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/constraint/TPHConstraint.java +++ /dev/null @@ -1,90 +0,0 @@ -package de.dhbwstuttgart.bytecode.constraint; - -import de.dhbwstuttgart.typeinference.constraints.Pair; -import de.dhbwstuttgart.typeinference.unify.model.PairOperator; -import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; -import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; - -public class TPHConstraint { - protected String left; - protected String right; - protected Relation rel; - protected int variance; //noch nicht benutzt - public enum Relation{ - EXTENDS, EQUAL - } - - public TPHConstraint(Pair p) { - this.left = ((TypePlaceholder)p.TA1).getName(); - this.right = ((TypePlaceholder)p.TA2).getName(); - this.rel = p.GetOperator().equals(PairOperator.SMALLERDOT) ? Relation.EXTENDS : Relation.EQUAL; - } - - public TPHConstraint(String left, String right, Relation rel) { - this.left = left; - this.right = right; - this.rel = rel; - } - - public String getLeft() { - return left; - } - - - public String getRight() { - return right; - } - - - public Relation getRel() { - return rel; - } - - public void setLeft(String left) { - this.left = left; - } - - public void setRight(String right) { - this.right = right; - } - - public void setRel(Relation rel) { - this.rel = rel; - } - - public boolean containTPH(String tph) { - return left.equals(tph)||right.equals(tph); - } - - public boolean equalConstraint(TPHConstraint constraint) { - return rel == constraint.getRel() && left.equals(constraint.getLeft()) && right.equals(constraint.getRight()); - } - - @Override - public int hashCode() { - return (left+right).hashCode(); - } - - @Override - public boolean equals (Object o) { - if (o instanceof TPHConstraint) { - TPHConstraint o_tphcons = (TPHConstraint)o; - return (this.left.equals(o_tphcons.getLeft()) - && this.right.equals(o_tphcons.getRight()) - && this.rel.equals(o_tphcons.rel)); - } - else { - return false; - } - } - - - @Override - public String toString() { - if(rel == Relation.EXTENDS) { - return left + " < " + right; - }else { - return left + " = " + right; - } - } -} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/ConstraintsWithSameLeftSide.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/ConstraintsWithSameLeftSide.java deleted file mode 100644 index 1b184048..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/ConstraintsWithSameLeftSide.java +++ /dev/null @@ -1,35 +0,0 @@ -package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; - -import java.util.List; - -import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; - -public class ConstraintsWithSameLeftSide { - private List constraints; - - /** - * @param constraints - */ - public ConstraintsWithSameLeftSide(List constraints) { - this.constraints = constraints; - } - - /** - * @return the constraints - */ - public List getConstraints() { - return constraints; - } - - /** - * @param constraints the constraints to set - */ - public void setConstraints(List constraints) { - this.constraints = constraints; - } - - @Override - public String toString() { - return "[" + constraints.toString() + "]"; - } -} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGeneratorResultsForAllMethods.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGeneratorResultsForAllMethods.java deleted file mode 100644 index 091587ff..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGeneratorResultsForAllMethods.java +++ /dev/null @@ -1,41 +0,0 @@ -/** - * - */ -package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; - -import java.util.Collections; -import java.util.List; - -/** - * @author fayez - * - */ -public class GenericGeneratorResultsForAllMethods { - private final List methodsAndConstraints; - - public GenericGeneratorResultsForAllMethods() { - this(Collections.emptyList()); - } - /** - * @param methodsAndConstraints - */ - public GenericGeneratorResultsForAllMethods(List methodsAndConstraints) { - this.methodsAndConstraints = methodsAndConstraints; - } - - /** - * @return the methodsAndConstraints - */ - public List getMethodsAndConstraints() { - return methodsAndConstraints; - } - - @Override - public String toString() { - String ret = ""; - ret = ret + methodsAndConstraints.stream().reduce("", (x,y) -> x + y.toString(), (x,y) -> x + y); - //ret = ret + "\n"; - return ret; - } - -} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGenratorResultForSourceFile.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGenratorResultForSourceFile.java deleted file mode 100644 index 3bcf1939..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericGenratorResultForSourceFile.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * - */ -package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; - -import de.dhbwstuttgart.parser.scope.JavaClassName; - -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(JavaClassName name) { - - if (this.pkgName.equals(name.getPackageName())) { - return genericGeneratorResultForAllClasses.stream() - .filter(sr -> sr.getClassName().equals(name)) - .findAny() - .orElse(new GenericsGeneratorResultForClass(name)); - } - return new GenericsGeneratorResultForClass(name); - } -} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResult.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResult.java deleted file mode 100644 index fe23f239..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResult.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * - */ -package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; - -import java.util.Set; - -import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; - -/** - * @author fayez - * - */ -public class GenericsGeneratorResult { - private TPHConstraint constraint; - /** - * contains the names of all type placeholders which are equals to the left side of - * the constraint {@link #constraint}. - */ - private Set equalsTPHs; - /** - * @param constraint - * @param equalsTPHs - */ - public GenericsGeneratorResult(TPHConstraint constraint, Set equalsTPHs) { - this.constraint = constraint; - this.equalsTPHs = equalsTPHs; - } - - /** - * @return the constraint - */ - public TPHConstraint getConstraint() { - return constraint; - } - /** - * @param constraint the constraint to set - */ - public void setConstraint(TPHConstraint constraint) { - this.constraint = constraint; - } - /** - * @return the equalsTPHs - */ - public Set getEqualsTPHs() { - return equalsTPHs; - } - /** - * @param equalsTPHs the equalsTPHs to set - */ - public void setEqualsTPHs(Set equalsTPHs) { - this.equalsTPHs = equalsTPHs; - } - - @Override - public String toString() { - return constraint.toString() + " EqualsTPS: " + equalsTPHs.toString(); - } -} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResultForClass.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResultForClass.java deleted file mode 100644 index 0a3626d2..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/GenericsGeneratorResultForClass.java +++ /dev/null @@ -1,84 +0,0 @@ -/** - * - */ -package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; - -import de.dhbwstuttgart.parser.scope.JavaClassName; - -import java.util.Collections; -import java.util.List; -import java.util.Optional; - -import com.google.common.collect.Lists; - -/** - * @author fayez - * - */ -public class GenericsGeneratorResultForClass { - private final JavaClassName className; - private final List classConstraints; - private final GenericGeneratorResultsForAllMethods methodsAndTheirConstraints; - - public GenericsGeneratorResultForClass(JavaClassName className) { - this(className, Collections.emptyList(), new GenericGeneratorResultsForAllMethods()); - } - /** - * @param className - * @param classConstraints - * @param methodsAndTheirConstraints - */ - public GenericsGeneratorResultForClass(JavaClassName className, List classConstraints, - GenericGeneratorResultsForAllMethods methodsAndTheirConstraints) { - this.className = className; - this.classConstraints = classConstraints; - this.methodsAndTheirConstraints = methodsAndTheirConstraints; - } - - /** - * @return the className - */ - public JavaClassName 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) { - Optional methodConstraints = methodsAndTheirConstraints.getMethodsAndConstraints() - .stream() - .filter(mc -> mc.getMethodID().equals(id)) - .findFirst(); - - if (methodConstraints.isPresent()) { - return methodConstraints.get().getConstraints(); - } else { - return Collections.emptyList(); - } - } - - @Override - public String toString() { - String ret = "Classconstraints: "; - ret = ret + classConstraints.stream().reduce("", (x,y) -> x + y.toString(), (x,y) -> x + y); - ret = ret + "\n" + methodsAndTheirConstraints.toString(); - return ret; - } -} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/MethodAndConstraints.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/MethodAndConstraints.java deleted file mode 100644 index b9437c3a..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/MethodAndConstraints.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * - */ -package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; - -import java.util.List; - -/** - * @author fayez - * - */ -public class MethodAndConstraints { - private final String methodID; - private final List constraints; - /** - * @param methodID - * @param constraints - */ - public MethodAndConstraints(String methodID, List constraints) { - this.methodID = methodID; - this.constraints = constraints; - } - /** - * @return the methodID - */ - public String getMethodID() { - // FIXME - return null; - } - /** - * @return the constraints - */ - public List getConstraints() { - return constraints; - } - - - @Override - public String toString() { - String ret = methodID + ": "; - ret = ret + constraints.stream().reduce("", - (x,y) -> x + y.toString(), - (x,y) -> x + y); - ret = ret + "\n"; - return ret; - } -} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/NameReplacementResult.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/NameReplacementResult.java deleted file mode 100644 index 4a91655e..00000000 --- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGeneratorTypes/NameReplacementResult.java +++ /dev/null @@ -1,49 +0,0 @@ -/** - * - */ -package de.dhbwstuttgart.bytecode.genericsGeneratorTypes; - -import java.util.List; - -/** - * @author fayez - * - */ -public class NameReplacementResult { - private String name; - private List oldNames; - /** - * @param name - * @param oldNames - */ - public NameReplacementResult(String name, List oldNames) { - this.name = name; - this.oldNames = oldNames; - } - /** - * @return the name - */ - public String getName() { - return name; - } - /** - * @param name the name to set - */ - public void setName(String name) { - this.name = name; - } - /** - * @return the oldNames - */ - public List getOldNames() { - return oldNames; - } - /** - * @param oldNames the oldNames to set - */ - public void setOldNames(List oldNames) { - this.oldNames = oldNames; - }; - - -} diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index a0eab274..33cb5b29 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -3,7 +3,6 @@ package de.dhbwstuttgart.core; import com.google.common.collect.Lists; import de.dhbwstuttgart.bytecode.Codegen; -import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.environment.CompilationEnvironment; import de.dhbwstuttgart.environment.DirectoryClassLoader; import de.dhbwstuttgart.exceptions.DebugException; @@ -857,10 +856,9 @@ public class JavaTXCompiler { generateBytecode(path, typeinferenceResult); } - private Map> generatedGenerics = new HashMap<>(); + private Map> generatedGenerics = new HashMap<>(); - // TODO This is a temporary solution, we should integrate with the old API for getting Generics - public Map> getGeneratedGenerics() { + public Map> getGeneratedGenerics() { return generatedGenerics; } @@ -880,22 +878,27 @@ public class JavaTXCompiler { path = new File(outputPath ,sf.getPkgName().replace(".","/")); //add package path to root path } - var converter = new ASTToTargetAST(typeinferenceResult, sf, classLoader); - var generatedClasses = new HashMap(); - for (var clazz : sf.getClasses()) { - var codegen = new Codegen(converter.convert(clazz)); - var code = codegen.generate(); - generatedClasses.put(clazz.getClassName(), code); - converter.auxiliaries.forEach((name, source) -> { - generatedClasses.put(new JavaClassName(name), source); - }); - } - generatedGenerics.put(f, converter.javaGenerics()); + var generatedClasses = generateBytecode(sf, typeinferenceResult); writeClassFile(generatedClasses, path); } } - private void writeClassFile(HashMap classFiles, File path) throws IOException { + public Map generateBytecode(SourceFile sf, List typeInferenceResult) { + var converter = new ASTToTargetAST(typeInferenceResult, sf, classLoader); + var generatedClasses = new HashMap(); + for (var clazz : sf.getClasses()) { + var codegen = new Codegen(converter.convert(clazz)); + var code = codegen.generate(); + generatedClasses.put(clazz.getClassName(), code); + converter.auxiliaries.forEach((name, source) -> { + generatedClasses.put(new JavaClassName(name), source); + }); + } + generatedGenerics.put(sf, converter.javaGenerics()); + return generatedClasses; + } + + private void writeClassFile(Map classFiles, File path) throws IOException { FileOutputStream output; for (JavaClassName name : classFiles.keySet()) { byte[] bytecode = classFiles.get(name); @@ -911,11 +914,6 @@ public class JavaTXCompiler { } } - public List getGeneratedGenericResultsForAllSourceFiles(List results) { - // FIXME - return null; - } - /* PL 2020-03-17 mit TypeExchanger in FCGenerator.java zusammenfuehren */ /** * Tauscht die GTVs in einem Typ gegen die entsprechenden Typen in der übergebenen Map aus. diff --git a/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java b/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java index ae16f6c3..e06bca66 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/GenericsResult.java @@ -4,28 +4,27 @@ import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.target.tree.type.TargetType; import de.dhbwstuttgart.typeinference.result.ResultPair; import java.util.*; public class GenericsResult { - final ASTToTargetAST.GenerateGenerics generics; + private final ASTToTargetAST.GenerateGenerics generics; GenericsResult(ASTToTargetAST.GenerateGenerics generics) { this.generics = generics; } - public Set> get(ClassOrInterface clazz) { + public GenericsResultSet get(ClassOrInterface clazz) { var generics = this.generics.computedGenericsOfClasses.get(clazz); - if (generics == null) return Set.of(); - return generics; + return new GenericsResultSet(generics, this.generics.equality); } // TODO Compute generics if not present? - public Set> get(Method method) { + public GenericsResultSet get(Method method) { var generics = this.generics.computedGenericsOfMethods.get(method); - if (generics == null) return Set.of(); - return generics; + return new GenericsResultSet(generics, this.generics.equality); } public BoundsList getBounds(RefTypeOrTPHOrWildcardOrGeneric type, ClassOrInterface clazz) { @@ -70,4 +69,8 @@ public class GenericsResult { return this.generics.getType(tph); return type; } + + public TargetType resolveTarget(RefTypeOrTPHOrWildcardOrGeneric type) { + return this.generics.getTargetType(type); + } } diff --git a/src/main/java/de/dhbwstuttgart/target/generate/GenericsResultSet.java b/src/main/java/de/dhbwstuttgart/target/generate/GenericsResultSet.java new file mode 100644 index 00000000..bc488da0 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/generate/GenericsResultSet.java @@ -0,0 +1,33 @@ +package de.dhbwstuttgart.target.generate; + +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.result.PairTPHEqualTPH; +import de.dhbwstuttgart.typeinference.result.ResultPair; + +import java.util.*; + +public class GenericsResultSet extends AbstractSet> { + + final Set> backing; + final Map equality; + + public GenericsResultSet(Set> backing, Map equality) { + this.backing = backing == null ? new HashSet<>() : new HashSet<>(backing); + this.equality = equality; + } + + @Override + public Iterator> iterator() { + return backing.iterator(); + } + + @Override + public int size() { + return backing.size(); + } + + public Optional> getResultPairFor(TypePlaceholder tph) { + var tph2 = equality.getOrDefault(tph, tph); + return this.stream().filter(pair -> pair.getLeft().equals(tph2)).findFirst(); + } +} diff --git a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java index c185018f..3cae417f 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java @@ -2,15 +2,13 @@ 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 de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; +import de.dhbwstuttgart.target.generate.GenericsResult; +import de.dhbwstuttgart.target.generate.GenericsResultSet; +import de.dhbwstuttgart.typeinference.result.*; import org.antlr.v4.runtime.Token; -import org.objectweb.asm.Type; -import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.SourceFile; @@ -20,12 +18,6 @@ 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: @@ -42,16 +34,12 @@ import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; */ public class TypeInsertFactory { - private static List newResults; - - - public static Set createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults, List newResults, List simplifyResultsForAllSourceFiles){ - TypeInsertFactory.newResults = newResults; - return new TypeInsertPlacer().getTypeInserts(forSourcefile, withResults, simplifyResultsForAllSourceFiles); + public static Set createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults, GenericsResult generics) { + return new TypeInsertPlacer().getTypeInserts(forSourcefile, withResults, generics); } public static TypeInsert createInsertPoints(RefTypeOrTPHOrWildcardOrGeneric type, Token offset, ClassOrInterface cl, Method m, - ResultSet resultSet, List constraints, List classConstraints) { + ResultSet resultSet, GenericsResultSet constraints, GenericsResultSet classConstraints) { /* PL 2020-04-11 auskommentiert * try { @@ -79,7 +67,7 @@ public class TypeInsertFactory { */ } - private static synchronized Set createGenericInsert(List methodConstraints, List classConstraints,ClassOrInterface cl, Method m, ResultSet resultSet, Token mOffset){ + private static synchronized Set createGenericInsert(GenericsResultSet methodConstraints, GenericsResultSet classConstraints,ClassOrInterface cl, Method m, ResultSet resultSet, Token mOffset){ Set result = createGenericClassInserts(classConstraints, cl); if (m != null) { @@ -90,7 +78,7 @@ public class TypeInsertFactory { return result; } - private static Set createMethodConstraints(List constraints, Token mOffset) { + private static Set createMethodConstraints(GenericsResultSet constraints, Token mOffset) { Set result = new HashSet<>(); Token offset = mOffset; @@ -100,11 +88,11 @@ public class TypeInsertFactory { String insert = " <"; - for (GenericsGeneratorResult genericInsertConstraint : constraints) { - if (genericInsertConstraint.getConstraint().getRight().equals(Type.getInternalName(Object.class))) { - insert += genericInsertConstraint.getConstraint().getLeft(); - } else { - insert += genericInsertConstraint.getConstraint().getLeft() + " extends " + genericInsertConstraint.getConstraint().getRight(); + for (var genericInsertConstraint : constraints) { + if (genericInsertConstraint instanceof PairTPHequalRefTypeOrWildcardType peq) { + insert += peq.left.getName(); + } else if (genericInsertConstraint instanceof PairTPHsmallerTPH psm) { + insert += psm.left.getName() + " extends " + psm.right.getName(); } insert += ", "; } @@ -116,25 +104,23 @@ public class TypeInsertFactory { return result; } - private static Set createGenericClassInserts(List classConstraints, ClassOrInterface cl) { + private static Set createGenericClassInserts(GenericsResultSet classConstraints, ClassOrInterface cl) { Set result = new HashSet<>(); Token offset = cl.getGenerics().getOffset(); - - //List classConstraints = genericResult.getClassConstraints(); - + if (classConstraints == null || classConstraints.size() == 0) { return result; } String insert = " <"; - for (GenericsGeneratorResult genericInsertConstraint : classConstraints) { - if (genericInsertConstraint.getConstraint().getRight().equals(Type.getInternalName(Object.class))) { - insert += genericInsertConstraint.getConstraint().getLeft(); - } else { - insert += genericInsertConstraint.getConstraint().getLeft() + " extends " + genericInsertConstraint.getConstraint().getRight(); - } - insert += ", "; + for (var genericInsertConstraint : classConstraints) { + if (genericInsertConstraint instanceof PairTPHequalRefTypeOrWildcardType peq) { + insert += peq.left.getName(); + } else if (genericInsertConstraint instanceof PairTPHsmallerTPH psm) { + insert += psm.left.getName() + " extends " + psm.right.getName(); + } + insert += ", "; } insert = insert.substring(0, insert.length() -2); @@ -148,11 +134,11 @@ public class TypeInsertFactory { class TypeToInsertString implements ResultSetVisitor{ String insert = ""; - private List constraints; - private List classConstraints; + private GenericsResultSet constraints; + private GenericsResultSet classConstraints; - TypeToInsertString(RefTypeOrTPHOrWildcardOrGeneric type, List constraints, List classConstraints){ + TypeToInsertString(RefTypeOrTPHOrWildcardOrGeneric type, GenericsResultSet constraints, GenericsResultSet classConstraints){ this.constraints = constraints; this.classConstraints = classConstraints; type.accept(this); @@ -200,32 +186,11 @@ class TypeToInsertString implements ResultSetVisitor{ @Override public void visit(TypePlaceholder typePlaceholder) { - 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; - } + insert += ((TypePlaceholder) constraints.getResultPairFor(typePlaceholder).get().getLeft()).getName(); } @Override public void visit(ExtendsWildcardType extendsWildcardType) { 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 aba0e72e..bbeda06c 100644 --- a/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java +++ b/src/main/java/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java @@ -1,11 +1,11 @@ package de.dhbwstuttgart.typedeployment; -import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; -import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult; -import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResultForClass; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.target.generate.GenericsResult; +import de.dhbwstuttgart.target.generate.GenericsResultSet; +import de.dhbwstuttgart.typeinference.result.ResultPair; import de.dhbwstuttgart.typeinference.result.ResultSet; import java.util.ArrayList; @@ -17,11 +17,11 @@ public class TypeInsertPlacer extends AbstractASTWalker{ Set inserts = new HashSet<>(); private ResultSet withResults; String pkgName; - private List simplifyResultsForAllSourceFiles; + private GenericsResult genericsResult; - public Set getTypeInserts(SourceFile forSourceFile, ResultSet withResults, List simplifyResultsForAllSourceFiles){ + public Set getTypeInserts(SourceFile forSourceFile, ResultSet withResults, GenericsResult genericsResult){ this.withResults = withResults; - this.simplifyResultsForAllSourceFiles = simplifyResultsForAllSourceFiles; + this.genericsResult = genericsResult; pkgName = forSourceFile.getPkgName(); forSourceFile.accept(this); return inserts; @@ -29,27 +29,22 @@ public class TypeInsertPlacer extends AbstractASTWalker{ @Override public void visit(ClassOrInterface classOrInterface) { - GenericsGeneratorResultForClass generatedGenerics = simplifyResultsForAllSourceFiles - .stream() - .map(sr->sr.getSimplifyResultsByName(classOrInterface.getClassName())) - .findFirst() - .get(); - TypeInsertPlacerClass cl = new TypeInsertPlacerClass(classOrInterface, withResults, generatedGenerics); + TypeInsertPlacerClass cl = new TypeInsertPlacerClass(classOrInterface, withResults, genericsResult); this.inserts.addAll(cl.inserts); } } class TypeInsertPlacerClass extends AbstractASTWalker{ protected final ResultSet results; - private GenericsGeneratorResultForClass generatedGenerics; + private GenericsResult generatedGenerics; protected final ClassOrInterface cl; public final Set inserts = new HashSet<>(); private Method method; - - List constraints; - List classConstraints; - TypeInsertPlacerClass(ClassOrInterface forClass, ResultSet withResults, GenericsGeneratorResultForClass generatedGenerics){ + GenericsResultSet constraints; + GenericsResultSet classConstraints; + + TypeInsertPlacerClass(ClassOrInterface forClass, ResultSet withResults, GenericsResult generatedGenerics){ this.cl = forClass; this.method = null; this.results = withResults; @@ -60,8 +55,8 @@ class TypeInsertPlacerClass extends AbstractASTWalker{ @Override public void visit(Method method) { this.method = method; - //constraints = generatedGenerics.getMethodConstraintsByID(id); - classConstraints = generatedGenerics.getClassConstraints(); + constraints = generatedGenerics.get(method); + classConstraints = generatedGenerics.get(cl); if(method.getReturnType() instanceof TypePlaceholder) inserts.add(TypeInsertFactory.createInsertPoints( method.getReturnType(), method.getReturnType().getOffset(), cl, method, results, constraints, classConstraints)); @@ -71,9 +66,9 @@ class TypeInsertPlacerClass extends AbstractASTWalker{ @Override public void visit(Field field) { if(field.getType() instanceof TypePlaceholder){ - classConstraints = generatedGenerics.getClassConstraints(); + classConstraints = generatedGenerics.get(cl); inserts.add(TypeInsertFactory.createInsertPoints( - field.getType(), field.getType().getOffset(), cl, method, results, new ArrayList<>(), classConstraints)); + field.getType(), field.getType().getOffset(), cl, method, results, null, classConstraints)); } super.visit(field); } diff --git a/src/test/java/TestTypeDeployment.java b/src/test/java/TestTypeDeployment.java new file mode 100644 index 00000000..bd2f5438 --- /dev/null +++ b/src/test/java/TestTypeDeployment.java @@ -0,0 +1,40 @@ +import de.dhbwstuttgart.core.JavaTXCompiler; +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.syntaxtree.SourceFile; +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.nio.file.Path; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class TestTypeDeployment { + + @Test + public void testTypeDeployment() throws Exception { + var path = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Cycle.jav"); + var file = path.toFile(); + var compiler = new JavaTXCompiler(file, false); + var parsedSource = compiler.sourceFiles.get(file); + var tiResults = compiler.typeInference(); + Set tips = new HashSet<>(); + + for (var sf : compiler.sourceFiles.values()) { + Map bytecode = compiler.generateBytecode(sf, tiResults); + } + + System.out.println(compiler.getGeneratedGenerics()); + + for (int i = 0; i < tiResults.size(); i++) { + ResultSet tiResult = tiResults.get(i); + tips.addAll(TypeInsertFactory.createTypeInsertPoints(parsedSource, tiResult, compiler.getGeneratedGenerics().get(compiler.sourceFiles.get(file)).get(i))); + for (TypeInsert p : tips) { + System.out.println(p); + } + } + } +} diff --git a/src/test/java/packages/OLOneFileTest.java b/src/test/java/packages/OLOneFileTest.java index 9050d133..ddae05b5 100644 --- a/src/test/java/packages/OLOneFileTest.java +++ b/src/test/java/packages/OLOneFileTest.java @@ -14,7 +14,6 @@ import org.junit.Test; import com.google.common.collect.Lists; -import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.typeinference.result.ResultSet; diff --git a/src/test/java/packages/OLTest.java b/src/test/java/packages/OLTest.java index ea3f9c1b..0518be96 100644 --- a/src/test/java/packages/OLTest.java +++ b/src/test/java/packages/OLTest.java @@ -14,9 +14,7 @@ import org.junit.Test; import com.google.common.collect.Lists; -import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.core.JavaTXCompiler; -import de.dhbwstuttgart.typeinference.result.ResultSet; public class OLTest { private static String path; diff --git a/src/test/java/targetast/TestGenerics.java b/src/test/java/targetast/TestGenerics.java index a9058361..3d4ad553 100644 --- a/src/test/java/targetast/TestGenerics.java +++ b/src/test/java/targetast/TestGenerics.java @@ -41,8 +41,9 @@ public class TestGenerics { var compiler = new JavaTXCompiler(List.of(file), List.of(file.getParentFile())); var inference = compiler.typeInference(); compiler.generateBytecode(new File(bytecodeDirectory), inference); - var clazz = compiler.sourceFiles.get(file).getClasses().get(0); - return new Result(compiler.getGeneratedGenerics().get(file), clazz); + var sf = compiler.sourceFiles.get(file); + var clazz = sf.getClasses().get(0); + return new Result(compiler.getGeneratedGenerics().get(sf), clazz); } @Test diff --git a/src/test/java/typeinference/JavaTXCompilerTest.java b/src/test/java/typeinference/JavaTXCompilerTest.java index 63570d95..60a27221 100644 --- a/src/test/java/typeinference/JavaTXCompilerTest.java +++ b/src/test/java/typeinference/JavaTXCompilerTest.java @@ -1,6 +1,5 @@ package typeinference; -import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; @@ -142,7 +141,6 @@ 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); @@ -151,8 +149,9 @@ public class JavaTXCompilerTest { //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); + for(var i = 0; i < results.size(); i++) { + var resultSet = results.get(i); + Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet, compiler.getGeneratedGenerics().get(sf).get(i)); 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 0eb4feab..22f7fd51 100644 --- a/src/test/java/typeinference/Meth_GenTest.java +++ b/src/test/java/typeinference/Meth_GenTest.java @@ -1,6 +1,5 @@ package typeinference; -import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; @@ -106,7 +105,6 @@ 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); @@ -115,8 +113,9 @@ public class Meth_GenTest { //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); + for(var i = 0; i < results.size(); i++){ + var resultSet = results.get(i); + Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet, compiler.getGeneratedGenerics().get(sf).get(i)); 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 366b010e..4881de34 100644 --- a/src/test/java/typeinference/UnifyTest.java +++ b/src/test/java/typeinference/UnifyTest.java @@ -1,6 +1,5 @@ package typeinference; -import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; @@ -132,7 +131,6 @@ 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); @@ -141,8 +139,9 @@ public class UnifyTest { //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); + for(var i = 0; i < results.size(); i++) { + var resultSet = results.get(i); + Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet, compiler.getGeneratedGenerics().get(sf).get(i)); assert result.size()>0; String content = readFile(f.getPath(), StandardCharsets.UTF_8); for(TypeInsert tip : result){