framework.src.org.checkerframework.framework.util.typeinference.constraint.F2AReducer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of checker Show documentation
Show all versions of checker Show documentation
The Checker Framework enhances Java’s type system to
make it more powerful and useful. This lets software developers
detect and prevent errors in their Java programs.
The Checker Framework includes compiler plug-ins ("checkers")
that find bugs or verify their absence. It also permits you to
write your own compiler plug-ins.
package org.checkerframework.framework.util.typeinference.constraint;
import org.checkerframework.framework.type.AnnotatedTypeFactory;
import org.checkerframework.framework.type.AnnotatedTypeMirror;
import java.util.Set;
/**
* F2AReducer takes an F2A constraint that is not irreducible (@see AFConstraint.isIrreducible)
* and reduces it by one step. The resulting constraint may still be reducible.
*
* Generally reductions should map to corresponding rules in
* http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.7
*/
public class F2AReducer implements AFReducer {
protected final F2AReducingVisitor visitor;
public F2AReducer(final AnnotatedTypeFactory typeFactory) {
this.visitor = new F2AReducingVisitor(typeFactory);
}
@Override
public boolean reduce(AFConstraint constraint, Set newConstraints) {
if (constraint instanceof F2A) {
final F2A f2A = (F2A) constraint;
visitor.visit(f2A.formalParameter, f2A.argument, newConstraints);
return true;
} else {
return false;
}
}
/**
* Given an F2A constraint of the form:
* F2A( typeFromFormalParameter, typeFromMethodArgument )
*
* F2AReducingVisitor visits the constraint as follows:
* visit ( typeFromFormalParameter, typeFromMethodArgument, newConstraints )
*
* The visit method will determine if the given constraint should either:
* a) be discarded - in this case, the visitor just returns
* b) reduced to a simpler constraint or set of constraints - in this case, the new constraint
* or set of constraints is added to newConstraints
*
* Sprinkled throughout this class are comments of the form:
*
* {@code
* // If F has the form G<..., Yk-1, ? super U, Yk+1, ...>, where U involves Tj
* }
*
* These are excerpts from the JLS, if you search for them you will find the corresponding
* JLS description of the case being covered.
*/
class F2AReducingVisitor extends AFReducingVisitor {
public F2AReducingVisitor(AnnotatedTypeFactory typeFactory) {
super(F2A.class, typeFactory);
}
@Override
public AFConstraint makeConstraint(AnnotatedTypeMirror subtype, AnnotatedTypeMirror supertype) {
return new F2A(subtype, supertype);
}
@Override
public AFConstraint makeInverseConstraint(AnnotatedTypeMirror subtype, AnnotatedTypeMirror supertype) {
return new A2F(subtype, supertype);
}
@Override
public AFConstraint makeEqualityConstraint(AnnotatedTypeMirror subtype, AnnotatedTypeMirror supertype) {
return new FIsA(subtype, supertype);
}
}
}