本文整理匯總了Java中com.squareup.javapoet.AnnotationSpec類的典型用法代碼示例。如果您正苦於以下問題:Java AnnotationSpec類的具體用法?Java AnnotationSpec怎麽用?Java AnnotationSpec使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
AnnotationSpec類屬於com.squareup.javapoet包,在下文中一共展示了AnnotationSpec類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getSingletonSetterMethod
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
private MethodSpec getSingletonSetterMethod(ClassName className, FieldSpec singletonField) {
ParameterSpec parameter = ParameterSpec
.builder(className, "wrapper")
.build();
AnnotationSpec visibleForTesting = AnnotationSpec
.builder(VisibleForTesting.class)
.addMember("otherwise", "VisibleForTesting.NONE")
.build();
return MethodSpec
.methodBuilder("setInstance")
.addAnnotation(visibleForTesting)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addParameter(parameter)
.addStatement("$N = $N", singletonField, parameter)
.build();
}
示例2: generate
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
private static TypeSpec generate(List<TypeElement> libraryModules,
Class<? extends Annotation> annotation) {
AnnotationSpec.Builder annotationBuilder =
AnnotationSpec.builder(Index.class);
String value = getAnnotationValue(annotation);
for (TypeElement childModule : libraryModules) {
annotationBuilder.addMember(value, "$S", ClassName.get(childModule).toString());
}
String indexerName = INDEXER_NAME_PREFIX + annotation.getSimpleName() + "_";
for (TypeElement element : libraryModules) {
indexerName += element.getQualifiedName().toString().replace(".", "_");
indexerName += "_";
}
indexerName = indexerName.substring(0, indexerName.length() - 1);
return TypeSpec.classBuilder(indexerName)
.addAnnotation(annotationBuilder.build())
.addModifiers(Modifier.PUBLIC)
.build();
}
示例3: generateAsMethod
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
private MethodSpec generateAsMethod(String generatedCodePackageName, TypeSpec requestBuilder) {
TypeVariableName resourceType = TypeVariableName.get("ResourceType");
ParameterizedTypeName classOfResouceType = ParameterizedTypeName
.get(ClassName.get(Class.class), resourceType);
ClassName generatedRequestBuilderClassName =
ClassName.get(generatedCodePackageName, requestBuilder.name);
ParameterizedTypeName requestBuilderOfResourceType = ParameterizedTypeName
.get(generatedRequestBuilderClassName, resourceType);
return MethodSpec.methodBuilder("as")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addTypeVariable(TypeVariableName.get("ResourceType"))
.addParameter(classOfResouceType, "resourceClass")
.addAnnotation(AnnotationSpec.builder(CHECK_RESULT_CLASS_NAME).build())
.returns(requestBuilderOfResourceType)
.addStatement("return new $T<>(glide, this, resourceClass, context)",
this.generatedRequestBuilderClassName)
.build();
}
示例4: generateRequestManagerRequestBuilderMethodOverride
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
/**
* Generates overrides of existing RequestManager methods so that they return our generated
* RequestBuilder subtype.
*/
private MethodSpec generateRequestManagerRequestBuilderMethodOverride(
ExecutableElement methodToOverride) {
// We've already verified that this method returns a RequestBuilder and RequestBuilders have
// exactly one type argument, so this is safe unless those assumptions change.
TypeMirror typeArgument =
((DeclaredType) methodToOverride.getReturnType()).getTypeArguments().get(0);
ParameterizedTypeName generatedRequestBuilderOfType =
ParameterizedTypeName.get(generatedRequestBuilderClassName, ClassName.get(typeArgument));
MethodSpec.Builder builder = ProcessorUtil.overriding(methodToOverride)
.returns(generatedRequestBuilderOfType)
.addCode(
ProcessorUtil.generateCastingSuperCall(
generatedRequestBuilderOfType, methodToOverride));
for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) {
builder.addAnnotation(AnnotationSpec.get(mirror));
}
return builder.build();
}
示例5: generate
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
private static TypeSpec generate(List<TypeElement> libraryModules,
Class<? extends Annotation> annotation) {
AnnotationSpec.Builder annotationBuilder =
AnnotationSpec.builder(Index.class);
String value = getAnnotationValue(annotation);
for (TypeElement childModule : libraryModules) {
annotationBuilder.addMember(value, "$S", ClassName.get(childModule).toString());
}
StringBuilder indexerName = new StringBuilder(
INDEXER_NAME_PREFIX + annotation.getSimpleName() + "_");
for (TypeElement element : libraryModules) {
indexerName.append(element.getQualifiedName().toString().replace(".", "_"));
indexerName.append("_");
}
indexerName = new StringBuilder(indexerName.substring(0, indexerName.length() - 1));
return TypeSpec.classBuilder(indexerName.toString())
.addAnnotation(annotationBuilder.build())
.addModifiers(Modifier.PUBLIC)
.build();
}
示例6: hasMember
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
public static Matcher<AnnotationSpec> hasMember(final String member) {
return new TypeSafeMatcher<AnnotationSpec>() {
@Override
protected boolean matchesSafely(AnnotationSpec item) {
return item.members.containsKey(member);
}
@Override
public void describeTo(Description description) {
description.appendText("has member " + member);
}
};
}
示例7: generate
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
void generate() {
TypeSpec shortbread = TypeSpec.classBuilder("ShortbreadGenerated")
.addAnnotation(AnnotationSpec.builder(suppressLint)
.addMember("value", "$S", "NewApi")
.addMember("value", "$S", "ResourceType")
.build())
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addMethod(createShortcuts())
.addMethod(callMethodShortcut())
.build();
JavaFile javaFile = JavaFile.builder("shortbread", shortbread)
.indent(" ")
.build();
try {
javaFile.writeTo(filer);
} catch (IOException e) {
e.printStackTrace();
}
}
示例8: defineLifecycleHook
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
private MethodSpec defineLifecycleHook() {
final String methodName = "onLifecycleEvent";
Lifecycle.Event lifecycleEvent = annotatedElement.getAnnotation(LifecycleAware.class).value();
AnnotationSpec archLifeCycleSpec = AnnotationSpec.builder(OnLifecycleEvent.class)
.addMember(ANNOT_DEFAULT_NAME, "$T.$L", Lifecycle.Event.class, lifecycleEvent)
.build();
return MethodSpec.methodBuilder(lifecycleEvent.name())
.addAnnotation(archLifeCycleSpec)
.addModifiers(Modifier.PUBLIC)
.addStatement("$L.$L($T.$L)", FIELD_OBSERVER, methodName, Lifecycle.Event.class, lifecycleEvent)
.build();
}
示例9: createInitMethodSpecBuilder
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
/**
* 創建init方法,用於數據初始化
*
* @param methodName 方法名
* @param genericTypesClass ArrayList泛型的類型
* @param gsonDataString ArrayList被Gson轉換出的字符串
* @param modelListVariableName GsonData被還原後的臨時變量名
* @return 初始化方法的構造器
*/
public static MethodSpec.Builder createInitMethodSpecBuilder(String methodName, Class genericTypesClass, String gsonDataString, String modelListVariableName) {
String tmpStr;
//Override注解
AnnotationSpec annotation = createOverrideAnnotation();
//return type,返回值
ParameterizedType returnType = GenericTypesUtil.type(ArrayList.class, genericTypesClass);
//protected ArrayList<DUnitModel> initUnitModels(),函數聲明
MethodSpec.Builder initMethodBuilder = MethodSpec
.methodBuilder(methodName)
.addModifiers(Modifier.PROTECTED)
.addAnnotation(annotation)
.returns(returnType);
//Gson gson = new Gson();
initMethodBuilder.addStatement("$T gson = new $T()", Gson.class, Gson.class);
//ParameterizedType type = GenericTypesUtil.type(ArrayList.class,DUnitModel.class);
initMethodBuilder.addStatement("$T type = $T.type($T.class,$T.class)", ParameterizedType.class, GenericTypesUtil.class, ArrayList.class, genericTypesClass);
//ArrayList<DUnitModel> unitModels = gson.fromJson(unitModelsString_c,type);
tmpStr = String.format("$T<$T> %s = gson.fromJson($S,type)", modelListVariableName);
initMethodBuilder.addStatement(tmpStr, ArrayList.class, genericTypesClass, gsonDataString);
return initMethodBuilder;
}
示例10: buildFields
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
@Nonnull
@Override
protected List<FieldSpec> buildFields() {
return getProperties()
.entrySet()
.stream()
.map(property -> {
final String name = property.getKey();
final TypeName type = property.getValue();
final String fieldName = fieldNamePolicy.convert(name, type);
return FieldSpec.builder(type, fieldName)
.addModifiers(Modifier.PRIVATE, Modifier.FINAL)
.addAnnotation(AnnotationSpec.builder(SerializedName.class)
.addMember("value", "$S", name)
.build())
.build();
})
.collect(Collectors.toList());
}
示例11: createJsArrayInterface
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
private TypeSpec createJsArrayInterface() {
TypeVariableName variableName = TypeVariableName.get("T");
return TypeSpec.classBuilder("JsArray")
.addModifiers(Modifier.STATIC, Modifier.FINAL)
.addTypeVariable(variableName)
.addAnnotation(AnnotationSpec.builder(ClassNames.JSINTEROP_JSTYPE)
.addMember("isNative", "true")
.addMember("namespace", "$T.GLOBAL", ClassNames.JSINTEROP_JSPACKAGE)
.addMember("name", "$S", "Array")
.build())
.addMethod(MethodSpec.methodBuilder("push")
.addModifiers(Modifier.PUBLIC, Modifier.NATIVE)
.addParameter(variableName, "item")
.build())
.build();
}
示例12: createGlobalInterface
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
private TypeSpec createGlobalInterface() {
return TypeSpec.classBuilder("Global")
.addModifiers(Modifier.STATIC, Modifier.FINAL)
.addAnnotation(AnnotationSpec.builder(ClassNames.JSINTEROP_JSTYPE)
.addMember("isNative", "true")
.addMember("namespace", "$T.GLOBAL", ClassNames.JSINTEROP_JSPACKAGE)
.build())
.addFields(Stream.of(Object.class,
Boolean.TYPE, Byte.TYPE, Character.TYPE, Double.TYPE,
Float.TYPE, Integer.TYPE, Long.TYPE, Short.TYPE)
.map(type -> FieldSpec.builder(type, "UNDEFINED_" + type.getSimpleName().toUpperCase())
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addAnnotation(AnnotationSpec.builder(ClassNames.JSINTEROP_JSPROPERTY)
.addMember("namespace", "$T.GLOBAL", ClassNames.JSINTEROP_JSPACKAGE)
.addMember("name", "$S", "undefined")
.build())
.build())
.collect(Collectors.toList()))
.build();
}
示例13: getMethodSpec
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
/**
* genera il metodo di test per method di clazz
*
* @param count
* @param method
* @param clazz
* @param infoFromMongoDb
* @param methodOutput
* @return
*/
private MethodSpec getMethodSpec(int count, Method method, Class<?> clazz, Document methodInputs,
Document methodOutput) {
String result = getAssignmentOfMethodResult(method);
String expected = getExpectedResultAsBooleanAssert(method, methodOutput);
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.getName() + count + TEST);
/*
* for non spring test
*/
String invokerName = getInvokerName(method, clazz, methodInputs, methodBuilder);
String params = getParams(method, methodBuilder, methodInputs);
AnnotationSpec.Builder annSpecBuilder = AnnotationSpec.builder(Test.class);
addExpectedExceptionIfAny(methodInputs, annSpecBuilder);
AnnotationSpec annTestSpec = annSpecBuilder.build();
methodBuilder.addAnnotation(annTestSpec)
.addStatement(result + invokerName + ".$N(" + params + ")", method.getName())
.addModifiers(Modifier.PUBLIC);
methodBuilder.addStatement("$L.assertTrue(" + expected + ")", Assert.class.getName());
methodBuilder.addJavadoc("\n");
return methodBuilder.build();
}
示例14: getTypeSpec
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
/**
* genera classe di test per clazz
*
* @param clazz
* @param prop
* @param mongo
* @return
*/
@Override
public TypeSpec getTypeSpec(Class<?> clazz) {
Builder classTestBuilder = TypeSpec.classBuilder(clazz.getSimpleName() + TEST);
ClassName superClass = ClassName.get(
PropertiesUtils.getRequiredProperty(prop, PropertiesUtils.TEST_BASE_PACKAGE),
PropertiesUtils.getRequiredProperty(prop, PropertiesUtils.TEST_BASE_CLASS));
classTestBuilder.superclass(superClass);
classTestBuilder.addJavadoc("@author \n");
classTestBuilder.addModifiers(Modifier.PUBLIC);
AnnotationSpec.Builder annSpecBuilder = AnnotationSpec.builder(Generated.class);
annSpecBuilder.addMember("value", "\"it.fratta.jerkoff.Generator\"");
annSpecBuilder.addMember("date", "\"" + Calendar.getInstance().getTime().toString() + "\"");
AnnotationSpec annGenSpec = annSpecBuilder.build();
classTestBuilder.addAnnotation(annGenSpec);
/*
* for spring test
*/
// FieldSpec.Builder spec = FieldSpec.builder(clazz,
// getNewInstanceOfNoParameters(clazz), Modifier.PRIVATE);
// spec.addAnnotation(Autowired.class);
// classTestBuilder.addField(spec.build());
addClassMethodsToBuilder(classTestBuilder, clazz);
return classTestBuilder.build();
}
示例15: visitTypeAsClass
import com.squareup.javapoet.AnnotationSpec; //導入依賴的package包/類
@Override
public DeferredTypeSpecBuilder visitTypeAsClass(final TypeElement element, final DeferredTypeSpecBuilder outerBuilder) {
final Collection<AnnotationSpec> descriptorAnnotations = descriptorInventory.typeAnnotations(element);
final TypeSubstitution substitution = applyDeclaredTypeSubstitution(element);
final DeferredTypeSpecBuilder innerBuilder = DeferredTypeSpecBuilder.create(classBuilder(substitution.raw())
.addTypeVariables(substitution.typeParameters())
.addModifiers(modifiers(element))
.addAnnotations(mergeAnnotations(copyableAnnotations(element), descriptorAnnotations))
.superclass(applyTypeSubstitution(element.getSuperclass())))
;
element.getInterfaces().forEach(iface -> innerBuilder.wrapped().addSuperinterface(applyTypeSubstitution(iface)));
element.getEnclosedElements().forEach(child -> visit(child, innerBuilder));
// If this is a nested type, we now roll up the new type into the parent.
return (outerBuilder != null)
? outerBuilder.addDeferred(innerBuilder)
: innerBuilder;
}