Search in sources :

Example 66 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer in project lombok by rzwitserloot.

the class HandleConstructor method createConstructor.

public static JCMethodDecl createConstructor(AccessLevel level, List<JCAnnotation> onConstructor, JavacNode typeNode, List<JavacNode> fields, boolean allToDefault, JavacNode source) {
    JavacTreeMaker maker = typeNode.getTreeMaker();
    boolean isEnum = (((JCClassDecl) typeNode.get()).mods.flags & Flags.ENUM) != 0;
    if (isEnum)
        level = AccessLevel.PRIVATE;
    boolean suppressConstructorProperties;
    if (fields.isEmpty()) {
        suppressConstructorProperties = false;
    } else {
        suppressConstructorProperties = Boolean.TRUE.equals(typeNode.getAst().readConfiguration(ConfigurationKeys.ANY_CONSTRUCTOR_SUPPRESS_CONSTRUCTOR_PROPERTIES));
    }
    ListBuffer<JCStatement> nullChecks = new ListBuffer<JCStatement>();
    ListBuffer<JCStatement> assigns = new ListBuffer<JCStatement>();
    ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
    for (JavacNode fieldNode : fields) {
        JCVariableDecl field = (JCVariableDecl) fieldNode.get();
        Name fieldName = removePrefixFromField(fieldNode);
        Name rawName = field.name;
        List<JCAnnotation> nonNulls = findAnnotations(fieldNode, NON_NULL_PATTERN);
        if (!allToDefault) {
            List<JCAnnotation> nullables = findAnnotations(fieldNode, NULLABLE_PATTERN);
            long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext());
            JCVariableDecl param = maker.VarDef(maker.Modifiers(flags, nonNulls.appendList(nullables)), fieldName, field.vartype, null);
            params.append(param);
            if (!nonNulls.isEmpty()) {
                JCStatement nullCheck = generateNullCheck(maker, fieldNode, source);
                if (nullCheck != null)
                    nullChecks.append(nullCheck);
            }
        }
        JCFieldAccess thisX = maker.Select(maker.Ident(fieldNode.toName("this")), rawName);
        JCExpression assign = maker.Assign(thisX, allToDefault ? getDefaultExpr(maker, field.vartype) : maker.Ident(fieldName));
        assigns.append(maker.Exec(assign));
    }
    JCModifiers mods = maker.Modifiers(toJavacModifier(level), List.<JCAnnotation>nil());
    if (!allToDefault && !suppressConstructorProperties && level != AccessLevel.PRIVATE && level != AccessLevel.PACKAGE && !isLocalType(typeNode) && LombokOptionsFactory.getDelombokOptions(typeNode.getContext()).getFormatPreferences().generateConstructorProperties()) {
        addConstructorProperties(mods, typeNode, fields);
    }
    if (onConstructor != null)
        mods.annotations = mods.annotations.appendList(copyAnnotations(onConstructor));
    return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName("<init>"), null, List.<JCTypeParameter>nil(), params.toList(), List.<JCExpression>nil(), maker.Block(0L, nullChecks.appendList(assigns).toList()), null), source.get(), typeNode.getContext());
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) 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) JavacNode(lombok.javac.JavacNode) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 67 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer in project lombok by rzwitserloot.

the class HandleConstructor method findAllFields.

public static List<JavacNode> findAllFields(JavacNode typeNode) {
    ListBuffer<JavacNode> fields = new ListBuffer<JavacNode>();
    for (JavacNode child : typeNode.down()) {
        if (child.getKind() != Kind.FIELD)
            continue;
        JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
        //Skip fields that start with $
        if (fieldDecl.name.toString().startsWith("$"))
            continue;
        long fieldFlags = fieldDecl.mods.flags;
        //Skip static fields.
        if ((fieldFlags & Flags.STATIC) != 0)
            continue;
        //Skip initialized final fields
        boolean isFinal = (fieldFlags & Flags.FINAL) != 0;
        if (!isFinal || fieldDecl.init == null)
            fields.append(child);
    }
    return fields.toList();
}
Also used : JavacNode(lombok.javac.JavacNode) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 68 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer 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 69 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer in project lombok by rzwitserloot.

the class HandleConstructor method findFields.

public static List<JavacNode> findFields(JavacNode typeNode, boolean nullMarked) {
    ListBuffer<JavacNode> fields = new ListBuffer<JavacNode>();
    for (JavacNode child : typeNode.down()) {
        if (child.getKind() != Kind.FIELD)
            continue;
        JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
        //Skip fields that start with $
        if (fieldDecl.name.toString().startsWith("$"))
            continue;
        long fieldFlags = fieldDecl.mods.flags;
        //Skip static fields.
        if ((fieldFlags & Flags.STATIC) != 0)
            continue;
        boolean isFinal = (fieldFlags & Flags.FINAL) != 0;
        boolean isNonNull = nullMarked && !findAnnotations(child, NON_NULL_PATTERN).isEmpty();
        if ((isFinal || isNonNull) && fieldDecl.init == null)
            fields.append(child);
    }
    return fields.toList();
}
Also used : JavacNode(lombok.javac.JavacNode) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 70 with ListBuffer

use of com.sun.tools.javac.util.ListBuffer in project lombok by rzwitserloot.

the class JavacJavaUtilListSetSingularizer method generatePluralMethod.

void generatePluralMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) {
    List<JCTypeParameter> typeParams = List.nil();
    List<JCExpression> thrown = List.nil();
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
    ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
    statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, false, source));
    JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), "addAll");
    JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, List.<JCExpression>of(maker.Ident(data.getPluralName())));
    statements.append(maker.Exec(invokeAdd));
    if (returnStatement != null)
        statements.append(returnStatement);
    JCBlock body = maker.Block(0, statements.toList());
    Name name = data.getPluralName();
    long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
    if (!fluent)
        name = builderType.toName(HandlerUtil.buildAccessorName("addAll", name.toString()));
    JCExpression paramType = chainDots(builderType, "java", "util", "Collection");
    paramType = addTypeArgs(1, true, builderType, paramType, data.getTypeArgs(), source);
    JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getPluralName(), paramType, null);
    JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), thrown, body, null);
    injectMethod(builderType, method);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) 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) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers)

Aggregations

ListBuffer (com.sun.tools.javac.util.ListBuffer)88 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)54 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)25 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)25 JCTree (com.sun.tools.javac.tree.JCTree)22 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)22 Name (com.sun.tools.javac.util.Name)19 JavacNode (lombok.javac.JavacNode)18 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)17 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)17 JavacTreeMaker (lombok.javac.JavacTreeMaker)17 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)14 Type (com.redhat.ceylon.model.typechecker.model.Type)12 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)12 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)11 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)8 List (com.sun.tools.javac.util.List)7 ArrayList (java.util.ArrayList)7 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)6 ModelUtil.appliedType (com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType)6