Java Code Examples for com.google.auto.common.MoreElements#isAnnotationPresent()
The following examples show how to use
com.google.auto.common.MoreElements#isAnnotationPresent() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReduceAction.java From reductor with Apache License 2.0 | 6 votes |
private static void validateActionCreator(ExecutableElement element, String actionName, TypeMirror actionCreator, ArrayList<VariableElement> args, Map<String, ActionCreatorElement> knownActionCreators, Env env) throws ValidationException { Element actionCreatorElement = MoreTypes.asElement(actionCreator); if (!MoreElements.isAnnotationPresent(actionCreatorElement, ActionCreator.class)) { throw new ValidationException(element, "Action creator %s should be annotated with @%s", actionCreator, ActionCreator.class.getSimpleName()); } ActionCreatorElement creatorElement = knownActionCreators.get(env.getElements().getBinaryName((TypeElement) actionCreatorElement).toString()); if (creatorElement == null) { throw new ElementNotReadyException(); } if (!creatorElement.hasAction(actionName, args)) { throw new ValidationException(element, "Cannot find action creator for action \"%s\" and args %s in interface %s", actionName, toString(args), creatorElement.getName(env)); } }
Example 2
Source File: ScopeExtractor.java From Mortar-architect with MIT License | 6 votes |
/** * Find annotation that is itself annoted with @Scope * If there is one, it will be later applied on the generated component * Otherwise the component will be unscoped * Throw error if more than one scope annotation found */ private AnnotationMirror findScope() { AnnotationMirror annotationTypeMirror = null; for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { Element annotationElement = annotationMirror.getAnnotationType().asElement(); if (MoreElements.isAnnotationPresent(annotationElement, Scope.class)) { // already found one scope if (annotationTypeMirror != null) { errors.addInvalid("Several dagger scopes on same element are not allowed"); continue; } annotationTypeMirror = annotationMirror; } } return annotationTypeMirror; }
Example 3
Source File: RetroFacebookBuilderProcessor.java From RetroFacebook with Apache License 2.0 | 5 votes |
private void validate(Element annotatedType, String errorMessage) { Element container = annotatedType.getEnclosingElement(); if (!MoreElements.isAnnotationPresent(container, RetroFacebook.class)) { processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, errorMessage, annotatedType); } }
Example 4
Source File: RetroFacebookBuilderProcessor.java From RetroFacebook with Apache License 2.0 | 5 votes |
private void validate(Element annotatedType, String errorMessage) { Element container = annotatedType.getEnclosingElement(); if (!MoreElements.isAnnotationPresent(container, RetroFacebook.class)) { processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, errorMessage, annotatedType); } }
Example 5
Source File: ComponentProcessing.java From Auto-Dagger2 with MIT License | 5 votes |
private List<MethodSpec> getSubcomponents() { if (extractor.getSubcomponentsTypeMirrors().isEmpty()) { return Collections.emptyList(); } List<MethodSpec> methodSpecs = new ArrayList<>(extractor.getSubcomponentsTypeMirrors().size()); for (TypeMirror typeMirror : extractor.getSubcomponentsTypeMirrors()) { Element e = MoreTypes.asElement(typeMirror); TypeName typeName; String name; if (MoreElements.isAnnotationPresent(e, AutoSubcomponent.class)) { ClassName cls = AutoComponentClassNameUtil.getComponentClassName(e); typeName = cls; name = cls.simpleName(); } else { typeName = TypeName.get(typeMirror); name = e.getSimpleName().toString(); } List<TypeMirror> modules = state.getSubcomponentModules(typeMirror); List<ParameterSpec> parameterSpecs; if(modules != null) { parameterSpecs = new ArrayList<>(modules.size()); int count = 0; for (TypeMirror moduleTypeMirror : modules) { parameterSpecs.add(ParameterSpec.builder(TypeName.get(moduleTypeMirror), String.format("module%d", ++count)).build()); } } else { parameterSpecs = new ArrayList<>(0); } methodSpecs.add(MethodSpec.methodBuilder("plus" + name) .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) .addParameters(parameterSpecs) .returns(typeName) .build()); } return methodSpecs; }
Example 6
Source File: AdditionProcessing.java From Auto-Dagger2 with MIT License | 5 votes |
@Override public boolean processElement(Element element, Errors.ElementErrors elementErrors) { // @AutoX applied on annotation if (element.getKind() == ElementKind.ANNOTATION_TYPE) { // @AutoX is applied on another annotation, find out the targets of that annotation Set<? extends Element> targetElements = roundEnvironment.getElementsAnnotatedWith(MoreElements.asType(element)); for (Element targetElement : targetElements) { if (!process(targetElement, element)) { return false; } } return true; } // @AutoX applied on method // only valid for @AutoExpose with @Provides if (element.getKind() == ElementKind.METHOD) { if (processedAnnotation.equals(AutoInjector.class)) { errors.addInvalid(element, "@AutoInjector cannot be applied on the method %s", element.getSimpleName()); return false; } if (!MoreElements.isAnnotationPresent(element, Provides.class)) { errors.addInvalid(element, "@AutoExpose can be applied on @Provides method only, %s is missing it", element.getSimpleName()); return false; } ExecutableElement executableElement = MoreElements.asExecutable(element); Element returnElement = MoreTypes.asElement(executableElement.getReturnType()); return process(returnElement, element); } process(element, element); return !errors.hasErrors(); }
Example 7
Source File: SubcomponentExtractor.java From Auto-Dagger2 with MIT License | 5 votes |
@Override public void extract() { modulesTypeMirrors = findTypeMirrors(element, AutoComponentExtractorUtil.ANNOTATION_MODULES); if (!MoreElements.isAnnotationPresent(element, AutoSubcomponent.class)) { return; } superinterfacesTypeMirrors = findTypeMirrors(element, AutoComponentExtractorUtil.ANNOTATION_SUPERINTERFACES); scopeAnnotationTypeMirror = findScope(); }
Example 8
Source File: ExtractorUtil.java From Auto-Dagger2 with MIT License | 5 votes |
public static List<AnnotationMirror> findAnnotatedAnnotation(Element element, Class<? extends Annotation> annotationCls) { List<AnnotationMirror> annotationMirrors = new ArrayList<>(); for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { Element annotationElement = annotationMirror.getAnnotationType().asElement(); if (MoreElements.isAnnotationPresent(annotationElement, annotationCls)) { annotationMirrors.add(annotationMirror); } } return annotationMirrors; }
Example 9
Source File: RetroWeiboBuilderProcessor.java From SimpleWeibo with Apache License 2.0 | 5 votes |
private void validate(Element annotatedType, String errorMessage) { Element container = annotatedType.getEnclosingElement(); if (!MoreElements.isAnnotationPresent(container, RetroWeibo.class)) { processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, errorMessage, annotatedType); } }
Example 10
Source File: BuilderSpec.java From RetroFacebook with Apache License 2.0 | 4 votes |
/** * Determines if the {@code @RetroFacebook} class for this instance has a correct nested * {@code @RetroFacebook.Builder} class or interface and return a representation of it in an * {@code Optional} if so. */ Optional<Builder> getBuilder() { Optional<TypeElement> builderTypeElement = Optional.absent(); for (TypeElement containedClass : ElementFilter.typesIn(autoValueClass.getEnclosedElements())) { if (MoreElements.isAnnotationPresent(containedClass, RetroFacebook.Builder.class)) { if (!CLASS_OR_INTERFACE.contains(containedClass.getKind())) { errorReporter.reportError( "@RetroFacebook.Builder can only apply to a class or an interface", containedClass); } else if (builderTypeElement.isPresent()) { errorReporter.reportError( autoValueClass + " already has a Builder: " + builderTypeElement.get(), containedClass); } else { builderTypeElement = Optional.of(containedClass); } } } Optional<ExecutableElement> validateMethod = Optional.absent(); for (ExecutableElement containedMethod : ElementFilter.methodsIn(autoValueClass.getEnclosedElements())) { if (MoreElements.isAnnotationPresent(containedMethod, RetroFacebook.Validate.class)) { if (containedMethod.getModifiers().contains(Modifier.STATIC)) { errorReporter.reportError( "@RetroFacebook.Validate cannot apply to a static method", containedMethod); } else if (!containedMethod.getParameters().isEmpty()) { errorReporter.reportError( "@RetroFacebook.Validate method must not have parameters", containedMethod); } else if (containedMethod.getReturnType().getKind() != TypeKind.VOID) { errorReporter.reportError( "Return type of @RetroFacebook.Validate method must be void", containedMethod); } else if (validateMethod.isPresent()) { errorReporter.reportError( "There can only be one @RetroFacebook.Validate method", containedMethod); } else { validateMethod = Optional.of(containedMethod); } } } if (builderTypeElement.isPresent()) { return builderFrom(builderTypeElement.get(), validateMethod); } else { if (validateMethod.isPresent()) { errorReporter.reportError( "@RetroFacebook.Validate is only meaningful if there is an @RetroFacebook.Builder", validateMethod.get()); } return Optional.absent(); } }
Example 11
Source File: BuilderSpec.java From RetroFacebook with Apache License 2.0 | 4 votes |
/** * Determines if the {@code @RetroFacebook} class for this instance has a correct nested * {@code @RetroFacebook.Builder} class or interface and return a representation of it in an * {@code Optional} if so. */ Optional<Builder> getBuilder() { Optional<TypeElement> builderTypeElement = Optional.absent(); for (TypeElement containedClass : ElementFilter.typesIn(autoValueClass.getEnclosedElements())) { if (MoreElements.isAnnotationPresent(containedClass, RetroFacebook.Builder.class)) { if (!CLASS_OR_INTERFACE.contains(containedClass.getKind())) { errorReporter.reportError( "@RetroFacebook.Builder can only apply to a class or an interface", containedClass); } else if (builderTypeElement.isPresent()) { errorReporter.reportError( autoValueClass + " already has a Builder: " + builderTypeElement.get(), containedClass); } else { builderTypeElement = Optional.of(containedClass); } } } Optional<ExecutableElement> validateMethod = Optional.absent(); for (ExecutableElement containedMethod : ElementFilter.methodsIn(autoValueClass.getEnclosedElements())) { if (MoreElements.isAnnotationPresent(containedMethod, RetroFacebook.Validate.class)) { if (containedMethod.getModifiers().contains(Modifier.STATIC)) { errorReporter.reportError( "@RetroFacebook.Validate cannot apply to a static method", containedMethod); } else if (!containedMethod.getParameters().isEmpty()) { errorReporter.reportError( "@RetroFacebook.Validate method must not have parameters", containedMethod); } else if (containedMethod.getReturnType().getKind() != TypeKind.VOID) { errorReporter.reportError( "Return type of @RetroFacebook.Validate method must be void", containedMethod); } else if (validateMethod.isPresent()) { errorReporter.reportError( "There can only be one @RetroFacebook.Validate method", containedMethod); } else { validateMethod = Optional.of(containedMethod); } } } if (builderTypeElement.isPresent()) { return builderFrom(builderTypeElement.get(), validateMethod); } else { if (validateMethod.isPresent()) { errorReporter.reportError( "@RetroFacebook.Validate is only meaningful if there is an @RetroFacebook.Builder", validateMethod.get()); } return Optional.absent(); } }
Example 12
Source File: ScopeProcessing.java From Mortar-architect with MIT License | 4 votes |
@Override protected ScopeSpec build() { architect.autostack.compiler.ScopeSpec spec = new ScopeSpec(buildClassName(extractor.getElement())); spec.setParentComponentTypeName(TypeName.get(extractor.getComponentDependency())); TypeName presenterTypeName = TypeName.get(extractor.getElement().asType()); ClassName moduleClassName = ClassName.get(spec.getClassName().packageName(), spec.getClassName().simpleName(), "Module"); AnnotationSpec.Builder builder = AnnotationSpec.get(extractor.getComponentAnnotationTypeMirror()).toBuilder(); builder.addMember("target", "$T.class", presenterTypeName); builder.addMember("modules", "$T.class", moduleClassName); spec.setComponentAnnotationSpec(builder.build()); spec.setDaggerComponentTypeName(ClassName.get(spec.getClassName().packageName(), String.format("Dagger%sComponent", spec.getClassName().simpleName()))); // dagger2 builder dependency method name and type can have 3 diff values // - name and type of the generated scope if dependency is annotated with @AutoScope // - name and type of the target if dependency is annotated with @AutoComponent (valid also for #2, so check #2 condition first) // - name and type of the class if dependency is a manually written component String methodName; TypeName typeName; Element daggerDependencyElement = MoreTypes.asElement(extractor.getComponentDependency()); if (MoreElements.isAnnotationPresent(daggerDependencyElement, AutoStackable.class)) { ClassName daggerDependencyScopeClassName = buildClassName(daggerDependencyElement); ClassName daggerDependencyClassName = AutoComponentClassNameUtil.getComponentClassName(daggerDependencyScopeClassName); methodName = StringUtils.uncapitalize(daggerDependencyClassName.simpleName()); typeName = daggerDependencyClassName; } else if (MoreElements.isAnnotationPresent(daggerDependencyElement, AutoComponent.class)) { methodName = StringUtils.uncapitalize(daggerDependencyElement.getSimpleName().toString()) + "Component"; typeName = AutoComponentClassNameUtil.getComponentClassName(daggerDependencyElement); } else { methodName = StringUtils.uncapitalize(daggerDependencyElement.getSimpleName().toString()); typeName = TypeName.get(extractor.getComponentDependency()); } spec.setDaggerComponentBuilderDependencyTypeName(typeName); spec.setDaggerComponentBuilderDependencyMethodName(methodName); if (extractor.getScopeAnnotationTypeMirror() != null) { spec.setScopeAnnotationSpec(AnnotationSpec.get(extractor.getScopeAnnotationTypeMirror())); } if (extractor.getPathViewTypeMirror() != null) { spec.setPathViewTypeName(TypeName.get(extractor.getPathViewTypeMirror())); } if (extractor.getPathLayout() != 0) { spec.setPathLayout(extractor.getPathLayout()); } ModuleSpec moduleSpec = new ModuleSpec(moduleClassName); moduleSpec.setPresenterTypeName(presenterTypeName); moduleSpec.setScopeAnnotationSpec(spec.getScopeAnnotationSpec()); for (VariableElement e : extractor.getConstructorsParamtersElements()) { ParameterSpec parameterSpec = ParameterSpec.builder(TypeName.get(e.asType()), e.getSimpleName().toString()).build(); moduleSpec.getPresenterArgs().add(parameterSpec); // not supported for now: // || extractor.getFromPathFieldsElements().contains(e) if (MoreElements.isAnnotationPresent(e, FromPath.class)) { moduleSpec.getInternalParameters().add(parameterSpec); } else { moduleSpec.getProvideParameters().add(parameterSpec); } } spec.setModuleSpec(moduleSpec); return spec; }
Example 13
Source File: ComponentExtractor.java From Auto-Dagger2 with MIT License | 4 votes |
@Override public void extract() { targetTypeMirror = ExtractorUtils.getValueFromAnnotation(element, AutoComponent.class, AutoComponentExtractorUtil.ANNOTATION_TARGET); if (targetTypeMirror == null) { targetTypeMirror = componentElement.asType(); } dependenciesTypeMirrors = findTypeMirrors(element, AutoComponentExtractorUtil.ANNOTATION_DEPENDENCIES); modulesTypeMirrors = findTypeMirrors(element, AutoComponentExtractorUtil.ANNOTATION_MODULES); superinterfacesTypeMirrors = findTypeMirrors(element, AutoComponentExtractorUtil.ANNOTATION_SUPERINTERFACES); subcomponentsTypeMirrors = findTypeMirrors(element, AutoComponentExtractorUtil.ANNOTATION_SUBCOMPONENTS); ComponentExtractor includesExtractor = null; TypeMirror includesTypeMirror = ExtractorUtils.getValueFromAnnotation(element, AutoComponent.class, AutoComponentExtractorUtil.ANNOTATION_INCLUDES); if (includesTypeMirror != null) { Element includesElement = MoreTypes.asElement(includesTypeMirror); if (!MoreElements.isAnnotationPresent(includesElement, AutoComponent.class)) { errors.getParent().addInvalid(includesElement, "Included element must be annotated with @AutoComponent"); return; } if (element.equals(includesElement)) { errors.addInvalid("Auto component %s cannot include himself", element.getSimpleName()); return; } includesExtractor = new ComponentExtractor(includesElement, includesElement, types, elements, errors.getParent()); if (errors.getParent().hasErrors()) { return; } } if (includesExtractor != null) { dependenciesTypeMirrors.addAll(includesExtractor.getDependenciesTypeMirrors()); modulesTypeMirrors.addAll(includesExtractor.getModulesTypeMirrors()); superinterfacesTypeMirrors.addAll(includesExtractor.getSuperinterfacesTypeMirrors()); subcomponentsTypeMirrors.addAll(includesExtractor.getSubcomponentsTypeMirrors()); } scopeAnnotationTypeMirror = findScope(); }
Example 14
Source File: ConfigurationAnnotations.java From dagger2-sample with Apache License 2.0 | 4 votes |
static boolean isComponent(TypeElement componentDefinitionType) { return MoreElements.isAnnotationPresent(componentDefinitionType, Component.class) || MoreElements.isAnnotationPresent(componentDefinitionType, ProductionComponent.class); }
Example 15
Source File: BuilderSpec.java From SimpleWeibo with Apache License 2.0 | 4 votes |
/** * Determines if the {@code @RetroWeibo} class for this instance has a correct nested * {@code @RetroWeibo.Builder} class or interface and return a representation of it in an * {@code Optional} if so. */ Optional<Builder> getBuilder() { Optional<TypeElement> builderTypeElement = Optional.absent(); for (TypeElement containedClass : ElementFilter.typesIn(autoValueClass.getEnclosedElements())) { if (MoreElements.isAnnotationPresent(containedClass, RetroWeibo.Builder.class)) { if (!CLASS_OR_INTERFACE.contains(containedClass.getKind())) { errorReporter.reportError( "@RetroWeibo.Builder can only apply to a class or an interface", containedClass); } else if (builderTypeElement.isPresent()) { errorReporter.reportError( autoValueClass + " already has a Builder: " + builderTypeElement.get(), containedClass); } else { builderTypeElement = Optional.of(containedClass); } } } Optional<ExecutableElement> validateMethod = Optional.absent(); for (ExecutableElement containedMethod : ElementFilter.methodsIn(autoValueClass.getEnclosedElements())) { if (MoreElements.isAnnotationPresent(containedMethod, RetroWeibo.Validate.class)) { if (containedMethod.getModifiers().contains(Modifier.STATIC)) { errorReporter.reportError( "@RetroWeibo.Validate cannot apply to a static method", containedMethod); } else if (!containedMethod.getParameters().isEmpty()) { errorReporter.reportError( "@RetroWeibo.Validate method must not have parameters", containedMethod); } else if (containedMethod.getReturnType().getKind() != TypeKind.VOID) { errorReporter.reportError( "Return type of @RetroWeibo.Validate method must be void", containedMethod); } else if (validateMethod.isPresent()) { errorReporter.reportError( "There can only be one @RetroWeibo.Validate method", containedMethod); } else { validateMethod = Optional.of(containedMethod); } } } if (builderTypeElement.isPresent()) { return builderFrom(builderTypeElement.get(), validateMethod); } else { if (validateMethod.isPresent()) { errorReporter.reportError( "@RetroWeibo.Validate is only meaningful if there is an @RetroWeibo.Builder", validateMethod.get()); } return Optional.absent(); } }