Search in sources :

Example 21 with Annotation

use of org.eclipse.jdt.internal.compiler.ast.Annotation 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 22 with Annotation

use of org.eclipse.jdt.internal.compiler.ast.Annotation in project lombok by rzwitserloot.

the class Eclipse method findAnnotations.

/**
	 * Searches the given field node for annotations and returns each one that matches the provided regular expression pattern.
	 * 
	 * Only the simple name is checked - the package and any containing class are ignored.
	 */
public static Annotation[] findAnnotations(FieldDeclaration field, Pattern namePattern) {
    List<Annotation> result = new ArrayList<Annotation>();
    if (field.annotations == null)
        return EMPTY_ANNOTATIONS_ARRAY;
    for (Annotation annotation : field.annotations) {
        TypeReference typeRef = annotation.type;
        if (typeRef != null && typeRef.getTypeName() != null) {
            char[][] typeName = typeRef.getTypeName();
            String suspect = new String(typeName[typeName.length - 1]);
            if (namePattern.matcher(suspect).matches()) {
                result.add(annotation);
            }
        }
    }
    return result.toArray(EMPTY_ANNOTATIONS_ARRAY);
}
Also used : ArrayList(java.util.ArrayList) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation)

Example 23 with Annotation

use of org.eclipse.jdt.internal.compiler.ast.Annotation in project lombok by rzwitserloot.

the class PatchDelegate method fillMethodBindingsForMethods.

private static void fillMethodBindingsForMethods(CompilationUnitDeclaration cud, ClassScope scope, List<BindingTuple> methodsToDelegate) {
    TypeDeclaration decl = scope.referenceContext;
    if (decl == null)
        return;
    if (decl.methods != null)
        for (AbstractMethodDeclaration methodDecl : decl.methods) {
            if (methodDecl.annotations == null)
                continue;
            for (Annotation ann : methodDecl.annotations) {
                if (!isDelegate(ann, decl))
                    continue;
                if (Annotation_applied.getAndSet(ann, true))
                    continue;
                if (!(methodDecl instanceof MethodDeclaration)) {
                    EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                    eclipseAst.get(ann).addError(LEGALITY_OF_DELEGATE);
                    break;
                }
                if (methodDecl.arguments != null) {
                    EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                    eclipseAst.get(ann).addError(LEGALITY_OF_DELEGATE);
                    break;
                }
                if ((methodDecl.modifiers & ClassFileConstants.AccStatic) != 0) {
                    EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                    eclipseAst.get(ann).addError(LEGALITY_OF_DELEGATE);
                    break;
                }
                MethodDeclaration method = (MethodDeclaration) methodDecl;
                List<ClassLiteralAccess> rawTypes = rawTypes(ann, "types");
                List<ClassLiteralAccess> excludedRawTypes = rawTypes(ann, "excludes");
                List<BindingTuple> methodsToExclude = new ArrayList<BindingTuple>();
                List<BindingTuple> methodsToDelegateForThisAnn = new ArrayList<BindingTuple>();
                try {
                    for (ClassLiteralAccess cla : excludedRawTypes) {
                        addAllMethodBindings(methodsToExclude, cla.type.resolveType(decl.initializerScope), new HashSet<String>(), method.selector, ann);
                    }
                    Set<String> banList = new HashSet<String>();
                    for (BindingTuple excluded : methodsToExclude) banList.add(printSig(excluded.parameterized));
                    if (rawTypes.isEmpty()) {
                        if (method.returnType == null)
                            continue;
                        addAllMethodBindings(methodsToDelegateForThisAnn, method.returnType.resolveType(decl.initializerScope), banList, method.selector, ann);
                    } else {
                        for (ClassLiteralAccess cla : rawTypes) {
                            addAllMethodBindings(methodsToDelegateForThisAnn, cla.type.resolveType(decl.initializerScope), banList, method.selector, ann);
                        }
                    }
                } catch (DelegateRecursion e) {
                    EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                    eclipseAst.get(ann).addError(String.format(RECURSION_NOT_ALLOWED, new String(e.member), new String(e.type)));
                    break;
                }
                // Not doing this right now because of problems - see commented-out-method for info.
                // removeExistingMethods(methodsToDelegate, decl, scope);
                String dupe = containsDuplicates(methodsToDelegateForThisAnn);
                if (dupe != null) {
                    EclipseAST eclipseAst = TransformEclipseAST.getAST(cud, true);
                    eclipseAst.get(ann).addError("The method '" + dupe + "' is being delegated by more than one specified type.");
                } else {
                    methodsToDelegate.addAll(methodsToDelegateForThisAnn);
                }
            }
        }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) EclipseAST(lombok.eclipse.EclipseAST) TransformEclipseAST(lombok.eclipse.TransformEclipseAST) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) List(java.util.List) ArrayList(java.util.ArrayList) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) ClassLiteralAccess(org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess) HashSet(java.util.HashSet)

Example 24 with Annotation

use of org.eclipse.jdt.internal.compiler.ast.Annotation in project lombok by rzwitserloot.

the class HandleSynchronized method createLockField.

public char[] createLockField(AnnotationValues<Synchronized> annotation, EclipseNode annotationNode, boolean isStatic, boolean reportErrors) {
    char[] lockName = annotation.getInstance().value().toCharArray();
    Annotation source = (Annotation) annotationNode.get();
    boolean autoMake = false;
    if (lockName.length == 0) {
        autoMake = true;
        lockName = isStatic ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME;
    }
    if (fieldExists(new String(lockName), annotationNode) == MemberExistsResult.NOT_EXISTS) {
        if (!autoMake) {
            if (reportErrors)
                annotationNode.addError(String.format("The field %s does not exist.", new String(lockName)));
            return null;
        }
        FieldDeclaration fieldDecl = new FieldDeclaration(lockName, 0, -1);
        setGeneratedBy(fieldDecl, source);
        fieldDecl.declarationSourceEnd = -1;
        fieldDecl.modifiers = (isStatic ? Modifier.STATIC : 0) | Modifier.FINAL | Modifier.PRIVATE;
        //We use 'new Object[0];' because unlike 'new Object();', empty arrays *ARE* serializable!
        ArrayAllocationExpression arrayAlloc = new ArrayAllocationExpression();
        setGeneratedBy(arrayAlloc, source);
        arrayAlloc.dimensions = new Expression[] { makeIntLiteral("0".toCharArray(), source) };
        arrayAlloc.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 });
        setGeneratedBy(arrayAlloc.type, source);
        fieldDecl.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 });
        setGeneratedBy(fieldDecl.type, source);
        fieldDecl.initialization = arrayAlloc;
        // TODO temporary workaround for issue 217. http://code.google.com/p/projectlombok/issues/detail?id=217
        // injectFieldSuppressWarnings(annotationNode.up().up(), fieldDecl);
        injectField(annotationNode.up().up(), fieldDecl);
    }
    return lockName;
}
Also used : ArrayAllocationExpression(org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Aggregations

Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)24 EclipseNode (lombok.eclipse.EclipseNode)12 ArrayList (java.util.ArrayList)11 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)8 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)6 SingleMemberAnnotation (org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation)6 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)5 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)5 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)5 List (java.util.List)4 AccessLevel (lombok.AccessLevel)4 MarkerAnnotation (org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation)4 MemberValuePair (org.eclipse.jdt.internal.compiler.ast.MemberValuePair)4 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)4 NormalAnnotation (org.eclipse.jdt.internal.compiler.ast.NormalAnnotation)4 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)4 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)4 ASTNode (org.eclipse.jdt.internal.compiler.ast.ASTNode)3 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)3 CastExpression (org.eclipse.jdt.internal.compiler.ast.CastExpression)3