Example usage for org.eclipse.jdt.internal.compiler.ast MarkerAnnotation MarkerAnnotation

List of usage examples for org.eclipse.jdt.internal.compiler.ast MarkerAnnotation MarkerAnnotation

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast MarkerAnnotation MarkerAnnotation.

Prototype

public MarkerAnnotation(TypeReference type, int sourceStart) 

Source Link

Usage

From source file:lombok.eclipse.handlers.ast.EclipseASTMaker.java

License:Open Source License

@Override
public ASTNode visitAnnotation(final lombok.ast.Annotation node, final Void p) {
    final Annotation ann;
    if (node.getValues().isEmpty()) {
        ann = new MarkerAnnotation(build(node.getType(), TypeReference.class), 0);
    } else if (node.getValues().containsKey("value") && node.getValues().size() == 1) {
        ann = new SingleMemberAnnotation(build(node.getType(), TypeReference.class), 0);
        ((SingleMemberAnnotation) ann).memberValue = build(node.getValues().get("value"));
    } else {/*from  w w w  . j  a  v a 2  s.c  o m*/
        ann = new NormalAnnotation(build(node.getType(), TypeReference.class), 0);
        List<MemberValuePair> valuePairs = new ArrayList<MemberValuePair>();
        for (Entry<String, lombok.ast.Expression<?>> entry : node.getValues().entrySet()) {
            MemberValuePair valuePair = new MemberValuePair(entry.getKey().toCharArray(), 0, 0,
                    build(entry.getValue(), Expression.class));
            setGeneratedByAndCopyPos(valuePair, source, posHintOf(node));
            valuePairs.add(valuePair);
        }
        ((NormalAnnotation) ann).memberValuePairs = valuePairs.toArray(new MemberValuePair[0]);
    }
    setGeneratedByAndCopyPos(ann, source, posHintOf(node));
    return ann;
}

From source file:lombok.eclipse.handlers.EclipseHandlerUtil.java

License:Open Source License

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);/*from   ww w . java 2  s.  c o m*/
    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;
}

From source file:lombok.eclipse.handlers.EclipseHandlerUtil.java

License:Open Source License

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);//from  w  w w.j  ava 2 s . co  m
        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;
}

From source file:lombok.eclipse.handlers.EclipseHandlerUtil.java

License:Open Source License

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) {
            char[] lastToken = null;

            if (ann.type instanceof QualifiedTypeReference) {
                char[][] t = ((QualifiedTypeReference) ann.type).tokens;
                lastToken = t[t.length - 1];
            } else if (ann.type instanceof SingleTypeReference) {
                lastToken = ((SingleTypeReference) ann.type).token;
            }/*from  w w  w  .j ava  2 s  .  c  o m*/

            if (lastToken != null && Arrays.equals(simpleName, lastToken))
                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;
}

From source file:lombok.eclipse.handlers.EclipseHandlerUtil.java

License:Open Source License

/**
 * Create an annotation of the given name, and is marked as being generated by the given source.
 *//*w ww. j a  v  a 2s.co m*/
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;
}

From source file:lombok.eclipse.handlers.PKG.java

License:Open Source License

static MarkerAnnotation makeMarkerAnnotation(char[][] name, ASTNode source) {
    long pos = source.sourceStart << 32 | source.sourceEnd;
    TypeReference typeRef = new QualifiedTypeReference(name, new long[] { pos, pos, pos });
    Eclipse.setGeneratedBy(typeRef, source);
    MarkerAnnotation ann = new MarkerAnnotation(typeRef, (int) (pos >> 32));
    ann.declarationSourceEnd = ann.sourceEnd = ann.statementEnd = (int) pos;
    Eclipse.setGeneratedBy(ann, source);
    return ann;//w ww .  ja va  2  s  .co m
}

From source file:org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.java

License:Open Source License

/**
 * Convert groovy annotations into JDT annotations
 * //from w w w.  j a v a 2 s .  c o m
 * @return an array of annotations or null if there are none
 */
private Annotation[] transformAnnotations(List<AnnotationNode> groovyAnnotations) {
    // FIXASC positions are crap
    if (groovyAnnotations != null && groovyAnnotations.size() > 0) {
        List<Annotation> annotations = new ArrayList<Annotation>();
        for (AnnotationNode annotationNode : groovyAnnotations) {
            ClassNode annoType = annotationNode.getClassNode();
            Map<String, Expression> memberValuePairs = annotationNode.getMembers();
            // FIXASC (M3) do more than pure marker annotations and do annotation values

            if (memberValuePairs == null || memberValuePairs.size() == 0) {
                // Marker annotation:
                TypeReference annotationReference = createTypeReferenceForClassNode(annoType);
                annotationReference.sourceStart = annotationNode.getStart();
                annotationReference.sourceEnd = annotationNode.getEnd();

                MarkerAnnotation annotation = new MarkerAnnotation(annotationReference,
                        annotationNode.getStart());
                annotation.declarationSourceEnd = annotation.sourceEnd;
                annotations.add(annotation);
            } else {

                if (memberValuePairs.size() == 1 && memberValuePairs.containsKey("value")) {
                    // Single member annotation

                    // Code written to only manage a single class literal value annotation - so that @RunWith works
                    Expression value = memberValuePairs.get("value");
                    if (value instanceof PropertyExpression) {
                        String pExpression = ((PropertyExpression) value).getPropertyAsString();
                        if (pExpression.equals("class")) {
                            TypeReference annotationReference = createTypeReferenceForClassNode(annoType);
                            annotationReference.sourceStart = annotationNode.getStart();
                            annotationReference.sourceEnd = annotationNode.getEnd();
                            SingleMemberAnnotation annotation = new SingleMemberAnnotation(annotationReference,
                                    annotationNode.getStart());
                            annotation.memberValue = new ClassLiteralAccess(value.getEnd(),
                                    classLiteralToTypeReference((PropertyExpression) value));
                            annotation.declarationSourceEnd = annotation.sourceStart
                                    + annoType.getNameWithoutPackage().length();
                            annotations.add(annotation);
                        }
                    } else if (value instanceof VariableExpression && annoType.getName().endsWith("RunWith")) {
                        // FIXASC special case for 'RunWith(Foo)' where for some reason groovy doesn't mind the missing
                        // '.class'
                        // FIXASC test this
                        TypeReference annotationReference = createTypeReferenceForClassNode(annoType);
                        annotationReference.sourceStart = annotationNode.getStart();
                        annotationReference.sourceEnd = annotationNode.getEnd();
                        SingleMemberAnnotation annotation = new SingleMemberAnnotation(annotationReference,
                                annotationNode.getStart());
                        String v = ((VariableExpression) value).getName();
                        TypeReference ref = null;
                        int start = annotationReference.sourceStart;
                        int end = annotationReference.sourceEnd;
                        if (v.indexOf(".") == -1) {
                            ref = new SingleTypeReference(v.toCharArray(), toPos(start, end - 1));
                        } else {
                            char[][] splits = CharOperation.splitOn('.', v.toCharArray());
                            ref = new QualifiedTypeReference(splits, positionsFor(splits, start, end - 2));
                        }
                        annotation.memberValue = new ClassLiteralAccess(value.getEnd(), ref);
                        annotation.declarationSourceEnd = annotation.sourceStart
                                + annoType.getNameWithoutPackage().length();
                        annotations.add(annotation);
                        // FIXASC underlining for SuppressWarnings doesn't seem right when included in messages
                    } else if (annoType.getName().equals("SuppressWarnings")
                            && (value instanceof ConstantExpression || value instanceof ListExpression)) {
                        if (value instanceof ListExpression) {
                            ListExpression listExpression = (ListExpression) value;
                            // FIXASC tidy up all this junk (err, i mean 'refactor') once we have confidence in test
                            // coverage
                            List<Expression> listOfExpressions = listExpression.getExpressions();
                            TypeReference annotationReference = createTypeReferenceForClassNode(annoType);
                            annotationReference.sourceStart = annotationNode.getStart();
                            annotationReference.sourceEnd = annotationNode.getEnd() - 1;
                            SingleMemberAnnotation annotation = new SingleMemberAnnotation(annotationReference,
                                    annotationNode.getStart());

                            ArrayInitializer arrayInitializer = new ArrayInitializer();
                            arrayInitializer.expressions = new org.eclipse.jdt.internal.compiler.ast.Expression[listOfExpressions
                                    .size()];
                            for (int c = 0; c < listOfExpressions.size(); c++) {
                                ConstantExpression cExpression = (ConstantExpression) listOfExpressions.get(c);
                                String v = (String) cExpression.getValue();
                                TypeReference ref = null;
                                int start = cExpression.getStart();
                                int end = cExpression.getEnd() - 1;
                                if (v.indexOf(".") == -1) {
                                    ref = new SingleTypeReference(v.toCharArray(), toPos(start, end - 1));
                                    annotation.declarationSourceEnd = annotation.sourceStart
                                            + annoType.getNameWithoutPackage().length() - 1;
                                } else {
                                    char[][] splits = CharOperation.splitOn('.', v.toCharArray());
                                    ref = new QualifiedTypeReference(splits,
                                            positionsFor(splits, start, end - 2));
                                    annotation.declarationSourceEnd = annotation.sourceStart
                                            + annoType.getName().length() - 1;
                                }
                                arrayInitializer.expressions[c] = new StringLiteral(v.toCharArray(), start, end,
                                        -1);
                            }
                            annotation.memberValue = arrayInitializer;
                            annotations.add(annotation);
                        } else {
                            ConstantExpression constantExpression = (ConstantExpression) value;
                            if (value.getType().getName().equals("java.lang.String")) {
                                // single value, eg. @SuppressWarnings("unchecked")
                                // FIXASC tidy up all this junk (err, i mean 'refactor') once we have confidence in test
                                // coverage
                                // FIXASC test positional info for conjured up anno refs
                                TypeReference annotationReference = createTypeReferenceForClassNode(annoType);
                                annotationReference.sourceStart = annotationNode.getStart();
                                annotationReference.sourceEnd = annotationNode.getEnd() - 1;
                                SingleMemberAnnotation annotation = new SingleMemberAnnotation(
                                        annotationReference, annotationNode.getStart());
                                String v = (String) constantExpression.getValue();
                                TypeReference ref = null;
                                int start = constantExpression.getStart();
                                int end = constantExpression.getEnd() - 1;
                                if (v.indexOf(".") == -1) {
                                    ref = new SingleTypeReference(v.toCharArray(), toPos(start, end - 1));
                                    annotation.declarationSourceEnd = annotation.sourceStart
                                            + annoType.getNameWithoutPackage().length() - 1;
                                } else {
                                    char[][] splits = CharOperation.splitOn('.', v.toCharArray());
                                    ref = new QualifiedTypeReference(splits,
                                            positionsFor(splits, start, end - 2));
                                    annotation.declarationSourceEnd = annotation.sourceStart
                                            + annoType.getName().length() - 1;
                                }
                                annotation.memberValue = new StringLiteral(v.toCharArray(), start, end, -1);
                                annotations.add(annotation);
                            }
                        }
                    }
                } else if (annoType.getNameWithoutPackage().equals("Test")) {
                    // normal annotation (with at least one member value pair)
                    // GRECLIPSE-569
                    // treat as a marker annotation
                    // this is specifically written so that annotations like @Test(expected = FooException) can be found
                    TypeReference annotationReference = createTypeReferenceForClassNode(annoType);
                    annotationReference.sourceStart = annotationNode.getStart();
                    annotationReference.sourceEnd = annotationNode.getEnd();

                    MarkerAnnotation annotation = new MarkerAnnotation(annotationReference,
                            annotationNode.getStart());
                    annotation.declarationSourceEnd = annotation.sourceEnd;
                    annotations.add(annotation);
                }
            }
        }
        if (annotations.size() > 0) {
            return annotations.toArray(new Annotation[annotations.size()]);
        }
    }
    return null;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitStructureRequestor.java

License:Open Source License

private Annotation convertToJDTAnnotation(
        org.aspectj.org.eclipse.jdt.internal.compiler.ast.Annotation ajAnnotation) {
    Annotation jdtAnnotation = null;
    if (ajAnnotation != null) {
        if (ajAnnotation instanceof org.aspectj.org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation) {
            jdtAnnotation = new MarkerAnnotation(convertToJDTTypeReference(ajAnnotation.type),
                    ajAnnotation.sourceStart);
        } else if (ajAnnotation instanceof org.aspectj.org.eclipse.jdt.internal.compiler.ast.NormalAnnotation) {
            org.aspectj.org.eclipse.jdt.internal.compiler.ast.NormalAnnotation castedAJ = (org.aspectj.org.eclipse.jdt.internal.compiler.ast.NormalAnnotation) ajAnnotation;
            NormalAnnotation castedJDT = new NormalAnnotation(convertToJDTTypeReference(castedAJ.type),
                    castedAJ.sourceStart);
            if (castedAJ.memberValuePairs != null) {
                castedJDT.memberValuePairs = new MemberValuePair[castedAJ.memberValuePairs.length];
                for (int j = 0; j < castedAJ.memberValuePairs.length; j++) {
                    org.aspectj.org.eclipse.jdt.internal.compiler.ast.MemberValuePair ajMVP = castedAJ.memberValuePairs[j];
                    if (ajMVP != null) {
                        MemberValuePair jdtMVP = new MemberValuePair(ajMVP.name, ajMVP.sourceStart,
                                ajMVP.sourceEnd, convertToJDTExpression(ajMVP.value));
                        jdtMVP.bits = ajMVP.bits;
                        castedJDT.memberValuePairs[j] = jdtMVP;
                    }/*www  . ja  v a 2  s.  c  om*/
                }
            }
            jdtAnnotation = castedJDT;
        } else { // SingleMemberAnnotation
            org.aspectj.org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation castedAJ = (org.aspectj.org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation) ajAnnotation;
            SingleMemberAnnotation castedJDT = new SingleMemberAnnotation(
                    convertToJDTTypeReference(castedAJ.type), castedAJ.sourceStart);
            castedJDT.memberValue = convertToJDTExpression(castedAJ.memberValue);

            jdtAnnotation = castedJDT;
        }
        jdtAnnotation.sourceEnd = ajAnnotation.sourceEnd;
        jdtAnnotation.declarationSourceEnd = ajAnnotation.declarationSourceEnd;
        jdtAnnotation.implicitConversion = ajAnnotation.implicitConversion;
        jdtAnnotation.bits = ajAnnotation.bits;
        jdtAnnotation.statementEnd = ajAnnotation.statementEnd;
    }
    return jdtAnnotation;
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void consumeMarkerAnnotation() {
    // MarkerAnnotation ::= '@' Name
    MarkerAnnotation markerAnnotation = null;

    int oldIndex = this.identifierPtr;

    TypeReference typeReference = getAnnotationType();
    markerAnnotation = new MarkerAnnotation(typeReference, this.intStack[this.intPtr--]);
    markerAnnotation.declarationSourceEnd = markerAnnotation.sourceEnd;
    pushOnExpressionStack(markerAnnotation);
    if (!this.statementRecoveryActivated && this.options.sourceLevel < ClassFileConstants.JDK1_5
            && this.lastErrorEndPositionBeforeRecovery < this.scanner.currentPosition) {
        problemReporter().invalidUsageOfAnnotation(markerAnnotation);
    }//w ww. j a v  a 2s. c  o  m
    this.recordStringLiterals = true;

    if (this.currentElement != null && this.currentElement instanceof RecoveredAnnotation) {
        this.currentElement = ((RecoveredAnnotation) this.currentElement).addAnnotation(markerAnnotation,
                oldIndex);
    }
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.AstClone.java

License:Open Source License

private static Annotation[] copyAnnotations(Annotation[] annotations, AstGenerator gen) {
    int length = annotations.length;
    Annotation[] result = new Annotation[length];
    int count = 0;
    for (int i = 0; i < annotations.length; i++) {
        if (annotations[i] instanceof MarkerAnnotation) { // refuse to generate complex annotations
            // don't blindly copy @Override:
            if (annotations[i].resolvedType != null) {
                if (annotations[i].resolvedType.id == TypeIds.T_JavaLangOverride)
                    continue;
            } else {
                if (CharOperation.equals(annotations[i].type.getLastToken(),
                        TypeConstants.JAVA_LANG_OVERRIDE[2]))
                    continue;
            }/*w  w w  . j  a va 2s.c  om*/
            TypeReference ref = copyTypeReference(annotations[i].type);
            result[count++] = new MarkerAnnotation(ref, annotations[i].sourceStart);
        }
    }
    if (count < length)
        System.arraycopy(result, 0, result = new Annotation[count], 0, count);
    return result;
}