forked from JavaTX/JavaCompilerCore
Add the placeholder mapping
This commit is contained in:
parent
9d2c85d686
commit
ad7e7ec42b
@ -0,0 +1,55 @@
|
||||
package de.dhbwstuttgart.inferWildcards;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
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;
|
||||
|
||||
public final class ConstraintsGenerationUtils
|
||||
{
|
||||
|
||||
private ConstraintsGenerationUtils () {
|
||||
throw new AssertionError("No ConstraintsGenerationUtils instance for you");
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public static ConstraintSet generateConstraints (JavaClassName name, TypePlaceholder tph) {
|
||||
ConstraintSet constraintSet = new ConstraintSet<>();
|
||||
Set<Constraint<?>> oderConstraints = new HashSet<>();
|
||||
constraintSet.addOderConstraint(oderConstraints);
|
||||
|
||||
Constraint<?> c = new Constraint<>();
|
||||
oderConstraints.add(c);
|
||||
|
||||
// single type
|
||||
RefType refType = new RefType(name, new NullToken());
|
||||
ConstraintsGenerationUtils.addToConstraint(c, tph, refType);
|
||||
|
||||
// extends type
|
||||
ExtendsWildcardType extendsWildcardType = new ExtendsWildcardType(refType, new NullToken());
|
||||
ConstraintsGenerationUtils.addToConstraint(c, tph, extendsWildcardType);
|
||||
|
||||
// super type
|
||||
SuperWildcardType superWildcardType = new SuperWildcardType(refType, new NullToken());
|
||||
ConstraintsGenerationUtils.addToConstraint(c, tph, superWildcardType);
|
||||
|
||||
return constraintSet;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private static void addToConstraint (Constraint c, TypePlaceholder tph, RefTypeOrTPHOrWildcardOrGeneric type) {
|
||||
Pair pair = new Pair(tph, type, PairOperator.EQUALSDOT);
|
||||
c.add(pair);
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
package de.dhbwstuttgart.inferWildcards;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.sun.source.tree.IdentifierTree;
|
||||
import com.sun.source.tree.ParameterizedTypeTree;
|
||||
@ -10,6 +12,9 @@ import com.sun.source.tree.TypeParameterTree;
|
||||
import com.sun.source.tree.VariableTree;
|
||||
import com.sun.source.util.TreeScanner;
|
||||
|
||||
import de.dhbwstuttgart.parser.NullToken;
|
||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
||||
|
||||
public class InferWildcardsVisitor
|
||||
@ -18,10 +23,18 @@ public class InferWildcardsVisitor
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private final ConstraintSet constraintsSet;
|
||||
private final Map<TypePlaceholder, Tree> placeholderMapping;
|
||||
|
||||
public InferWildcardsVisitor () {
|
||||
super();
|
||||
this.constraintsSet = new ConstraintSet<>();
|
||||
this.placeholderMapping = new HashMap<>();
|
||||
}
|
||||
|
||||
private TypePlaceholder generateTypePlaceholder (Tree t) {
|
||||
TypePlaceholder tph = TypePlaceholder.fresh(new NullToken());
|
||||
placeholderMapping.put(tph, t);
|
||||
return tph;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@ -31,8 +44,16 @@ public class InferWildcardsVisitor
|
||||
|
||||
List<? extends Tree> typeArguments = node.getTypeArguments();
|
||||
typeArguments.forEach(t -> {
|
||||
ConstraintSet s = t.accept(new TypeParamVisitor(), null);
|
||||
constraintsSet.addAll(s);
|
||||
// generate Type Placeholder
|
||||
TypePlaceholder tph = generateTypePlaceholder(t);
|
||||
|
||||
// Search for the type parameter
|
||||
List<JavaClassName> foundClassNames = t.accept(new TypeParamVisitor(), null);
|
||||
foundClassNames.forEach(name -> {
|
||||
ConstraintSet constraints = ConstraintsGenerationUtils.generateConstraints(name, tph);
|
||||
constraintsSet.addAll(constraints);
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
return super.visitParameterizedType(node, p);
|
||||
@ -71,4 +92,8 @@ public class InferWildcardsVisitor
|
||||
public ConstraintSet getConstraintSet() {
|
||||
return constraintsSet;
|
||||
}
|
||||
|
||||
public Map<TypePlaceholder, Tree> getPlaceholderMapping () {
|
||||
return placeholderMapping;
|
||||
}
|
||||
}
|
||||
|
@ -1,71 +1,30 @@
|
||||
package de.dhbwstuttgart.inferWildcards;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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>
|
||||
extends TreeScanner<List<JavaClassName>, 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 List<JavaClassName> visitIdentifier (IdentifierTree node, Void p) {
|
||||
ArrayList<JavaClassName> list = new ArrayList<>();
|
||||
list.add(new JavaClassName(node.getName().toString()));
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConstraintSet visitIdentifier (IdentifierTree node, Void p) {
|
||||
return TypeParamVisitor.generateConstraints(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConstraintSet reduce (ConstraintSet r1, ConstraintSet r2) {
|
||||
public List<JavaClassName> reduce (List<JavaClassName> r1, List<JavaClassName> r2) {
|
||||
r1.addAll(r2);
|
||||
return r1;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package inferWildcards;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.JavaFileObject;
|
||||
@ -13,9 +14,11 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.sun.source.tree.CompilationUnitTree;
|
||||
import com.sun.source.tree.Tree;
|
||||
import com.sun.source.util.JavacTask;
|
||||
|
||||
import de.dhbwstuttgart.inferWildcards.InferWildcardsVisitor;
|
||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
||||
|
||||
public class TestInferWildcards
|
||||
@ -48,9 +51,15 @@ public class TestInferWildcards
|
||||
|
||||
InferWildcardsVisitor inferWildcardsVisitor = new InferWildcardsVisitor();
|
||||
t.accept(inferWildcardsVisitor, null);
|
||||
|
||||
// Generated Constraints
|
||||
ConstraintSet constraintSet = inferWildcardsVisitor.getConstraintSet();
|
||||
System.out.println("Constraints: " + constraintSet);
|
||||
}
|
||||
|
||||
// Generated TPH
|
||||
Map<TypePlaceholder, Tree> placeholderMapping = inferWildcardsVisitor.getPlaceholderMapping();
|
||||
System.out.println("Placeholder: " + placeholderMapping.toString());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user