public static ImmutableList<AnnotationSpec> extractValidAnnotations(TypeElement element) { final List<AnnotationSpec> annotations = new ArrayList<>(); for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { if (isValidAnnotation(annotationMirror)) { annotations.add(AnnotationSpec.get(annotationMirror)); } } return ImmutableList.copyOf(annotations); }
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(); } }
@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 Set<String> extractServiceInterfaceNames(TypeElement typeElement) { ImmutableList<TypeMirror> typesMirrors = AnnotationMirrors.getTypesFromMirrors( Metainf.Service.class.getCanonicalName(), "value", typeElement.getAnnotationMirrors()); if (typesMirrors.isEmpty()) { return useIntrospectedInterfacesForServices(typeElement); } return useProvidedTypesForServices(typeElement, typesMirrors); }
private Set<String> useProvidedTypesForServices(TypeElement typeElement, ImmutableList<TypeMirror> typesMirrors) { List<String> wrongTypes = Lists.newArrayList(); List<String> types = Lists.newArrayList(); for (TypeMirror typeMirror : typesMirrors) { if (typeMirror.getKind() != TypeKind.DECLARED || !processing().getTypeUtils().isAssignable(typeElement.asType(), typeMirror)) { wrongTypes.add(typeMirror.toString()); } else { types.add(typeMirror.toString()); } } if (!wrongTypes.isEmpty()) { processing().getMessager().printMessage( Diagnostic.Kind.ERROR, "@Metainf.Service(value = {...}) contains types that are not implemented by " + typeElement.getSimpleName() + ": " + wrongTypes, typeElement, AnnotationMirrors.findAnnotation(typeElement.getAnnotationMirrors(), Metainf.Service.class)); } return FluentIterable.from(types).toSet(); }
for(AnnotationMirror m : typeElement.getAnnotationMirrors()) { if(m.getAnnotationType().toString().equals(clazzName)) { return m;
@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; }
@Nullable InterStageStore interStageStore, EnumSet<RunMode> runMode) { final List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
&& 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(); for (AnnotationMirror parentAnnotation : annotationElement.getAnnotationMirrors()) { TypeElement parentElement = (TypeElement) parentAnnotation.getAnnotationType().asElement();
protected void setGeneratedClass() throws Exception { String annotatedComponentQualifiedName = annotatedElement.getQualifiedName().toString(); annotatedClass = getCodeModel().directClass(annotatedElement.asType().toString()); if (annotatedElement.getNestingKind().isNested()) { Element enclosingElement = annotatedElement.getEnclosingElement(); GeneratedClassHolder enclosingHolder = environment.getGeneratedClassHolder(enclosingElement); String generatedBeanSimpleName = annotatedElement.getSimpleName().toString() + classSuffix(); int modifier = PUBLIC | STATIC; if (environment.getOptionBooleanValue(OPTION_GENERATE_FINAL_CLASSES)) { modifier |= FINAL; } generatedClass = enclosingHolder.getGeneratedClass()._class(modifier, generatedBeanSimpleName, EClassType.CLASS); } else { String generatedClassQualifiedName = annotatedComponentQualifiedName + classSuffix(); int modifier = PUBLIC; if (environment.getOptionBooleanValue(OPTION_GENERATE_FINAL_CLASSES)) { modifier |= FINAL; } generatedClass = getCodeModel()._class(modifier, generatedClassQualifiedName, EClassType.CLASS); } codeModelHelper.generify(generatedClass, annotatedElement); setExtends(); codeModelHelper.copyNonAAAnnotations(generatedClass, annotatedElement.getAnnotationMirrors()); }
updateCommandAttributes(result, cmd); List<CommandSpec> subcommands = findSubcommands(typeElement.getAnnotationMirrors(), factory, commands, commandTypes, options, parameters); for (CommandSpec sub : subcommands) {
/** * Get a certain annotation of a {@link TypeElement}. * See <a href="https://stackoverflow.com/a/10167558">stackoverflow.com</a> for more information. * * @param typeElement the type element, that contains the requested annotation * @param annotationClass the class of the requested annotation * @return the requested annotation or null, if no annotation of the provided class was found * @throws NullPointerException if <code>typeElement</code> or <code>annotationClass</code> is null */ public static AnnotationMirror getAnnotationMirror(TypeElement typeElement, Class<?> annotationClass) { String annotationClassName = annotationClass.getName(); for (AnnotationMirror m : typeElement.getAnnotationMirrors()) { if (m.getAnnotationType().toString().equals(annotationClassName)) { return m; } } return null; }
private boolean containsDirectAnnotation(TypeElement element, AnnotationMirror elementOfMultiValuedAnnotation) { Types typeUtils = constraintHelper.getTypeUtils(); for ( AnnotationMirror annotationMirror : element.getAnnotationMirrors() ) { if ( typeUtils.isSameType( annotationMirror.getAnnotationType(), elementOfMultiValuedAnnotation.getAnnotationType() ) ) { return true; } } return false; }
/** * Lookup {@link javax.inject.Scope} annotated annotations to provide the name of the scope the {@code typeElement} belongs to. * The method logs an error if the {@code typeElement} has multiple scope annotations. * * @param typeElement the element for which a scope is to be found. * @return the scope of this {@code typeElement} or {@code null} if it has no scope annotations. */ private String getScopeName(TypeElement typeElement) { String scopeName = null; for (AnnotationMirror annotationMirror : typeElement.getAnnotationMirrors()) { TypeElement annotationTypeElement = (TypeElement) annotationMirror.getAnnotationType().asElement(); if (annotationTypeElement.getAnnotation(Scope.class) != null) { checkScopeAnnotationValidity(annotationTypeElement); if (scopeName != null) { error(typeElement, "Only one @Scope qualified annotation is allowed : %s", scopeName); } scopeName = annotationTypeElement.getQualifiedName().toString(); } } return scopeName; }
private void checkAnnotations(TypeElement d, MultiMap<String, String> result) { for (AnnotationMirror a : d.getAnnotationMirrors()) { getModel(a.getAnnotationType()).parse(a,result); // check meta-annotations for (AnnotationMirror b : a.getAnnotationType().asElement().getAnnotationMirrors()) { getModel(b.getAnnotationType()).parse(b,result); } } }
@Override public Set<ConstraintCheckIssue> checkAnnotationType(TypeElement element, AnnotationMirror annotation) { Target target = element.getAnnotation( Target.class ); //no target given allows the annotation to be declared at any type if ( target == null ) { return Collections.emptySet(); } if ( !containsAtLeastOneSupportedElementType( target ) ) { return CollectionHelper.asSet( ConstraintCheckIssue.error( element, annotationApiHelper.getMirror( element.getAnnotationMirrors(), Target.class ), "CONSTRAINT_TYPE_WITH_WRONG_TARGET" ) ); } return Collections.emptySet(); }
for (AnnotationMirror a : element.getAnnotationMirrors()) { TypeElement atd = (TypeElement) a.getAnnotationType().asElement(); Contract c = atd.getAnnotation(Contract.class);
@Override public Set<ConstraintCheckIssue> checkAnnotationType(TypeElement element, AnnotationMirror annotation) { AnnotationMirror constraintMirror = annotationApiHelper.getMirror( element.getAnnotationMirrors(), BeanValidationTypes.CONSTRAINT ); boolean atLeastOneValidatorGiven = !annotationApiHelper.getAnnotationArrayValue( constraintMirror, "validatedBy" ).isEmpty(); if ( !( atLeastOneValidatorGiven || constraintHelper.isComposedConstraint( element ) ) ) { return CollectionHelper.asSet( ConstraintCheckIssue.warning( element, constraintMirror, "CONSTRAINT_TYPE_WITHOUT_VALIDATOR" ) ); } return Collections.emptySet(); }
private Set<ConstraintCheckIssue> checkHostingElement(TypeElement element, AnnotationMirror annotation) { if ( !element.getKind().isClass() ) { return CollectionHelper.asSet( ConstraintCheckIssue.error( element, annotation, "GROUP_SEQUENCE_PROVIDER_ANNOTATION_MUST_BE_DEFINED_ON_A_CLASS" ) ); } //this error should be raised only if the GroupSequenceProvider annotations is on a class if ( annotationApiHelper.getMirror( element.getAnnotationMirrors(), BeanValidationTypes.GROUP_SEQUENCE ) != null ) { return CollectionHelper.asSet( ConstraintCheckIssue.error( element, annotation, "GROUP_SEQUENCE_PROVIDER_ANNOTATION_NOT_ALLOWED_ON_CLASS_WITH_GROUP_SEQUENCE_ANNOTATION" ) ); } return Collections.emptySet(); }
@Override public Set<ConstraintCheckIssue> checkAnnotationType(TypeElement element, AnnotationMirror annotation) { Retention retention = element.getAnnotation( Retention.class ); if ( retention == null ) { return CollectionHelper.asSet( ConstraintCheckIssue.error( element, null, "CONSTRAINT_TYPE_WITH_MISSING_OR_WRONG_RETENTION" ) ); } if ( !retention.value().equals( RetentionPolicy.RUNTIME ) ) { return CollectionHelper.asSet( ConstraintCheckIssue.error( element, annotationApiHelper.getMirror( element.getAnnotationMirrors(), Retention.class ), "CONSTRAINT_TYPE_WITH_MISSING_OR_WRONG_RETENTION" ) ); } return Collections.emptySet(); }