From ca816fba85d57c328b40e240863d5bf717a92ad8 Mon Sep 17 00:00:00 2001 From: Till Schnell Date: Sat, 27 Mar 2021 17:29:37 +0100 Subject: [PATCH] Add JavaCompiler parsing --- .../inferWildcards/InferWildcardsVisitor.java | 73 +++++++++++++++++++ .../inferWildcards/TypeParamVisitor.java | 72 ++++++++++++++++++ .../inferWildcards/TestInferWildcards.java | 51 +++++++++++++ .../inferWildcards/TestClassWildcards.java | 20 +++++ 4 files changed, 216 insertions(+) create mode 100644 src/main/java/de/dhbwstuttgart/inferWildcards/InferWildcardsVisitor.java create mode 100644 src/main/java/de/dhbwstuttgart/inferWildcards/TypeParamVisitor.java create mode 100644 src/test/java/inferWildcards/TestInferWildcards.java create mode 100644 src/test/resources/inferWildcards/TestClassWildcards.java diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/InferWildcardsVisitor.java b/src/main/java/de/dhbwstuttgart/inferWildcards/InferWildcardsVisitor.java new file mode 100644 index 00000000..f1a36ab6 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/InferWildcardsVisitor.java @@ -0,0 +1,73 @@ +package de.dhbwstuttgart.inferWildcards; + +import java.util.List; + +import com.sun.source.tree.IdentifierTree; +import com.sun.source.tree.ParameterizedTypeTree; +import com.sun.source.tree.PrimitiveTypeTree; +import com.sun.source.tree.Tree; +import com.sun.source.tree.TypeParameterTree; +import com.sun.source.tree.VariableTree; +import com.sun.source.util.TreeScanner; + +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; + +public class InferWildcardsVisitor + extends TreeScanner +{ + + @SuppressWarnings("rawtypes") + private final ConstraintSet constraintsSet; + + public InferWildcardsVisitor () { + super(); + this.constraintsSet = new ConstraintSet<>(); + } + + + + @SuppressWarnings("rawtypes") + @Override + public Void visitParameterizedType (ParameterizedTypeTree node, Void p) { + System.out.println("ParameterizedType: " + node.toString()); + + List typeArguments = node.getTypeArguments(); + typeArguments.forEach(t -> { + ConstraintSet s = t.accept(new TypeParamVisitor(), null); + constraintsSet.addAll(s); + }); + + return super.visitParameterizedType(node, p); + } + + @Override + public Void visitPrimitiveType (PrimitiveTypeTree node, Void p) { + System.out.println("PrimitiveType: " + node.toString()); + return super.visitPrimitiveType(node, p); + } + + @Override + public Void visitIdentifier (IdentifierTree node, Void p) { + System.out.println("Identifier: " + node.toString()); + return super.visitIdentifier(node, p); + } + + @Override + public Void visitTypeParameter (TypeParameterTree node, Void p) { + System.out.println("TypeParameter: " + node.toString()); + return super.visitTypeParameter(node, p); + } + + @Override + public Void visitOther (Tree node, Void p) { + System.out.println("Other: " + node.toString()); + return super.visitOther(node, p); + } + + @Override + public Void visitVariable (VariableTree node, Void p) { + System.out.println("Variable: " + node.toString()); + return super.visitVariable(node, p); + } + +} diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/TypeParamVisitor.java b/src/main/java/de/dhbwstuttgart/inferWildcards/TypeParamVisitor.java new file mode 100644 index 00000000..53d0eeee --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/TypeParamVisitor.java @@ -0,0 +1,72 @@ +package de.dhbwstuttgart.inferWildcards; + +import java.util.HashSet; +import java.util.Set; + +import com.sun.source.tree.IdentifierTree; +import com.sun.source.util.TreeScanner; + +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; +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.constraints.Constraint; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; + +@SuppressWarnings("rawtypes") +public class TypeParamVisitor + extends TreeScanner +{ + + public TypeParamVisitor () { + } + + @SuppressWarnings("unchecked") + private static ConstraintSet generateConstraints (IdentifierTree type) { + ConstraintSet constraintSet = new ConstraintSet<>(); + Set> oderConstraints = new HashSet<>(); + constraintSet.addOderConstraint(oderConstraints); + + Constraint c = new Constraint<>(); + oderConstraints.add(c); + + // generate Type Placeholder + TypePlaceholder tph = TypePlaceholder.fresh(new NullToken()); + + // single type + RefType refType = new RefType(new JavaClassName(type.getName().toString()), new NullToken()); + TypeParamVisitor.addToConstraint(c, tph, refType); + + // extends type + ExtendsWildcardType extendsWildcardType = new ExtendsWildcardType(refType, new NullToken()); + TypeParamVisitor.addToConstraint(c, tph, extendsWildcardType); + + // super type + SuperWildcardType superWildcardType = new SuperWildcardType(refType, new NullToken()); + TypeParamVisitor.addToConstraint(c, tph, superWildcardType); + + return constraintSet; + } + + @SuppressWarnings({ "unchecked" }) + private static void addToConstraint (Constraint c, TypePlaceholder tph, RefTypeOrTPHOrWildcardOrGeneric type) { + Pair pair = new Pair(tph, type, PairOperator.EQUALSDOT); + c.add(pair); + } + + @Override + public ConstraintSet visitIdentifier (IdentifierTree node, Void p) { + return TypeParamVisitor.generateConstraints(node); + } + + @Override + public ConstraintSet reduce (ConstraintSet r1, ConstraintSet r2) { + r1.addAll(r2); + return r1; + } +} diff --git a/src/test/java/inferWildcards/TestInferWildcards.java b/src/test/java/inferWildcards/TestInferWildcards.java new file mode 100644 index 00000000..885d5969 --- /dev/null +++ b/src/test/java/inferWildcards/TestInferWildcards.java @@ -0,0 +1,51 @@ +package inferWildcards; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; + +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; + +import org.junit.Before; +import org.junit.Test; + +import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.util.JavacTask; + +import de.dhbwstuttgart.inferWildcards.InferWildcardsVisitor; + +public class TestInferWildcards +{ + + private JavaCompiler compiler; + private StandardJavaFileManager fileManager; + + @Before + void setup () { + compiler = ToolProvider.getSystemJavaCompiler(); + fileManager = compiler.getStandardFileManager(null, null, null); + } + + private Iterable parse (File[] files) throws IOException { + Iterable compilationUnits1 = fileManager + .getJavaFileObjectsFromFiles(Arrays.asList(files)); + JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null, compilationUnits1); + return task.parse(); + } + + @Test + void test () throws Exception { + String resourcePath = System.getProperty("user.dir") + "/src/test/resources/inferWildcards"; + File[] files1 = { new File(resourcePath + "/TestClassWildcards.java") }; + + Iterable iterable = parse(files1); + for (CompilationUnitTree t : iterable) { + System.out.println("unit: " + t.toString()); + + t.accept(new InferWildcardsVisitor(), null); + } + } +} diff --git a/src/test/resources/inferWildcards/TestClassWildcards.java b/src/test/resources/inferWildcards/TestClassWildcards.java new file mode 100644 index 00000000..cb597cf6 --- /dev/null +++ b/src/test/resources/inferWildcards/TestClassWildcards.java @@ -0,0 +1,20 @@ +import java.util.ArrayList; + +class TestClassWildcards { + + private List field1; + private int counter; + + public TestClassWildcards(){ + field1 = new ArrayList<>(); + counter = 0; + } + + public List test(List param1){ + List localVar = field1; + field1 = param; + counter++; + return localVar; + } + +} \ No newline at end of file