Search in sources :

Example 1 with Value

use of lombok.Value in project lombok by rzwitserloot.

the class HandleValue method handle.

@Override
public void handle(AnnotationValues<Value> annotation, JCAnnotation ast, JavacNode annotationNode) {
    @SuppressWarnings("deprecation") Class<? extends Annotation> oldExperimentalValue = lombok.experimental.Value.class;
    handleFlagUsage(annotationNode, ConfigurationKeys.VALUE_FLAG_USAGE, "@Value");
    deleteAnnotationIfNeccessary(annotationNode, Value.class, oldExperimentalValue);
    JavacNode typeNode = annotationNode.up();
    boolean notAClass = !isClass(typeNode);
    if (notAClass) {
        annotationNode.addError("@Value is only supported on a class.");
        return;
    }
    String staticConstructorName = annotation.getInstance().staticConstructor();
    if (!hasAnnotationAndDeleteIfNeccessary(NonFinal.class, typeNode)) {
        JCModifiers jcm = ((JCClassDecl) typeNode.get()).mods;
        if ((jcm.flags & Flags.FINAL) == 0) {
            jcm.flags |= Flags.FINAL;
            typeNode.rebuild();
        }
    }
    new HandleFieldDefaults().generateFieldDefaultsForType(typeNode, annotationNode, AccessLevel.PRIVATE, true, true);
    // TODO move this to the end OR move it to the top in eclipse.
    new HandleConstructor().generateAllArgsConstructor(typeNode, AccessLevel.PUBLIC, staticConstructorName, SkipIfConstructorExists.YES, annotationNode);
    new HandleGetter().generateGetterForType(typeNode, annotationNode, AccessLevel.PUBLIC, true);
    new HandleEqualsAndHashCode().generateEqualsAndHashCodeForType(typeNode, annotationNode);
    new HandleToString().generateToStringForType(typeNode, annotationNode);
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavacNode(lombok.javac.JavacNode) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) Value(lombok.Value) NonFinal(lombok.experimental.NonFinal)

Example 2 with Value

use of lombok.Value in project lombok by rzwitserloot.

the class HandleValue method handle.

public void handle(AnnotationValues<Value> annotation, Annotation ast, EclipseNode annotationNode) {
    handleFlagUsage(annotationNode, ConfigurationKeys.VALUE_FLAG_USAGE, "@Value");
    Value ann = annotation.getInstance();
    EclipseNode typeNode = annotationNode.up();
    TypeDeclaration typeDecl = null;
    if (typeNode.get() instanceof TypeDeclaration)
        typeDecl = (TypeDeclaration) typeNode.get();
    int modifiers = typeDecl == null ? 0 : typeDecl.modifiers;
    boolean notAClass = (modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation | ClassFileConstants.AccEnum)) != 0;
    if (typeDecl == null || notAClass) {
        annotationNode.addError("@Value is only supported on a class.");
        return;
    }
    // Make class final.
    if (!hasAnnotation(NonFinal.class, typeNode)) {
        if ((typeDecl.modifiers & ClassFileConstants.AccFinal) == 0) {
            typeDecl.modifiers |= ClassFileConstants.AccFinal;
            typeNode.rebuild();
        }
    }
    new HandleFieldDefaults().generateFieldDefaultsForType(typeNode, annotationNode, AccessLevel.PRIVATE, true, true);
    //Careful: Generate the public static constructor (if there is one) LAST, so that any attempt to
    //'find callers' on the annotation node will find callers of the constructor, which is by far the
    //most useful of the many methods built by @Value. This trick won't work for the non-static constructor,
    //for whatever reason, though you can find callers of that one by focusing on the class name itself
    //and hitting 'find callers'.
    new HandleGetter().generateGetterForType(typeNode, annotationNode, AccessLevel.PUBLIC, true);
    new HandleEqualsAndHashCode().generateEqualsAndHashCodeForType(typeNode, annotationNode);
    new HandleToString().generateToStringForType(typeNode, annotationNode);
    new HandleConstructor().generateAllArgsConstructor(typeNode, AccessLevel.PUBLIC, ann.staticConstructor(), SkipIfConstructorExists.YES, Collections.<Annotation>emptyList(), annotationNode);
}
Also used : Value(lombok.Value) EclipseNode(lombok.eclipse.EclipseNode) NonFinal(lombok.experimental.NonFinal) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Aggregations

Value (lombok.Value)2 NonFinal (lombok.experimental.NonFinal)2 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)1 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)1 EclipseNode (lombok.eclipse.EclipseNode)1 JavacNode (lombok.javac.JavacNode)1 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)1