本文整理汇总了Java中com.google.auto.common.MoreTypes.asElement方法的典型用法代码示例。如果您正苦于以下问题:Java MoreTypes.asElement方法的具体用法?Java MoreTypes.asElement怎么用?Java MoreTypes.asElement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.auto.common.MoreTypes
的用法示例。
在下文中一共展示了MoreTypes.asElement方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validateActionCreator
import com.google.auto.common.MoreTypes; //导入方法依赖的package包/类
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));
}
}
示例2: getSubcomponents
import com.google.auto.common.MoreTypes; //导入方法依赖的package包/类
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;
}
示例3: processElement
import com.google.auto.common.MoreTypes; //导入方法依赖的package包/类
@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();
}
示例4: build
import com.google.auto.common.MoreTypes; //导入方法依赖的package包/类
@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;
}
示例5: build
import com.google.auto.common.MoreTypes; //导入方法依赖的package包/类
@Override
protected ScreenSpec build() {
ScreenSpec spec = new ScreenSpec(buildClassName(extractor.getElement()));
spec.setComponentClassName(AutoComponentClassNameUtil.getComponentClassName(spec.getClassName()));
spec.setParentComponentTypeName(TypeName.get(extractor.getComponentDependency()));
spec.setConfigSpec(state.getConfigSpec());
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, AutoScreen.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.getScreenAnnotationsMirrors().isEmpty()) {
for (AnnotationMirror annotationMirror : extractor.getScreenAnnotationsMirrors()) {
spec.getScreenAnnotationSpecs().add(AnnotationSpec.get(annotationMirror));
}
}
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);
if (MoreElements.isAnnotationPresent(e, ScreenParam.class)) {
moduleSpec.getInternalParameters().add(parameterSpec);
} else {
moduleSpec.getProvideParameters().add(parameterSpec);
}
}
spec.setModuleSpec(moduleSpec);
return spec;
}
示例6: extract
import com.google.auto.common.MoreTypes; //导入方法依赖的package包/类
@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();
}