Search in sources :

Example 51 with ClassTree

use of com.sun.source.tree.ClassTree in project checker-framework by typetools.

the class InterningVisitor method equalsImplementation.

// **********************************************************************
// Helper methods
// **********************************************************************
/**
 * Returns the method that overrides Object.equals, or null.
 *
 * @param node a class
 * @return the class's implementation of equals, or null
 */
private MethodTree equalsImplementation(ClassTree node) {
    List<? extends Tree> members = node.getMembers();
    for (Tree member : members) {
        if (member instanceof MethodTree) {
            MethodTree mTree = (MethodTree) member;
            ExecutableElement enclosing = TreeUtils.elementFromDeclaration(mTree);
            if (overrides(enclosing, Object.class, "equals")) {
                return mTree;
            }
        }
    }
    return null;
}
Also used : MethodTree(com.sun.source.tree.MethodTree) 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 52 with ClassTree

use of com.sun.source.tree.ClassTree in project checker-framework by typetools.

the class UpperBoundVisitor method visitAnnotation.

/**
 * Warns about LTLengthOf annotations with arguments whose lengths do not match.
 */
@Override
public Void visitAnnotation(AnnotationTree node, Void p) {
    AnnotationMirror anno = TreeUtils.annotationFromAnnotationTree(node);
    if (atypeFactory.areSameByClass(anno, LTLengthOf.class)) {
        List<? extends ExpressionTree> args = node.getArguments();
        if (args.size() == 2) {
            // If offsets are provided, there must be the same number of them as there are arrays.
            List<String> sequences = AnnotationUtils.getElementValueArray(anno, atypeFactory.ltLengthOfValueElement, String.class);
            List<String> offsets = AnnotationUtils.getElementValueArray(anno, atypeFactory.ltLengthOfOffsetElement, String.class, Collections.emptyList());
            if (sequences.size() != offsets.size() && !offsets.isEmpty()) {
                checker.reportError(node, "different.length.sequences.offsets", sequences.size(), offsets.size());
                return null;
            }
        }
    } else if (atypeFactory.areSameByClass(anno, HasSubsequence.class)) {
        // Check that the arguments to a HasSubsequence annotation are valid JavaExpressions,
        // and issue an error if one of them is not.
        String seq = atypeFactory.hasSubsequenceSubsequenceValue(anno);
        String from = atypeFactory.hasSubsequenceFromValue(anno);
        String to = atypeFactory.hasSubsequenceToValue(anno);
        // check that each expression is parsable at the declaration of this class
        ClassTree enclosingClass = TreePathUtil.enclosingClass(getCurrentPath());
        checkEffectivelyFinalAndParsable(seq, enclosingClass, node);
        checkEffectivelyFinalAndParsable(from, enclosingClass, node);
        checkEffectivelyFinalAndParsable(to, enclosingClass, node);
    }
    return super.visitAnnotation(node, p);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ClassTree(com.sun.source.tree.ClassTree) HasSubsequence(org.checkerframework.checker.index.qual.HasSubsequence)

Example 53 with ClassTree

use of com.sun.source.tree.ClassTree in project checker-framework by typetools.

the class TypesIntoElements method store.

/**
 * The entry point.
 *
 * @param processingEnv the environment
 * @param atypeFactory the type factory
 * @param tree the ClassTree to process
 */
public static void store(ProcessingEnvironment processingEnv, AnnotatedTypeFactory atypeFactory, ClassTree tree) {
    Symbol.ClassSymbol csym = (Symbol.ClassSymbol) TreeUtils.elementFromDeclaration(tree);
    Types types = processingEnv.getTypeUtils();
    storeTypeParameters(processingEnv, types, atypeFactory, tree.getTypeParameters(), csym);
    for (Tree mem : tree.getMembers()) {
        if (mem.getKind() == Tree.Kind.METHOD) {
            storeMethod(processingEnv, types, atypeFactory, (MethodTree) mem);
        } else if (mem.getKind() == Tree.Kind.VARIABLE) {
            storeVariable(processingEnv, types, atypeFactory, (VariableTree) mem);
        } else {
        // System.out.println("Unhandled member tree: " + mem);
        }
    }
}
Also used : Types(javax.lang.model.util.Types) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) VariableTree(com.sun.source.tree.VariableTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) JCTree(com.sun.tools.javac.tree.JCTree)

Example 54 with ClassTree

use of com.sun.source.tree.ClassTree in project checker-framework by typetools.

the class AnnotatedTypeFactory method setRoot.

/**
 * Set the CompilationUnitTree that should be used.
 *
 * @param root the new compilation unit to use
 */
public void setRoot(@Nullable CompilationUnitTree root) {
    if (root != null && wholeProgramInference != null) {
        for (Tree typeDecl : root.getTypeDecls()) {
            if (typeDecl.getKind() == Tree.Kind.CLASS) {
                ClassTree classTree = (ClassTree) typeDecl;
                wholeProgramInference.preprocessClassTree(classTree);
            }
        }
    }
    this.root = root;
    // if this isn't a GenericATF, then it must clear it itself.
    if (!(this instanceof GenericAnnotatedTypeFactory)) {
        artificialTreeToEnclosingElementMap.clear();
    }
    if (shouldCache) {
        // Clear the caches with trees because once the compilation unit changes,
        // the trees may be modified and lose type arguments.
        elementToTreeCache.clear();
        fromExpressionTreeCache.clear();
        fromMemberTreeCache.clear();
        fromTypeTreeCache.clear();
        classAndMethodTreeCache.clear();
    // There is no need to clear the following cache, it is limited by cache size and it
    // contents won't change between compilation units.
    // elementCache.clear();
    }
    if (root != null && checker.hasOption("ajava")) {
        // Search for an ajava file with annotations for the current source file and the current
        // checker. It will be in a directory specified by the "ajava" option in a subdirectory
        // corresponding to this file's package. For example, a file in package a.b would be in a
        // subdirectory a/b. The filename is ClassName-checker.qualified.name.ajava. If such a file
        // exists, read its detailed annotation data, including annotations on private elements.
        String packagePrefix = root.getPackageName() != null ? TreeUtils.nameExpressionToString(root.getPackageName()) + "." : "";
        // The method getName() returns a path.
        String className = root.getSourceFile().getName();
        // Extract the basename.
        int lastSeparator = className.lastIndexOf(File.separator);
        if (lastSeparator != -1) {
            className = className.substring(lastSeparator + 1);
        }
        // Drop the ".java" extension.
        if (className.endsWith(".java")) {
            className = className.substring(0, className.length() - ".java".length());
        }
        String qualifiedName = packagePrefix + className;
        for (String ajavaLocation : checker.getOption("ajava").split(File.pathSeparator)) {
            String ajavaPath = ajavaLocation + File.separator + qualifiedName.replaceAll("\\.", "/") + "-" + checker.getClass().getCanonicalName() + ".ajava";
            File ajavaFile = new File(ajavaPath);
            if (ajavaFile.exists()) {
                currentFileAjavaTypes = new AnnotationFileElementTypes(this);
                currentFileAjavaTypes.parseAjavaFileWithTree(ajavaPath, root);
                break;
            }
        }
    } else {
        currentFileAjavaTypes = null;
    }
}
Also used : AnnotationFileElementTypes(org.checkerframework.framework.stub.AnnotationFileElementTypes) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ReturnTree(com.sun.source.tree.ReturnTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) NewArrayTree(com.sun.source.tree.NewArrayTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) ClassTree(com.sun.source.tree.ClassTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) File(java.io.File) FromStubFile(org.checkerframework.framework.qual.FromStubFile)

Example 55 with ClassTree

use of com.sun.source.tree.ClassTree in project checker-framework by typetools.

the class CFGTranslationPhaseOne method visitBindingPattern17.

/**
 * Visit a BindingPatternTree
 *
 * @param bindingPatternTree a BindingPatternTree, typed as Tree to be backward-compatible
 * @param p parameter
 * @return the result of visiting the binding pattern tree
 */
public Node visitBindingPattern17(Tree bindingPatternTree, Void p) {
    ClassTree enclosingClass = TreePathUtil.enclosingClass(getCurrentPath());
    TypeElement classElem = TreeUtils.elementFromDeclaration(enclosingClass);
    Node receiver = new ImplicitThisNode(classElem.asType());
    VariableTree varTree = TreeUtils.bindingPatternTreeGetVariable(bindingPatternTree);
    LocalVariableNode varNode = new LocalVariableNode(varTree, receiver);
    extendWithNode(varNode);
    return varNode;
}
Also used : ImplicitThisNode(org.checkerframework.dataflow.cfg.node.ImplicitThisNode) TypeElement(javax.lang.model.element.TypeElement) NumericalMultiplicationNode(org.checkerframework.dataflow.cfg.node.NumericalMultiplicationNode) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) ValueLiteralNode(org.checkerframework.dataflow.cfg.node.ValueLiteralNode) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode) UnsignedRightShiftNode(org.checkerframework.dataflow.cfg.node.UnsignedRightShiftNode) LeftShiftNode(org.checkerframework.dataflow.cfg.node.LeftShiftNode) PrimitiveTypeNode(org.checkerframework.dataflow.cfg.node.PrimitiveTypeNode) FloatLiteralNode(org.checkerframework.dataflow.cfg.node.FloatLiteralNode) LessThanNode(org.checkerframework.dataflow.cfg.node.LessThanNode) BitwiseOrNode(org.checkerframework.dataflow.cfg.node.BitwiseOrNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) NarrowingConversionNode(org.checkerframework.dataflow.cfg.node.NarrowingConversionNode) EqualToNode(org.checkerframework.dataflow.cfg.node.EqualToNode) NumericalPlusNode(org.checkerframework.dataflow.cfg.node.NumericalPlusNode) ConditionalAndNode(org.checkerframework.dataflow.cfg.node.ConditionalAndNode) VariableDeclarationNode(org.checkerframework.dataflow.cfg.node.VariableDeclarationNode) ClassDeclarationNode(org.checkerframework.dataflow.cfg.node.ClassDeclarationNode) IntegerDivisionNode(org.checkerframework.dataflow.cfg.node.IntegerDivisionNode) AssertionErrorNode(org.checkerframework.dataflow.cfg.node.AssertionErrorNode) InstanceOfNode(org.checkerframework.dataflow.cfg.node.InstanceOfNode) BooleanLiteralNode(org.checkerframework.dataflow.cfg.node.BooleanLiteralNode) ThisNode(org.checkerframework.dataflow.cfg.node.ThisNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) NullLiteralNode(org.checkerframework.dataflow.cfg.node.NullLiteralNode) ArrayTypeNode(org.checkerframework.dataflow.cfg.node.ArrayTypeNode) LambdaResultExpressionNode(org.checkerframework.dataflow.cfg.node.LambdaResultExpressionNode) IntegerRemainderNode(org.checkerframework.dataflow.cfg.node.IntegerRemainderNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) ConditionalOrNode(org.checkerframework.dataflow.cfg.node.ConditionalOrNode) NotEqualNode(org.checkerframework.dataflow.cfg.node.NotEqualNode) BitwiseXorNode(org.checkerframework.dataflow.cfg.node.BitwiseXorNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) ExplicitThisNode(org.checkerframework.dataflow.cfg.node.ExplicitThisNode) StringConcatenateNode(org.checkerframework.dataflow.cfg.node.StringConcatenateNode) NullChkNode(org.checkerframework.dataflow.cfg.node.NullChkNode) CharacterLiteralNode(org.checkerframework.dataflow.cfg.node.CharacterLiteralNode) FloatingDivisionNode(org.checkerframework.dataflow.cfg.node.FloatingDivisionNode) FunctionalInterfaceNode(org.checkerframework.dataflow.cfg.node.FunctionalInterfaceNode) StringConcatenateAssignmentNode(org.checkerframework.dataflow.cfg.node.StringConcatenateAssignmentNode) TypeCastNode(org.checkerframework.dataflow.cfg.node.TypeCastNode) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) WideningConversionNode(org.checkerframework.dataflow.cfg.node.WideningConversionNode) LongLiteralNode(org.checkerframework.dataflow.cfg.node.LongLiteralNode) MarkerNode(org.checkerframework.dataflow.cfg.node.MarkerNode) ImplicitThisNode(org.checkerframework.dataflow.cfg.node.ImplicitThisNode) FloatingRemainderNode(org.checkerframework.dataflow.cfg.node.FloatingRemainderNode) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) PackageNameNode(org.checkerframework.dataflow.cfg.node.PackageNameNode) DoubleLiteralNode(org.checkerframework.dataflow.cfg.node.DoubleLiteralNode) SuperNode(org.checkerframework.dataflow.cfg.node.SuperNode) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) SignedRightShiftNode(org.checkerframework.dataflow.cfg.node.SignedRightShiftNode) ThrowNode(org.checkerframework.dataflow.cfg.node.ThrowNode) GreaterThanOrEqualNode(org.checkerframework.dataflow.cfg.node.GreaterThanOrEqualNode) StringLiteralNode(org.checkerframework.dataflow.cfg.node.StringLiteralNode) TernaryExpressionNode(org.checkerframework.dataflow.cfg.node.TernaryExpressionNode) BitwiseAndNode(org.checkerframework.dataflow.cfg.node.BitwiseAndNode) ParameterizedTypeNode(org.checkerframework.dataflow.cfg.node.ParameterizedTypeNode) CaseNode(org.checkerframework.dataflow.cfg.node.CaseNode) SwitchExpressionNode(org.checkerframework.dataflow.cfg.node.SwitchExpressionNode) NumericalAdditionNode(org.checkerframework.dataflow.cfg.node.NumericalAdditionNode) NumericalSubtractionNode(org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode) BitwiseComplementNode(org.checkerframework.dataflow.cfg.node.BitwiseComplementNode) ConditionalNotNode(org.checkerframework.dataflow.cfg.node.ConditionalNotNode) NumericalMinusNode(org.checkerframework.dataflow.cfg.node.NumericalMinusNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) GreaterThanNode(org.checkerframework.dataflow.cfg.node.GreaterThanNode) LessThanOrEqualNode(org.checkerframework.dataflow.cfg.node.LessThanOrEqualNode) SynchronizedNode(org.checkerframework.dataflow.cfg.node.SynchronizedNode) Node(org.checkerframework.dataflow.cfg.node.Node) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) VariableTree(com.sun.source.tree.VariableTree) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Aggregations

ClassTree (com.sun.source.tree.ClassTree)119 Tree (com.sun.source.tree.Tree)76 MethodTree (com.sun.source.tree.MethodTree)66 VariableTree (com.sun.source.tree.VariableTree)59 NewClassTree (com.sun.source.tree.NewClassTree)48 ExpressionTree (com.sun.source.tree.ExpressionTree)45 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)40 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)31 AnnotationTree (com.sun.source.tree.AnnotationTree)29 BlockTree (com.sun.source.tree.BlockTree)28 IdentifierTree (com.sun.source.tree.IdentifierTree)27 NewArrayTree (com.sun.source.tree.NewArrayTree)26 AssignmentTree (com.sun.source.tree.AssignmentTree)25 MemberSelectTree (com.sun.source.tree.MemberSelectTree)25 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)24 TypeElement (javax.lang.model.element.TypeElement)24 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)23 ArrayList (java.util.ArrayList)23 ReturnTree (com.sun.source.tree.ReturnTree)22 TreePath (com.sun.source.util.TreePath)22