Search in sources :

Example 26 with MethodTree

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

the class GuiEffectVisitor method visitReturn.

// Returning a lambda expression also triggers inference based on the method's return type.
@Override
public Void visitReturn(ReturnTree node, Void p) {
    Void result = super.visitReturn(node, p);
    if (node.getExpression() != null && node.getExpression().getKind() == Tree.Kind.LAMBDA_EXPRESSION) {
        // Unfortunatelly, need to duplicate a fair bit of BaseTypeVisitor.visitReturn after lambdas have been
        // inferred.
        Pair<Tree, AnnotatedTypeMirror> preAssCtxt = visitorState.getAssignmentContext();
        try {
            Tree enclosing = TreeUtils.enclosingMethodOrLambda(getCurrentPath());
            AnnotatedTypeMirror ret = null;
            if (enclosing.getKind() == Tree.Kind.METHOD) {
                MethodTree enclosingMethod = (MethodTree) enclosing;
                boolean valid = validateTypeOf(enclosing);
                if (valid) {
                    ret = atypeFactory.getMethodReturnType(enclosingMethod, node);
                }
            } else {
                ret = atypeFactory.getFnInterfaceFromTree((LambdaExpressionTree) enclosing).second.getReturnType();
            }
            if (ret != null) {
                visitorState.setAssignmentContext(Pair.of((Tree) node, ret));
                lambdaAssignmentCheck(ret, (LambdaExpressionTree) node.getExpression(), "return.type.incompatible");
            }
        } finally {
            visitorState.setAssignmentContext(preAssCtxt);
        }
    }
    return result;
}
Also used : LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) MethodTree(com.sun.source.tree.MethodTree) ReturnTree(com.sun.source.tree.ReturnTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 27 with MethodTree

use of com.sun.source.tree.MethodTree 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 28 with MethodTree

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

the class NewClassWriter method getInlineFunctionDeclaration.

/**
 * check by {@link org.stjs.generator.check.expression.NewClassInlineFunctionCheck} generate the code for inline
 * functions:
 *
 * <pre>
 * new FunctionInterface(){
 * 	  public void $invoke(args){
 *    }
 * }
 * </pre>
 *
 * is transformed in
 *
 * <pre>
 * function(args){
 * }
 * </pre>
 */
private JS getInlineFunctionDeclaration(WriterVisitor<JS> visitor, TreeWrapper<NewClassTree, JS> tw) {
    // special construction for inline function definition
    if (!tw.child(tw.getTree().getIdentifier()).isJavaScriptFunction()) {
        return null;
    }
    // the check verifies the existence of a single method (first is the generated
    // constructor)
    Tree method = tw.getTree().getClassBody().getMembers().get(1);
    JS func = visitor.scan(method, tw.getContext());
    int specialThisParamPos = MethodWriter.getTHISParamPos(((MethodTree) method).getParameters());
    // accessOuterScope(visitor, tree, context) ||
    if (specialThisParamPos >= 0) {
        // bind for inline functions accessing the outher scope or with special this
        JavaScriptBuilder<JS> js = tw.getContext().js();
        JS target = js.keyword(Keyword.THIS);
        JS stjsBind = js.property(js.name("stjs"), "bind");
        return js.functionCall(stjsBind, Arrays.asList(target, func, js.number(specialThisParamPos)));
    }
    return func;
}
Also used : MethodTree(com.sun.source.tree.MethodTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NewClassTree(com.sun.source.tree.NewClassTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) BlockTree(com.sun.source.tree.BlockTree) StatementTree(com.sun.source.tree.StatementTree)

Example 29 with MethodTree

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

the class MethodOverloadCheck method visit.

/**
 * {@inheritDoc}
 */
@Override
public Void visit(CheckVisitor visitor, MethodTree tree, GenerationContext<Void> context) {
    ExecutableElement methodElement = TreeUtils.elementFromDeclaration(tree);
    if (JavaNodes.isNative(methodElement)) {
        // no need to check the native ones - only the one with the body
        return null;
    }
    TreeWrapper<Tree, Void> tw = context.getCurrentWrapper();
    if (MemberWriters.shouldSkip(tw)) {
        return null;
    }
    boolean hasVarArgs = tree.getParameters().size() == 1 && InternalUtils.isVarArg(tree.getParameters().get(0));
    TypeElement typeElement = (TypeElement) methodElement.getEnclosingElement();
    // for constructors take only the class's other constructors. For regular methods, checks agains all the methods
    // in the class' hierarchy
    List<? extends Element> allMembers = methodElement.getKind() == ElementKind.CONSTRUCTOR ? typeElement.getEnclosedElements() : context.getElements().getAllMembers(typeElement);
    for (Element memberElement : allMembers) {
        if (memberElement.getAnnotation(ServerSide.class) == null) {
            checkMember(memberElement, methodElement, tree, context, hasVarArgs);
        }
    }
    return null;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ServerSide(org.stjs.javascript.annotation.ServerSide) ExecutableElement(javax.lang.model.element.ExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) MethodTree(com.sun.source.tree.MethodTree) Tree(com.sun.source.tree.Tree)

Example 30 with MethodTree

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

the class ASTHelpersTest method testHasDirectAnnotationWithSimpleName.

@Test
public void testHasDirectAnnotationWithSimpleName() {
    writeFile(// 
    "A.java", "public class A {", "  @Deprecated public void doIt() {}", "}");
    TestScanner scanner = new TestScanner() {

        @Override
        public Void visitMethod(MethodTree tree, VisitorState state) {
            if (tree.getName().contentEquals("doIt")) {
                setAssertionsComplete();
                Symbol sym = ASTHelpers.getSymbol(tree);
                assertThat(ASTHelpers.hasDirectAnnotationWithSimpleName(sym, "Deprecated")).isTrue();
                assertThat(ASTHelpers.hasDirectAnnotationWithSimpleName(sym, "Nullable")).isFalse();
            }
            return super.visitMethod(tree, state);
        }
    };
    tests.add(scanner);
    assertCompiles(scanner);
}
Also used : MethodTree(com.sun.source.tree.MethodTree) VisitorState(com.google.errorprone.VisitorState) Symbol(com.sun.tools.javac.code.Symbol) CompilerBasedAbstractTest(com.google.errorprone.matchers.CompilerBasedAbstractTest) Test(org.junit.Test)

Aggregations

MethodTree (com.sun.source.tree.MethodTree)91 Tree (com.sun.source.tree.Tree)46 ClassTree (com.sun.source.tree.ClassTree)45 VariableTree (com.sun.source.tree.VariableTree)39 ExpressionTree (com.sun.source.tree.ExpressionTree)36 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)28 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)22 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)20 TreePath (com.sun.source.util.TreePath)20 IdentifierTree (com.sun.source.tree.IdentifierTree)19 NewClassTree (com.sun.source.tree.NewClassTree)19 ReturnTree (com.sun.source.tree.ReturnTree)18 ExecutableElement (javax.lang.model.element.ExecutableElement)17 Symbol (com.sun.tools.javac.code.Symbol)16 ArrayList (java.util.ArrayList)15 AssignmentTree (com.sun.source.tree.AssignmentTree)14 BlockTree (com.sun.source.tree.BlockTree)14 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)14 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)14 MemberSelectTree (com.sun.source.tree.MemberSelectTree)14