Search in sources :

Example 71 with ClassTree

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

the class IdentifierGlobalScopeNameClashCheck method findVariablesInMethod.

private static void findVariablesInMethod(final String name, final GenerationContext<Void> context) {
    MethodTree enclosingMethod = getEnclosingMethod(context.getCurrentPath());
    if (enclosingMethod == null) {
        // don't see a reason why!?
        return;
    }
    enclosingMethod.accept(new TreeScanner<Void, Void>() {

        private boolean checkStopped;

        @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 visitVariable(VariableTree var, Void arg1) {
            if (!checkStopped && var.getName().toString().equals(name)) {
                context.addError(var, "A variable with the same name as your global variable is already defined in this method's scope. " + "Please rename either the local variable/parameter or the global variable.");
            }
            return super.visitVariable(var, arg1);
        }
    }, null);
}
Also used : MethodTree(com.sun.source.tree.MethodTree) ClassTree(com.sun.source.tree.ClassTree) VariableTree(com.sun.source.tree.VariableTree)

Example 72 with ClassTree

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

the class ClassWriter method getMembers.

/**
 * @return the JavaScript node for the class' members
 */
private JS getMembers(WriterVisitor<JS> visitor, ClassTree clazz, GenerationContext<JS> context) {
    // the following members must not appear in the initializer function:
    // - constructors (they are printed elsewhere)
    // - abstract methods (they should be omitted)
    List<Tree> nonConstructors = getAllMembersExceptConstructors(clazz);
    if (nonConstructors.isEmpty()) {
        return context.js().keyword(Keyword.NULL);
    }
    @SuppressWarnings("unchecked") List<JS> params = Arrays.asList(context.js().name(JavascriptKeywords.CONSTRUCTOR), context.js().name(JavascriptKeywords.PROTOTYPE));
    List<JS> stmts = new ArrayList<JS>();
    for (Tree member : nonConstructors) {
        stmts.add(visitor.scan(member, context));
    }
    return context.js().function(null, params, context.js().block(stmts));
}
Also used : ArrayList(java.util.ArrayList) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NewArrayTree(com.sun.source.tree.NewArrayTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) BlockTree(com.sun.source.tree.BlockTree)

Example 73 with ClassTree

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

the class ClassWriter method getSuperClass.

/**
 * @return the node to put in the super class. for intefaces, the super class goes also in the interfaces list
 */
private JS getSuperClass(ClassTree clazz, GenerationContext<JS> context) {
    Element type = TreeUtils.elementFromDeclaration(clazz);
    if (clazz.getExtendsClause() == null || type.getKind() == ElementKind.INTERFACE) {
        // no super class found
        return context.js().keyword(Keyword.NULL);
    }
    TreeWrapper<Tree, JS> superType = context.getCurrentWrapper().child(clazz.getExtendsClause());
    if (superType.isSyntheticType()) {
        return context.js().keyword(Keyword.NULL);
    }
    DependencyType depType = getDependencyTypeForClassDef(type);
    return context.js().name(superType.getTypeName(depType));
}
Also used : VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) DependencyType(org.stjs.generator.name.DependencyType) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NewArrayTree(com.sun.source.tree.NewArrayTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) BlockTree(com.sun.source.tree.BlockTree)

Example 74 with ClassTree

use of com.sun.source.tree.ClassTree in project AndroidLife by CaMnter.

the class IdScanner method visitClassDef.

@Override
public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
    for (JCTree tree : jcClassDecl.defs) {
        if (tree instanceof ClassTree) {
            ClassTree classTree = (ClassTree) tree;
            String className = classTree.getSimpleName().toString();
            if (SUPPORTED_TYPES.contains(className)) {
                ClassName rClassName = ClassName.get(rPackageName, "R", className);
                VarScanner scanner = new VarScanner(ids, rClassName, respectivePackageName);
                ((JCTree) classTree).accept(scanner);
            }
        }
    }
}
Also used : ClassTree(com.sun.source.tree.ClassTree) ClassName(com.squareup.javapoet.ClassName) JCTree(com.sun.tools.javac.tree.JCTree)

Example 75 with ClassTree

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

the class TypeFromTypeTreeVisitor method getTypeVariableFromDeclaration.

/**
 * If a tree is can be found for the declaration of the type variable {@code type}, then a {@link
 * AnnotatedTypeVariable} is returned with explicit annotations from the type variables declared
 * bounds. If a tree cannot be found, then {@code type}, converted to a use, is returned.
 *
 * @param type type variable used to find declaration tree
 * @param f annotated type factory
 * @return the AnnotatedTypeVariable from the declaration of {@code type} or {@code type} if no
 *     tree is found.
 */
private AnnotatedTypeVariable getTypeVariableFromDeclaration(AnnotatedTypeVariable type, AnnotatedTypeFactory f) {
    TypeVariable typeVar = type.getUnderlyingType();
    TypeParameterElement tpe = (TypeParameterElement) typeVar.asElement();
    Element elt = tpe.getGenericElement();
    if (elt instanceof TypeElement) {
        TypeElement typeElt = (TypeElement) elt;
        int idx = typeElt.getTypeParameters().indexOf(tpe);
        ClassTree cls = (ClassTree) f.declarationFromElement(typeElt);
        if (cls == null || cls.getTypeParameters().isEmpty()) {
            // contains all necessary information and we can return that.
            return type.asUse();
        }
        // will return a declaration ATV.  So change it to a use.
        return visitTypeParameter(cls.getTypeParameters().get(idx), f).asUse();
    } else if (elt instanceof ExecutableElement) {
        ExecutableElement exElt = (ExecutableElement) elt;
        int idx = exElt.getTypeParameters().indexOf(tpe);
        MethodTree meth = (MethodTree) f.declarationFromElement(exElt);
        if (meth == null) {
            // + elt);
            return type.asUse();
        }
        // This works the same as the case above.  Even though `meth` itself is not a
        // type declaration tree, the elements of `meth.getTypeParameters()` still are.
        AnnotatedTypeVariable result = visitTypeParameter(meth.getTypeParameters().get(idx), f).shallowCopy();
        result.setDeclaration(false);
        return result;
    } else if (TypesUtils.isCapturedTypeVariable(typeVar)) {
        // not an element at all, namely Symtab.noSymbol.
        return type.asUse();
    } else {
        throw new BugInCF("TypeFromTree.forTypeVariable: not a supported element: " + elt);
    }
}
Also used : AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeVariable(javax.lang.model.type.TypeVariable) MethodTree(com.sun.source.tree.MethodTree) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ClassTree(com.sun.source.tree.ClassTree) BugInCF(org.checkerframework.javacutil.BugInCF) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeParameterElement(javax.lang.model.element.TypeParameterElement)

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