Search in sources :

Example 1 with AnnotationValues

use of lombok.core.AnnotationValues in project lombok by rzwitserloot.

the class JavacHandlerUtil method findGetter.

private static GetterMethod findGetter(JavacNode field) {
    JCVariableDecl decl = (JCVariableDecl) field.get();
    JavacNode typeNode = field.up();
    for (String potentialGetterName : toAllGetterNames(field)) {
        for (JavacNode potentialGetter : typeNode.down()) {
            if (potentialGetter.getKind() != Kind.METHOD)
                continue;
            JCMethodDecl method = (JCMethodDecl) potentialGetter.get();
            if (!method.name.toString().equalsIgnoreCase(potentialGetterName))
                continue;
            /**
             * static getX() methods don't count.
             */
            if ((method.mods.flags & Flags.STATIC) != 0)
                continue;
            /**
             * Nor do getters with a non-empty parameter list.
             */
            if (method.params != null && method.params.size() > 0)
                continue;
            return new GetterMethod(method.name, method.restype);
        }
    }
    // Check if the field has a @Getter annotation.
    boolean hasGetterAnnotation = false;
    for (JavacNode child : field.down()) {
        if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
            AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
            // Definitely WONT have a getter.
            if (ann.getInstance().value() == AccessLevel.NONE)
                return null;
            hasGetterAnnotation = true;
        }
    }
    if (!hasGetterAnnotation && HandleGetter.fieldQualifiesForGetterGeneration(field)) {
        // Check if the class has @Getter or @Data annotation.
        JavacNode containingType = field.up();
        if (containingType != null)
            for (JavacNode child : containingType.down()) {
                if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Data.class, child))
                    hasGetterAnnotation = true;
                if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
                    AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
                    // Definitely WONT have a getter.
                    if (ann.getInstance().value() == AccessLevel.NONE)
                        return null;
                    hasGetterAnnotation = true;
                }
            }
    }
    if (hasGetterAnnotation) {
        String getterName = toGetterName(field);
        if (getterName == null)
            return null;
        return new GetterMethod(field.toName(getterName), decl.vartype);
    }
    return null;
}
Also used : JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacNode(lombok.javac.JavacNode) Getter(lombok.Getter) AnnotationValues(lombok.core.AnnotationValues) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 2 with AnnotationValues

use of lombok.core.AnnotationValues in project lombok by rzwitserloot.

the class EclipseHandlerUtil method findGetter.

private static GetterMethod findGetter(EclipseNode field) {
    FieldDeclaration fieldDeclaration = (FieldDeclaration) field.get();
    boolean forceBool = FieldDeclaration_booleanLazyGetter.get(fieldDeclaration);
    TypeReference fieldType = fieldDeclaration.type;
    boolean isBoolean = forceBool || isBoolean(fieldType);
    EclipseNode typeNode = field.up();
    for (String potentialGetterName : toAllGetterNames(field, isBoolean)) {
        for (EclipseNode potentialGetter : typeNode.down()) {
            if (potentialGetter.getKind() != Kind.METHOD)
                continue;
            if (!(potentialGetter.get() instanceof MethodDeclaration))
                continue;
            MethodDeclaration method = (MethodDeclaration) potentialGetter.get();
            if (!potentialGetterName.equalsIgnoreCase(new String(method.selector)))
                continue;
            /**
             * static getX() methods don't count.
             */
            if ((method.modifiers & ClassFileConstants.AccStatic) != 0)
                continue;
            /**
             * Nor do getters with a non-empty parameter list.
             */
            if (method.arguments != null && method.arguments.length > 0)
                continue;
            return new GetterMethod(method.selector, method.returnType);
        }
    }
    // Check if the field has a @Getter annotation.
    boolean hasGetterAnnotation = false;
    for (EclipseNode child : field.down()) {
        if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
            AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
            // Definitely WONT have a getter.
            if (ann.getInstance().value() == AccessLevel.NONE)
                return null;
            hasGetterAnnotation = true;
        }
    }
    if (!hasGetterAnnotation && HandleGetter.fieldQualifiesForGetterGeneration(field)) {
        // Check if the class has @Getter or @Data annotation.
        EclipseNode containingType = field.up();
        if (containingType != null)
            for (EclipseNode child : containingType.down()) {
                if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Data.class, child))
                    hasGetterAnnotation = true;
                if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
                    AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
                    // Definitely WONT have a getter.
                    if (ann.getInstance().value() == AccessLevel.NONE)
                        return null;
                    hasGetterAnnotation = true;
                }
            }
    }
    if (hasGetterAnnotation) {
        String getterName = toGetterName(field, isBoolean);
        if (getterName == null)
            return null;
        return new GetterMethod(getterName.toCharArray(), fieldType);
    }
    return null;
}
Also used : MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) Getter(lombok.Getter) AnnotationValues(lombok.core.AnnotationValues) EclipseNode(lombok.eclipse.EclipseNode) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 3 with AnnotationValues

use of lombok.core.AnnotationValues in project lombok by rzwitserloot.

the class EclipseHandlerUtil method createAnnotation.

/**
 * Provides AnnotationValues with the data it needs to do its thing.
 */
public static <A extends java.lang.annotation.Annotation> AnnotationValues<A> createAnnotation(Class<A> type, final EclipseNode annotationNode) {
    final Annotation annotation = (Annotation) annotationNode.get();
    Map<String, AnnotationValue> values = new HashMap<String, AnnotationValue>();
    MemberValuePair[] memberValuePairs = annotation.memberValuePairs();
    if (memberValuePairs != null)
        for (final MemberValuePair pair : memberValuePairs) {
            List<String> raws = new ArrayList<String>();
            List<Object> expressionValues = new ArrayList<Object>();
            List<Object> guesses = new ArrayList<Object>();
            Expression[] expressions = null;
            char[] n = pair.name;
            String mName = (n == null || n.length == 0) ? "value" : new String(pair.name);
            final Expression rhs = pair.value;
            if (rhs instanceof ArrayInitializer) {
                expressions = ((ArrayInitializer) rhs).expressions;
            } else if (rhs != null) {
                expressions = new Expression[] { rhs };
            }
            if (expressions != null)
                for (Expression ex : expressions) {
                    StringBuffer sb = new StringBuffer();
                    ex.print(0, sb);
                    raws.add(sb.toString());
                    expressionValues.add(ex);
                    guesses.add(calculateValue(ex));
                }
            final Expression[] exprs = expressions;
            values.put(mName, new AnnotationValue(annotationNode, raws, expressionValues, guesses, true) {

                @Override
                public void setError(String message, int valueIdx) {
                    Expression ex;
                    if (valueIdx == -1)
                        ex = rhs;
                    else
                        ex = exprs != null ? exprs[valueIdx] : null;
                    if (ex == null)
                        ex = annotation;
                    int sourceStart = ex.sourceStart;
                    int sourceEnd = ex.sourceEnd;
                    annotationNode.addError(message, sourceStart, sourceEnd);
                }

                @Override
                public void setWarning(String message, int valueIdx) {
                    Expression ex;
                    if (valueIdx == -1)
                        ex = rhs;
                    else
                        ex = exprs != null ? exprs[valueIdx] : null;
                    if (ex == null)
                        ex = annotation;
                    int sourceStart = ex.sourceStart;
                    int sourceEnd = ex.sourceEnd;
                    annotationNode.addWarning(message, sourceStart, sourceEnd);
                }
            });
        }
    for (Method m : type.getDeclaredMethods()) {
        if (!Modifier.isPublic(m.getModifiers()))
            continue;
        String name = m.getName();
        if (!values.containsKey(name)) {
            values.put(name, new AnnotationValue(annotationNode, new ArrayList<String>(), new ArrayList<Object>(), new ArrayList<Object>(), false) {

                @Override
                public void setError(String message, int valueIdx) {
                    annotationNode.addError(message);
                }

                @Override
                public void setWarning(String message, int valueIdx) {
                    annotationNode.addWarning(message);
                }
            });
        }
    }
    return new AnnotationValues<A>(type, values, annotationNode);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) MarkerAnnotation(org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation) SingleMemberAnnotation(org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation) NormalAnnotation(org.eclipse.jdt.internal.compiler.ast.NormalAnnotation) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) MemberValuePair(org.eclipse.jdt.internal.compiler.ast.MemberValuePair) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) CastExpression(org.eclipse.jdt.internal.compiler.ast.CastExpression) AnnotationValues(lombok.core.AnnotationValues) AnnotationValue(lombok.core.AnnotationValues.AnnotationValue) List(java.util.List) ArrayList(java.util.ArrayList) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)

Example 4 with AnnotationValues

use of lombok.core.AnnotationValues in project lombok by rzwitserloot.

the class JavacHandlerUtil method createAnnotation.

/**
 * Creates an instance of {@code AnnotationValues} for the provided AST Node.
 *
 * @param type An annotation class type, such as {@code lombok.Getter.class}.
 * @param node A Lombok AST node representing an annotation in source code.
 */
public static <A extends Annotation> AnnotationValues<A> createAnnotation(Class<A> type, final JavacNode node) {
    Map<String, AnnotationValue> values = new HashMap<String, AnnotationValue>();
    JCAnnotation anno = (JCAnnotation) node.get();
    List<JCExpression> arguments = anno.getArguments();
    for (JCExpression arg : arguments) {
        String mName;
        JCExpression rhs;
        java.util.List<String> raws = new ArrayList<String>();
        java.util.List<Object> guesses = new ArrayList<Object>();
        java.util.List<Object> expressions = new ArrayList<Object>();
        final java.util.List<DiagnosticPosition> positions = new ArrayList<DiagnosticPosition>();
        if (arg instanceof JCAssign) {
            JCAssign assign = (JCAssign) arg;
            mName = assign.lhs.toString();
            rhs = assign.rhs;
        } else {
            rhs = arg;
            mName = "value";
        }
        if (rhs instanceof JCNewArray) {
            List<JCExpression> elems = ((JCNewArray) rhs).elems;
            for (JCExpression inner : elems) {
                raws.add(inner.toString());
                expressions.add(inner);
                guesses.add(calculateGuess(inner));
                positions.add(inner.pos());
            }
        } else {
            raws.add(rhs.toString());
            expressions.add(rhs);
            guesses.add(calculateGuess(rhs));
            positions.add(rhs.pos());
        }
        values.put(mName, new AnnotationValue(node, raws, expressions, guesses, true) {

            @Override
            public void setError(String message, int valueIdx) {
                if (valueIdx < 0)
                    node.addError(message);
                else
                    node.addError(message, positions.get(valueIdx));
            }

            @Override
            public void setWarning(String message, int valueIdx) {
                if (valueIdx < 0)
                    node.addWarning(message);
                else
                    node.addWarning(message, positions.get(valueIdx));
            }
        });
    }
    for (Method m : type.getDeclaredMethods()) {
        if (!Modifier.isPublic(m.getModifiers()))
            continue;
        String name = m.getName();
        if (!values.containsKey(name)) {
            values.put(name, new AnnotationValue(node, new ArrayList<String>(), new ArrayList<Object>(), new ArrayList<Object>(), false) {

                @Override
                public void setError(String message, int valueIdx) {
                    node.addError(message);
                }

                @Override
                public void setWarning(String message, int valueIdx) {
                    node.addWarning(message);
                }
            });
        }
    }
    return new AnnotationValues<A>(type, values, node);
}
Also used : HashMap(java.util.HashMap) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) DiagnosticPosition(com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition) AnnotationValues(lombok.core.AnnotationValues) AnnotationValue(lombok.core.AnnotationValues.AnnotationValue) JCNewArray(com.sun.tools.javac.tree.JCTree.JCNewArray) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Aggregations

AnnotationValues (lombok.core.AnnotationValues)4 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Getter (lombok.Getter)2 AnnotationValue (lombok.core.AnnotationValues.AnnotationValue)2 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)1 JCAssign (com.sun.tools.javac.tree.JCTree.JCAssign)1 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)1 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)1 JCNewArray (com.sun.tools.javac.tree.JCTree.JCNewArray)1 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)1 DiagnosticPosition (com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition)1 List (java.util.List)1 EclipseNode (lombok.eclipse.EclipseNode)1 JavacNode (lombok.javac.JavacNode)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)1 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)1 ArrayInitializer (org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)1