Search in sources :

Example 1 with JCAssign

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

the class HandleSetter method createSetter.

public static JCMethodDecl createSetter(long access, JavacNode field, JavacTreeMaker treeMaker, String setterName, boolean shouldReturnThis, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
    if (setterName == null)
        return null;
    JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
    JCExpression fieldRef = createFieldAccessor(treeMaker, field, FieldAccess.ALWAYS_FIELD);
    JCAssign assign = treeMaker.Assign(fieldRef, treeMaker.Ident(fieldDecl.name));
    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
    List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN);
    List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN);
    Name methodName = field.toName(setterName);
    List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables);
    long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, field.getContext());
    JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(flags, annsOnParam), fieldDecl.name, fieldDecl.vartype, null);
    if (nonNulls.isEmpty()) {
        statements.append(treeMaker.Exec(assign));
    } else {
        JCStatement nullCheck = generateNullCheck(treeMaker, field, source);
        if (nullCheck != null)
            statements.append(nullCheck);
        statements.append(treeMaker.Exec(assign));
    }
    JCExpression methodType = null;
    if (shouldReturnThis) {
        methodType = cloneSelfType(field);
    }
    if (methodType == null) {
        //WARNING: Do not use field.getSymbolTable().voidType - that field has gone through non-backwards compatible API changes within javac1.6.
        methodType = treeMaker.Type(Javac.createVoidType(field.getSymbolTable(), CTC_VOID));
        shouldReturnThis = false;
    }
    if (shouldReturnThis) {
        JCReturn returnStatement = treeMaker.Return(treeMaker.Ident(field.toName("this")));
        statements.append(returnStatement);
    }
    JCBlock methodBody = treeMaker.Block(0, statements.toList());
    List<JCTypeParameter> methodGenericParams = List.nil();
    List<JCVariableDecl> parameters = List.of(param);
    List<JCExpression> throwsClauses = List.nil();
    JCExpression annotationMethodDefaultValue = null;
    List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod);
    if (isFieldDeprecated(field)) {
        annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil()));
    }
    JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source.get(), field.getContext());
    copyJavadoc(field, decl, CopyJavadoc.SETTER);
    return decl;
}
Also used : JCReturn(com.sun.tools.javac.tree.JCTree.JCReturn) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 2 with JCAssign

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

the class PrettyPrinter method visitAnnotation.

@Override
public void visitAnnotation(JCAnnotation tree) {
    print("@");
    print(tree.annotationType);
    if (tree.args.isEmpty())
        return;
    print("(");
    boolean done = false;
    if (tree.args.length() == 1 && tree.args.get(0) instanceof JCAssign) {
        JCAssign arg1 = (JCAssign) tree.args.get(0);
        JCIdent arg1Name = arg1.lhs instanceof JCIdent ? ((JCIdent) arg1.lhs) : null;
        if (arg1Name != null && arg1Name.name == name_value(arg1Name.name)) {
            print(arg1.rhs);
            done = true;
        }
    }
    if (!done)
        print(tree.args, ", ");
    print(")");
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign)

Example 3 with JCAssign

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

the class HandleSetter method createSetter.

public static JCMethodDecl createSetter(long access, boolean deprecate, JavacNode field, JavacTreeMaker treeMaker, String setterName, Name booleanFieldToSet, boolean shouldReturnThis, JavacNode source, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
    if (setterName == null)
        return null;
    JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
    JCExpression fieldRef = createFieldAccessor(treeMaker, field, FieldAccess.ALWAYS_FIELD);
    JCAssign assign = treeMaker.Assign(fieldRef, treeMaker.Ident(fieldDecl.name));
    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
    List<JCAnnotation> nonNulls = findAnnotations(field, NON_NULL_PATTERN);
    List<JCAnnotation> nullables = findAnnotations(field, NULLABLE_PATTERN);
    Name methodName = field.toName(setterName);
    List<JCAnnotation> annsOnParam = copyAnnotations(onParam).appendList(nonNulls).appendList(nullables);
    long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, field.getContext());
    JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(flags, annsOnParam), fieldDecl.name, fieldDecl.vartype, null);
    if (nonNulls.isEmpty()) {
        statements.append(treeMaker.Exec(assign));
    } else {
        JCStatement nullCheck = generateNullCheck(treeMaker, field, source);
        if (nullCheck != null)
            statements.append(nullCheck);
        statements.append(treeMaker.Exec(assign));
    }
    if (booleanFieldToSet != null) {
        JCAssign setBool = treeMaker.Assign(treeMaker.Ident(booleanFieldToSet), treeMaker.Literal(CTC_BOOLEAN, 1));
        statements.append(treeMaker.Exec(setBool));
    }
    JCExpression methodType = null;
    if (shouldReturnThis) {
        methodType = cloneSelfType(field);
    }
    if (methodType == null) {
        // WARNING: Do not use field.getSymbolTable().voidType - that field has gone through non-backwards compatible API changes within javac1.6.
        methodType = treeMaker.Type(Javac.createVoidType(field.getSymbolTable(), CTC_VOID));
        shouldReturnThis = false;
    }
    if (shouldReturnThis) {
        JCReturn returnStatement = treeMaker.Return(treeMaker.Ident(field.toName("this")));
        statements.append(returnStatement);
    }
    JCBlock methodBody = treeMaker.Block(0, statements.toList());
    List<JCTypeParameter> methodGenericParams = List.nil();
    List<JCVariableDecl> parameters = List.of(param);
    List<JCExpression> throwsClauses = List.nil();
    JCExpression annotationMethodDefaultValue = null;
    List<JCAnnotation> annsOnMethod = copyAnnotations(onMethod);
    if (isFieldDeprecated(field) || deprecate) {
        annsOnMethod = annsOnMethod.prepend(treeMaker.Annotation(genJavaLangTypeRef(field, "Deprecated"), List.<JCExpression>nil()));
    }
    JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source.get(), field.getContext());
    copyJavadoc(field, decl, CopyJavadoc.SETTER);
    return decl;
}
Also used : JCReturn(com.sun.tools.javac.tree.JCTree.JCReturn) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 4 with JCAssign

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

the class DoubleCheckedLocking method handleLocal.

/**
 * Report a diagnostic for an instance of DCL on a local variable. A match is only reported if a
 * non-volatile field is written to the variable after acquiring the lock and before the second
 * null-check on the local.
 *
 * <p>e.g.
 *
 * <pre>{@code
 * if ($X == null) {
 *   synchronized (...) {
 *     $X = myNonVolatileField;
 *     if ($X == null) {
 *       ...
 *     }
 *     ...
 *   }
 * }
 * }</pre>
 */
private Description handleLocal(DCLInfo info, VisitorState state) {
    JCExpressionStatement expr = getChild(info.synchTree().getBlock(), JCExpressionStatement.class);
    if (expr == null) {
        return Description.NO_MATCH;
    }
    if (expr.getStartPosition() > ((JCTree) info.innerIf()).getStartPosition()) {
        return Description.NO_MATCH;
    }
    if (!(expr.getExpression() instanceof JCAssign)) {
        return Description.NO_MATCH;
    }
    JCAssign assign = (JCAssign) expr.getExpression();
    if (!Objects.equals(ASTHelpers.getSymbol(assign.getVariable()), info.sym())) {
        return Description.NO_MATCH;
    }
    Symbol sym = ASTHelpers.getSymbol(assign.getExpression());
    if (!(sym instanceof VarSymbol)) {
        return Description.NO_MATCH;
    }
    VarSymbol fvar = (VarSymbol) sym;
    if (fvar.getKind() != ElementKind.FIELD) {
        return Description.NO_MATCH;
    }
    return handleField(info.outerIf(), fvar, state);
}
Also used : JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement)

Example 5 with JCAssign

use of com.sun.tools.javac.tree.JCTree.JCAssign in project com.revolsys.open by revolsys.

the class DocumentationProcessor method process.

@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
    if (this.elementUtils != null) {
        final Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Documentation.class);
        for (final Element element : elements) {
            String docComment = this.elementUtils.getDocComment(element);
            if (docComment != null) {
                docComment = docComment.trim();
                if (docComment.length() > 0) {
                    final JCModifiers modifiers = getModifiers(element);
                    for (final JCAnnotation annotation : modifiers.annotations) {
                        if (annotation.type.toString().equals(Documentation.class.getName())) {
                            boolean hasValue = false;
                            final JCLiteral commentLiteral = this.maker.Literal(docComment);
                            for (final JCExpression arg : annotation.args) {
                                if (arg instanceof JCAssign) {
                                    final JCAssign assign = (JCAssign) arg;
                                    final JCExpression lhs = assign.lhs;
                                    if (lhs instanceof JCIdent) {
                                        final JCIdent ident = (JCIdent) lhs;
                                        if (ident.name.contentEquals("value")) {
                                            assign.rhs = commentLiteral;
                                            hasValue = true;
                                        }
                                    }
                                }
                            }
                            if (!hasValue) {
                                final ClassSymbol classSymbol = (ClassSymbol) ((JCIdent) annotation.annotationType).sym;
                                MethodSymbol valueMethod = null;
                                for (final Symbol symbol : classSymbol.members_field.getElementsByName(this.names.value)) {
                                    valueMethod = (MethodSymbol) symbol;
                                }
                                final JCAssign valueArg = this.maker.Assign(this.maker.Ident(valueMethod), commentLiteral);
                                annotation.args = annotation.args.append(valueArg);
                            }
                        }
                    }
                }
            }
        }
    }
    return true;
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) Documentation(com.revolsys.open.compiler.annotation.Documentation) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCLiteral(com.sun.tools.javac.tree.JCTree.JCLiteral) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Aggregations

JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)14 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)8 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)8 JCIdent (com.sun.tools.javac.tree.JCTree.JCIdent)6 Name (com.sun.tools.javac.util.Name)5 JCTree (com.sun.tools.javac.tree.JCTree)4 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)4 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)4 JCBinary (com.sun.tools.javac.tree.JCTree.JCBinary)3 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)3 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)3 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)3 JCNewArray (com.sun.tools.javac.tree.JCTree.JCNewArray)3 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)3 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)3 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)2 AnnotationTree (com.sun.source.tree.AnnotationTree)2 ExpressionTree (com.sun.source.tree.ExpressionTree)2 Symbol (com.sun.tools.javac.code.Symbol)2 JCArrayAccess (com.sun.tools.javac.tree.JCTree.JCArrayAccess)2