本文整理匯總了Java中com.squareup.javapoet.ClassName.bestGuess方法的典型用法代碼示例。如果您正苦於以下問題:Java ClassName.bestGuess方法的具體用法?Java ClassName.bestGuess怎麽用?Java ClassName.bestGuess使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.squareup.javapoet.ClassName
的用法示例。
在下文中一共展示了ClassName.bestGuess方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: bestGuess
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
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);
}
}
示例2: generateConstructor
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
private MethodSpec generateConstructor(ClassName appGlideModule,
Set<String> libraryGlideModuleClassNames, Set<String> excludedGlideModuleClassNames) {
MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder();
constructorBuilder.addStatement("appGlideModule = new $T()", appGlideModule);
ClassName androidLogName = ClassName.get("android.util", "Log");
// Add some log lines to indicate to developers which modules where discovered.
constructorBuilder.beginControlFlow("if ($T.isLoggable($S, $T.DEBUG))",
androidLogName, GLIDE_LOG_TAG, androidLogName);
constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG,
"Discovered AppGlideModule from annotation: " + appGlideModule);
// Excluded GlideModule classes from the manifest are logged in Glide's singleton.
for (String glideModule : libraryGlideModuleClassNames) {
ClassName moduleClassName = ClassName.bestGuess(glideModule);
if (excludedGlideModuleClassNames.contains(glideModule)) {
constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG,
"AppGlideModule excludes LibraryGlideModule from annotation: " + moduleClassName);
} else {
constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG,
"Discovered LibraryGlideModule from annotation: " + moduleClassName);
}
}
constructorBuilder.endControlFlow();
return constructorBuilder.build();
}
示例3: generateAdditionalRequestManagerMethodNew
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
private MethodSpec generateAdditionalRequestManagerMethodNew(
ExecutableElement extensionMethod) {
String returnType = processorUtil.findClassValuesFromAnnotationOnClassAsNames(extensionMethod,
GlideType.class).iterator().next();
ClassName returnTypeClassName = ClassName.bestGuess(returnType);
ParameterizedTypeName parameterizedTypeName =
ParameterizedTypeName.get(generatedRequestBuilderClassName, returnTypeClassName);
return MethodSpec.methodBuilder(extensionMethod.getSimpleName().toString())
.addModifiers(Modifier.PUBLIC)
.returns(parameterizedTypeName)
.addJavadoc(processorUtil.generateSeeMethodJavadoc(extensionMethod))
.addStatement(
"return ($T) $T.$N(this.as($T.class))",
parameterizedTypeName,
extensionMethod.getEnclosingElement(),
extensionMethod.getSimpleName(),
returnTypeClassName)
.build();
}
示例4: generateRegisterComponents
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
private MethodSpec generateRegisterComponents(Set<String> libraryGlideModuleClassNames,
Set<String> excludedGlideModuleClassNames) {
MethodSpec.Builder registerComponents =
MethodSpec.methodBuilder("registerComponents")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addParameter(ClassName.get("android.content", "Context"), "context")
.addParameter(ClassName.get("com.bumptech.glide", "Glide"), "glide")
.addParameter(ClassName.get("com.bumptech.glide", "Registry"), "registry");
for (String glideModule : libraryGlideModuleClassNames) {
if (excludedGlideModuleClassNames.contains(glideModule)) {
continue;
}
ClassName moduleClassName = ClassName.bestGuess(glideModule);
registerComponents.addStatement(
"new $T().registerComponents(context, glide, registry)", moduleClassName);
}
// Order matters here. The AppGlideModule must be called last.
registerComponents.addStatement("appGlideModule.registerComponents(context, glide, registry)");
return registerComponents.build();
}
示例5: generateListType
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
@Nonnull
private TypeName generateListType(@Nonnull String className, @Nonnull JsonValue value, @Nonnull ClassBuilder builder) {
if (value.isArray()) {
final JsonValue firstValue = value.asArray()
.stream()
.findFirst()
.orElse(new JsonNull());
final TypeName type = generateListType(className, firstValue, builder);
return ParameterizedTypeName.get(ClassName.get(List.class), type);
}
if (value.isObject()) {
final TypeSpec innerClass = fromObject(className, value.asObject(), Modifier.PUBLIC, Modifier.STATIC);
builder.addInnerType(innerClass);
final TypeName innerClassType = ClassName.bestGuess(innerClass.name);
return ParameterizedTypeName.get(ClassName.get(List.class), innerClassType);
}
return ParameterizedTypeName.get(ClassName.get(List.class), value.getType().box());
}
示例6: callMethodShortcut
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
private MethodSpec callMethodShortcut() {
HashMap<String, List<String>> classMethodsMap = new HashMap<>();
for (final ShortcutAnnotatedElement annotatedElement : annotatedElements) {
if (annotatedElement instanceof ShortcutAnnotatedMethod) {
final ShortcutAnnotatedMethod annotatedMethod = (ShortcutAnnotatedMethod) annotatedElement;
if (classMethodsMap.containsKey(annotatedMethod.getClassName())) {
classMethodsMap.get(annotatedElement.getClassName()).add(annotatedMethod.getMethodName());
} else {
classMethodsMap.put(annotatedMethod.getClassName(), new ArrayList<String>() {{
add(annotatedMethod.getMethodName());
}});
}
}
}
final MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("callMethodShortcut")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(void.class)
.addParameter(activity, "activity");
for (final Map.Entry<String, List<String>> annotatedMethodName : classMethodsMap.entrySet()) {
ClassName activityClassName = ClassName.bestGuess(annotatedMethodName.getKey());
List<String> methodNames = annotatedMethodName.getValue();
methodBuilder.beginControlFlow("if (activity instanceof $T)", activityClassName);
for (final String methodName : methodNames) {
methodBuilder.beginControlFlow("if ($S.equals(activity.getIntent().getStringExtra($S)))", methodName, EXTRA_METHOD);
methodBuilder.addStatement("(($T) activity).$L()", activityClassName, methodName);
methodBuilder.endControlFlow();
}
methodBuilder.endControlFlow();
}
return methodBuilder
.build();
}
示例7: PresenterCommandSourceWriter
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
public PresenterCommandSourceWriter(ProcessorElement processorElement, String targetPackage,
String className, String presenterName) {
super(processorElement);
this.targetPackage = targetPackage;
this.className = className;
this.presenterType = ClassName.bestGuess(presenterName);
}
示例8: testNestedBeanTypeField
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
@Test
public void testNestedBeanTypeField() throws Exception {
ClassName serializer = ClassName.bestGuess("com.progressoft.brix.domino.jacksonapt.processor.TestBeanBeanJsonSerializerImpl");
TypeRegistry.registerSerializer("com.progressoft.brix.domino.jacksonapt.processor.TestBean", serializer);
addFieldTest("testBean", result -> assertEquals(buildTestString("new $T()", serializer), result));
runTests();
}
示例9: getJavadocSafeName
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
/**
* Returns a safe String to use in a Javadoc that will function in a link.
*
* <p>This method exists because by Javadoc doesn't handle type parameters({@literal <T>}
* in {@literal RequestOptions<T>} for example).
*/
private TypeName getJavadocSafeName(Element element) {
Types typeUtils = processingEnv.getTypeUtils();
TypeMirror type = element.asType();
if (typeUtils.asElement(type) == null) {
// If there is no Element, it's a primitive and can't have additional types, so we're done.
return ClassName.get(element.asType());
}
Name simpleName = typeUtils.asElement(type).getSimpleName();
return ClassName.bestGuess(simpleName.toString());
}
示例10: bestGuess
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
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);
}
}
示例11: bestGuessDeep
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
/**
* @param type
* @return
*/
public static TypeName bestGuessDeep(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('<');
int right = type.indexOf('>');
if (-1 != left && -1 != right) {
ClassName typeClassName = ClassName.bestGuess(type.substring(0, left));
List<TypeName> typeArguments = new ArrayList<>();
do {
typeArguments.add(WildcardTypeName.subtypeOf(bestGuess(type.substring(left + 1, right))));
left = type.indexOf('<', left + 1);
right = type.indexOf('>', right - 1);
} while (left != -1);
return ParameterizedTypeName.get(typeClassName,
typeArguments.toArray(new TypeName[typeArguments.size()]));
}
return ClassName.bestGuess(type);
}
}
示例12: processGet
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
void processGet(ElementDescription elem, MethodSpec.Builder builder) {
if(elem.code != null && elem.code.isGeneric()) {
String[] types = elem.code.genericTypes();
Object[] args = new Object[types.length + 2];
args[0] = ClazzNames.GSON_HELPER;
args[1] = ClazzNames.STORAGE_HELPER;
args[2] = ClazzNames.TYPE_TOKEN;
for (int i=3; i<args.length; i++) {
args[i] = ClassName.bestGuess(types[i-2]);
}
builder.addStatement("return $T.fromJson($T.get(app, \""
+ elem.key + "\"), new $T<" + elem.code.genericTypes()[0]+">(){}.getType())",
args);
} else if(isBool(elem)) {
builder.addStatement("return $T.getBoolean(app, \"" + elem.key + "\")", ClazzNames.STORAGE_HELPER);
} else if(isFloat(elem)) {
builder.addStatement("return $T.getFloat(app, \"" + elem.key + "\")", ClazzNames.STORAGE_HELPER);
} else if(isInt(elem)) {
builder.addStatement("return $T.getInt(app, \"" + elem.key + "\")", ClazzNames.STORAGE_HELPER);
} else if(isLong(elem)) {
builder.addStatement("return $T.getLong(app, \"" + elem.key + "\")", ClazzNames.STORAGE_HELPER);
} else if(isString(elem)) {
builder.addStatement("return $T.get(app, \"" + elem.key + "\")", ClazzNames.STORAGE_HELPER);
} else if(isStringSet(elem)) {
builder.addStatement("return $T.getStringSet(app, \"" + elem.key + "\")", ClazzNames.STORAGE_HELPER);
} else {
builder.addStatement("return $T.fromJson($T.get(app, \""
+ elem.key + "\"), " + elem.fieldType.toString()+".class)",
ClazzNames.GSON_HELPER,
ClazzNames.STORAGE_HELPER);
}
}
示例13: toWrite
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
public static void toWrite(ProcessorHelper helper, LJSONTypeElement ljsonElement, Filer filer) throws IOException {
// create common ClassName
ClassName thisObj = ClassName.bestGuess(ljsonElement.getTypeName());
ClassName jsonObject = ClassName.get("org.json", "JSONObject");
MethodSpec.Builder createMethod = MethodSpec.methodBuilder("create")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(thisObj)
.addParameter(ClassName.get(String.class), "json", Modifier.FINAL)
.addParameter(TypeName.BOOLEAN, "allowNull", Modifier.FINAL)
.addStatement("$T thisObj = new $T()", thisObj, thisObj)
.beginControlFlow("try")
.addStatement("$T root = new $T(json)", jsonObject, jsonObject);
List<LIMITJSONVariable> ljsonVariableElements = ljsonElement.getVariableElements();
addLJSONVariable(helper, createMethod, ljsonVariableElements);
createMethod.nextControlFlow("catch (Exception e)")
.beginControlFlow("if (allowNull)")
.addStatement("return null")
.endControlFlow()
.endControlFlow();
createMethod.addStatement("return thisObj");
// generate whole class
TypeSpec finderClass = TypeSpec.classBuilder(ljsonElement.getTypeName() + "$$CREATOR")
.addModifiers(Modifier.PUBLIC)
// .addSuperinterface(ParameterizedTypeName.get(ClassName.get(CORE_PACKAGE_NAME, "Core"), TypeName.get(classElement.asType())))
.addMethod(createMethod.build())
// .addMethod(parseIntentBuilder.build())
// .addMethod(saveInstanceStateBuilder.build())
.build();
String packageName = ljsonElement.getTypePackageName();
// generate file
JavaFile.builder(packageName, finderClass).build().writeTo(filer);
}
示例14: process
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
@TargetApi(24)
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
if (annotations.isEmpty()) {
return true;
}
TypeElement originalClass = roundEnvironment.getElementsAnnotatedWith(WrapStatic.class).stream()
.filter(element -> element.getKind() == ElementKind.CLASS)
.map(element -> (TypeElement) element)
.findFirst()
.get();
List<ExecutableElement> staticMethods = originalClass.getEnclosedElements().stream()
.filter(element -> element.getKind() == ElementKind.METHOD)
.map(element -> (ExecutableElement) element)
.filter(executableElement -> executableElement.getModifiers().stream().anyMatch(modifier -> modifier == Modifier.STATIC))
.filter(executableElement -> executableElement.getModifiers().stream().noneMatch(modifier -> modifier == Modifier.PRIVATE))
.collect(Collectors.toList());
List<MethodSpec> proxyMethods = staticMethods.stream()
.map((method) -> getProxyMethod(originalClass, method))
.collect(Collectors.toList());
String targetPackage = elementUtils.getPackageOf(originalClass).getQualifiedName().toString();
String targetClassName = getTargetClassName(originalClass);
ClassName className = ClassName.bestGuess(String.format("%s.%s", targetPackage, targetClassName));
FieldSpec singletonField = getSingletonField(className);
TypeSpec targetTypeSpec = TypeSpec
.classBuilder(targetClassName)
.addModifiers(getTargetClassModifiers(originalClass))
.addField(singletonField)
.addMethod(getSingletonGetterMethod(className, singletonField))
.addMethod(getSingletonSetterMethod(className, singletonField))
.addMethods(proxyMethods)
.build();
JavaFile javaFile = JavaFile
.builder(targetPackage, targetTypeSpec)
.addFileComment("This is auto-generated code. Do not modify this directly.")
.build();
try {
javaFile.writeTo(filer);
} catch (IOException e) {
messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage());
}
return true;
}
示例15: addType
import com.squareup.javapoet.ClassName; //導入方法依賴的package包/類
public void addType(TargetDescriptor des) {
ClassName serviceType = ClassName.bestGuess(des.name);
ClassName builderName = ClassName.bestGuess(des.name + AnnotationConstants.SUFFIX_SERVICE_TARGET);
addType(des.getSimpleName(), des.name, serviceType, builderName, null);
}