本文整理了Java中com.squareup.javapoet.ClassName
类的一些代码示例,展示了ClassName
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ClassName
类的具体详情如下:
包路径:com.squareup.javapoet.ClassName
类名称:ClassName
[英]A fully-qualified class name for top-level and member classes.
[中]顶级类和成员类的完全限定类名。
代码示例来源:origin: ACRA/acra
private void createConfigClass(@NonNull List<Element> elements) throws IOException {
final TypeSpec.Builder classBuilder = TypeSpec.classBuilder(configName)
.addSuperinterface(Serializable.class)
.addSuperinterface(org.acra.config.Configuration.class)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
Strings.addClassJavadoc(classBuilder, ClassName.get(baseAnnotation.asType()));
final MethodSpec.Builder constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC)
.addParameter(ParameterSpec.builder(ClassName.get(PACKAGE, builderName), PARAM_0).addAnnotation(Types.NON_NULL).build());
elements.stream().filter(ConfigElement.class::isInstance).map(ConfigElement.class::cast).forEach(element -> element.addToConfig(classBuilder, constructor));
classBuilder.addMethod(constructor.build());
Strings.writeClass(processingEnv.getFiler(), classBuilder.build());
}
代码示例来源:origin: square/wire
private TypeSpec enumAdapter(ClassName javaType, ClassName adapterJavaType) {
return TypeSpec.classBuilder(adapterJavaType.simpleName())
.superclass(enumAdapterOf(javaType))
.addModifiers(PRIVATE, STATIC, FINAL)
.addMethod(MethodSpec.constructorBuilder()
.addStatement("super($T.class)", javaType)
.build())
.addMethod(MethodSpec.methodBuilder("fromValue")
.addAnnotation(Override.class)
.addModifiers(PROTECTED)
.returns(javaType)
.addParameter(int.class, "value")
.addStatement("return $T.fromValue(value)", javaType)
.build())
.build();
}
代码示例来源:origin: JakeWharton/butterknife
private static TypeName bestGuess(String type) {
switch (type) {
case "void": return TypeName.VOID;
case "boolean": return TypeName.BOOLEAN;
case "byte": return TypeName.BYTE;
case "char": return TypeName.CHAR;
case "double": return TypeName.DOUBLE;
case "float": return TypeName.FLOAT;
case "int": return TypeName.INT;
case "long": return TypeName.LONG;
case "short": return TypeName.SHORT;
default:
int left = type.indexOf('<');
if (left != -1) {
ClassName typeClassName = ClassName.bestGuess(type.substring(0, left));
List<TypeName> typeArguments = new ArrayList<>();
do {
typeArguments.add(WildcardTypeName.subtypeOf(Object.class));
left = type.indexOf('<', left + 1);
} while (left != -1);
return ParameterizedTypeName.get(typeClassName,
typeArguments.toArray(new TypeName[typeArguments.size()]));
}
return ClassName.bestGuess(type);
}
}
代码示例来源:origin: facebook/litho
private static String getTypeNameString(ClassName className) {
ClassName enclosingClass = className.enclosingClassName();
if (enclosingClass == null) {
return className.toString();
}
return enclosingClass.toString() + "$" + className.simpleName();
}
}
代码示例来源:origin: square/dagger
/** Returns a class name to complement {@code type}. */
public static ClassName adapterName(ClassName type, String suffix) {
return ClassName.get(type.packageName(),
Joiner.on('$').join(type.simpleNames()) + suffix);
}
代码示例来源:origin: square/javapoet
/** Returns the class named {@code simpleName} when nested in the class at {@code stackDepth}. */
private ClassName stackClassName(int stackDepth, String simpleName) {
ClassName className = ClassName.get(packageName, typeSpecStack.get(0).name);
for (int i = 1; i <= stackDepth; i++) {
className = className.nestedClass(typeSpecStack.get(i).name);
}
return className.nestedClass(simpleName);
}
代码示例来源:origin: f2prateek/dart
private void emitResolvedOptionalSequence(TypeSpec.Builder intentBuilderTypeBuilder) {
// find superclass
final ClassName optionalSequence =
get(target.classPackage, builderClassName(), OPTIONAL_SEQUENCE_CLASS);
final ClassName resolvedOptional =
get(target.classPackage, builderClassName(), RESOLVED_OPTIONAL_SEQUENCE_CLASS);
TypeSpec.Builder resolvedOptionalSequenceBuilder =
TypeSpec.classBuilder(RESOLVED_OPTIONAL_SEQUENCE_CLASS)
.superclass(ParameterizedTypeName.get(optionalSequence, resolvedOptional))
.addModifiers(Modifier.PUBLIC)
.addModifiers(Modifier.STATIC);
MethodSpec.Builder constructorBuilder =
MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addParameter(Bundler.class, "bundler")
.addParameter(get("android.content", "Intent"), "intent")
.addStatement("super(bundler, intent)");
resolvedOptionalSequenceBuilder.addMethod(constructorBuilder.build());
intentBuilderTypeBuilder.addType(resolvedOptionalSequenceBuilder.build());
}
代码示例来源:origin: bingoogolapple/BGABadgeView-Android
private void setDragDismissDelegate(TypeSpec.Builder typeBuilder) {
MethodSpec methodSpec = MethodSpec.methodBuilder("setDragDismissDelegate")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(ClassName.get(PACKAGE_NAME, "BGADragDismissDelegate"), "delegate")
.addStatement("mBadgeViewHelper.setDragDismissDelegate(delegate)")
.build();
typeBuilder.addMethod(methodSpec);
}
代码示例来源:origin: web3j/web3j
private static ParameterizedTypeName buildRemoteCall(TypeName typeName) {
return ParameterizedTypeName.get(
ClassName.get(RemoteCall.class), typeName);
}
代码示例来源:origin: web3j/web3j
MethodSpec buildDefaultEventFlowableFunction(
String responseClassName,
String functionName) {
String generatedFunctionName =
Strings.lowercaseFirstLetter(functionName) + "EventFlowable";
ParameterizedTypeName parameterizedTypeName =
ParameterizedTypeName.get(ClassName.get(Flowable.class),
ClassName.get("", responseClassName));
MethodSpec.Builder flowableMethodBuilder =
MethodSpec.methodBuilder(generatedFunctionName)
.addModifiers(Modifier.PUBLIC)
.addParameter(DefaultBlockParameter.class, START_BLOCK)
.addParameter(DefaultBlockParameter.class, END_BLOCK)
.returns(parameterizedTypeName);
flowableMethodBuilder.addStatement("$1T filter = new $1T($2L, $3L, "
+ "getContractAddress())", EthFilter.class, START_BLOCK, END_BLOCK)
.addStatement("filter.addSingleTopic($T.encode("
+ buildEventDefinitionName(functionName) + "))", EventEncoder.class)
.addStatement("return " + generatedFunctionName + "(filter)");
return flowableMethodBuilder
.build();
}
代码示例来源:origin: web3j/web3j
Strings.lowercaseFirstLetter(functionName) + "EventFlowable";
ParameterizedTypeName parameterizedTypeName =
ParameterizedTypeName.get(ClassName.get(Flowable.class),
ClassName.get("", responseClassName));
TypeSpec converter = TypeSpec.anonymousClassBuilder("")
.addSuperinterface(ParameterizedTypeName.get(
ClassName.get(io.reactivex.functions.Function.class),
ClassName.get(Log.class),
ClassName.get("", responseClassName)))
.addMethod(MethodSpec.methodBuilder("apply")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(Log.class, "log")
.returns(ClassName.get("", responseClassName))
.addStatement("$T eventValues = extractEventParametersWithLog("
+ buildEventDefinitionName(functionName) + ", log)",
Contract.EventValuesWithLog.class)
.addStatement("$1T typedResponse = new $1T()",
ClassName.get("", responseClassName))
.addCode(buildTypedResponse("typedResponse", indexedParameters,
nonIndexedParameters, true))
.addStatement("return typedResponse")
.build())
.build();
代码示例来源:origin: square/javapoet
/** Returns a parameterized type equivalent to {@code type}. */
static ParameterizedTypeName get(ParameterizedType type, Map<Type, TypeVariableName> map) {
ClassName rawType = ClassName.get((Class<?>) type.getRawType());
ParameterizedType ownerType = (type.getOwnerType() instanceof ParameterizedType)
&& !Modifier.isStatic(((Class<?>) type.getRawType()).getModifiers())
? (ParameterizedType) type.getOwnerType() : null;
List<TypeName> typeArguments = TypeName.list(type.getActualTypeArguments(), map);
return (ownerType != null)
? get(ownerType, map).nestedClass(rawType.simpleName(), typeArguments)
: new ParameterizedTypeName(null, rawType, typeArguments);
}
}
代码示例来源:origin: square/dagger
/**
* Write a companion class for {@code type} that extends {@link StaticInjection}.
*/
private void generateStaticInjection(TypeElement type, List<Element> fields) throws IOException {
ClassName typeName = ClassName.get(type);
ClassName adapterClassName = adapterName(ClassName.get(type), STATIC_INJECTION_SUFFIX);
TypeSpec.Builder result = TypeSpec.classBuilder(adapterClassName.simpleName())
.addOriginatingElement(type)
.addJavadoc(AdapterJavadocs.STATIC_INJECTION_TYPE, type)
.addModifiers(PUBLIC, FINAL)
.superclass(StaticInjection.class);
for (Element field : fields) {
result.addField(memberBindingField(false, field));
}
result.addMethod(attachMethod(null, fields, false, typeName, null, true));
result.addMethod(staticInjectMethod(fields, typeName));
String packageName = getPackage(type).getQualifiedName().toString();
JavaFile javaFile = JavaFile.builder(packageName, result.build())
.addFileComment(AdapterJavadocs.GENERATED_BY_DAGGER)
.build();
javaFile.writeTo(processingEnv.getFiler());
}
代码示例来源:origin: square/javapoet
public Builder addAnnotation(Class<?> annotation) {
return addAnnotation(ClassName.get(annotation));
}
代码示例来源:origin: square/wire
private TypeSpec generateEnclosingType(EnclosingType type) {
ClassName javaType = (ClassName) typeName(type.type());
TypeSpec.Builder builder = TypeSpec.classBuilder(javaType.simpleName())
.addModifiers(PUBLIC, FINAL);
if (javaType.enclosingClassName() != null) {
builder.addModifiers(STATIC);
}
String documentation = type.documentation();
if (!documentation.isEmpty()) {
documentation += "\n\n<p>";
}
documentation += "<b>NOTE:</b> This type only exists to maintain class structure"
+ " for its nested types and is not an actual message.";
builder.addJavadoc("$L\n", documentation);
builder.addMethod(MethodSpec.constructorBuilder()
.addModifiers(PRIVATE)
.addStatement("throw new $T()", AssertionError.class)
.build());
for (Type nestedType : type.nestedTypes()) {
builder.addType(generateType(nestedType));
}
return builder.build();
}
代码示例来源:origin: square/javapoet
public static Builder classBuilder(ClassName className) {
return classBuilder(checkNotNull(className, "className == null").simpleName());
}
代码示例来源:origin: ACRA/acra
private void createFactoryClass() throws IOException {
final TypeName configurationBuilderFactory = Types.CONFIGURATION_BUILDER_FACTORY;
Strings.writeClass(processingEnv.getFiler(), TypeSpec.classBuilder(factoryName)
.addModifiers(Modifier.PUBLIC)
.addSuperinterface(configurationBuilderFactory)
.addAnnotation(AnnotationSpec.builder(AutoService.class).addMember("value", "$T.class", configurationBuilderFactory).build())
.addMethod(Types.overriding(Types.getOnlyMethod(processingEnv, Strings.CONFIGURATION_BUILDER_FACTORY))
.addAnnotation(Types.NON_NULL)
.addStatement("return new $T($L)", ClassName.get(PACKAGE, builderName), PARAM_0)
.build())
.build());
}
}
代码示例来源:origin: facebook/litho
static TypeSpecDataHolder generateEventDispatcher(EventDeclarationModel eventDeclaration) {
final TypeSpecDataHolder.Builder typeSpecDataHolder = TypeSpecDataHolder.newBuilder();
final String poolName = "s" + eventDeclaration.name.simpleName() + "Pool";
final TypeName poolType =
ParameterizedTypeName.get(ClassNames.SYNCHRONIZED_POOL, eventDeclaration.name);
typeSpecDataHolder.addField(
FieldSpec.builder(poolType, poolName, Modifier.STATIC, Modifier.FINAL)
MethodSpec.methodBuilder("dispatch" + eventDeclaration.name.simpleName())
.addModifiers(Modifier.STATIC)
.addParameter(ClassNames.EVENT_HANDLER, "_eventHandler");
.addStatement("$T _eventState = $L.acquire()", eventDeclaration.name, poolName)
.beginControlFlow("if (_eventState == null)")
.addStatement("_eventState = new $T()", eventDeclaration.name)
代码示例来源:origin: janishar/PlaceHolderView
@Override
protected TypeSpec.Builder createClassBuilder() {
return TypeSpec.classBuilder(getClassDetail().getGeneratedClassName())
.superclass(ParameterizedTypeName.get(
getClassDetail().getSuperClassName(),
TypeVariableName.get(getClassDetail().getTypeElement().asType()),
TypeVariableName.get(getClassDetail().getViewTypeParameterClassName().simpleName())))
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Keep.class);
}
代码示例来源:origin: airbnb/epoxy
private void generateHelperClassForController(ControllerClassInfo controllerInfo)
throws IOException {
ParameterizedTypeName parameterizeSuperClass =
ParameterizedTypeName
.get(ClassNames.EPOXY_CONTROLLER_HELPER, controllerInfo.getControllerClassType());
TypeSpec.Builder builder = TypeSpec.classBuilder(controllerInfo.getGeneratedClassName())
.addJavadoc("Generated file. Do not modify!")
.addModifiers(Modifier.PUBLIC)
.superclass(parameterizeSuperClass)
.addField(controllerInfo.getControllerClassType(), "controller", Modifier.FINAL,
Modifier.PRIVATE)
.addMethod(buildConstructor(controllerInfo))
.addMethod(buildResetModelsMethod(controllerInfo));
if (configManager.shouldValidateModelUsage()) {
builder.addFields(buildFieldsToSaveModelsForValidation(controllerInfo))
.addMethod(buildValidateModelsHaveNotChangedMethod(controllerInfo))
.addMethod(buildValidateSameValueMethod())
.addMethod(buildSaveModelsForNextValidationMethod(controllerInfo));
}
JavaFile.builder(controllerInfo.getGeneratedClassName().packageName(), builder.build())
.build()
.writeTo(filer);
}
内容来源于网络,如有侵权,请联系作者删除!