Search in sources :

Example 1 with FieldDefaults

use of lombok.experimental.FieldDefaults in project lombok by rzwitserloot.

the class HandleFieldDefaults method visitType.

@Override
public void visitType(EclipseNode typeNode, TypeDeclaration type) {
    AnnotationValues<FieldDefaults> fieldDefaults = null;
    EclipseNode source = typeNode;
    boolean levelIsExplicit = false;
    boolean makeFinalIsExplicit = false;
    FieldDefaults fd = null;
    for (EclipseNode jn : typeNode.down()) {
        if (jn.getKind() != Kind.ANNOTATION)
            continue;
        Annotation ann = (Annotation) jn.get();
        TypeReference typeTree = ann.type;
        if (typeTree == null)
            continue;
        if (typeTree instanceof SingleTypeReference) {
            char[] t = ((SingleTypeReference) typeTree).token;
            if (!Arrays.equals(t, FIELD_DEFAULTS))
                continue;
        } else if (typeTree instanceof QualifiedTypeReference) {
            char[][] t = ((QualifiedTypeReference) typeTree).tokens;
            if (!Eclipse.nameEquals(t, "lombok.experimental.FieldDefaults"))
                continue;
        } else {
            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;
        }
        break;
    }
    if (fd == null && (type.modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 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 : SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) EclipseNode(lombok.eclipse.EclipseNode) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) AccessLevel(lombok.AccessLevel) FieldDefaults(lombok.experimental.FieldDefaults)

Example 2 with FieldDefaults

use of lombok.experimental.FieldDefaults 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)

Aggregations

AccessLevel (lombok.AccessLevel)2 FieldDefaults (lombok.experimental.FieldDefaults)2 JCTree (com.sun.tools.javac.tree.JCTree)1 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)1 EclipseNode (lombok.eclipse.EclipseNode)1 JavacNode (lombok.javac.JavacNode)1 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)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