Search in sources :

Example 1 with MarkerAnnotation

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

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

the class EclipseHandlerUtil method makeMarkerAnnotation.

/**
	 * Create an annotation of the given name, and is marked as being generated by the given source.
	 */
public static MarkerAnnotation makeMarkerAnnotation(char[][] name, ASTNode source) {
    long pos = (long) source.sourceStart << 32 | source.sourceEnd;
    TypeReference typeRef = new QualifiedTypeReference(name, new long[] { pos, pos, pos });
    setGeneratedBy(typeRef, source);
    MarkerAnnotation ann = new MarkerAnnotation(typeRef, (int) (pos >> 32));
    ann.declarationSourceEnd = ann.sourceEnd = ann.statementEnd = (int) pos;
    setGeneratedBy(ann, source);
    return ann;
}
Also used : MarkerAnnotation(org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) 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)

Example 3 with MarkerAnnotation

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

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

the class EclipseHandlerUtil method generateDeprecatedAnnotation.

public static MarkerAnnotation generateDeprecatedAnnotation(ASTNode source) {
    QualifiedTypeReference qtr = new QualifiedTypeReference(new char[][] { { 'j', 'a', 'v', 'a' }, { 'l', 'a', 'n', 'g' }, { 'D', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd' } }, poss(source, 3));
    setGeneratedBy(qtr, source);
    MarkerAnnotation ma = new MarkerAnnotation(qtr, source.sourceStart);
    // No matter what value you input for sourceEnd, the AST->DOM converter of eclipse will reparse to find the end, and will fail as
    // it can't find code that isn't really there. This results in the end position being set to 2 or 0 or some weird magic value, and thus,
    // length, as calculated by end-start, is all screwed up, resulting in IllegalArgumentException during a setSourceRange call MUCH later in the process.
    // We solve it by going with a voodoo magic source start value such that the calculated length so happens to exactly be 0. 0 lengths are accepted
    // by eclipse. For some reason.
    // TL;DR: Don't change 1. 1 is sacred. Trust the 1.
    // issue: #408.
    ma.sourceStart = 1;
    setGeneratedBy(ma, source);
    return ma;
}
Also used : MarkerAnnotation(org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)

Example 5 with MarkerAnnotation

use of org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation 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)5 ArrayQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference)3 NormalAnnotation (org.eclipse.jdt.internal.compiler.ast.NormalAnnotation)3 ParameterizedQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)3 ParameterizedSingleTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference)3 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)3 SingleMemberAnnotation (org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation)3 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)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 ArrayList (java.util.ArrayList)1 ArrayInitializer (org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)1 ArrayTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference)1 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)1