Add JavaCompiler parsing
This commit is contained in:
parent
54a2dbfedc
commit
ca816fba85
@ -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<Void, Void>
|
||||
{
|
||||
|
||||
@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<? extends Tree> 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);
|
||||
}
|
||||
|
||||
}
|
@ -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<ConstraintSet, Void>
|
||||
{
|
||||
|
||||
public TypeParamVisitor () {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static ConstraintSet generateConstraints (IdentifierTree type) {
|
||||
ConstraintSet constraintSet = new ConstraintSet<>();
|
||||
Set<Constraint<?>> 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;
|
||||
}
|
||||
}
|
51
src/test/java/inferWildcards/TestInferWildcards.java
Normal file
51
src/test/java/inferWildcards/TestInferWildcards.java
Normal file
@ -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<? extends CompilationUnitTree> parse (File[] files) throws IOException {
|
||||
Iterable<? extends JavaFileObject> 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<? extends CompilationUnitTree> iterable = parse(files1);
|
||||
for (CompilationUnitTree t : iterable) {
|
||||
System.out.println("unit: " + t.toString());
|
||||
|
||||
t.accept(new InferWildcardsVisitor(), null);
|
||||
}
|
||||
}
|
||||
}
|
20
src/test/resources/inferWildcards/TestClassWildcards.java
Normal file
20
src/test/resources/inferWildcards/TestClassWildcards.java
Normal file
@ -0,0 +1,20 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
class TestClassWildcards {
|
||||
|
||||
private List<String> field1;
|
||||
private int counter;
|
||||
|
||||
public TestClassWildcards(){
|
||||
field1 = new ArrayList<>();
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
public List<String> test(List<String> param1){
|
||||
List<String> localVar = field1;
|
||||
field1 = param;
|
||||
counter++;
|
||||
return localVar;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user