Search in sources :

Example 36 with JavacNode

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

the class HandleSetter method handle.

@Override
public void handle(AnnotationValues<Setter> annotation, JCAnnotation ast, JavacNode annotationNode) {
    handleFlagUsage(annotationNode, ConfigurationKeys.SETTER_FLAG_USAGE, "@Setter");
    Collection<JavacNode> fields = annotationNode.upFromAnnotationToFields();
    deleteAnnotationIfNeccessary(annotationNode, Setter.class);
    deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel");
    JavacNode node = annotationNode.up();
    AccessLevel level = annotation.getInstance().value();
    if (level == AccessLevel.NONE || node == null)
        return;
    List<JCAnnotation> onMethod = unboxAndRemoveAnnotationParameter(ast, "onMethod", "@Setter(onMethod", annotationNode);
    List<JCAnnotation> onParam = unboxAndRemoveAnnotationParameter(ast, "onParam", "@Setter(onParam", annotationNode);
    switch(node.getKind()) {
        case FIELD:
            createSetterForFields(level, fields, annotationNode, true, onMethod, onParam);
            break;
        case TYPE:
            if (!onMethod.isEmpty())
                annotationNode.addError("'onMethod' is not supported for @Setter on a type.");
            if (!onParam.isEmpty())
                annotationNode.addError("'onParam' is not supported for @Setter on a type.");
            generateSetterForType(node, annotationNode, level, false);
            break;
    }
}
Also used : JavacNode(lombok.javac.JavacNode) AccessLevel(lombok.AccessLevel) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 37 with JavacNode

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

the class HandleToString method createToString.

static JCMethodDecl createToString(JavacNode typeNode, Collection<JavacNode> fields, boolean includeFieldNames, boolean callSuper, FieldAccess fieldAccess, JCTree source) {
    JavacTreeMaker maker = typeNode.getTreeMaker();
    JCAnnotation overrideAnnotation = maker.Annotation(genJavaLangTypeRef(typeNode, "Override"), List.<JCExpression>nil());
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation));
    JCExpression returnType = genJavaLangTypeRef(typeNode, "String");
    boolean first = true;
    String typeName = getTypeName(typeNode);
    String infix = ", ";
    String suffix = ")";
    String prefix;
    if (callSuper) {
        prefix = typeName + "(super=";
    } else if (fields.isEmpty()) {
        prefix = typeName + "()";
    } else if (includeFieldNames) {
        prefix = typeName + "(" + ((JCVariableDecl) fields.iterator().next().get()).name.toString() + "=";
    } else {
        prefix = typeName + "(";
    }
    JCExpression current = maker.Literal(prefix);
    if (callSuper) {
        JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("toString")), List.<JCExpression>nil());
        current = maker.Binary(CTC_PLUS, current, callToSuper);
        first = false;
    }
    for (JavacNode fieldNode : fields) {
        JCExpression expr;
        JCExpression fieldAccessor = createFieldAccessor(maker, fieldNode, fieldAccess);
        JCExpression fieldType = getFieldType(fieldNode, fieldAccess);
        // The distinction between primitive and object will be useful if we ever add a 'hideNulls' option.
        boolean fieldIsPrimitive = fieldType instanceof JCPrimitiveTypeTree;
        boolean fieldIsPrimitiveArray = fieldType instanceof JCArrayTypeTree && ((JCArrayTypeTree) fieldType).elemtype instanceof JCPrimitiveTypeTree;
        boolean fieldIsObjectArray = !fieldIsPrimitiveArray && fieldType instanceof JCArrayTypeTree;
        @SuppressWarnings("unused") boolean fieldIsObject = !fieldIsPrimitive && !fieldIsPrimitiveArray && !fieldIsObjectArray;
        if (fieldIsPrimitiveArray || fieldIsObjectArray) {
            JCExpression tsMethod = chainDots(typeNode, "java", "util", "Arrays", fieldIsObjectArray ? "deepToString" : "toString");
            expr = maker.Apply(List.<JCExpression>nil(), tsMethod, List.<JCExpression>of(fieldAccessor));
        } else
            expr = fieldAccessor;
        if (first) {
            current = maker.Binary(CTC_PLUS, current, expr);
            first = false;
            continue;
        }
        if (includeFieldNames) {
            current = maker.Binary(CTC_PLUS, current, maker.Literal(infix + fieldNode.getName() + "="));
        } else {
            current = maker.Binary(CTC_PLUS, current, maker.Literal(infix));
        }
        current = maker.Binary(CTC_PLUS, current, expr);
    }
    if (!first)
        current = maker.Binary(CTC_PLUS, current, maker.Literal(suffix));
    JCStatement returnStatement = maker.Return(current);
    JCBlock body = maker.Block(0, List.of(returnStatement));
    return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName("toString"), returnType, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null), source, typeNode.getContext());
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) ToString(lombok.ToString) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) 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) JCArrayTypeTree(com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)

Example 38 with JavacNode

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

the class HandleToString method handle.

@Override
public void handle(AnnotationValues<ToString> annotation, JCAnnotation ast, JavacNode annotationNode) {
    handleFlagUsage(annotationNode, ConfigurationKeys.TO_STRING_FLAG_USAGE, "@ToString");
    deleteAnnotationIfNeccessary(annotationNode, ToString.class);
    ToString ann = annotation.getInstance();
    List<String> excludes = List.from(ann.exclude());
    List<String> includes = List.from(ann.of());
    JavacNode typeNode = annotationNode.up();
    checkForBogusFieldNames(typeNode, annotation);
    Boolean callSuper = ann.callSuper();
    if (!annotation.isExplicit("callSuper"))
        callSuper = null;
    if (!annotation.isExplicit("exclude"))
        excludes = null;
    if (!annotation.isExplicit("of"))
        includes = null;
    if (excludes != null && includes != null) {
        excludes = null;
        annotation.setWarning("exclude", "exclude and of are mutually exclusive; the 'exclude' parameter will be ignored.");
    }
    Boolean doNotUseGettersConfiguration = annotationNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_DO_NOT_USE_GETTERS);
    boolean doNotUseGetters = annotation.isExplicit("doNotUseGetters") || doNotUseGettersConfiguration == null ? ann.doNotUseGetters() : doNotUseGettersConfiguration;
    FieldAccess fieldAccess = doNotUseGetters ? FieldAccess.PREFER_FIELD : FieldAccess.GETTER;
    Boolean fieldNamesConfiguration = annotationNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_INCLUDE_FIELD_NAMES);
    boolean includeFieldNames = annotation.isExplicit("includeFieldNames") || fieldNamesConfiguration == null ? ann.includeFieldNames() : fieldNamesConfiguration;
    generateToString(typeNode, annotationNode, excludes, includes, includeFieldNames, callSuper, true, fieldAccess);
}
Also used : JavacNode(lombok.javac.JavacNode) ToString(lombok.ToString) ToString(lombok.ToString)

Example 39 with JavacNode

use of lombok.javac.JavacNode 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 40 with JavacNode

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

the class HandleFieldDefaults method generateFieldDefaultsForType.

public boolean generateFieldDefaultsForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean makeFinal, boolean checkForTypeLevelFieldDefaults) {
    if (checkForTypeLevelFieldDefaults) {
        if (hasAnnotation(FieldDefaults.class, typeNode)) {
            //The annotation will make it happen, so we can skip it.
            return true;
        }
    }
    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("@FieldDefaults is only supported on a class or an enum.");
        return false;
    }
    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;
        setFieldDefaultsForField(field, level, makeFinal);
    }
    return true;
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacNode(lombok.javac.JavacNode) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Aggregations

JavacNode (lombok.javac.JavacNode)52 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)27 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)23 ListBuffer (com.sun.tools.javac.util.ListBuffer)18 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)15 JavacTreeMaker (lombok.javac.JavacTreeMaker)15 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)13 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)13 Name (com.sun.tools.javac.util.Name)11 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)9 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)8 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)7 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)6 ArrayList (java.util.ArrayList)6 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)5 JCTree (com.sun.tools.javac.tree.JCTree)4 JCArrayTypeTree (com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)4 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)4 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)4 AccessLevel (lombok.AccessLevel)4