Example usage for org.eclipse.jdt.internal.compiler.ast Annotation memberValuePairs

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

Introduction

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

Prototype

public abstract MemberValuePair[] memberValuePairs();

Source Link

Usage

From source file:com.android.build.gradle.tasks.annotations.Extractor.java

License:Apache License

@SuppressWarnings("unused")
static boolean hasSourceRetention(@NonNull Annotation[] annotations) {
    for (Annotation annotation : annotations) {
        String typeName = Extractor.getFqn(annotation);
        if ("java.lang.annotation.Retention".equals(typeName)) {
            MemberValuePair[] pairs = annotation.memberValuePairs();
            if (pairs == null || pairs.length != 1) {
                warning("Expected exactly one parameter passed to @Retention");
                return false;
            }//from   w  w  w .j  a va 2s. co  m
            MemberValuePair pair = pairs[0];
            Expression value = pair.value;
            if (value instanceof NameReference) {
                NameReference reference = (NameReference) value;
                Binding binding = reference.binding;
                if (binding != null) {
                    if (binding instanceof FieldBinding) {
                        FieldBinding fb = (FieldBinding) binding;
                        if ("SOURCE".equals(new String(fb.name)) && "java.lang.annotation.RetentionPolicy"
                                .equals(new String(fb.declaringClass.readableName()))) {
                            return true;
                        }
                    }
                }
            }
        }
    }

    return false;
}

From source file:com.android.build.gradle.tasks.annotations.Extractor.java

License:Apache License

public AnnotationData createData(@NonNull String name, @NonNull Annotation annotation) {
    MemberValuePair[] pairs = annotation.memberValuePairs();
    if (pairs == null || pairs.length == 0) {
        return new AnnotationData(name);
    }/*from   w  w  w .ja  va 2  s  .com*/
    return new AnnotationData(name, pairs);
}

From source file:com.codenvy.ide.ext.java.server.internal.core.CompilationUnitStructureRequestor.java

License:Open Source License

protected IAnnotation acceptAnnotation(org.eclipse.jdt.internal.compiler.ast.Annotation annotation,
        AnnotatableInfo parentInfo, JavaElement parentHandle) {
    String nameString = new String(CharOperation.concatWith(annotation.type.getTypeName(), '.'));
    Annotation handle = createAnnotation(parentHandle, nameString); //NB: occurenceCount is computed in resolveDuplicates
    resolveDuplicates(handle);//www .ja  v a2 s. c  o  m

    AnnotationInfo info = new AnnotationInfo();

    // populate the maps here as getValue(...) below may need them
    this.newElements.put(handle, info);
    this.handleStack.push(handle);

    info.setSourceRangeStart(annotation.sourceStart());
    info.nameStart = annotation.type.sourceStart();
    info.nameEnd = annotation.type.sourceEnd();
    MemberValuePair[] memberValuePairs = annotation.memberValuePairs();
    int membersLength = memberValuePairs.length;
    if (membersLength == 0) {
        info.members = Annotation.NO_MEMBER_VALUE_PAIRS;
    } else {
        info.members = getMemberValuePairs(memberValuePairs);
    }

    if (parentInfo != null) {
        IAnnotation[] annotations = parentInfo.annotations;
        int length = annotations.length;
        System.arraycopy(annotations, 0, annotations = new IAnnotation[length + 1], 0, length);
        annotations[length] = handle;
        parentInfo.annotations = annotations;
    }
    info.setSourceRangeEnd(annotation.declarationSourceEnd);
    this.handleStack.pop();
    return handle;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.LocalVariable.java

License:Open Source License

private IAnnotation getAnnotation(final org.eclipse.jdt.internal.compiler.ast.Annotation annotation,
        JavaElement parentElement) {//from  www. java  2  s.  c  o m
    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, localVar.manager, 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;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
 * Report matching in annotations.//from w  ww  .j ava 2  s.co  m
 * @param otherElements TODO
 */
protected void reportMatching(Annotation[] annotations, IJavaElement enclosingElement,
        IJavaElement[] otherElements, Binding elementBinding, MatchingNodeSet nodeSet, boolean matchedContainer,
        boolean enclosesElement) throws CoreException {
    for (int i = 0, al = annotations.length; i < al; i++) {
        Annotation annotationType = annotations[i];
        IJavaElement localAnnotation = null;
        IJavaElement[] otherAnnotations = null;
        int length = otherElements == null ? 0 : otherElements.length;
        boolean handlesCreated = false;

        // Look for annotation type ref
        TypeReference typeRef = annotationType.type;
        Integer level = (Integer) nodeSet.matchingNodes.removeKey(typeRef);
        if (level != null && enclosesElement && matchedContainer) {
            localAnnotation = createHandle(annotationType, (IAnnotatable) enclosingElement);
            if (length > 0) {
                otherAnnotations = new IJavaElement[length];
                for (int o = 0; o < length; o++) {
                    otherAnnotations[o] = createHandle(annotationType, (IAnnotatable) otherElements[o]);
                }
            }
            handlesCreated = true;
            this.patternLocator.matchReportReference(typeRef, enclosingElement, localAnnotation,
                    otherAnnotations, elementBinding, level.intValue(), this);
        }

        // Look for attribute ref
        MemberValuePair[] pairs = annotationType.memberValuePairs();
        for (int j = 0, pl = pairs.length; j < pl; j++) {
            MemberValuePair pair = pairs[j];
            level = (Integer) nodeSet.matchingNodes.removeKey(pair);
            if (level != null && enclosesElement) {
                ASTNode reference = (annotationType instanceof SingleMemberAnnotation)
                        ? (ASTNode) annotationType
                        : pair;
                if (!handlesCreated) {
                    localAnnotation = createHandle(annotationType, (IAnnotatable) enclosingElement);
                    if (length > 0) {
                        otherAnnotations = new IJavaElement[length];
                        for (int o = 0; o < length; o++) {
                            otherAnnotations[o] = createHandle(annotationType, (IAnnotatable) otherElements[o]);
                        }
                    }
                    handlesCreated = true;
                }
                this.patternLocator.matchReportReference(reference, enclosingElement, localAnnotation,
                        otherAnnotations, pair.binding, level.intValue(), this);
            }
        }

        // Look for reference inside annotation
        ASTNode[] nodes = nodeSet.matchingNodes(annotationType.sourceStart,
                annotationType.declarationSourceEnd);
        if (nodes != null) {
            if (!matchedContainer) {
                for (int j = 0, nl = nodes.length; j < nl; j++) {
                    nodeSet.matchingNodes.removeKey(nodes[j]);
                }
            } else {
                for (int j = 0, nl = nodes.length; j < nl; j++) {
                    ASTNode node = nodes[j];
                    level = (Integer) nodeSet.matchingNodes.removeKey(node);
                    if (enclosesElement) {
                        if (!handlesCreated) {
                            localAnnotation = createHandle(annotationType, (IAnnotatable) enclosingElement);
                            if (length > 0) {
                                otherAnnotations = new IJavaElement[length];
                                for (int o = 0; o < length; o++) {
                                    otherAnnotations[o] = createHandle(annotationType,
                                            (IAnnotatable) otherElements[o]);
                                }
                            }
                            handlesCreated = true;
                        }
                        this.patternLocator.matchReportReference(node, enclosingElement, localAnnotation,
                                otherAnnotations, elementBinding, level.intValue(), this);
                    }
                }
            }
        }
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MethodLocator.java

License:Open Source License

public int match(Annotation node, MatchingNodeSet nodeSet) {
    if (!this.pattern.findReferences)
        return IMPOSSIBLE_MATCH;
    MemberValuePair[] pairs = node.memberValuePairs();
    if (pairs == null || pairs.length == 0)
        return IMPOSSIBLE_MATCH;

    int length = pairs.length;
    MemberValuePair pair = null;/*  w w w. j a  v  a  2s  . co  m*/
    for (int i = 0; i < length; i++) {
        pair = node.memberValuePairs()[i];
        if (matchesName(this.pattern.selector, pair.name)) {
            ASTNode possibleNode = (node instanceof SingleMemberAnnotation) ? (ASTNode) node : pair;
            return nodeSet.addMatch(possibleNode, this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH);
        }
    }
    return IMPOSSIBLE_MATCH;
}

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

License:Open Source License

static Set<String> getSuppressedWarnings(Annotation[] annotations) {
    if (annotations != null) {
        for (Annotation a : annotations) {
            if (SuppressWarnings.class.getName()
                    .equals(CharOperation.toString(((ReferenceBinding) a.resolvedType).compoundName))) {
                for (MemberValuePair pair : a.memberValuePairs()) {
                    if (String.valueOf(pair.name).equals("value")) {
                        Expression valueExpr = pair.value;
                        if (valueExpr instanceof StringLiteral) {
                            // @SuppressWarnings("Foo")
                            return Sets.create(((StringLiteral) valueExpr).constant.stringValue()
                                    .toLowerCase(Locale.ENGLISH));
                        } else if (valueExpr instanceof ArrayInitializer) {
                            // @SuppressWarnings({ "Foo", "Bar"})
                            ArrayInitializer ai = (ArrayInitializer) valueExpr;
                            String[] values = new String[ai.expressions.length];
                            for (int i = 0, j = values.length; i < j; i++) {
                                values[i] = ((StringLiteral) ai.expressions[i]).constant.stringValue()
                                        .toLowerCase(Locale.ENGLISH);
                            }//from   www. ja v a  2  s  .c  o m
                            return Sets.create(values);
                        } else {
                            throw new InternalCompilerException(
                                    "Unable to analyze SuppressWarnings annotation");
                        }
                    }
                }
            }
        }
    }
    return Sets.create();
}

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

License:Apache License

Set<String> getSuppressedWarnings(Annotation[] annotations) {
    if (annotations == null) {
        return ImmutableSet.of();
    }//  w ww.j  a v a 2  s . c o  m

    for (Annotation a : annotations) {
        if (!SuppressWarnings.class.getName()
                .equals(CharOperation.toString(((ReferenceBinding) a.resolvedType).compoundName))) {
            continue;
        }
        for (MemberValuePair pair : a.memberValuePairs()) {
            if (!String.valueOf(pair.name).equals("value")) {
                continue;
            }
            Expression valueExpr = pair.value;
            if (valueExpr instanceof StringLiteral) {
                // @SuppressWarnings("Foo")
                return ImmutableSet
                        .of(((StringLiteral) valueExpr).constant.stringValue().toLowerCase(Locale.ROOT));
            } else if (valueExpr instanceof ArrayInitializer) {
                // @SuppressWarnings({ "Foo", "Bar"})
                ArrayInitializer ai = (ArrayInitializer) valueExpr;
                ImmutableSet.Builder valuesSetBuilder = ImmutableSet.builder();
                for (int i = 0, j = ai.expressions.length; i < j; i++) {
                    if ((ai.expressions[i]) instanceof StringLiteral) {
                        StringLiteral expression = (StringLiteral) ai.expressions[i];
                        valuesSetBuilder.add(expression.constant.stringValue().toLowerCase(Locale.ROOT));
                    } else {
                        suppressionAnnotationWarning(a, "Unable to analyze SuppressWarnings annotation, "
                                + ai.expressions[i].toString() + " not a string constant.");
                    }
                }
                return valuesSetBuilder.build();
            } else {
                suppressionAnnotationWarning(a, "Unable to analyze SuppressWarnings annotation, "
                        + valueExpr.toString() + " not a string constant.");
            }
        }
    }
    return ImmutableSet.of();
}

From source file:lombok.eclipse.agent.PatchDelegate.java

License:Open Source License

private static List<ClassLiteralAccess> rawTypes(Annotation ann, String name) {
    List<ClassLiteralAccess> rawTypes = new ArrayList<ClassLiteralAccess>();
    for (MemberValuePair pair : ann.memberValuePairs()) {
        if (charArrayEquals(name, pair.name)) {
            if (pair.value instanceof ArrayInitializer) {
                for (Expression expr : ((ArrayInitializer) pair.value).expressions) {
                    if (expr instanceof ClassLiteralAccess)
                        rawTypes.add((ClassLiteralAccess) expr);
                }//from w  w  w .  j  a va 2  s . c  om
            }
            if (pair.value instanceof ClassLiteralAccess) {
                rawTypes.add((ClassLiteralAccess) pair.value);
            }
        }
    }
    return rawTypes;
}

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

License:Open Source License

/**
 * Provides AnnotationValues with the data it needs to do its thing.
 *///w ww .jav  a 2  s .co m
public static <A extends java.lang.annotation.Annotation> AnnotationValues<A> createAnnotation(Class<A> type,
        final EclipseNode annotationNode) {

    final Annotation annotation = (Annotation) annotationNode.get();
    Map<String, AnnotationValue> values = new HashMap<String, AnnotationValue>();

    MemberValuePair[] memberValuePairs = annotation.memberValuePairs();

    if (memberValuePairs != null)
        for (final MemberValuePair pair : memberValuePairs) {
            List<String> raws = new ArrayList<String>();
            List<Object> expressionValues = new ArrayList<Object>();
            List<Object> guesses = new ArrayList<Object>();
            Expression[] expressions = null;

            char[] n = pair.name;
            String mName = (n == null || n.length == 0) ? "value" : new String(pair.name);
            final Expression rhs = pair.value;
            if (rhs instanceof ArrayInitializer) {
                expressions = ((ArrayInitializer) rhs).expressions;
            } else if (rhs != null) {
                expressions = new Expression[] { rhs };
            }
            if (expressions != null)
                for (Expression ex : expressions) {
                    StringBuffer sb = new StringBuffer();
                    ex.print(0, sb);
                    raws.add(sb.toString());
                    expressionValues.add(ex);
                    guesses.add(calculateValue(ex));
                }

            final Expression[] exprs = expressions;
            values.put(mName, new AnnotationValue(annotationNode, raws, expressionValues, guesses, true) {
                @Override
                public void setError(String message, int valueIdx) {
                    Expression ex;
                    if (valueIdx == -1)
                        ex = rhs;
                    else
                        ex = exprs != null ? exprs[valueIdx] : null;

                    if (ex == null)
                        ex = annotation;

                    int sourceStart = ex.sourceStart;
                    int sourceEnd = ex.sourceEnd;

                    annotationNode.addError(message, sourceStart, sourceEnd);
                }

                @Override
                public void setWarning(String message, int valueIdx) {
                    Expression ex;
                    if (valueIdx == -1)
                        ex = rhs;
                    else
                        ex = exprs != null ? exprs[valueIdx] : null;

                    if (ex == null)
                        ex = annotation;

                    int sourceStart = ex.sourceStart;
                    int sourceEnd = ex.sourceEnd;

                    annotationNode.addWarning(message, sourceStart, sourceEnd);
                }
            });
        }

    for (Method m : type.getDeclaredMethods()) {
        if (!Modifier.isPublic(m.getModifiers()))
            continue;
        String name = m.getName();
        if (!values.containsKey(name)) {
            values.put(name, new AnnotationValue(annotationNode, new ArrayList<String>(),
                    new ArrayList<Object>(), new ArrayList<Object>(), false) {
                @Override
                public void setError(String message, int valueIdx) {
                    annotationNode.addError(message);
                }

                @Override
                public void setWarning(String message, int valueIdx) {
                    annotationNode.addWarning(message);
                }
            });
        }
    }

    return new AnnotationValues<A>(type, values, annotationNode);
}