Search in sources :

Example 91 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project androidannotations by androidannotations.

the class RoboGuiceHandler method extractListenerTypeMirrors.

private List<TypeMirror> extractListenerTypeMirrors(Element activityElement) {
    List<? extends AnnotationMirror> annotationMirrors = activityElement.getAnnotationMirrors();
    String annotationName = RoboGuice.class.getName();
    for (AnnotationMirror annotationMirror : annotationMirrors) {
        if (annotationName.equals(annotationMirror.getAnnotationType().toString())) {
            for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet()) {
                if ("value".equals(entry.getKey().getSimpleName().toString())) {
                    AnnotationValue action = entry.getValue();
                    @SuppressWarnings("unchecked") List<AnnotationValue> elements = (List<AnnotationValue>) action.getValue();
                    List<TypeMirror> listenerTypeMirrors = new ArrayList<>(elements.size());
                    for (AnnotationValue annotationValue : elements) {
                        listenerTypeMirrors.add((TypeMirror) annotationValue.getValue());
                    }
                    return listenerTypeMirrors;
                }
            }
        }
    }
    return Collections.emptyList();
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) AnnotationValue(javax.lang.model.element.AnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 92 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project graal by oracle.

the class LanguageRegistrationProcessor method validateFileTypeDetectors.

private boolean validateFileTypeDetectors(Element annotatedElement, AnnotationMirror mirror) {
    AnnotationValue value = ElementUtils.getAnnotationValue(mirror, "fileTypeDetectors", true);
    for (TypeMirror fileTypeDetectorType : ElementUtils.getAnnotationValueList(TypeMirror.class, mirror, "fileTypeDetectors")) {
        TypeElement fileTypeDetectorElement = ElementUtils.fromTypeMirror(fileTypeDetectorType);
        if (!fileTypeDetectorElement.getModifiers().contains(Modifier.PUBLIC)) {
            emitError("Registered FileTypeDetector class must be public.", annotatedElement, mirror, value);
            return false;
        }
        if (fileTypeDetectorElement.getEnclosingElement().getKind() != ElementKind.PACKAGE && !fileTypeDetectorElement.getModifiers().contains(Modifier.STATIC)) {
            emitError("Registered FileTypeDetector inner-class must be static.", annotatedElement, mirror, value);
            return false;
        }
        boolean foundConstructor = false;
        for (ExecutableElement constructor : ElementFilter.constructorsIn(fileTypeDetectorElement.getEnclosedElements())) {
            if (!constructor.getModifiers().contains(Modifier.PUBLIC)) {
                continue;
            }
            if (!constructor.getParameters().isEmpty()) {
                continue;
            }
            foundConstructor = true;
            break;
        }
        if (!foundConstructor) {
            emitError("A FileTypeDetector subclass must have a public no argument constructor.", annotatedElement, mirror, value);
            return false;
        }
    }
    return true;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) CodeAnnotationValue(com.oracle.truffle.dsl.processor.java.model.CodeAnnotationValue) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 93 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project graal by oracle.

the class OptionProcessor method processElement.

private boolean processElement(Element element, AnnotationMirror elementAnnotation, OptionsInfo info) {
    ProcessorContext context = ProcessorContext.getInstance();
    TruffleTypes types = context.getTypes();
    if (!element.getModifiers().contains(Modifier.STATIC)) {
        error(element, elementAnnotation, "Option field must be static");
        return false;
    }
    if (element.getModifiers().contains(Modifier.PRIVATE)) {
        error(element, elementAnnotation, "Option field cannot be private");
        return false;
    }
    List<String> groupPrefixStrings = null;
    AnnotationMirror prefix = ElementUtils.findAnnotationMirror(info.type, types.Option_Group);
    if (prefix != null) {
        groupPrefixStrings = ElementUtils.getAnnotationValueList(String.class, prefix, "value");
    } else {
        TypeMirror erasedTruffleType = context.getEnvironment().getTypeUtils().erasure(types.TruffleLanguage);
        if (context.getEnvironment().getTypeUtils().isAssignable(info.type.asType(), erasedTruffleType)) {
            AnnotationMirror registration = ElementUtils.findAnnotationMirror(info.type, types.TruffleLanguage_Registration);
            if (registration != null) {
                String languageId = resolveLanguageId(info.type, registration);
                groupPrefixStrings = Arrays.asList(languageId);
                if (groupPrefixStrings.get(0).isEmpty()) {
                    error(element, elementAnnotation, "%s must specify an id such that Truffle options can infer their prefix.", types.TruffleLanguage_Registration.asElement().getSimpleName().toString());
                    return false;
                }
            }
        } else if (context.getEnvironment().getTypeUtils().isAssignable(info.type.asType(), types.TruffleInstrument)) {
            AnnotationMirror registration = ElementUtils.findAnnotationMirror(info.type, types.TruffleInstrument_Registration);
            if (registration != null) {
                groupPrefixStrings = Arrays.asList(ElementUtils.getAnnotationValue(String.class, registration, "id"));
                if (groupPrefixStrings.get(0).isEmpty()) {
                    error(element, elementAnnotation, "%s must specify an id such that Truffle options can infer their prefix.", types.TruffleInstrument_Registration.asElement().getSimpleName().toString());
                    return false;
                }
            }
        }
    }
    if (groupPrefixStrings == null || groupPrefixStrings.isEmpty()) {
        groupPrefixStrings = Arrays.asList("");
    }
    AnnotationMirror annotation = ElementUtils.findAnnotationMirror(element, types.Option);
    assert annotation != null;
    assert element instanceof VariableElement;
    assert element.getKind() == ElementKind.FIELD;
    VariableElement field = (VariableElement) element;
    String fieldName = field.getSimpleName().toString();
    Types typeUtils = processingEnv.getTypeUtils();
    TypeMirror fieldType = field.asType();
    if (fieldType.getKind() != TypeKind.DECLARED) {
        error(element, elementAnnotation, "Option field must be of type " + ElementUtils.getQualifiedName(types.OptionKey));
        return false;
    }
    if (!typeUtils.isSubtype(fieldType, typeUtils.erasure(types.OptionKey))) {
        error(element, elementAnnotation, "Option field type %s is not a subclass of %s", fieldType, types.OptionKey);
        return false;
    }
    if (!field.getModifiers().contains(Modifier.STATIC)) {
        error(element, elementAnnotation, "Option field must be static");
        return false;
    }
    if (field.getModifiers().contains(Modifier.PRIVATE)) {
        error(element, elementAnnotation, "Option field cannot be private");
        return false;
    }
    boolean optionMap = false;
    TypeMirror optionMapType = types.OptionMap;
    List<? extends TypeMirror> typeArguments = ((DeclaredType) fieldType).getTypeArguments();
    if (typeArguments.size() == 1) {
        optionMap = typeUtils.isSubtype(typeArguments.get(0), typeUtils.erasure(optionMapType));
    }
    String help = ElementUtils.getAnnotationValue(String.class, annotation, "help");
    if (help.length() != 0) {
        char firstChar = help.charAt(0);
        if (!Character.isUpperCase(firstChar)) {
            error(element, elementAnnotation, "Option help text must start with upper case letter");
            return false;
        }
    }
    String usageSyntax = ElementUtils.getAnnotationValue(String.class, annotation, "usageSyntax");
    AnnotationValue value = ElementUtils.getAnnotationValue(elementAnnotation, "name", false);
    String optionName;
    if (value == null) {
        optionName = fieldName;
    } else {
        optionName = ElementUtils.getAnnotationValue(String.class, annotation, "name");
    }
    // implementations.
    if (optionMap && optionName.contains(".")) {
        error(element, elementAnnotation, "Option (maps) cannot contain a '.' in the name");
        return false;
    }
    boolean deprecated = ElementUtils.getAnnotationValue(Boolean.class, annotation, "deprecated");
    VariableElement categoryElement = ElementUtils.getAnnotationValue(VariableElement.class, annotation, "category");
    String category = categoryElement != null ? categoryElement.getSimpleName().toString() : null;
    if (category == null) {
        category = "INTERNAL";
    }
    VariableElement stabilityElement = ElementUtils.getAnnotationValue(VariableElement.class, annotation, "stability");
    String stability = stabilityElement != null ? stabilityElement.getSimpleName().toString() : null;
    String deprecationMessage = ElementUtils.getAnnotationValue(String.class, annotation, "deprecationMessage");
    if (deprecationMessage.length() != 0) {
        if (!deprecated) {
            error(element, elementAnnotation, "Deprecation message can be specified only for deprecated options.");
            return false;
        }
        char firstChar = deprecationMessage.charAt(0);
        if (!Character.isUpperCase(firstChar)) {
            error(element, elementAnnotation, "Option deprecation message must start with upper case letter.");
            return false;
        }
    }
    for (String group : groupPrefixStrings) {
        String name;
        if (group.isEmpty() && optionName.isEmpty()) {
            error(element, elementAnnotation, "Both group and option name cannot be empty");
            continue;
        } else if (optionName.isEmpty()) {
            name = group;
        } else {
            if (group.isEmpty()) {
                name = optionName;
            } else {
                name = group + "." + optionName;
            }
        }
        info.options.add(new OptionInfo(name, help, field, elementAnnotation, deprecated, category, stability, optionMap, deprecationMessage, usageSyntax));
    }
    return true;
}
Also used : SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) VariableElement(javax.lang.model.element.VariableElement) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue) DeclaredType(javax.lang.model.type.DeclaredType)

Example 94 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project graal by oracle.

the class ExpectError method collectExpectedErrors.

private static void collectExpectedErrors(List<String> expectedErrors, Element element, TypeElement eee) {
    if (eee != null) {
        for (AnnotationMirror am : element.getAnnotationMirrors()) {
            if (am.getAnnotationType().asElement().equals(eee)) {
                Map<? extends ExecutableElement, ? extends AnnotationValue> vals = am.getElementValues();
                if (vals.size() == 1) {
                    AnnotationValue av = vals.values().iterator().next();
                    if (av.getValue() instanceof List) {
                        List<?> arr = (List<?>) av.getValue();
                        for (Object o : arr) {
                            if (o instanceof AnnotationValue) {
                                AnnotationValue ov = (AnnotationValue) o;
                                expectedErrors.add((String) ov.getValue());
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotationValue(javax.lang.model.element.AnnotationValue) List(java.util.List) ArrayList(java.util.ArrayList)

Example 95 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project graal by oracle.

the class AbstractBridgeParser method getAnnotationValueWithDefaults.

Object getAnnotationValueWithDefaults(AnnotationMirror mirror, String elementName) {
    ExecutableElement element = resolveElement(mirror, elementName);
    if (element == null) {
        throw new IllegalArgumentException("The " + elementName + " is not a valid annotation attribute.");
    }
    AnnotationValue annotationValue = elements.getElementValuesWithDefaults(mirror).get(element);
    return annotationValue == null ? null : annotationValue.getValue();
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue)

Aggregations

AnnotationValue (javax.lang.model.element.AnnotationValue)182 AnnotationMirror (javax.lang.model.element.AnnotationMirror)81 TypeElement (javax.lang.model.element.TypeElement)62 ArrayList (java.util.ArrayList)54 ExecutableElement (javax.lang.model.element.ExecutableElement)53 TypeMirror (javax.lang.model.type.TypeMirror)48 List (java.util.List)45 Test (org.junit.Test)30 DeclaredType (javax.lang.model.type.DeclaredType)27 Map (java.util.Map)20 HashSet (java.util.HashSet)19 HashMap (java.util.HashMap)18 VariableElement (javax.lang.model.element.VariableElement)15 Element (javax.lang.model.element.Element)14 ElementUtils.getAnnotationValue (com.oracle.truffle.dsl.processor.java.ElementUtils.getAnnotationValue)10 ElementUtils.fromTypeMirror (com.oracle.truffle.dsl.processor.java.ElementUtils.fromTypeMirror)9 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)8 ElementUtils.resolveAnnotationValue (com.oracle.truffle.dsl.processor.java.ElementUtils.resolveAnnotationValue)7 ElementUtils.unboxAnnotationValue (com.oracle.truffle.dsl.processor.java.ElementUtils.unboxAnnotationValue)7 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)7