Search in sources :

Example 31 with JavacTreeMaker

use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.

the class HandleBuilder method makeSimpleSetterMethodForBuilder.

private void makeSimpleSetterMethodForBuilder(JavacNode builderType, JavacNode fieldNode, JavacNode source, boolean fluent, boolean chain) {
    Name fieldName = ((JCVariableDecl) fieldNode.get()).name;
    for (JavacNode child : builderType.down()) {
        if (child.getKind() != Kind.METHOD)
            continue;
        JCMethodDecl methodDecl = (JCMethodDecl) child.get();
        Name existingName = methodDecl.name;
        if (existingName.equals(fieldName) && !isTolerate(fieldNode, methodDecl))
            return;
    }
    String setterName = fluent ? fieldNode.getName() : HandlerUtil.buildAccessorName("set", fieldNode.getName());
    JavacTreeMaker maker = fieldNode.getTreeMaker();
    JCMethodDecl newMethod = HandleSetter.createSetter(Flags.PUBLIC, fieldNode, maker, setterName, chain, source, List.<JCAnnotation>nil(), List.<JCAnnotation>nil());
    injectMethod(builderType, newMethod);
}
Also used : JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacTreeMaker(lombok.javac.JavacTreeMaker) JavacNode(lombok.javac.JavacNode) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name)

Example 32 with JavacTreeMaker

use of lombok.javac.JavacTreeMaker 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 JavacTreeMaker

use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.

the class HandleBuilder method generateBuilderMethod.

public JCMethodDecl generateBuilderMethod(boolean isStatic, String builderMethodName, String builderClassName, JavacNode source, JavacNode type, List<JCTypeParameter> typeParams) {
    JavacTreeMaker maker = type.getTreeMaker();
    ListBuffer<JCExpression> typeArgs = new ListBuffer<JCExpression>();
    for (JCTypeParameter typeParam : typeParams) {
        typeArgs.append(maker.Ident(typeParam.name));
    }
    JCExpression call = maker.NewClass(null, List.<JCExpression>nil(), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), List.<JCExpression>nil(), null);
    JCStatement statement = maker.Return(call);
    JCBlock body = maker.Block(0, List.<JCStatement>of(statement));
    int modifiers = Flags.PUBLIC;
    if (isStatic)
        modifiers |= Flags.STATIC;
    return maker.MethodDef(maker.Modifiers(modifiers), type.toName(builderMethodName), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), copyTypeParams(source, typeParams), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null);
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 34 with JavacTreeMaker

use of lombok.javac.JavacTreeMaker 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 35 with JavacTreeMaker

use of lombok.javac.JavacTreeMaker in project lombok by rzwitserloot.

the class JavacGuavaSingularizer method generateMethods.

@Override
public void generateMethods(SingularData data, JavacNode builderType, JCTree source, boolean fluent, boolean chain) {
    JavacTreeMaker maker = builderType.getTreeMaker();
    Symtab symbolTable = builderType.getSymbolTable();
    JCExpression returnType = chain ? cloneSelfType(builderType) : maker.Type(createVoidType(symbolTable, CTC_VOID));
    JCStatement returnStatement = chain ? maker.Return(maker.Ident(builderType.toName("this"))) : null;
    generateSingularMethod(maker, returnType, returnStatement, data, builderType, source, fluent);
    returnType = chain ? cloneSelfType(builderType) : maker.Type(createVoidType(symbolTable, CTC_VOID));
    returnStatement = chain ? maker.Return(maker.Ident(builderType.toName("this"))) : null;
    generatePluralMethod(maker, returnType, returnStatement, data, builderType, source, fluent);
    returnType = chain ? cloneSelfType(builderType) : maker.Type(createVoidType(symbolTable, CTC_VOID));
    returnStatement = chain ? maker.Return(maker.Ident(builderType.toName("this"))) : null;
    generateClearMethod(maker, returnType, returnStatement, data, builderType, source);
}
Also used : Symtab(com.sun.tools.javac.code.Symtab) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Aggregations

JavacTreeMaker (lombok.javac.JavacTreeMaker)39 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)33 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)18 ListBuffer (com.sun.tools.javac.util.ListBuffer)17 Name (com.sun.tools.javac.util.Name)17 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)16 JavacNode (lombok.javac.JavacNode)15 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)13 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)13 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)10 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)9 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)8 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)5 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)5 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)5 ArrayList (java.util.ArrayList)5 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)4 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)4 Context (com.sun.tools.javac.util.Context)4 List (com.sun.tools.javac.util.List)4