Search in sources :

Example 6 with AccessLevel

use of lombok.AccessLevel 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 7 with AccessLevel

use of lombok.AccessLevel in project lombok by rzwitserloot.

the class HandleFieldDefaults method visitType.

@Override
public void visitType(JavacNode typeNode, JCClassDecl type) {
    AnnotationValues<FieldDefaults> fieldDefaults = null;
    JavacNode source = typeNode;
    boolean levelIsExplicit = false;
    boolean makeFinalIsExplicit = false;
    FieldDefaults fd = null;
    for (JavacNode jn : typeNode.down()) {
        if (jn.getKind() != Kind.ANNOTATION)
            continue;
        JCAnnotation ann = (JCAnnotation) jn.get();
        JCTree typeTree = ann.annotationType;
        if (typeTree == null)
            continue;
        String typeTreeToString = typeTree.toString();
        if (!typeTreeToString.equals("FieldDefaults") && !typeTreeToString.equals("lombok.experimental.FieldDefaults"))
            continue;
        if (!typeMatches(FieldDefaults.class, jn, typeTree))
            continue;
        source = jn;
        fieldDefaults = createAnnotation(FieldDefaults.class, jn);
        levelIsExplicit = fieldDefaults.isExplicit("level");
        makeFinalIsExplicit = fieldDefaults.isExplicit("makeFinal");
        handleExperimentalFlagUsage(jn, ConfigurationKeys.FIELD_DEFAULTS_FLAG_USAGE, "@FieldDefaults");
        fd = fieldDefaults.getInstance();
        if (!levelIsExplicit && !makeFinalIsExplicit) {
            jn.addError("This does nothing; provide either level or makeFinal or both.");
        }
        if (levelIsExplicit && fd.level() == AccessLevel.NONE) {
            jn.addError("AccessLevel.NONE doesn't mean anything here. Pick another value.");
            levelIsExplicit = false;
        }
        deleteAnnotationIfNeccessary(jn, FieldDefaults.class);
        deleteImportFromCompilationUnit(jn, "lombok.AccessLevel");
        break;
    }
    if (fd == null && (type.mods.flags & (Flags.INTERFACE | Flags.ANNOTATION)) != 0)
        return;
    boolean defaultToPrivate = levelIsExplicit ? false : Boolean.TRUE.equals(typeNode.getAst().readConfiguration(ConfigurationKeys.FIELD_DEFAULTS_PRIVATE_EVERYWHERE));
    boolean defaultToFinal = makeFinalIsExplicit ? false : Boolean.TRUE.equals(typeNode.getAst().readConfiguration(ConfigurationKeys.FIELD_DEFAULTS_FINAL_EVERYWHERE));
    if (!defaultToPrivate && !defaultToFinal && fieldDefaults == null)
        return;
    AccessLevel fdAccessLevel = (fieldDefaults != null && levelIsExplicit) ? fd.level() : defaultToPrivate ? AccessLevel.PRIVATE : null;
    boolean fdToFinal = (fieldDefaults != null && makeFinalIsExplicit) ? fd.makeFinal() : defaultToFinal;
    generateFieldDefaultsForType(typeNode, source, fdAccessLevel, fdToFinal, false);
}
Also used : JavacNode(lombok.javac.JavacNode) JCTree(com.sun.tools.javac.tree.JCTree) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) AccessLevel(lombok.AccessLevel) FieldDefaults(lombok.experimental.FieldDefaults)

Example 8 with AccessLevel

use of lombok.AccessLevel in project lombok by rzwitserloot.

the class HandleGetter method handle.

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

Aggregations

AccessLevel (lombok.AccessLevel)8 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)4 EclipseNode (lombok.eclipse.EclipseNode)4 JavacNode (lombok.javac.JavacNode)4 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)4 Getter (lombok.Getter)2 FieldDefaults (lombok.experimental.FieldDefaults)2 JCTree (com.sun.tools.javac.tree.JCTree)1 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)1 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)1 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)1