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

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

Introduction

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

Prototype

public SingleMemberAnnotation(TypeReference type, int sourceStart) 

Source Link

Usage

From source file:com.google.gwt.dev.javac.BinaryTypeReferenceRestrictionsCheckerTest.java

License:Apache License

/**
 * Creates a mock {@link CompilationUnitDeclaration} that has binary type
 * references in a superclass reference, in a method return type, in an
 * annotation and in a local variable declaration. It then checks that the we
 * find all of these locations except for the one used in an annotation.
 *///from www . java2s  . c  o m
public void testFindAllBinaryTypeReferenceSites() {
    CompilationResult compilationResult = new CompilationResult("TestCompilationUnit.java".toCharArray(), 0, 0,
            0);
    CompilationUnitDeclaration cud = new CompilationUnitDeclaration(null, compilationResult, 1);
    LookupEnvironment lookupEnvironment = createMockLookupEnvironment();
    cud.scope = new CompilationUnitScope(cud, lookupEnvironment);

    TypeDeclaration typeDeclaration = new TypeDeclaration(compilationResult);
    typeDeclaration.scope = new ClassScope(cud.scope, null);
    typeDeclaration.staticInitializerScope = new MethodScope(typeDeclaration.scope, null, false);
    cud.types = new TypeDeclaration[] { typeDeclaration };

    BinaryTypeBinding binaryTypeBinding = new BinaryTypeBinding(null, new MockBinaryType(BINARY_TYPE_NAME),
            lookupEnvironment);
    typeDeclaration.superclass = createMockBinaryTypeReference(binaryTypeBinding);

    MethodDeclaration methodDeclaration = new MethodDeclaration(compilationResult);
    methodDeclaration.scope = new MethodScope(typeDeclaration.scope, null, false);
    methodDeclaration.returnType = createMockBinaryTypeReference(binaryTypeBinding);

    LocalDeclaration localDeclaration = new LocalDeclaration(null, 0, 0);
    localDeclaration.type = createMockBinaryTypeReference(binaryTypeBinding);
    methodDeclaration.statements = new Statement[] { localDeclaration };

    SingleMemberAnnotation annotation = new SingleMemberAnnotation(
            createMockBinaryTypeReference(binaryTypeBinding), 0);
    annotation.memberValue = annotation.type;
    typeDeclaration.annotations = new Annotation[] { annotation };

    typeDeclaration.methods = new AbstractMethodDeclaration[] { methodDeclaration };

    /*
     * Check that we find binary type references in the following expected
     * locations.
     */
    Expression[] expectedExpressions = new Expression[] { typeDeclaration.superclass,
            methodDeclaration.returnType, localDeclaration.type };

    List<BinaryTypeReferenceSite> binaryTypeReferenceSites = BinaryTypeReferenceRestrictionsChecker
            .findAllBinaryTypeReferenceSites(cud);
    assertEquals(expectedExpressions.length, binaryTypeReferenceSites.size());
    for (int i = 0; i < binaryTypeReferenceSites.size(); ++i) {
        BinaryTypeReferenceSite binaryTypeReferenceSite = binaryTypeReferenceSites.get(i);
        assertSame(binaryTypeBinding, binaryTypeReferenceSite.getBinaryTypeBinding());
        assertSame(expectedExpressions[i], binaryTypeReferenceSite.getExpression());
    }
}

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  . ja v a 2s  .  c om
        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 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);/*w w  w  . j av a  2s . c om*/
        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  a v a  2  s .  com*/

            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.HandleConstructor.java

License:Open Source License

public static Annotation[] createConstructorProperties(ASTNode source, Collection<EclipseNode> fields) {
    if (fields.isEmpty())
        return null;

    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    long[] poss = new long[3];
    Arrays.fill(poss, p);/*from ww w.  j  ava 2  s .  c o m*/
    QualifiedTypeReference constructorPropertiesType = new QualifiedTypeReference(
            JAVA_BEANS_CONSTRUCTORPROPERTIES, poss);
    setGeneratedBy(constructorPropertiesType, source);
    SingleMemberAnnotation ann = new SingleMemberAnnotation(constructorPropertiesType, pS);
    ann.declarationSourceEnd = pE;

    ArrayInitializer fieldNames = new ArrayInitializer();
    fieldNames.sourceStart = pS;
    fieldNames.sourceEnd = pE;
    fieldNames.expressions = new Expression[fields.size()];

    int ctr = 0;
    for (EclipseNode field : fields) {
        char[] fieldName = removePrefixFromField(field);
        fieldNames.expressions[ctr] = new StringLiteral(fieldName, pS, pE, 0);
        setGeneratedBy(fieldNames.expressions[ctr], source);
        ctr++;
    }

    ann.memberValue = fieldNames;
    setGeneratedBy(ann, source);
    setGeneratedBy(ann.memberValue, source);
    return new Annotation[] { ann };
}

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  av  a2 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;
                    }//from w w w  .j  a  v  a 2  s  .c o  m
                }
            }
            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 consumeSingleMemberAnnotation() {
    // SingleMemberAnnotation ::= '@' Name '(' MemberValue ')'
    SingleMemberAnnotation singleMemberAnnotation = null;

    int oldIndex = this.identifierPtr;

    TypeReference typeReference = getAnnotationType();
    singleMemberAnnotation = new SingleMemberAnnotation(typeReference, this.intStack[this.intPtr--]);
    singleMemberAnnotation.memberValue = this.expressionStack[this.expressionPtr--];
    this.expressionLengthPtr--;
    singleMemberAnnotation.declarationSourceEnd = this.rParenPos;
    pushOnExpressionStack(singleMemberAnnotation);

    if (this.currentElement != null) {
        annotationRecoveryCheckPoint(singleMemberAnnotation.sourceStart,
                singleMemberAnnotation.declarationSourceEnd);

        if (this.currentElement instanceof RecoveredAnnotation) {
            this.currentElement = ((RecoveredAnnotation) this.currentElement)
                    .addAnnotation(singleMemberAnnotation, oldIndex);
        }//from w w w .  j  av a 2  s  . co  m
    }

    if (!this.statementRecoveryActivated && this.options.sourceLevel < ClassFileConstants.JDK1_5
            && this.lastErrorEndPositionBeforeRecovery < this.scanner.currentPosition) {
        problemReporter().invalidUsageOfAnnotation(singleMemberAnnotation);
    }
    this.recordStringLiterals = true;
}

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

License:Open Source License

public SingleMemberAnnotation singleMemberAnnotation(char[][] compoundName, Expression memberValue) {
    SingleMemberAnnotation result = new SingleMemberAnnotation(qualifiedTypeReference(compoundName),
            this.sourceStart);
    result.sourceEnd = this.sourceEnd;
    result.declarationSourceEnd = this.sourceEnd;
    result.memberValue = memberValue;//from  w  ww.  ja v a 2s .c  o  m
    return result;
}

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

License:Open Source License

public SingleMemberAnnotation singleStringsMemberAnnotation(char[][] compoundName, char[][] memberValues) {
    SingleMemberAnnotation result = new SingleMemberAnnotation(qualifiedTypeReference(compoundName),
            this.sourceStart);
    result.sourceEnd = this.sourceEnd;
    result.declarationSourceEnd = this.sourceEnd;
    ArrayInitializer arrayInitializer = new ArrayInitializer();
    arrayInitializer.expressions = new Expression[memberValues.length];
    for (int i = 0; i < memberValues.length; i++)
        arrayInitializer.expressions[i] = stringLiteral(memberValues[i]);
    result.memberValue = arrayInitializer;
    return result;
}