Search in sources :

Example 26 with VariableTree

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

the class ClassWriter method getAnnotationDescription.

/**
 * build the annotation description element
 *
 * <pre>
 * $annotations : {
 * _: {....}
 * field1: {...}
 * method1: {...}
 * method1$0:  {...}
 * method1$1:  {...}...
 * }
 * </pre>
 *
 * for each annotation list you have:
 *
 * <pre>
 * {
 * "annotationType1": [expr1, expr2, expr3],
 * "annotationType2": []
 * }
 * </pre>
 */
private JS getAnnotationDescription(WriterVisitor<JS> visitor, ClassTree classTree, GenerationContext<JS> context) {
    List<NameValue<JS>> props = new ArrayList<NameValue<JS>>();
    addAnnotationsForElement("_", props, visitor, classTree.getModifiers().getAnnotations(), context);
    for (Tree member : classTree.getMembers()) {
        if (MemberWriters.shouldSkip(context.getCurrentWrapper().child(member))) {
            continue;
        }
        if (member instanceof VariableTree) {
            VariableTree field = (VariableTree) member;
            addAnnotationsForElement(field.getName().toString(), props, visitor, field.getModifiers().getAnnotations(), context);
        } else if (member instanceof MethodTree) {
            addAnnotationsForMethod((MethodTree) member, props, visitor, context);
        }
    }
    return context.js().object(props);
}
Also used : NameValue(org.stjs.generator.javascript.NameValue) MethodTree(com.sun.source.tree.MethodTree) ArrayList(java.util.ArrayList) VariableTree(com.sun.source.tree.VariableTree) 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 27 with VariableTree

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

the class ClassDuplicateMemberNameCheck method checkField.

private void checkField(Tree member, GenerationContext<Void> context, Multimap<String, Element> existingNames) {
    if (member instanceof VariableTree) {
        TreeWrapper<Tree, Void> tw = context.getCurrentWrapper().child(member);
        if (MemberWriters.shouldSkip(tw)) {
            return;
        }
        String name = ((VariableTree) member).getName().toString();
        Collection<Element> sameName = existingNames.get(name);
        if (sameName.isEmpty()) {
            Element variableElement = TreeUtils.elementFromDeclaration((VariableTree) member);
            existingNames.put(name, variableElement);
        } else {
            if (!hasOnlyFields(sameName)) {
                // accept fields with the same name, but not methods and fields
                context.addError(member, "The type (or one of its parents) contains already a method called [" + name + "]. Javascript cannot distinguish methods/fields with the same name");
            }
        }
    }
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) VariableTree(com.sun.source.tree.VariableTree) 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 28 with VariableTree

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

the class ClassEnumWithoutMembersCheck method checkMember.

private void checkMember(Tree member, GenerationContext<Void> context) {
    if (InternalUtils.isSyntheticConstructor(member)) {
        return;
    }
    boolean ok = false;
    if (member instanceof VariableTree) {
        Element memberElement = TreeUtils.elementFromDeclaration((VariableTree) member);
        ok = memberElement.getKind() == ElementKind.ENUM_CONSTANT;
    }
    if (!ok) {
        context.addError(member, "Enums with fields or methods are not supported");
    }
}
Also used : Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) VariableTree(com.sun.source.tree.VariableTree)

Example 29 with VariableTree

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

the class FieldInitializerCheck method visit.

/**
 * {@inheritDoc}
 */
@Override
public Void visit(CheckVisitor visitor, VariableTree tree, GenerationContext<Void> context) {
    Element element = TreeUtils.elementFromDeclaration(tree);
    if (element.getKind() != ElementKind.FIELD) {
        // only deals with fields
        return null;
    }
    TreeWrapper<Tree, Void> tw = context.getCurrentWrapper();
    if (MemberWriters.shouldSkip(tw)) {
        return null;
    }
    // static -> ok1
    if (JavaNodes.isStatic(element)) {
        return null;
    }
    if (checkInitializer(tree)) {
        return null;
    }
    if (!JavaNodes.isJavaScriptPrimitive(context.getTrees().getTypeMirror(context.getCurrentPath()))) {
        context.addError(tree, "Instance field inline initialization is allowed only for string and number field types");
        return null;
    }
    context.addError(tree, "Instance field inline initialization can only done with literal constants");
    return null;
}
Also used : Element(javax.lang.model.element.Element) LiteralTree(com.sun.source.tree.LiteralTree) UnaryTree(com.sun.source.tree.UnaryTree) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree)

Example 30 with VariableTree

use of com.sun.source.tree.VariableTree in project error-prone by google.

the class ASTHelpersTest method getUpperBoundScanner.

/* Tests for ASTHelpers#getUpperBound */
private TestScanner getUpperBoundScanner(final String expectedBound) {
    return new TestScanner() {

        @Override
        public Void visitVariable(VariableTree tree, VisitorState state) {
            setAssertionsComplete();
            Type varType = ASTHelpers.getType(tree.getType());
            assertThat(ASTHelpers.getUpperBound(varType.getTypeArguments().get(0), state.getTypes()).toString()).isEqualTo(expectedBound);
            return super.visitVariable(tree, state);
        }
    };
}
Also used : TargetType(com.google.errorprone.util.ASTHelpers.TargetType) Type(com.sun.tools.javac.code.Type) VisitorState(com.google.errorprone.VisitorState) VariableTree(com.sun.source.tree.VariableTree)

Aggregations

VariableTree (com.sun.source.tree.VariableTree)86 Tree (com.sun.source.tree.Tree)43 ClassTree (com.sun.source.tree.ClassTree)37 MethodTree (com.sun.source.tree.MethodTree)37 ExpressionTree (com.sun.source.tree.ExpressionTree)34 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)25 NewClassTree (com.sun.source.tree.NewClassTree)23 TreePath (com.sun.source.util.TreePath)23 IdentifierTree (com.sun.source.tree.IdentifierTree)22 JCTree (com.sun.tools.javac.tree.JCTree)21 MemberSelectTree (com.sun.source.tree.MemberSelectTree)19 AssignmentTree (com.sun.source.tree.AssignmentTree)17 ArrayList (java.util.ArrayList)17 BlockTree (com.sun.source.tree.BlockTree)16 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)13 Type (com.sun.tools.javac.code.Type)13 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)12 ReturnTree (com.sun.source.tree.ReturnTree)12 StatementTree (com.sun.source.tree.StatementTree)12 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)12