示例#1
0
  private IAnnotation getAnnotation(
      final org.eclipse.jdt.internal.compiler.ast.Annotation annotation,
      JavaElement parentElement) {
    final int typeStart = annotation.type.sourceStart();
    final int typeEnd = annotation.type.sourceEnd();
    final int sourceStart = annotation.sourceStart();
    final int sourceEnd = annotation.declarationSourceEnd;
    class LocalVarAnnotation extends Annotation {
      IMemberValuePair[] memberValuePairs;

      public LocalVarAnnotation(JavaElement localVar, String elementName) {
        super(localVar, elementName);
      }

      public IMemberValuePair[] getMemberValuePairs() throws JavaModelException {
        return this.memberValuePairs;
      }

      public ISourceRange getNameRange() throws JavaModelException {
        return new SourceRange(typeStart, typeEnd - typeStart + 1);
      }

      public ISourceRange getSourceRange() throws JavaModelException {
        return new SourceRange(sourceStart, sourceEnd - sourceStart + 1);
      }

      public boolean exists() {
        return this.parent.exists();
      }
    }
    String annotationName =
        new String(CharOperation.concatWith(annotation.type.getTypeName(), '.'));
    LocalVarAnnotation localVarAnnotation = new LocalVarAnnotation(parentElement, annotationName);
    org.eclipse.jdt.internal.compiler.ast.MemberValuePair[] astMemberValuePairs =
        annotation.memberValuePairs();
    int length;
    IMemberValuePair[] memberValuePairs;
    if (astMemberValuePairs == null || (length = astMemberValuePairs.length) == 0) {
      memberValuePairs = Annotation.NO_MEMBER_VALUE_PAIRS;
    } else {
      memberValuePairs = new IMemberValuePair[length];
      for (int i = 0; i < length; i++) {
        org.eclipse.jdt.internal.compiler.ast.MemberValuePair astMemberValuePair =
            astMemberValuePairs[i];
        MemberValuePair memberValuePair = new MemberValuePair(new String(astMemberValuePair.name));
        memberValuePair.value =
            getAnnotationMemberValue(memberValuePair, astMemberValuePair.value, localVarAnnotation);
        memberValuePairs[i] = memberValuePair;
      }
    }
    localVarAnnotation.memberValuePairs = memberValuePairs;
    return localVarAnnotation;
  }
示例#2
0
 /*
  * Creates the value wrapper from the given expression, and sets the valueKind on the given memberValuePair
  */
 private Object getAnnotationMemberValue(
     MemberValuePair memberValuePair, Expression expression, JavaElement parentElement) {
   if (expression instanceof NullLiteral) {
     return null;
   } else if (expression instanceof Literal) {
     ((Literal) expression).computeConstant();
     return Util.getAnnotationMemberValue(memberValuePair, expression.constant);
   } else if (expression instanceof org.eclipse.jdt.internal.compiler.ast.Annotation) {
     memberValuePair.valueKind = IMemberValuePair.K_ANNOTATION;
     return getAnnotation(
         (org.eclipse.jdt.internal.compiler.ast.Annotation) expression, parentElement);
   } else if (expression instanceof ClassLiteralAccess) {
     ClassLiteralAccess classLiteral = (ClassLiteralAccess) expression;
     char[] typeName = CharOperation.concatWith(classLiteral.type.getTypeName(), '.');
     memberValuePair.valueKind = IMemberValuePair.K_CLASS;
     return new String(typeName);
   } else if (expression instanceof QualifiedNameReference) {
     char[] qualifiedName =
         CharOperation.concatWith(((QualifiedNameReference) expression).tokens, '.');
     memberValuePair.valueKind = IMemberValuePair.K_QUALIFIED_NAME;
     return new String(qualifiedName);
   } else if (expression instanceof SingleNameReference) {
     char[] simpleName = ((SingleNameReference) expression).token;
     if (simpleName == RecoveryScanner.FAKE_IDENTIFIER) {
       memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
       return null;
     }
     memberValuePair.valueKind = IMemberValuePair.K_SIMPLE_NAME;
     return new String(simpleName);
   } else if (expression instanceof ArrayInitializer) {
     memberValuePair.valueKind = -1; // modified below by the first call to getMemberValue(...)
     Expression[] expressions = ((ArrayInitializer) expression).expressions;
     int length = expressions == null ? 0 : expressions.length;
     Object[] values = new Object[length];
     for (int i = 0; i < length; i++) {
       int previousValueKind = memberValuePair.valueKind;
       Object value = getAnnotationMemberValue(memberValuePair, expressions[i], parentElement);
       if (previousValueKind != -1 && memberValuePair.valueKind != previousValueKind) {
         // values are heterogeneous, value kind is thus unknown
         memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
       }
       values[i] = value;
     }
     if (memberValuePair.valueKind == -1) memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
     return values;
   } else {
     memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
     return null;
   }
 }