Search in sources :

Example 1 with NormalAnnotation

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

the class EclipseHandlerUtil method addAnnotation.

private static Annotation[] addAnnotation(ASTNode source, Annotation[] originalAnnotationArray, char[][] annotationTypeFqn, ASTNode arg) {
    char[] simpleName = annotationTypeFqn[annotationTypeFqn.length - 1];
    if (originalAnnotationArray != null)
        for (Annotation ann : originalAnnotationArray) {
            if (ann.type instanceof QualifiedTypeReference) {
                char[][] t = ((QualifiedTypeReference) ann.type).tokens;
                if (Arrays.deepEquals(t, annotationTypeFqn))
                    return originalAnnotationArray;
            }
            if (ann.type instanceof SingleTypeReference) {
                char[] lastToken = ((SingleTypeReference) ann.type).token;
                if (Arrays.equals(lastToken, simpleName))
                    return originalAnnotationArray;
            }
        }
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    long[] poss = new long[annotationTypeFqn.length];
    Arrays.fill(poss, p);
    QualifiedTypeReference qualifiedType = new QualifiedTypeReference(annotationTypeFqn, poss);
    setGeneratedBy(qualifiedType, source);
    Annotation ann;
    if (arg instanceof Expression) {
        SingleMemberAnnotation sma = new SingleMemberAnnotation(qualifiedType, pS);
        sma.declarationSourceEnd = pE;
        arg.sourceStart = pS;
        arg.sourceEnd = pE;
        sma.memberValue = (Expression) arg;
        setGeneratedBy(sma.memberValue, source);
        ann = sma;
    } else if (arg instanceof MemberValuePair) {
        NormalAnnotation na = new NormalAnnotation(qualifiedType, pS);
        na.declarationSourceEnd = pE;
        arg.sourceStart = pS;
        arg.sourceEnd = pE;
        na.memberValuePairs = new MemberValuePair[] { (MemberValuePair) arg };
        setGeneratedBy(na.memberValuePairs[0], source);
        setGeneratedBy(na.memberValuePairs[0].value, source);
        na.memberValuePairs[0].value.sourceStart = pS;
        na.memberValuePairs[0].value.sourceEnd = pE;
        ann = na;
    } else {
        MarkerAnnotation ma = new MarkerAnnotation(qualifiedType, pS);
        ma.declarationSourceEnd = pE;
        ann = ma;
    }
    setGeneratedBy(ann, source);
    if (originalAnnotationArray == null)
        return new Annotation[] { ann };
    Annotation[] newAnnotationArray = new Annotation[originalAnnotationArray.length + 1];
    System.arraycopy(originalAnnotationArray, 0, newAnnotationArray, 0, originalAnnotationArray.length);
    newAnnotationArray[originalAnnotationArray.length] = ann;
    return newAnnotationArray;
}
Also used : SingleMemberAnnotation(org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation) 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) MarkerAnnotation(org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation) MemberValuePair(org.eclipse.jdt.internal.compiler.ast.MemberValuePair) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) 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) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) NormalAnnotation(org.eclipse.jdt.internal.compiler.ast.NormalAnnotation)

Example 2 with NormalAnnotation

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

the class EclipseHandlerUtil method unboxAndRemoveAnnotationParameter.

public static List<Annotation> unboxAndRemoveAnnotationParameter(Annotation annotation, String annotationName, String errorName, EclipseNode errorNode) {
    if ("value".equals(annotationName)) {
        // and onConstructor. Let's exit early and very obviously:
        throw new UnsupportedOperationException("Lombok cannot unbox 'value' from SingleMemberAnnotation at this time.");
    }
    if (!NormalAnnotation.class.equals(annotation.getClass())) {
        // CompletionOnAnnotationMemberValuePair from triggering this handler.
        return Collections.emptyList();
    }
    NormalAnnotation normalAnnotation = (NormalAnnotation) annotation;
    MemberValuePair[] pairs = normalAnnotation.memberValuePairs;
    if (pairs == null)
        return Collections.emptyList();
    char[] nameAsCharArray = annotationName.toCharArray();
    top: for (int i = 0; i < pairs.length; i++) {
        boolean allowRaw;
        char[] name = pairs[i].name;
        if (name == null)
            continue;
        if (name.length < nameAsCharArray.length)
            continue;
        for (int j = 0; j < nameAsCharArray.length; j++) {
            if (name[j] != nameAsCharArray[j])
                continue top;
        }
        allowRaw = name.length > nameAsCharArray.length;
        for (int j = nameAsCharArray.length; j < name.length; j++) {
            if (name[j] != '_')
                continue top;
        }
        // If we're still here it's the targeted annotation param.
        Expression value = pairs[i].value;
        MemberValuePair[] newPairs = new MemberValuePair[pairs.length - 1];
        if (i > 0)
            System.arraycopy(pairs, 0, newPairs, 0, i);
        if (i < pairs.length - 1)
            System.arraycopy(pairs, i + 1, newPairs, i, pairs.length - i - 1);
        normalAnnotation.memberValuePairs = newPairs;
        // We have now removed the annotation parameter and stored the value,
        // which we must now unbox. It's either annotations, or @__(annotations).
        Expression content = null;
        if (value instanceof ArrayInitializer) {
            if (!allowRaw) {
                addError(errorName, errorNode);
                return Collections.emptyList();
            }
            content = value;
        } else if (!(value instanceof Annotation)) {
            addError(errorName, errorNode);
            return Collections.emptyList();
        } else {
            Annotation atDummyIdentifier = (Annotation) value;
            if (atDummyIdentifier.type instanceof SingleTypeReference && isAllValidOnXCharacters(((SingleTypeReference) atDummyIdentifier.type).token)) {
                if (atDummyIdentifier instanceof MarkerAnnotation) {
                    return Collections.emptyList();
                } else if (atDummyIdentifier instanceof NormalAnnotation) {
                    MemberValuePair[] mvps = ((NormalAnnotation) atDummyIdentifier).memberValuePairs;
                    if (mvps == null || mvps.length == 0) {
                        return Collections.emptyList();
                    }
                    if (mvps.length == 1 && Arrays.equals("value".toCharArray(), mvps[0].name)) {
                        content = mvps[0].value;
                    }
                } else if (atDummyIdentifier instanceof SingleMemberAnnotation) {
                    content = ((SingleMemberAnnotation) atDummyIdentifier).memberValue;
                } else {
                    addError(errorName, errorNode);
                    return Collections.emptyList();
                }
            } else {
                if (allowRaw) {
                    content = atDummyIdentifier;
                } else {
                    addError(errorName, errorNode);
                    return Collections.emptyList();
                }
            }
        }
        if (content == null) {
            addError(errorName, errorNode);
            return Collections.emptyList();
        }
        if (content instanceof Annotation) {
            return Collections.singletonList((Annotation) content);
        } else if (content instanceof ArrayInitializer) {
            Expression[] expressions = ((ArrayInitializer) content).expressions;
            List<Annotation> result = new ArrayList<Annotation>();
            if (expressions != null)
                for (Expression ex : expressions) {
                    if (ex instanceof Annotation)
                        result.add((Annotation) ex);
                    else {
                        addError(errorName, errorNode);
                        return Collections.emptyList();
                    }
                }
            return result;
        } else {
            addError(errorName, errorNode);
            return Collections.emptyList();
        }
    }
    return Collections.emptyList();
}
Also used : SingleMemberAnnotation(org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation) ArrayList(java.util.ArrayList) 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) MarkerAnnotation(org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation) 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) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) NormalAnnotation(org.eclipse.jdt.internal.compiler.ast.NormalAnnotation) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)

Example 3 with NormalAnnotation

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

the class EclipseHandlerUtil method copyAnnotation.

public static Annotation copyAnnotation(Annotation annotation, ASTNode source) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    if (annotation instanceof MarkerAnnotation) {
        MarkerAnnotation ann = new MarkerAnnotation(copyType(annotation.type, source), pS);
        setGeneratedBy(ann, source);
        ann.declarationSourceEnd = ann.sourceEnd = ann.statementEnd = pE;
        return ann;
    }
    if (annotation instanceof SingleMemberAnnotation) {
        SingleMemberAnnotation ann = new SingleMemberAnnotation(copyType(annotation.type, source), pS);
        setGeneratedBy(ann, source);
        ann.declarationSourceEnd = ann.sourceEnd = ann.statementEnd = pE;
        //TODO memberValue(s) need to be copied as well (same for copying a NormalAnnotation as below).
        ann.memberValue = ((SingleMemberAnnotation) annotation).memberValue;
        return ann;
    }
    if (annotation instanceof NormalAnnotation) {
        NormalAnnotation ann = new NormalAnnotation(copyType(annotation.type, source), pS);
        setGeneratedBy(ann, source);
        ann.declarationSourceEnd = ann.statementEnd = ann.sourceEnd = pE;
        ann.memberValuePairs = ((NormalAnnotation) annotation).memberValuePairs;
        return ann;
    }
    return annotation;
}
Also used : MarkerAnnotation(org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation) SingleMemberAnnotation(org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation) NormalAnnotation(org.eclipse.jdt.internal.compiler.ast.NormalAnnotation)

Aggregations

MarkerAnnotation (org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation)3 NormalAnnotation (org.eclipse.jdt.internal.compiler.ast.NormalAnnotation)3 SingleMemberAnnotation (org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation)3 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)2 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)2 CastExpression (org.eclipse.jdt.internal.compiler.ast.CastExpression)2 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)2 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)2 MemberValuePair (org.eclipse.jdt.internal.compiler.ast.MemberValuePair)2 ParameterizedSingleTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference)2 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)2 ArrayList (java.util.ArrayList)1 ArrayInitializer (org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)1 ArrayQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference)1 ParameterizedQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)1 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)1