Search in sources :

Example 31 with JCClassDecl

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

the class HandleSetter method createSetterForField.

public void createSetterForField(AccessLevel level, JavacNode fieldNode, JavacNode sourceNode, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
    if (fieldNode.getKind() != Kind.FIELD) {
        fieldNode.addError("@Setter is only supported on a class or a field.");
        return;
    }
    JCVariableDecl fieldDecl = (JCVariableDecl) fieldNode.get();
    String methodName = toSetterName(fieldNode);
    if (methodName == null) {
        fieldNode.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list.");
        return;
    }
    if ((fieldDecl.mods.flags & Flags.FINAL) != 0) {
        fieldNode.addWarning("Not generating setter for this field: Setters cannot be generated for final fields.");
        return;
    }
    for (String altName : toAllSetterNames(fieldNode)) {
        switch(methodExists(altName, fieldNode, false, 1)) {
            case EXISTS_BY_LOMBOK:
                return;
            case EXISTS_BY_USER:
                if (whineIfExists) {
                    String altNameExpl = "";
                    if (!altName.equals(methodName))
                        altNameExpl = String.format(" (%s)", altName);
                    fieldNode.addWarning(String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl));
                }
                return;
            default:
            case NOT_EXISTS:
        }
    }
    long access = toJavacModifier(level) | (fieldDecl.mods.flags & Flags.STATIC);
    JCMethodDecl createdSetter = createSetter(access, fieldNode, fieldNode.getTreeMaker(), sourceNode, onMethod, onParam);
    Type fieldType = getMirrorForFieldType(fieldNode);
    Type returnType;
    if (shouldReturnThis(fieldNode)) {
        ClassSymbol sym = ((JCClassDecl) fieldNode.up().get()).sym;
        returnType = sym == null ? null : sym.type;
    } else {
        returnType = Javac.createVoidType(fieldNode.getSymbolTable(), CTC_VOID);
    }
    injectMethod(fieldNode.up(), createdSetter, fieldType == null ? null : List.of(fieldType), returnType);
}
Also used : Type(com.sun.tools.javac.code.Type) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 32 with JCClassDecl

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

the class HandleLog method selfType.

public static JCFieldAccess selfType(JavacNode typeNode) {
    JavacTreeMaker maker = typeNode.getTreeMaker();
    Name name = ((JCClassDecl) typeNode.get()).name;
    return maker.Select(maker.Ident(name), typeNode.toName("class"));
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTreeMaker(lombok.javac.JavacTreeMaker) Name(com.sun.tools.javac.util.Name)

Example 33 with JCClassDecl

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

the class HandleConstructor method checkLegality.

public static boolean checkLegality(JavacNode typeNode, JavacNode errorNode, String name) {
    JCClassDecl typeDecl = null;
    if (typeNode.get() instanceof JCClassDecl)
        typeDecl = (JCClassDecl) typeNode.get();
    long modifiers = typeDecl == null ? 0 : typeDecl.mods.flags;
    boolean notAClass = (modifiers & (Flags.INTERFACE | Flags.ANNOTATION)) != 0;
    if (typeDecl == null || notAClass) {
        errorNode.addError(name + " is only supported on a class or an enum.");
        return false;
    }
    return true;
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl)

Example 34 with JCClassDecl

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

the class HandleConstructor method generateConstructor.

public void generateConstructor(JavacNode typeNode, AccessLevel level, List<JCAnnotation> onConstructor, List<JavacNode> fields, boolean allToDefault, String staticName, SkipIfConstructorExists skipIfConstructorExists, JavacNode source) {
    boolean staticConstrRequired = staticName != null && !staticName.equals("");
    if (skipIfConstructorExists != SkipIfConstructorExists.NO && constructorExists(typeNode) != MemberExistsResult.NOT_EXISTS)
        return;
    if (skipIfConstructorExists != SkipIfConstructorExists.NO) {
        for (JavacNode child : typeNode.down()) {
            if (child.getKind() == Kind.ANNOTATION) {
                boolean skipGeneration = annotationTypeMatches(NoArgsConstructor.class, child) || annotationTypeMatches(AllArgsConstructor.class, child) || annotationTypeMatches(RequiredArgsConstructor.class, child);
                if (!skipGeneration && skipIfConstructorExists == SkipIfConstructorExists.YES) {
                    skipGeneration = annotationTypeMatches(Builder.class, child);
                }
                if (skipGeneration) {
                    if (staticConstrRequired) {
                        // @Data has asked us to generate a constructor, but we're going to skip this instruction, as an explicit 'make a constructor' annotation
                        // will take care of it. However, @Data also wants a specific static name; this will be ignored; the appropriate way to do this is to use
                        // the 'staticName' parameter of the @XArgsConstructor you've stuck on your type.
                        // We should warn that we're ignoring @Data's 'staticConstructor' param.
                        source.addWarning("Ignoring static constructor name: explicit @XxxArgsConstructor annotation present; its `staticName` parameter will be used.");
                    }
                    return;
                }
            }
        }
    }
    JCMethodDecl constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, onConstructor, typeNode, fields, allToDefault, source);
    ListBuffer<Type> argTypes = new ListBuffer<Type>();
    for (JavacNode fieldNode : fields) {
        Type mirror = getMirrorForFieldType(fieldNode);
        if (mirror == null) {
            argTypes = null;
            break;
        }
        argTypes.append(mirror);
    }
    List<Type> argTypes_ = argTypes == null ? null : argTypes.toList();
    injectMethod(typeNode, constr, argTypes_, Javac.createVoidType(typeNode.getSymbolTable(), CTC_VOID));
    if (staticConstrRequired) {
        ClassSymbol sym = ((JCClassDecl) typeNode.get()).sym;
        Type returnType = sym == null ? null : sym.type;
        JCMethodDecl staticConstr = createStaticConstructor(staticName, level, typeNode, allToDefault ? List.<JavacNode>nil() : fields, source.get());
        injectMethod(typeNode, staticConstr, argTypes_, returnType);
    }
}
Also used : Type(com.sun.tools.javac.code.Type) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacNode(lombok.javac.JavacNode) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Builder(lombok.Builder) ListBuffer(com.sun.tools.javac.util.ListBuffer) RequiredArgsConstructor(lombok.RequiredArgsConstructor)

Example 35 with JCClassDecl

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

the class HandleWither method generateWitherForType.

public void generateWitherForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelWither) {
    if (checkForTypeLevelWither) {
        if (hasAnnotation(Wither.class, typeNode)) {
            // The annotation will make it happen, so we can skip it.
            return;
        }
    }
    JCClassDecl typeDecl = null;
    if (typeNode.get() instanceof JCClassDecl)
        typeDecl = (JCClassDecl) typeNode.get();
    long modifiers = typeDecl == null ? 0 : typeDecl.mods.flags;
    boolean notAClass = (modifiers & (Flags.INTERFACE | Flags.ANNOTATION | Flags.ENUM)) != 0;
    if (typeDecl == null || notAClass) {
        errorNode.addError("@Wither is only supported on a class or a field.");
        return;
    }
    for (JavacNode field : typeNode.down()) {
        if (field.getKind() != Kind.FIELD)
            continue;
        JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
        // Skip fields that start with $
        if (fieldDecl.name.toString().startsWith("$"))
            continue;
        // Skip static fields.
        if ((fieldDecl.mods.flags & Flags.STATIC) != 0)
            continue;
        // Skip final initialized fields.
        if ((fieldDecl.mods.flags & Flags.FINAL) != 0 && fieldDecl.init != null)
            continue;
        generateWitherForField(field, errorNode.get(), level);
    }
}
Also used : 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