Search in sources :

Example 66 with ClassTree

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

the class InitializationVisitor method visitBlock.

@Override
public Void visitBlock(BlockTree node, Void p) {
    // then check that all static fields have been initialized.
    if (node.isStatic()) {
        ClassTree enclosingClass = TreeUtils.enclosingClass(getCurrentPath());
        boolean isStaticInitBlock = false;
        boolean isLastStaticInitBlock = true;
        for (Tree m : enclosingClass.getMembers()) {
            if (m == node) {
                isStaticInitBlock = true;
                continue;
            }
            if (isStaticInitBlock && m.getKind() == Kind.BLOCK && ((BlockTree) m).isStatic()) {
                isLastStaticInitBlock = false;
            }
        }
        if (isLastStaticInitBlock && isStaticInitBlock) {
            boolean isStatic = true;
            Store store = atypeFactory.getRegularExitStore(node);
            // Add field values for fields with an initializer.
            for (Pair<VariableElement, Value> t : store.getAnalysis().getFieldValues()) {
                store.addInitializedField(t.first);
            }
            // Check that all static fields are initialized.
            List<AnnotationMirror> receiverAnnotations = Collections.emptyList();
            checkFieldsInitialized(node, isStatic, store, receiverAnnotations);
        }
    }
    return super.visitBlock(node, p);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) CFAbstractValue(org.checkerframework.framework.flow.CFAbstractValue) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) TypeCastTree(com.sun.source.tree.TypeCastTree) NewClassTree(com.sun.source.tree.NewClassTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) BlockTree(com.sun.source.tree.BlockTree) CFAbstractStore(org.checkerframework.framework.flow.CFAbstractStore) VariableElement(javax.lang.model.element.VariableElement)

Example 67 with ClassTree

use of com.sun.source.tree.ClassTree in project st-js by st-js.

the class LambdaExpressionWriter method accessOuterScope.

private boolean accessOuterScope(LambdaExpressionTree lambda) {
    AtomicBoolean outerScopeAccess = new AtomicBoolean(false);
    lambda.accept(new TreeScanner<Void, Void>() {

        private boolean checkStopped;

        @Override
        public Void visitIdentifier(IdentifierTree tree, Void arg1) {
            if (checkStopped) {
                return super.visitIdentifier(tree, arg1);
            }
            Element fieldElement = TreeUtils.elementFromUse(tree);
            if (IdentifierAccessOuterScopeCheck.isRegularInstanceField(fieldElement, tree) || GeneratorConstants.THIS.equals(tree.getName().toString())) {
                outerScopeAccess.set(true);
            }
            return super.visitIdentifier(tree, arg1);
        }

        @Override
        public Void visitClass(ClassTree arg0, Void arg1) {
            // stop the checks if a new type is encountered
            checkStopped = true;
            return super.visitClass(arg0, arg1);
        }

        @Override
        public Void visitMethodInvocation(MethodInvocationTree tree, Void arg1) {
            if (checkStopped) {
                return super.visitMethodInvocation(tree, arg1);
            }
            Element methodElement = TreeUtils.elementFromUse(tree);
            if (JavaNodes.isStatic(methodElement)) {
                // only instance methods
                return super.visitMethodInvocation(tree, arg1);
            }
            String name = MethodInvocationWriter.buildMethodName(tree);
            if (GeneratorConstants.THIS.equals(name) || GeneratorConstants.SUPER.equals(name)) {
                // this and super call are ok
                return super.visitMethodInvocation(tree, arg1);
            }
            if (!(tree.getMethodSelect() instanceof IdentifierTree)) {
                // check for Outer.this check
                return super.visitMethodInvocation(tree, arg1);
            }
            outerScopeAccess.set(true);
            return super.visitMethodInvocation(tree, arg1);
        }
    }, null);
    return outerScopeAccess.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) Element(javax.lang.model.element.Element) ClassTree(com.sun.source.tree.ClassTree) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 68 with ClassTree

use of com.sun.source.tree.ClassTree in project st-js by st-js.

the class ClassDuplicateMemberNameCheck method visit.

/**
 * {@inheritDoc}
 */
@Override
public Void visit(CheckVisitor visitor, ClassTree tree, GenerationContext<Void> context) {
    Multimap<String, Element> names = LinkedListMultimap.create();
    // TypeElement classElement = TreeUtils.elementFromDeclaration(tree);
    TypeElement classElement = (TypeElement) context.getTrees().getElement(context.getCurrentPath());
    TypeMirror superType = classElement.getSuperclass();
    if (superType.getKind() != TypeKind.NONE) {
        // add the names from the super class
        TypeElement superclassElement = (TypeElement) ((DeclaredType) superType).asElement();
        for (Element memberElement : context.getElements().getAllMembers(superclassElement)) {
            if (!JavaNodes.isNative(memberElement)) {
                names.put(memberElement.getSimpleName().toString(), memberElement);
            }
        }
    }
    // check first the methods
    for (Tree member : tree.getMembers()) {
        checkMethod(classElement, member, context, names);
    }
    // check the fields
    for (Tree member : tree.getMembers()) {
        checkField(member, context, names);
    }
    return null;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Example 69 with ClassTree

use of com.sun.source.tree.ClassTree in project st-js by st-js.

the class ClassForbidExtendsSyntheticTypeCheck method visit.

/**
 * {@inheritDoc}
 */
@Override
public Void visit(CheckVisitor visitor, ClassTree tree, GenerationContext<Void> context) {
    TreeWrapper<ClassTree, Void> tw = context.getCurrentWrapper();
    TypeElement element = (TypeElement) tw.getElement();
    if (element.getNestingKind() == NestingKind.ANONYMOUS) {
        return null;
    }
    if (tree.getExtendsClause() != null && tw.child(tree.getExtendsClause()).isSyntheticType()) {
        context.addError(tree, "You cannot extend from a class that is marked as synthetic (@SyntheticType)");
    }
    for (Tree iface : tree.getImplementsClause()) {
        checkInterface(tw.child(iface));
    }
    return null;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ClassTree(com.sun.source.tree.ClassTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Example 70 with ClassTree

use of com.sun.source.tree.ClassTree in project st-js by st-js.

the class IdentifierAccessOuterScopeCheck method visit.

/**
 * {@inheritDoc}
 */
@Override
public Void visit(CheckVisitor visitor, IdentifierTree tree, GenerationContext<Void> context) {
    Element fieldElement = TreeUtils.elementFromUse(tree);
    if (!isRegularInstanceField(fieldElement, tree)) {
        return null;
    }
    ClassTree enclosingClassTree = enclosingClassSkipAnonymousInitializer(context.getCurrentPath());
    TypeElement currentScopeClassElement = TreeUtils.elementFromDeclaration(enclosingClassTree);
    TypeElement fieldOwnerElement = (TypeElement) fieldElement.getEnclosingElement();
    if (isOuterType(context, fieldOwnerElement, currentScopeClassElement)) {
        context.addError(tree, "In Javascript you cannot access a field from the outer type. " + "You should define a variable var that=this outside your function definition and use the property of this object. The field: " + tree);
    }
    return null;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ClassTree(com.sun.source.tree.ClassTree)

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