Search in sources :

Example 1 with InternMethod

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;
}
Also used : InternMethod(org.checkerframework.checker.interning.qual.InternMethod) TreePath(com.sun.source.util.TreePath) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnTree(com.sun.source.tree.ReturnTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) NewClassTree(com.sun.source.tree.NewClassTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) IfTree(com.sun.source.tree.IfTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) BlockTree(com.sun.source.tree.BlockTree) StatementTree(com.sun.source.tree.StatementTree)

Example 2 with InternMethod

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);
}
Also used : InternMethod(org.checkerframework.checker.interning.qual.InternMethod) ExecutableElement(javax.lang.model.element.ExecutableElement) CompareToMethod(org.checkerframework.checker.interning.qual.CompareToMethod) EqualsMethod(org.checkerframework.checker.interning.qual.EqualsMethod)

Aggregations

ExecutableElement (javax.lang.model.element.ExecutableElement)2 InternMethod (org.checkerframework.checker.interning.qual.InternMethod)2 BinaryTree (com.sun.source.tree.BinaryTree)1 BlockTree (com.sun.source.tree.BlockTree)1 ClassTree (com.sun.source.tree.ClassTree)1 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 IdentifierTree (com.sun.source.tree.IdentifierTree)1 IfTree (com.sun.source.tree.IfTree)1 LiteralTree (com.sun.source.tree.LiteralTree)1 MemberSelectTree (com.sun.source.tree.MemberSelectTree)1 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)1 MethodTree (com.sun.source.tree.MethodTree)1 NewClassTree (com.sun.source.tree.NewClassTree)1 ReturnTree (com.sun.source.tree.ReturnTree)1 StatementTree (com.sun.source.tree.StatementTree)1 Tree (com.sun.source.tree.Tree)1 TreePath (com.sun.source.util.TreePath)1 CompareToMethod (org.checkerframework.checker.interning.qual.CompareToMethod)1 EqualsMethod (org.checkerframework.checker.interning.qual.EqualsMethod)1