Example usage for javax.lang.model.element AnnotationMirror getElementValues

List of usage examples for javax.lang.model.element AnnotationMirror getElementValues

Introduction

In this page you can find the example usage for javax.lang.model.element AnnotationMirror getElementValues.

Prototype

Map<? extends ExecutableElement, ? extends AnnotationValue> getElementValues();

Source Link

Document

Returns the values of this annotation's elements.

Usage

From source file:org.leandreck.endpoints.processor.model.VariableAnnotations.java

public static boolean isOptional(final AnnotationMirror annotationMirror) {
    final boolean relevantAnnotation = Arrays.stream(VariableAnnotations.values())
            .map(it -> annotationMirror.getAnnotationType().toString().startsWith(it.annotation))
            .reduce((a, b) -> a || b).orElse(false);

    final boolean optional;
    if (relevantAnnotation) {
        final String required = annotationMirror.getElementValues().entrySet().stream()
                .filter(e -> e.getKey().toString().equals("required()")).map(e -> e.getValue().toString())
                .findFirst().orElse("true");
        optional = !Boolean.valueOf(required);
    } else {//from  w w w .  j  a  va 2  s.c o  m
        optional = false;
    }

    return optional;
}

From source file:cop.raml.processor.RestProcessor.java

private static AnnotationValue getAnnotationValue(AnnotationMirror mirror, String valueName) {
    if (mirror == null || StringUtils.isBlank(valueName))
        return null;

    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : mirror.getElementValues()
            .entrySet())/*  ww w . j  a v a2 s .co  m*/
        if (valueName.equals(entry.getKey().toString()))
            return entry.getValue();

    return null;
}

From source file:com.github.hexocraftapi.checkargs.processor.AnnotatedClass.java

/**
 * @param classElement TypeElement/*from w  w  w . j a  va2 s  . c  o  m*/
 * @param elementUtils TypeElement
 * @throws ClassNotFoundException ClassNotFoundException
 */
public AnnotatedClass(TypeElement classElement, Elements elementUtils) throws ClassNotFoundException {
    this.annotatedClassElement = classElement;

    CheckArgs annotation = classElement.getAnnotation(CheckArgs.class);
    List<? extends AnnotationMirror> annotationMirrors = elementUtils.getAllAnnotationMirrors(classElement);

    for (AnnotationMirror annotationMirror : annotationMirrors) {
        Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror
                .getElementValues();

        for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : elementValues
                .entrySet()) {
            String key = entry.getKey().getSimpleName().toString();
            Object value = entry.getValue().getValue();

            switch (key) {
            case "count":
                this.count = (int) value;
                break;
            case "types":
                List<? extends AnnotationValue> typeMirrors = (List<? extends AnnotationValue>) value;
                for (AnnotationValue annotationValue : typeMirrors) {
                    String clazz = annotationValue.getValue().toString();
                    this.qualifiedClassName.add(clazz);
                    this.simpleClassName.add(clazz.substring(clazz.lastIndexOf(".") + 1));
                }
                break;
            }
        }
    }

    if (this.qualifiedClassName.size() > 0 && this.count != this.qualifiedClassName.size())
        this.count = this.qualifiedClassName.size();
}

From source file:io.github.jeddict.jcode.util.JavaSourceHelper.java

public static boolean annotationHasAttributeValue(AnnotationMirror am, String attr, String value) {
    return value.equals(am.getElementValues().get(attr).getValue());
}

From source file:io.github.jeddict.jcode.util.JavaSourceHelper.java

public static boolean annotationHasAttributeValue(AnnotationMirror am, String value) {
    if (am != null) {
        for (AnnotationValue av : am.getElementValues().values()) {
            if (value.equals(av.getValue())) {
                return true;
            }//from   w w  w  . j a  va 2 s . co  m
        }
    }
    return false;
}

From source file:io.github.jeddict.jcode.util.JavaSourceHelper.java

public static Collection<String> getAnnotationValuesForAllMethods(JavaSource source, final String annotation) {
    final Collection<String> results = new HashSet<>();
    try {/*  w  ww.j  a va 2  s  .  co m*/
        source.runUserActionTask(new AbstractTask<CompilationController>() {

            public void run(CompilationController controller) throws IOException {
                controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
                TypeElement classElement = getTopLevelClassElement(controller);
                if (classElement == null) {
                    return;
                }
                List<ExecutableElement> methods = ElementFilter.methodsIn(classElement.getEnclosedElements());

                for (ExecutableElement method : methods) {
                    for (AnnotationMirror mirror : method.getAnnotationMirrors()) {
                        if (((TypeElement) mirror.getAnnotationType().asElement()).getQualifiedName()
                                .contentEquals(annotation)) {
                            for (AnnotationValue value : mirror.getElementValues().values()) {
                                results.add(value.getValue().toString());
                            }
                        }
                    }
                }
            }
        }, true);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }

    return results;
}

From source file:ch.rasc.constgen.CodeGenerator.java

private String getValue(VariableElement el) {
    String alternateValue = null;
    for (AnnotationMirror am : this.elements.getAllAnnotationMirrors(el)) {
        Name qualifiedName = ((TypeElement) am.getAnnotationType().asElement()).getQualifiedName();
        if (qualifiedName.contentEquals("org.springframework.data.mongodb.core.mapping.Field")
                || qualifiedName.contentEquals("org.mongodb.morphia.annotations.Property")
                || qualifiedName.contentEquals("ch.rasc.bsoncodec.annotation.Field")) {

            alternateValue = am.getElementValues().entrySet().stream()
                    .filter(e -> e.getKey().getSimpleName().toString().equals("value"))
                    .map(e -> (String) e.getValue().getValue())
                    .filter(s -> !".".equals(s) && !"".equals(s.trim())).findAny().orElse(null);
        } else if (qualifiedName.contentEquals("ch.rasc.bsoncodec.annotation.Id")) {
            alternateValue = "_id";
        }// w w w.java2s  .  c o m
    }
    if (alternateValue == null) {
        String simpleName = el.getSimpleName().toString();
        if (bsoncodecProject) {
            if ("id".equals(simpleName)) {
                return "_id";
            }
        }
        return simpleName;
    }
    return alternateValue;
}

From source file:com.contentful.vault.compiler.Processor.java

private void parseSpace(TypeElement element, Map<TypeElement, SpaceInjection> spaces,
        Map<TypeElement, ModelInjection> models) {
    Space annotation = element.getAnnotation(Space.class);
    String id = annotation.value();
    if (id.isEmpty()) {
        error(element, "@%s id may not be empty. (%s)", Space.class.getSimpleName(),
                element.getQualifiedName());
        return;/*from  w w  w.  ja  va 2 s . c o  m*/
    }

    TypeMirror spaceMirror = elementUtils.getTypeElement(Space.class.getName()).asType();
    List<ModelInjection> includedModels = new ArrayList<>();
    for (AnnotationMirror mirror : element.getAnnotationMirrors()) {
        if (typeUtils.isSameType(mirror.getAnnotationType(), spaceMirror)) {
            Set<? extends Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> items = mirror
                    .getElementValues().entrySet();

            for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : items) {
                if ("models".equals(entry.getKey().getSimpleName().toString())) {
                    List l = (List) entry.getValue().getValue();
                    if (l.size() == 0) {
                        error(element, "@%s models must not be empty. (%s)", Space.class.getSimpleName(),
                                element.getQualifiedName());
                        return;
                    }

                    Set<String> modelIds = new LinkedHashSet<>();
                    for (Object model : l) {
                        TypeElement e = (TypeElement) ((Type) ((Attribute) model).getValue()).asElement();
                        ModelInjection modelInjection = models.get(e);
                        if (modelInjection == null) {
                            return;
                        } else {
                            String rid = modelInjection.remoteId;
                            if (!modelIds.add(rid)) {
                                error(element, "@%s includes multiple models with the same id \"%s\". (%s)",
                                        Space.class.getSimpleName(), rid, element.getQualifiedName());
                                return;
                            }
                            includedModels.add(modelInjection);
                        }
                    }
                }
            }
        }
    }

    List<String> locales = Arrays.asList(annotation.locales());
    Set<String> checked = new HashSet<>();
    for (int i = locales.size() - 1; i >= 0; i--) {
        String code = locales.get(i);
        if (!checked.add(code)) {
            error(element, "@%s contains duplicate locale code '%s'. (%s)", Space.class.getSimpleName(), code,
                    element.getQualifiedName());
            return;
        } else if (code.contains(" ") || code.isEmpty()) {
            error(element, "Invalid locale code '%s', must not be empty and may not contain spaces. (%s)", code,
                    element.getQualifiedName());
            return;
        }
    }
    if (checked.size() == 0) {
        error(element, "@%s at least one locale must be configured. (%s)", Space.class.getSimpleName(),
                element.getQualifiedName());
        return;
    }

    ClassName injectionClassName = getInjectionClassName(element, SUFFIX_SPACE);
    String dbName = "space_" + SqliteUtils.hashForId(id);
    String copyPath = StringUtils.defaultIfBlank(annotation.copyPath(), null);
    spaces.put(element, new SpaceInjection(id, injectionClassName, element, includedModels, dbName,
            annotation.dbVersion(), copyPath, locales));
}

From source file:com.mastfrog.parameters.processor.Processor.java

private String findParamName(AnnotationMirror mir) {
    String result = null;/*w  ww.j  a  v  a 2s  . c  om*/
    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> e : mir.getElementValues()
            .entrySet()) {
        if (e.getKey().getSimpleName().contentEquals("value")) {
            if (e.getValue().getValue() instanceof String) {
                result = (String) e.getValue().getValue();
                break;
            }
        }
    }
    return result;
}

From source file:com.mastfrog.parameters.processor.Processor.java

private List<AnnotationMirror> findParamAnnotations(AnnotationMirror mir, List<AnnotationMirror> result) {
    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> e : mir.getElementValues()
            .entrySet()) {/*ww w  .  j a  v a 2  s . c  o m*/
        if ("value()".equals(e.getKey().toString())) {
            if (e.getValue().getValue() instanceof List) {
                List<?> l = (List<?>) e.getValue().getValue();
                for (Object o : l) {
                    if (o instanceof AnnotationMirror) {
                        AnnotationMirror param = (AnnotationMirror) o;
                        result.add(param);
                        findValidatorClassNames(param);
                    }
                }
            }
        }
    }
    return result;
}