Search in sources :

Example 26 with JCClassDecl

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

the class NonOverridingEquals method matchMethod.

@Override
public Description matchMethod(MethodTree methodTree, VisitorState state) {
    if (!MATCHER.matches(methodTree, state)) {
        return Description.NO_MATCH;
    }
    // this is a type-specific helper method and give advice to either inline it or rename it.
    if (enclosingClassOverridesEquals.matches(methodTree, state)) {
        return buildDescription(methodTree).setMessage(MESSAGE_BASE + "; if this is a type-specific helper for a method that does" + " override Object.equals, either inline it into the callers or rename it to" + " avoid ambiguity").build();
    }
    // Don't provide a fix if the method is static, non-public, or returns a boxed Boolean
    if (noFixMatcher.matches(methodTree, state)) {
        return describeMatch(methodTree);
    }
    JCClassDecl cls = (JCClassDecl) state.findEnclosing(ClassTree.class);
    if ((cls.getModifiers().flags & ENUM) != 0) {
        /* If the enclosing class is an enum, then just delete the equals method since enums
       * should always be compared for reference equality. Enum defines a final equals method for
       * just this reason. */
        return buildDescription(methodTree).setMessage(MESSAGE_BASE + "; enum instances can safely be compared by reference " + "equality, so please delete this").addFix(SuggestedFix.delete(methodTree)).build();
    } else {
        /* Otherwise, change the covariant equals method to override Object.equals. */
        SuggestedFix.Builder fix = SuggestedFix.builder();
        // Add @Override annotation if not present.
        if (ASTHelpers.getAnnotation(methodTree, Override.class) == null) {
            fix.prefixWith(methodTree, "@Override\n");
        }
        // Change method signature, substituting Object for parameter type.
        JCTree parameterType = (JCTree) methodTree.getParameters().get(0).getType();
        Name parameterName = ((JCVariableDecl) methodTree.getParameters().get(0)).getName();
        fix.replace(parameterType, "Object");
        // If there is a method body...
        if (methodTree.getBody() != null) {
            // Add type check at start
            String typeCheckStmt = "if (!(" + parameterName + " instanceof " + parameterType + ")) {\n" + "  return false;\n" + "}\n";
            fix.prefixWith(methodTree.getBody().getStatements().get(0), typeCheckStmt);
            // Cast all uses of the parameter name using a recursive TreeScanner.
            new CastScanner().scan(methodTree.getBody(), new CastState(parameterName, parameterType.toString(), fix));
        }
        return describeMatch(methodTree, fix.build());
    }
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ClassTree(com.sun.source.tree.ClassTree) JCTree(com.sun.tools.javac.tree.JCTree) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name)

Example 27 with JCClassDecl

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

the class JavacHandlerUtil method cloneSelfType.

public static JCExpression cloneSelfType(JavacNode childOfType) {
    JavacNode typeNode = childOfType;
    JavacTreeMaker maker = childOfType.getTreeMaker();
    while (typeNode != null && typeNode.getKind() != Kind.TYPE) typeNode = typeNode.up();
    if (typeNode != null && typeNode.get() instanceof JCClassDecl) {
        JCClassDecl type = (JCClassDecl) typeNode.get();
        ListBuffer<JCExpression> typeArgs = new ListBuffer<JCExpression>();
        if (!type.typarams.isEmpty()) {
            for (JCTypeParameter tp : type.typarams) {
                typeArgs.append(maker.Ident(tp.name));
            }
            return maker.TypeApply(maker.Ident(type.name), typeArgs.toList());
        } else {
            return maker.Ident(type.name);
        }
    } else {
        return null;
    }
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) ListBuffer(com.sun.tools.javac.util.ListBuffer)

Example 28 with JCClassDecl

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

the class JavacHandlerUtil method isClassAndDoesNotHaveFlags.

public static boolean isClassAndDoesNotHaveFlags(JavacNode typeNode, int flags) {
    JCClassDecl typeDecl = null;
    if (typeNode.get() instanceof JCClassDecl)
        typeDecl = (JCClassDecl) typeNode.get();
    else
        return false;
    long typeDeclflags = typeDecl == null ? 0 : typeDecl.mods.flags;
    return (typeDeclflags & flags) == 0;
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl)

Example 29 with JCClassDecl

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

the class HandleUtilityClass method changeModifiersAndGenerateConstructor.

private void changeModifiersAndGenerateConstructor(JavacNode typeNode, JavacNode errorNode) {
    JCClassDecl classDecl = (JCClassDecl) typeNode.get();
    boolean makeConstructor = true;
    classDecl.mods.flags |= Flags.FINAL;
    boolean markStatic = true;
    if (typeNode.up().getKind() == Kind.COMPILATION_UNIT)
        markStatic = false;
    if (markStatic && typeNode.up().getKind() == Kind.TYPE) {
        JCClassDecl typeDecl = (JCClassDecl) typeNode.up().get();
        if ((typeDecl.mods.flags & Flags.INTERFACE) != 0)
            markStatic = false;
    }
    if (markStatic)
        classDecl.mods.flags |= Flags.STATIC;
    for (JavacNode element : typeNode.down()) {
        if (element.getKind() == Kind.FIELD) {
            JCVariableDecl fieldDecl = (JCVariableDecl) element.get();
            fieldDecl.mods.flags |= Flags.STATIC;
        } else if (element.getKind() == Kind.METHOD) {
            JCMethodDecl methodDecl = (JCMethodDecl) element.get();
            if (methodDecl.name.contentEquals("<init>")) {
                if (getGeneratedBy(methodDecl) == null && (methodDecl.mods.flags & Flags.GENERATEDCONSTR) == 0) {
                    element.addError("@UtilityClasses cannot have declared constructors.");
                    makeConstructor = false;
                    continue;
                }
            }
            methodDecl.mods.flags |= Flags.STATIC;
        } else if (element.getKind() == Kind.TYPE) {
            JCClassDecl innerClassDecl = (JCClassDecl) element.get();
            innerClassDecl.mods.flags |= Flags.STATIC;
        }
    }
    if (makeConstructor)
        createPrivateDefaultConstructor(typeNode);
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacNode(lombok.javac.JavacNode) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 30 with JCClassDecl

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

the class JavacHandlerUtil method createFieldAccessor.

static JCExpression createFieldAccessor(JavacTreeMaker maker, JavacNode field, FieldAccess fieldAccess, JCExpression receiver) {
    boolean lookForGetter = lookForGetter(field, fieldAccess);
    GetterMethod getter = lookForGetter ? findGetter(field) : null;
    JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
    if (getter == null) {
        if (receiver == null) {
            if ((fieldDecl.mods.flags & Flags.STATIC) == 0) {
                receiver = maker.Ident(field.toName("this"));
            } else {
                JavacNode containerNode = field.up();
                if (containerNode != null && containerNode.get() instanceof JCClassDecl) {
                    JCClassDecl container = (JCClassDecl) field.up().get();
                    receiver = maker.Ident(container.name);
                }
            }
        }
        return receiver == null ? maker.Ident(fieldDecl.name) : maker.Select(receiver, fieldDecl.name);
    }
    if (receiver == null)
        receiver = maker.Ident(field.toName("this"));
    JCMethodInvocation call = maker.Apply(List.<JCExpression>nil(), maker.Select(receiver, getter.name), List.<JCExpression>nil());
    return call;
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacNode(lombok.javac.JavacNode) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Aggregations

JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)46 JavacNode (lombok.javac.JavacNode)24 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)23 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)14 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)11 ListBuffer (com.sun.tools.javac.util.ListBuffer)11 JCTree (com.sun.tools.javac.tree.JCTree)10 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)9 JavacTreeMaker (lombok.javac.JavacTreeMaker)9 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)6 Name (com.sun.tools.javac.util.Name)6 Type (com.sun.tools.javac.code.Type)5 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)5 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)4 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)4 JCIdent (com.sun.tools.javac.tree.JCTree.JCIdent)4 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)4 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)3 Type (com.redhat.ceylon.model.typechecker.model.Type)2 Tree (com.sun.source.tree.Tree)2