private static boolean hasAnnotationWithName(Element element, String simpleName) { for (AnnotationMirror mirror : element.getAnnotationMirrors()) { String annotationName = mirror.getAnnotationType().asElement().getSimpleName().toString(); if (simpleName.equals(annotationName)) { return true; } } return false; }
private static void qualifierToString(AnnotationMirror qualifier, StringBuilder result) { // TODO: guarantee that element values are sorted by name (if there are multiple) result.append('@'); typeToString(qualifier.getAnnotationType(), result, '$'); result.append('('); for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : qualifier.getElementValues().entrySet()) { result.append(entry.getKey().getSimpleName()); result.append('='); result.append(entry.getValue().getValue()); } result.append(")/"); }
public static AnnotationSpec get(AnnotationMirror annotation) { TypeElement element = (TypeElement) annotation.getAnnotationType().asElement(); AnnotationSpec.Builder builder = AnnotationSpec.builder(ClassName.get(element)); Visitor visitor = new Visitor(builder); for (ExecutableElement executableElement : annotation.getElementValues().keySet()) { String name = executableElement.getSimpleName().toString(); AnnotationValue value = annotation.getElementValues().get(executableElement); value.accept(visitor, name); } return builder.build(); }
public boolean isCallbackMethodAnnotationValid(Element element, String annotationName) { TypeElement enclosingElement = (TypeElement)element.getEnclosingElement(); if (enclosingElement.getAnnotation(JsonObject.class) == null) { error(enclosingElement, "%s: @%s methods can only be in classes annotated with @%s.", enclosingElement.getQualifiedName(), annotationName, JsonObject.class.getSimpleName()); return false; } ExecutableElement executableElement = (ExecutableElement)element; if (executableElement.getParameters().size() > 0) { error(element, "%s: @%s methods must not take any parameters.", enclosingElement.getQualifiedName(), annotationName); return false; } List<? extends Element> allElements = enclosingElement.getEnclosedElements(); int methodInstances = 0; for (Element enclosedElement : allElements) { for (AnnotationMirror am : enclosedElement.getAnnotationMirrors()) { if (am.getAnnotationType().asElement().getSimpleName().toString().equals(annotationName)) { methodInstances++; } } } if (methodInstances != 1) { error(element, "%s: There can only be one @%s method per class.", enclosingElement.getQualifiedName(), annotationName); return false; } return true; }
TypeMirror dmc = type(DynamicMenuContent.class); for (Element e : roundEnv.getElementsAnnotatedWith(ActionRegistration.class)) { ActionRegistration ar = e.getAnnotation(ActionRegistration.class); if (ar == null) { continue; ActionID aid = e.getAnnotation(ActionID.class); if (aid == null) { throw new LayerGenerationException("@ActionRegistration can only be used together with @ActionID annotation", e, processingEnv, ar); if (e.getKind() == ElementKind.FIELD) { VariableElement var = (VariableElement)e; TypeMirror stringType = type(String.class); AnnotationMirror arMirror = null; for (AnnotationMirror m : e.getAnnotationMirrors()) { if (m.getAnnotationType().toString().equals(ActionRegistration.class.getCanonicalName())) { arMirror = m; for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> entry : m.getElementValues().entrySet()) { if (entry.getKey().getSimpleName().contentEquals("lazy")) { direct = ! (Boolean) entry.getValue().getValue(); assert direct == !ar.lazy(); break;
/** * Retrieve the string value inside an @Contract annotation without statically depending on the * type. * * @param sym A method which has an @Contract annotation. * @return The string value spec inside the annotation. */ private static @Nullable String getContractFromAnnotation(Symbol.MethodSymbol sym) { for (AnnotationMirror annotation : sym.getAnnotationMirrors()) { Element element = annotation.getAnnotationType().asElement(); assert element.getKind().equals(ElementKind.ANNOTATION_TYPE); if (((TypeElement) element) .getQualifiedName() .contentEquals("org.jetbrains.annotations.Contract")) { for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> e : annotation.getElementValues().entrySet()) { if (e.getKey().getSimpleName().contentEquals("value")) { String value = e.getValue().toString(); if (value.startsWith("\"") && value.endsWith("\"")) { value = value.substring(1, value.length() - 1); } return value; } } } } return null; } }
@Override public Iterable<? extends Completion> getCompletions(Element annotated, AnnotationMirror annotation, ExecutableElement attr, String userText) { if (processingEnv == null || annotated == null || !annotated.getKind().isClass()) { return Collections.emptyList(); || !"org.openide.util.lookup.ServiceProvider".contentEquals(((TypeElement) annotation.getAnnotationType().asElement()).getQualifiedName())) { return Collections.emptyList(); if (!"service".contentEquals(attr.getSimpleName())) { return Collections.emptyList(); TypeElement c = toProcess.remove(0); result.add(new TypeCompletion(c.getQualifiedName().toString() + ".class")); parents.add(c.getSuperclass()); parents.addAll(c.getInterfaces());
@Value.Lazy @Override public JacksonMode jacksonSerializeMode() { boolean wasJacksonSerialize = false; for (AnnotationMirror a : element().getAnnotationMirrors()) { TypeElement e = (TypeElement) a.getAnnotationType().asElement(); if (!wasJacksonSerialize && e.getQualifiedName().contentEquals(JACKSON_SERIALIZE)) { wasJacksonSerialize = true; } if (e.getQualifiedName().contentEquals(JACKSON_DESERIALIZE)) { for (ExecutableElement attr : a.getElementValues().keySet()) { if (attr.getSimpleName().contentEquals("builder")) { // If builder attribute is specified, we don't consider this as // our, immutables, business to generate anything. return JacksonMode.BUILDER; } } return JacksonMode.DELEGATED; } } return wasJacksonSerialize ? JacksonMode.DELEGATED : JacksonMode.NONE; }
private void handleBaseBuilder() { if (!MoreTypes.isTypeOf(Object.class, baseBuilder.asType())) { final List<ExecutableElement> constructors = ElementFilter.constructorsIn(baseBuilder.getEnclosedElements()); Optional<ExecutableElement> constructor = constructors.stream().filter(c -> c.getParameters().size() == 0).findAny(); if (constructor.isPresent()) { elements.add(modelFactory.fromDelegateConstructor(constructor.get(), false)); } else { constructor = constructors.stream().filter(c -> c.getParameters().size() == 1 && Types.CONTEXT.equals(TypeName.get(c.getParameters().get(0).asType()))).findAny(); if (constructor.isPresent()) { elements.add(modelFactory.fromDelegateConstructor(constructor.get(), true)); } else { final AnnotationMirror mirror = baseAnnotation.getAnnotationMirrors().stream() .filter(m -> MoreTypes.isTypeOf(Configuration.class, m.getAnnotationType())) .findAny().orElseThrow(IllegalArgumentException::new); messager.printMessage(Diagnostic.Kind.ERROR, "Classes used as base builder must have a constructor which takes no arguments, " + "or exactly one argument of type Class", baseAnnotation, mirror, mirror.getElementValues().entrySet().stream() .filter(entry -> entry.getKey().getSimpleName().toString().equals("builderSuperClass")).findAny().map(Map.Entry::getValue).orElse(null)); throw new IllegalArgumentException(); } } handleBaseBuilderMethods(); } }
if (!(mte.getTypeMirror() instanceof NoType)) { DeclaredType classTypeMirror = (DeclaredType) mte.getTypeMirror(); TypeElement classTypeElement = (TypeElement) classTypeMirror.asElement(); String targetActivityClassName = classTypeElement.getQualifiedName().toString(); blockBuilder.add(".setActivity(new $T(context, $T.class))\n", componentName, ClassName.bestGuess(targetActivityClassName)); final List<? extends AnnotationMirror> annotationMirrors = annotatedElement.getElement().getAnnotationMirrors(); for (final AnnotationMirror annotationMirror : annotationMirrors) { if (annotationMirror.getAnnotationType().toString().equals(Shortcut.class.getName())) { for (Map.Entry<? extends Element, ? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet()) { if ("backStack".equals(entry.getKey().getSimpleName().toString())) { final String value = entry.getValue().getValue().toString(); if (!"".equals(value.trim())) { for (final String backstackActivityClassName : value.split(",")) {
ElementType elementType, @Nullable NullabilityAnnotationInfo nullability) { String qualifiedName = annotationElement.getQualifiedName().toString(); && hasJacksonPackagePrefix(Iterables.getOnlyElement(includeAnnotations)))) { if (Proto.isAnnotatedWith(annotationElement, Proto.JACKSON_ANNOTATIONS_INSIDE)) { for (AnnotationMirror metaAnnotation : annotationElement.getAnnotationMirrors()) { TypeElement metaAnnotationElement = (TypeElement) metaAnnotation.getAnnotationType().asElement(); String qname = metaAnnotationElement.getQualifiedName().toString(); if (nullability != null && annotationElement.getQualifiedName().contentEquals(nullability.qualifiedName())) { if (element.getKind() != ElementKind.METHOD) { if (qualifiedName.equals(Proto.JACKSON_DESERIALIZE) || qualifiedName.equals(Proto.JACKSON_SERIALIZE)) { return false; for (AnnotationMirror parentAnnotation : annotationElement.getAnnotationMirrors()) { TypeElement parentElement = (TypeElement) parentAnnotation.getAnnotationType().asElement();
public ClassGenerator(TypeElement clz, boolean generateNoopConfigInjector) throws JClassAlreadyExistsException { this.clz = clz; this.generateNoopConfigInjector = generateNoopConfigInjector; Configured c = clz.getAnnotation(Configured.class); String name = clz.getQualifiedName().toString(); targetType = cm.ref(name); elementName = Dom.convertName(clz.getSimpleName().toString()); for (AnnotationMirror am : clz.getAnnotationMirrors()) { InhabitantAnnotation ia = am.getAnnotationType().asElement().getAnnotation(InhabitantAnnotation.class); if (ia != null) { targetHabitats.add(ia.value());
private static Map<String, Object> annotationKeyValues(AnnotationMirror mirror) { Map<String, Object> result = new LinkedHashMap<>(); for (ExecutableElement key : mirror.getElementValues().keySet()) { result.put(key.getSimpleName().toString(), mirror.getElementValues().get(key).getValue()); } return result; } }
public static AnnotationValue getAnnotationTypeMirrorValue(AnnotationMirror annotationMirror, String key) { for (Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet()) { if (entry.getKey().getSimpleName().contentEquals(key)) { return entry.getValue(); } } return null; }
uniqueKeyCodes.add(keyCode); Element enclosingElement = element.getEnclosingElement(); List<? extends Element> enclosedMethodElements = ElementFilter.methodsIn(enclosingElement.getEnclosedElements()); for (Element oneEnclosedElement : enclosedMethodElements) { if (oneEnclosedElement != element) { List<? extends AnnotationMirror> annotationMirrors = oneEnclosedElement.getAnnotationMirrors(); for (AnnotationMirror annotationMirror : annotationMirrors) { if (annotationMirror.getAnnotationType().asElement().toString().equals(targetAnnotationClass)) { int[] keyCodes = extractKeyCode(oneEnclosedElement); for (int keyCode : keyCodes) {
/** Does not test for multiple qualifiers. This is tested in {@code ValidationProcessor}. */ private static AnnotationMirror getQualifier( List<? extends AnnotationMirror> annotations) { AnnotationMirror qualifier = null; for (AnnotationMirror annotation : annotations) { if (annotation.getAnnotationType().asElement().getAnnotation(Qualifier.class) == null) { continue; } qualifier = annotation; } return qualifier; } }
@SuppressWarnings("unchecked") private int getIdFromStrategy(final TypeElement e) { List<? extends AnnotationMirror> annotationMirrors = e.getAnnotationMirrors(); if (annotationMirrors == null || annotationMirrors.size() == 0) { return 0; } try { for (AnnotationMirror annotationMirror : annotationMirrors) { DeclaredType annotationType = annotationMirror.getAnnotationType(); ClassIdStrategy strategyAnn = annotationType.asElement().getAnnotation(ClassIdStrategy.class); if (strategyAnn != null) { String classBinaryName = elementUtils.getBinaryName(e).toString(); TypeElement element = (TypeElement) annotationType.asElement(); final Annotation sourceAnnotation = e.getAnnotation((Class<Annotation>) Class.forName(elementUtils.getBinaryName(element).toString())); ClassIdGenerationStrategy idGenerationStrategy = sourceAnnotation.annotationType().getAnnotation(ClassIdStrategy.class).value().newInstance(); return idGenerationStrategy.generateIdForClass(sourceAnnotation, classBinaryName); } } } catch (Exception ex) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error processing the class id for: "); } return 0; }
@SuppressWarnings("unchecked") public static Set<TypeElement> getAnnotationValuesAsElements(AnnotationMirror mirror, String method) { Set<TypeElement> elements = new HashSet<TypeElement>(); for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> entry : mirror.getElementValues().entrySet()) { if (entry.getKey().getSimpleName().toString().equals(method)) { List<AnnotationValue> values = ((List) entry.getValue().getValue()); for (AnnotationValue value : values) { DeclaredType type = (DeclaredType) value.getValue(); elements.add((TypeElement) type.asElement()); } } } return elements; }
public <T> Optional<T> getAnnotationValue(ProcessingEnvironment processingEnvironment, Element rootElement, Class annotationClass, String propertyName) { TypeMirror annotationType = processingEnvironment.getElementUtils().getTypeElement(annotationClass.getCanonicalName()).asType(); return (Optional<T>) rootElement.getAnnotationMirrors() .stream() .filter(annotation -> processingEnvironment.getTypeUtils().isSameType(annotation.getAnnotationType(), annotationType)) .findAny() .map(annotation -> annotation.getElementValues() .keySet() .stream() .filter(method -> method.getSimpleName().toString().equals(propertyName)) .findFirst() .map(method -> annotation .getElementValues() .get(method)) .map(AnnotationValue::getValue) .orElse(null)); }
public AnnotationMirror findAnnotationMirror(Element annotatedElement, String annotationName) { List<? extends AnnotationMirror> annotationMirrors = annotatedElement.getAnnotationMirrors(); for (AnnotationMirror annotationMirror : annotationMirrors) { TypeElement annotationElement = (TypeElement) annotationMirror.getAnnotationType().asElement(); if (isAnnotation(annotationElement, annotationName)) { return annotationMirror; } } return null; }