use of org.checkerframework.checker.interning.qual.InternMethod in project checker-framework by typetools.
the class InterningVisitor method checkCreationOfInternedObject.
/**
* Issue an error if {@code newInternedObject} is not immediately interned.
*
* @param newInternedObject call to a constructor of an interned class
* @param constructor declared type of the constructor
* @return false unless {@code newInternedObject} is immediately interned
*/
private boolean checkCreationOfInternedObject(NewClassTree newInternedObject, AnnotatedExecutableType constructor) {
if (constructor.getReturnType().hasAnnotation(Interned.class)) {
return true;
}
TreePath path = getCurrentPath();
if (path != null) {
TreePath parentPath = path.getParentPath();
while (parentPath != null && parentPath.getLeaf().getKind() == Tree.Kind.PARENTHESIZED) {
parentPath = parentPath.getParentPath();
}
if (parentPath != null && parentPath.getParentPath() != null) {
Tree parent = parentPath.getParentPath().getLeaf();
if (parent.getKind() == Tree.Kind.METHOD_INVOCATION) {
// Allow new MyInternType().intern(), where "intern" is any method marked @InternMethod.
ExecutableElement elt = TreeUtils.elementFromUse((MethodInvocationTree) parent);
if (atypeFactory.getDeclAnnotation(elt, InternMethod.class) != null) {
return true;
}
}
}
}
checker.reportError(newInternedObject, "interned.object.creation");
return false;
}
use of org.checkerframework.checker.interning.qual.InternMethod in project checker-framework by typetools.
the class InterningVisitor method visitMethod.
// Ensure that method annotations are not written on methods they don't apply to.
@Override
public Void visitMethod(MethodTree node, Void p) {
ExecutableElement methElt = TreeUtils.elementFromDeclaration(node);
boolean hasCompareToMethodAnno = atypeFactory.getDeclAnnotation(methElt, CompareToMethod.class) != null;
boolean hasEqualsMethodAnno = atypeFactory.getDeclAnnotation(methElt, EqualsMethod.class) != null;
boolean hasInternMethodAnno = atypeFactory.getDeclAnnotation(methElt, InternMethod.class) != null;
int params = methElt.getParameters().size();
if (hasCompareToMethodAnno && !(params == 1 || params == 2)) {
checker.reportError(node, "invalid.method.annotation", "@CompareToMethod", "1 or 2", methElt, params);
} else if (hasEqualsMethodAnno && !(params == 1 || params == 2)) {
checker.reportError(node, "invalid.method.annotation", "@EqualsMethod", "1 or 2", methElt, params);
} else if (hasInternMethodAnno && !(params == 0)) {
checker.reportError(node, "invalid.method.annotation", "@InternMethod", "0", methElt, params);
}
return super.visitMethod(node, p);
}
Aggregations