use of com.squareup.javapoet.TypeName in project wire by square.
the class JavaGenerator method fieldType.
private TypeName fieldType(Field field) {
ProtoType type = field.type();
if (type.isMap()) {
return ParameterizedTypeName.get(ClassName.get(Map.class), typeName(type.keyType()), typeName(type.valueType()));
}
TypeName messageType = typeName(type);
return field.isRepeated() ? listOf(messageType) : messageType;
}
use of com.squareup.javapoet.TypeName in project graylog2-server by Graylog2.
the class WithBeanGetterExtension method generateGetterMethod.
private MethodSpec generateGetterMethod(String name, ExecutableElement element) {
final TypeName returnType = ClassName.get(element.getReturnType());
final String prefix = isBoolean(returnType) ? "is" : "get";
final String getterName = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, name);
final MethodSpec.Builder builder = MethodSpec.methodBuilder(prefix + getterName).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addAnnotation(AnnotationSpec.builder(JsonIgnore.class).build()).addStatement("return $N()", name).returns(returnType);
// Copy all annotations but @JsonProperty, @JsonIgnore, and @Override to the new method.
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
final TypeName annotationType = ClassName.get(annotationMirror.getAnnotationType());
if (SKIP_ANNOTATIONS.contains(annotationType)) {
continue;
}
builder.addAnnotation(AnnotationSpec.get(annotationMirror));
}
return builder.build();
}
use of com.squareup.javapoet.TypeName in project dagger by square.
the class ModuleAdapterProcessor method generateProvidesAdapter.
private TypeSpec generateProvidesAdapter(ClassName moduleClassName, ClassName adapterName, ExecutableElement providerMethod, Map<ExecutableElement, ClassName> methodToClassName, Map<String, AtomicInteger> methodNameToNextId, boolean library) {
String methodName = providerMethod.getSimpleName().toString();
TypeMirror moduleType = providerMethod.getEnclosingElement().asType();
ClassName className = bindingClassName(adapterName, providerMethod, methodToClassName, methodNameToNextId);
TypeName returnType = Util.injectableType(providerMethod.getReturnType());
List<? extends VariableElement> parameters = providerMethod.getParameters();
boolean dependent = !parameters.isEmpty();
TypeSpec.Builder result = TypeSpec.classBuilder(className.simpleName()).addJavadoc("$L", bindingTypeDocs(returnType, false, false, dependent)).addModifiers(PUBLIC, STATIC, FINAL).superclass(ParameterizedTypeName.get(ClassName.get(ProvidesBinding.class), returnType));
result.addField(moduleClassName, "module", PRIVATE, FINAL);
for (Element parameter : parameters) {
result.addField(bindingOf(parameter.asType()), parameterName(parameter), PRIVATE);
}
boolean singleton = providerMethod.getAnnotation(Singleton.class) != null;
String key = GeneratorKeys.get(providerMethod);
result.addMethod(MethodSpec.constructorBuilder().addModifiers(PUBLIC).addParameter(moduleClassName, "module").addStatement("super($S, $L, $S, $S)", key, (singleton ? "IS_SINGLETON" : "NOT_SINGLETON"), typeToString(moduleType), methodName).addStatement("this.module = module").addStatement("setLibrary($L)", library).build());
if (dependent) {
MethodSpec.Builder attachBuilder = MethodSpec.methodBuilder("attach").addJavadoc(AdapterJavadocs.ATTACH_METHOD).addAnnotation(Override.class).addAnnotation(Util.UNCHECKED).addModifiers(PUBLIC).addParameter(Linker.class, "linker");
for (VariableElement parameter : parameters) {
String parameterKey = GeneratorKeys.get(parameter);
attachBuilder.addStatement("$N = ($T) linker.requestBinding($S, $T.class, getClass().getClassLoader())", parameterName(parameter), bindingOf(parameter.asType()), parameterKey, moduleClassName);
}
result.addMethod(attachBuilder.build());
MethodSpec.Builder getDependenciesBuilder = MethodSpec.methodBuilder("getDependencies").addJavadoc(AdapterJavadocs.GET_DEPENDENCIES_METHOD).addAnnotation(Override.class).addModifiers(PUBLIC).addParameter(Util.SET_OF_BINDINGS, "getBindings").addParameter(Util.SET_OF_BINDINGS, "injectMembersBindings");
for (Element parameter : parameters) {
getDependenciesBuilder.addStatement("getBindings.add($N)", parameterName(parameter));
}
result.addMethod(getDependenciesBuilder.build());
}
MethodSpec.Builder getBuilder = MethodSpec.methodBuilder("get").addJavadoc(AdapterJavadocs.GET_METHOD, returnType).addAnnotation(Override.class).addModifiers(PUBLIC).returns(returnType).addCode("return module.$N(", methodName);
boolean first = true;
for (Element parameter : parameters) {
if (!first)
getBuilder.addCode(", ");
getBuilder.addCode("$N.get()", parameterName(parameter));
first = false;
}
getBuilder.addCode(");\n");
result.addMethod(getBuilder.build());
return result.build();
}
use of com.squareup.javapoet.TypeName in project CoreData by FangCloud-Android.
the class BindCursorMethod method bindCursorToField.
private void bindCursorToField(MethodSpec.Builder builder, Element element, String prefix) {
// 基本类型
// String
// 内嵌类型
// 关联类型
// 转换类型
ClassName stringClassName = ClassName.get(String.class);
TypeName typeName = ClassName.get(element.asType());
Element typeElement = processingEnv.getTypeUtils().asElement(element.asType());
if (typeName.isPrimitive() || typeName.isBoxedPrimitive() || typeName.equals(stringClassName)) {
// 基础类型 // 封装基础类型 // 字符串类型
builder.addStatement(Utils.methodSetFormat(element, prefix), cursorGetMethod(element, Utils.getDbType(element)));
} else {
Embedded embedded = element.getAnnotation(Embedded.class);
if (embedded != null) {
// 内嵌类型循环添加
// 创建临时变量
String embeddedTempName = "__" + element.getSimpleName() + "Temp";
builder.addStatement("$T $N = new $T()", typeName, embeddedTempName, typeName);
// 赋值
List<Element> elementsForDbEmbedded = new ArrayList<>();
Utils.fillElementsForDbAndReturnPrimaryKey(processingEnv, elementsForDbEmbedded, (TypeElement) typeElement);
;
for (Element eleEmbeddedChild : elementsForDbEmbedded) {
bindCursorToField(builder, eleEmbeddedChild, embeddedTempName);
}
builder.addStatement(Utils.methodSetFormat(element, prefix), embeddedTempName);
return;
}
Convert convert = element.getAnnotation(Convert.class);
if (convert != null) {
ClassName classConverter = ClassName.bestGuess(Utils.getConverterType(convert).toString());
TypeName convertDbType = Utils.getConvertDbType(convert);
if (!convertDbType.isPrimitive()) {
builder.addCode("if(!cursor.isNull(cursorIndexOf$N)){\n ", Utils.getColumnName(element));
}
builder.addStatement(Utils.methodSetFormat(element, prefix), Utils.converterName(element, classConverter) + ".convertToValue(" + cursorGetMethod(element, Utils.getDbType(element)) + ")");
if (!convertDbType.isPrimitive()) {
builder.addCode("}");
}
return;
}
Relation relation = element.getAnnotation(Relation.class);
if (relation != null) {
TypeElement typeRelation = (TypeElement) processingEnv.getTypeUtils().asElement(element.asType());
EntityDetail relationEntityDetail = EntityDetail.parse(processingEnv, typeRelation);
Element primaryKeyElement = relationEntityDetail.getPrimaryKey();
if (primaryKeyElement != null) {
TypeName typePrimary = ClassName.get(primaryKeyElement.asType());
String columnName = Utils.getColumnName(element);
if (!typePrimary.isPrimitive()) {
builder.addCode("if(!cursor.isNull(cursorIndexOf$N)){\n ", columnName);
}
builder.addStatement("__$NMap.put($N, entity)", columnName, cursorGetMethod(element, Utils.getDbType(primaryKeyElement)));
if (!typePrimary.isPrimitive()) {
builder.addCode("}");
}
}
return;
}
}
}
use of com.squareup.javapoet.TypeName in project CoreData by FangCloud-Android.
the class BindCursorMethod method cursorGetMethod.
private String cursorGetMethod(Element element, TypeName dbType) {
// TypeName typeName = Utils.getDbType(element);
TypeName unbox = null;
try {
unbox = dbType.unbox();
} catch (Exception ignored) {
}
String getMethod = null;
if (unbox != null) {
if (unbox == TypeName.BYTE) {
getMethod = "getInt";
} else if (unbox == TypeName.SHORT) {
getMethod = "getShort";
} else if (unbox == TypeName.INT) {
getMethod = "getInt";
} else if (unbox == TypeName.LONG) {
getMethod = "getLong";
} else if (unbox == TypeName.LONG) {
getMethod = "getLong";
} else if (unbox == TypeName.CHAR) {
getMethod = "getInt";
} else if (unbox == TypeName.FLOAT) {
getMethod = "getFloat";
} else if (unbox == TypeName.FLOAT) {
getMethod = "getDouble";
} else if (unbox == TypeName.BOOLEAN) {
return String.format("cursor.getInt(cursorIndexOf%s) != 0 ? true : false", Utils.getColumnName(element));
}
} else {
getMethod = "getString";
}
return String.format("cursor.%s(cursorIndexOf%s)", getMethod, Utils.getColumnName(element));
}
Aggregations