Search in sources :

Example 6 with JCFieldAccess

use of com.sun.tools.javac.tree.JCTree.JCFieldAccess in project lombok by rzwitserloot.

the class HandleLog method processAnnotation.

public static void processAnnotation(LoggingFramework framework, AnnotationValues<?> annotation, JavacNode annotationNode, String loggerTopic) {
    deleteAnnotationIfNeccessary(annotationNode, framework.getAnnotationClass());
    JavacNode typeNode = annotationNode.up();
    switch(typeNode.getKind()) {
        case TYPE:
            String logFieldName = annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_ANY_FIELD_NAME);
            if (logFieldName == null)
                logFieldName = "log";
            boolean useStatic = !Boolean.FALSE.equals(annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_ANY_FIELD_IS_STATIC));
            if ((((JCClassDecl) typeNode.get()).mods.flags & Flags.INTERFACE) != 0) {
                annotationNode.addError("@Log is legal only on classes and enums.");
                return;
            }
            if (fieldExists(logFieldName, typeNode) != MemberExistsResult.NOT_EXISTS) {
                annotationNode.addWarning("Field '" + logFieldName + "' already exists.");
                return;
            }
            JCFieldAccess loggingType = selfType(typeNode);
            createField(framework, typeNode, loggingType, annotationNode.get(), logFieldName, useStatic, loggerTopic);
            break;
        default:
            annotationNode.addError("@Log is legal only on types.");
            break;
    }
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacNode(lombok.javac.JavacNode) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess)

Example 7 with JCFieldAccess

use of com.sun.tools.javac.tree.JCTree.JCFieldAccess in project lombok by rzwitserloot.

the class PrettyPrinter method visitApply.

@Override
public void visitApply(JCMethodInvocation tree) {
    if (tree.typeargs.nonEmpty()) {
        if (tree.meth instanceof JCFieldAccess) {
            JCFieldAccess fa = (JCFieldAccess) tree.meth;
            print(fa.selected);
            print(".<");
            print(tree.typeargs, ", ");
            print(">");
            print(fa.name);
        } else {
            print("<");
            print(tree.typeargs, ", ");
            print(">");
            print(tree.meth);
        }
    } else {
        print(tree.meth);
    }
    print("(");
    print(tree.args, ", ");
    print(")");
}
Also used : JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess)

Example 8 with JCFieldAccess

use of com.sun.tools.javac.tree.JCTree.JCFieldAccess in project error-prone by google.

the class ProtoFieldNullComparison method isGetListMethodInvocation.

private static boolean isGetListMethodInvocation(ExpressionTree tree, VisitorState state) {
    if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) {
        MethodInvocationTree method = (MethodInvocationTree) tree;
        if (!method.getArguments().isEmpty()) {
            return false;
        }
        if (!returnsListMatcher.matches(method, state)) {
            return false;
        }
        ExpressionTree expressionTree = method.getMethodSelect();
        if (expressionTree instanceof JCFieldAccess) {
            JCFieldAccess access = (JCFieldAccess) expressionTree;
            String methodName = access.sym.getQualifiedName().toString();
            return isFieldGetMethod(methodName);
        }
        return true;
    }
    return false;
}
Also used : JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 9 with JCFieldAccess

use of com.sun.tools.javac.tree.JCTree.JCFieldAccess in project error-prone by google.

the class ProtoFieldNullComparison method isGetMethodInvocation.

private static boolean isGetMethodInvocation(ExpressionTree tree, VisitorState state) {
    if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) {
        MethodInvocationTree method = (MethodInvocationTree) tree;
        if (!method.getArguments().isEmpty()) {
            return false;
        }
        if (returnsListMatcher.matches(method, state)) {
            return false;
        }
        ExpressionTree expressionTree = method.getMethodSelect();
        if (expressionTree instanceof JCFieldAccess) {
            JCFieldAccess access = (JCFieldAccess) expressionTree;
            String methodName = access.sym.getQualifiedName().toString();
            return isFieldGetMethod(methodName);
        }
        return true;
    }
    return false;
}
Also used : JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 10 with JCFieldAccess

use of com.sun.tools.javac.tree.JCTree.JCFieldAccess in project error-prone by google.

the class ASTHelpers method getRootAssignable.

/**
   * Find the root assignable expression of a chain of field accesses.  If there is no root
   * (i.e, a bare method call or a static method call), return null.
   *
   * <p>Examples:
   * <pre>
   * {@code
   *    a.trim().intern() ==> a
   *    a.b.trim().intern() ==> a.b
   *    this.intValue.foo() ==> this.intValue
   *    this.foo() ==> this
   *    intern() ==> null
   *    String.format() ==> null
   *    java.lang.String.format() ==> null
   * }
   * </pre>
   */
public static ExpressionTree getRootAssignable(MethodInvocationTree methodInvocationTree) {
    if (!(methodInvocationTree instanceof JCMethodInvocation)) {
        throw new IllegalArgumentException("Expected type to be JCMethodInvocation, but was " + methodInvocationTree.getClass());
    }
    // Check for bare method call, e.g. intern().
    if (((JCMethodInvocation) methodInvocationTree).getMethodSelect() instanceof JCIdent) {
        return null;
    }
    // Unwrap the field accesses until you get to an identifier.
    ExpressionTree expr = methodInvocationTree;
    while (expr instanceof JCMethodInvocation) {
        expr = ((JCMethodInvocation) expr).getMethodSelect();
        if (expr instanceof JCFieldAccess) {
            expr = ((JCFieldAccess) expr).getExpression();
        }
    }
    // We only want assignable identifiers.
    Symbol sym = getSymbol(expr);
    if (sym instanceof VarSymbol) {
        return expr;
    }
    return null;
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ExpressionTree(com.sun.source.tree.ExpressionTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Aggregations

JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)35 JCTree (com.sun.tools.javac.tree.JCTree)13 ExpressionTree (com.sun.source.tree.ExpressionTree)12 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)11 JCIdent (com.sun.tools.javac.tree.JCTree.JCIdent)11 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)8 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)7 Type (com.sun.tools.javac.code.Type)6 ListBuffer (com.sun.tools.javac.util.ListBuffer)6 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)5 JCImport (com.sun.tools.javac.tree.JCTree.JCImport)5 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)5 Name (com.sun.tools.javac.util.Name)5 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)4 Symbol (com.sun.tools.javac.code.Symbol)4 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)4 JCTypeApply (com.sun.tools.javac.tree.JCTree.JCTypeApply)4 JavacTreeMaker (lombok.javac.JavacTreeMaker)4 Fix (com.google.errorprone.fixes.Fix)3 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)3