本文整理匯總了Java中com.google.auto.service.AutoService類的典型用法代碼示例。如果您正苦於以下問題:Java AutoService類的具體用法?Java AutoService怎麽用?Java AutoService使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
AutoService類屬於com.google.auto.service包,在下文中一共展示了AutoService類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: write
import com.google.auto.service.AutoService; //導入依賴的package包/類
@Override
public String write() throws IOException {
AnnotationSpec autoServiceAnnotation = AnnotationSpec.builder(AutoService.class)
.addMember("value", "$T.class", ServerModuleConfiguration.class).build();
TypeSpec.Builder serverModuleTypeBuilder = DominoTypeBuilder.build(processorElement.getAnnotation(ServerModule.class).name() +
"ServerModule", ServerModuleAnnotationProcessor.class)
.addAnnotation(autoServiceAnnotation)
.addSuperinterface(ServerModuleConfiguration.class);
writeRegisterMethods(serverModuleTypeBuilder);
StringBuilder asString = new StringBuilder();
JavaFile.builder(processorElement.elementPackage(), serverModuleTypeBuilder.build()).skipJavaLangImports(true)
.build().writeTo(asString);
return asString.toString();
}
示例2: write
import com.google.auto.service.AutoService; //導入依賴的package包/類
public String write() throws IOException {
AnnotationSpec autoService = AnnotationSpec.builder(AutoService.class).addMember("value", "ModuleConfigurationProvider.class").build();
AnnotationSpec gwtIncompatible = AnnotationSpec.builder(GwtIncompatible.class).addMember("value", "\"Unused in GWT compilation\"").build();
TypeSpec configurationProvider = DominoTypeBuilder.build(element.getAnnotation(ClientModule.class).name() + "ModuleConfiguration_Provider", ConfigurationProviderAnnotationProcessor.class)
.addAnnotation(gwtIncompatible)
.addAnnotation(autoService)
.addSuperinterface(ModuleConfigurationProvider.class)
.addMethod(makeGetMethod())
.build();
JavaFile javaFile = JavaFile.builder(element.elementPackage(), configurationProvider).skipJavaLangImports(true).build();
StringBuilder classAsString = new StringBuilder();
javaFile.writeTo(classAsString);
return classAsString.toString();
}
示例3: handleAnnotatedElements
import com.google.auto.service.AutoService; //導入依賴的package包/類
@Override
protected void handleAnnotatedElements(
final IBuilderContext builderCtx,
final Class<? extends Annotation> annotationType,
final Set<? extends Element> elements
) throws KernelException {
elements.forEach(classElement -> {
if (classElement.getKind() != ElementKind.CLASS) {
throw new KernelException(
"The Service annotation only can be applied on class - {}",
classElement.getSimpleName().toString());
}
builderCtx.checkModifiers(classElement, Service.class, Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL);
// Receive service id array
ClassMeta.Builder classBuilder = builderCtx.findClassBuilder(classElement);
AnnotationMirror svcAnnoMirror = MoreElements.getAnnotationMirror(classElement, Service.class).get();
Service service = classElement.getAnnotation(Service.class);
String[] serviceIds = mergeId(getTypesInAnnotation(svcAnnoMirror, "value", builderCtx.getLogger()), service.ids());
if (serviceIds.length == 0) {
final StringBuilder svcId = new StringBuilder();
// Check service factory type argument first
Observable.from(((TypeElement) classElement).getInterfaces())
.filter(declareType -> declareType.toString().startsWith(IServiceFactory.class.getName()))
.map(declareType -> ((DeclaredType) declareType).getTypeArguments().get(0))
.subscribe(svcId::append);
if (svcId.length() == 0) {
// If the service is not a factory, using service class type
svcId.append(StringHelper.makeString("{}.{}",
classBuilder.getPackageName(), classElement.getSimpleName().toString()));
}
serviceIds = new String[] { svcId.toString() };
}
Template tempGetIds = builderCtx.loadTemplate(TEMPLATE_GET_IDS);
this._helper.addServiceId(classBuilder, serviceIds);
// Build class builder
classBuilder
.addAnnotationBuilder(AnnotationMeta.builder()
.setName(AutoService.class.getCanonicalName())
.addArgument(ArgumentMeta.builder()
.setName("value")
.setIsString(false)
.setValue(IService.class.getCanonicalName() + ".class")))
.addImplement(IService.class.getCanonicalName())
.addMethodBuilder(MethodMeta.builder()
.addAnnotationBuilder(AnnotationMeta.builder()
.setName(AnnotationMeta.OVERRIDE))
.setName(IService.METHOD_GETIDS)
.addModifier(Modifier.PUBLIC)
.setReturnTypeName(IService.METHOD_GETIDS_RETURN_TYPE)
.addCodeBuilder(CodeMeta.builder()
.setTemplate(tempGetIds)
.setModel(classBuilder.getTransience(MODEL_GET_IDS))));
});
}
示例4: writeFile
import com.google.auto.service.AutoService; //導入依賴的package包/類
private void writeFile(Map<String, TypeMirror> types,
PackageElement pkg) throws IOException
{
TypeName wildcardClass = ParameterizedTypeName.get(ClassName.get(Class.class),
WildcardTypeName
.subtypeOf(Command.class));
ParameterizedTypeName returnType =
ParameterizedTypeName.get(ClassName.get(Map.class),
ClassName.get(String.class), wildcardClass);
MethodSpec.Builder methodBuilder =
MethodSpec.methodBuilder("getCommands")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.returns(returnType)
.addStatement("$T knownTypes = new $T<>()",
returnType, ClassName.get(HashMap.class));
for (Map.Entry<String, TypeMirror> entry : types.entrySet()) {
methodBuilder.addStatement("knownTypes.put($S, $T.class)", entry.getKey(),
entry.getValue());
}
MethodSpec getcommands = methodBuilder.addStatement("return knownTypes")
.build();
AnnotationSpec autoServiceAnnotation =
AnnotationSpec.builder(AutoService.class)
.addMember("value", "$T.class", CommandProvider.class)
.build();
TypeSpec commandProvider = TypeSpec.classBuilder("CommandProviderImpl")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addSuperinterface(CommandProvider.class)
.addAnnotation(autoServiceAnnotation)
.addMethod(getcommands)
.build();
JavaFile javaFile = JavaFile.builder(pkg.getQualifiedName().toString() + ".provider",
commandProvider).build();
javaFile.writeTo(processingEnv.getFiler());
}