use of com.squareup.javapoet.TypeName in project AndroidLife by CaMnter.
the class ButterKnifeProcessor method parseBindView.
/**
* 解析 BindView
*
* 1.拿到 BindArray 注解元素 的 .java 类元素
* 2.检查注解使用错误,或者注解所在的环境问题( 比如 private or static,所在的 .java 是 private 等 )
* - 有错误就 return
* 3.获取元素的类型
* 4.校验元素类型( 不是 View 的子类,并且不是接口类型 ),报错返回
* 5.获取 id 构造一个 QualifiedId;获取该 .java 元素对应的 BindingSet.Builder,没有则创建
* 6.检查 QualifiedId 包装成的 FieldResourceBinding 是否存在 BindingSet.Builder
* - 是,报错。防止生成重复代码
* - 不是,添加进去
* 7.记录 .java 元素 为要删除的目录
*
* @param element BindBitmap 注解元素
* @param builderMap BindingSet.Builder 缓存 Map
* @param erasedTargetNames 要删除元素的目录,存在的是 .java 的元素
*/
private void parseBindView(Element element, Map<TypeElement, BindingSet.Builder> builderMap, Set<TypeElement> erasedTargetNames) {
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
// Start by verifying common generated code restrictions.
boolean hasError = isInaccessibleViaGeneratedCode(BindView.class, "fields", element) || isBindingInWrongPackage(BindView.class, element);
// Verify that the target type extends from View.
TypeMirror elementType = element.asType();
if (elementType.getKind() == TypeKind.TYPEVAR) {
TypeVariable typeVariable = (TypeVariable) elementType;
elementType = typeVariable.getUpperBound();
}
Name qualifiedName = enclosingElement.getQualifiedName();
Name simpleName = element.getSimpleName();
if (!isSubtypeOfType(elementType, VIEW_TYPE) && !isInterface(elementType)) {
if (elementType.getKind() == TypeKind.ERROR) {
note(element, "@%s field with unresolved type (%s) " + "must elsewhere be generated as a View or interface. (%s.%s)", BindView.class.getSimpleName(), elementType, qualifiedName, simpleName);
} else {
error(element, "@%s fields must extend from View or be an interface. (%s.%s)", BindView.class.getSimpleName(), qualifiedName, simpleName);
hasError = true;
}
}
if (hasError) {
return;
}
// Assemble information on the field.
int id = element.getAnnotation(BindView.class).value();
BindingSet.Builder builder = builderMap.get(enclosingElement);
QualifiedId qualifiedId = elementToQualifiedId(element, id);
if (builder != null) {
String existingBindingName = builder.findExistingBindingName(getId(qualifiedId));
if (existingBindingName != null) {
error(element, "Attempt to use @%s for an already bound ID %d on '%s'. (%s.%s)", BindView.class.getSimpleName(), id, existingBindingName, enclosingElement.getQualifiedName(), element.getSimpleName());
return;
}
} else {
builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
}
String name = simpleName.toString();
TypeName type = TypeName.get(elementType);
boolean required = isFieldRequired(element);
builder.addField(getId(qualifiedId), new FieldViewBinding(name, type, required));
// Add the type-erased version to the valid binding targets set.
erasedTargetNames.add(enclosingElement);
}
use of com.squareup.javapoet.TypeName in project AndroidLife by CaMnter.
the class RouterClass method putFieldMethodBuilder.
/**
* SmartRouter # putValue(final int value)
* SmartRouter # putValue(@NonNull final Integer value)
*
* @return List<MethodSpec>
*/
private List<MethodSpec> putFieldMethodBuilder() {
final List<MethodSpec> putMethods = new ArrayList<>();
for (RouterFieldAnnotation routerFieldAnnotation : this.routerFieldAnnotationList) {
final TypeMirror fieldTypeMirror = routerFieldAnnotation.getFieldType();
final TypeName fieldTypeName = TypeName.get(fieldTypeMirror);
final String fieldTypeString = fieldTypeMirror.toString();
final String fieldName = routerFieldAnnotation.getFieldName().toString();
final String expectName = "put" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
final MethodSpec.Builder putMethodBuilder = MethodSpec.methodBuilder(expectName).addModifiers(Modifier.PUBLIC).returns(TypeName.VOID).addCode(CodeBlock.of("this.put($S, value);\n", fieldName));
switch(fieldTypeString) {
case CHAR:
case BYTE:
case SHORT:
case INT:
case FLOAT:
case DOUBLE:
case LONG:
case BOOLEAN:
putMethodBuilder.addParameter(fieldTypeName, "value", Modifier.FINAL);
putMethods.add(putMethodBuilder.build());
break;
case BOXED_CHAR:
case BOXED_BYTE:
case BOXED_SHORT:
case BOXED_INT:
case BOXED_FLOAT:
case BOXED_DOUBLE:
case BOXED_LONG:
case BOXED_BOOLEAN:
case STRING:
putMethodBuilder.addParameter(createNonNullParameter(fieldTypeName, "value", Modifier.FINAL));
putMethods.add(putMethodBuilder.build());
break;
}
}
return putMethods;
}
use of com.squareup.javapoet.TypeName in project AndroidLife by CaMnter.
the class AnnotatedClass method saveMethodBuilder.
/**
* void save(T target, Adapter adapter)
*/
private MethodSpec.Builder saveMethodBuilder() {
MethodSpec.Builder saveMethodBuilder = MethodSpec.methodBuilder("save").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(TypeVariableName.get("T"), "target", Modifier.FINAL).addParameter(SaveType.ADAPTER, "adapter", Modifier.FINAL);
// findViewById, getString, getDrawable
for (SaveField saveField : this.saveFields) {
final TypeMirror fieldType = saveField.getFieldType();
final String fieldTypeString;
if (SaveHelper.isSubtypeOfType(fieldType, SaveType.ANDROID_VIEW.toString())) {
// findViewById
saveMethodBuilder.addStatement("target.$N = ($T) (adapter.findViewById(target, $L))", saveField.getFieldName(), ClassName.get(fieldType), saveField.getResId());
} else if ((SaveType.STRING.toString()).equals((fieldTypeString = fieldType.toString()))) {
// getString
saveMethodBuilder.addStatement("target.$N = adapter.getString(target, $L)", saveField.getFieldName(), saveField.getResId());
} else if ((SaveType.ANDROID_DRAWABLE.toString()).equals(fieldTypeString)) {
// getDrawable
saveMethodBuilder.addStatement("target.$N = adapter.getDrawable(target, $L)", saveField.getFieldName(), saveField.getResId());
}
}
// getColor
for (SaveColorField saveColorField : this.saveColorFields) {
saveMethodBuilder.addStatement("target.$N = adapter.getColor(target, $L)", saveColorField.getFieldName(), saveColorField.getResId());
}
// getDimension, getDimensionPixelSize
TypeName typeName;
for (SaveDimensionField saveDimensionField : this.saveDimensionFields) {
if ((typeName = TypeName.get(saveDimensionField.getFieldType())).equals(TypeName.FLOAT)) {
// getDimension
saveMethodBuilder.addStatement("target.$N = adapter.getDimension(target, $L)", saveDimensionField.getFieldName(), saveDimensionField.getResId());
} else if (typeName.equals(TypeName.INT)) {
// getDimensionPixelSize
saveMethodBuilder.addStatement("target.$N = adapter.getDimensionPixelSize(target, $L)", saveDimensionField.getFieldName(), saveDimensionField.getResId());
}
}
// setOnClickListener
for (SaveOnClickMethod saveOnClickMethod : this.saveOnClickMethods) {
final boolean firstParameterViewExist = saveOnClickMethod.isFirstParameterViewExist();
for (final int id : saveOnClickMethod.getIds()) {
saveMethodBuilder.addStatement("adapter.findViewById(target, $L).setOnClickListener($L)", id, TypeSpec.anonymousClassBuilder("").addSuperinterface(SaveType.ANDROID_ON_CLICK_LISTENER).addMethod(MethodSpec.methodBuilder("onClick").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(TypeName.VOID).addParameter(SaveType.ANDROID_VIEW, "view", Modifier.FINAL).addStatement(firstParameterViewExist ? "target.$N(view)" : "target.$N()", saveOnClickMethod.getMethodName()).build()).build());
}
}
return saveMethodBuilder;
}
use of com.squareup.javapoet.TypeName in project AndroidLife by CaMnter.
the class AnnotatedClass method unSaveMethodBuilder.
private MethodSpec.Builder unSaveMethodBuilder() {
MethodSpec.Builder unSaveMethodBuilder = MethodSpec.methodBuilder("unSave").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(TypeVariableName.get("T"), "target", Modifier.FINAL).addParameter(SaveType.ADAPTER, "adapter", Modifier.FINAL);
// findViewById, getString, getDrawable
for (SaveField saveField : this.saveFields) {
unSaveMethodBuilder.addStatement("target.$N = null", saveField.getFieldName());
}
// getColor
for (SaveColorField saveColorField : this.saveColorFields) {
unSaveMethodBuilder.addStatement("target.$N = 0", saveColorField.getFieldName());
}
// getDimension, getDimensionPixelSize
TypeName typeName;
for (SaveDimensionField saveDimensionField : this.saveDimensionFields) {
if ((typeName = TypeName.get(saveDimensionField.getFieldType())).equals(TypeName.FLOAT)) {
// getDimension
unSaveMethodBuilder.addStatement("target.$N = 0.0f", saveDimensionField.getFieldName());
} else if (typeName.equals(TypeName.INT)) {
// getDimensionPixelSize
unSaveMethodBuilder.addStatement("target.$N = 0", saveDimensionField.getFieldName());
}
}
// setOnClickListener
for (SaveOnClickMethod saveOnClickMethod : this.saveOnClickMethods) {
for (final int id : saveOnClickMethod.getIds()) {
unSaveMethodBuilder.addStatement("adapter.findViewById(target, $L).setOnClickListener(null)", id);
}
}
return unSaveMethodBuilder;
}
use of com.squareup.javapoet.TypeName in project kripton by xcesco.
the class ModifyRawHelper method generateJavaDoc.
/**
* @param daoDefinition
* @param method
* @param methodBuilder
* @param updateMode
* @param whereCondition
* @param where
* @param methodParams
* @param updateableParams
*
* @return sql generated
*/
private void generateJavaDoc(final SQLiteModelMethod method, MethodSpec.Builder methodBuilder, boolean updateMode, String whereCondition, Pair<String, List<Pair<String, TypeName>>> where, List<Pair<String, TypeName>> methodParams) {
final List<SQLProperty> updatedProperties = new ArrayList<>();
final One<Boolean> onWhereStatement = new One<Boolean>(false);
String sqlModify = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {
@Override
public void onWhereStatementBegin(Where_stmtContext ctx) {
onWhereStatement.value0 = true;
}
@Override
public void onWhereStatementEnd(Where_stmtContext ctx) {
onWhereStatement.value0 = false;
}
@Override
public String onColumnNameToUpdate(String columnName) {
SQLProperty tempProperty = currentEntity.get(columnName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
updatedProperties.add(tempProperty);
return tempProperty.columnName;
}
@Override
public String onColumnName(String columnName) {
SQLProperty tempProperty = currentEntity.get(columnName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
return tempProperty.columnName;
}
@Override
public String onBindParameter(String bindParameterName) {
String resolvedParamName = method.findParameterNameByAlias(bindParameterName);
AssertKripton.assertTrueOrUnknownParamInJQLException(resolvedParamName != null, method, bindParameterName);
if (onWhereStatement.value0) {
return "${" + bindParameterName + "}";
} else {
return ":" + bindParameterName;
}
}
});
if (updateMode) {
methodBuilder.addJavadoc("<h2>SQL update</h2>\n");
methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlModify);
methodBuilder.addJavadoc("\n");
// list of updated fields
methodBuilder.addJavadoc("<h2>Updated columns:</h2>\n");
methodBuilder.addJavadoc("<ul>\n");
for (SQLProperty property : updatedProperties) {
methodBuilder.addJavadoc("\t<li>$L</li>\n", property.columnName);
// methodBuilder.addJavadoc("<dd>is binded to query's parameter
// <strong>$L</strong> and method's parameter
// <strong>$L</strong></dd>\n", "${" + resolvedName + "}",
// property.value0);
}
methodBuilder.addJavadoc("</ul>");
methodBuilder.addJavadoc("\n\n");
} else {
methodBuilder.addJavadoc("<h2>SQL delete</h2>\n");
methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlModify);
methodBuilder.addJavadoc("\n\n");
}
// list of where parameter
methodBuilder.addJavadoc("<h2>Where parameters:</h2>\n");
methodBuilder.addJavadoc("<dl>\n");
for (Pair<String, TypeName> property : where.value1) {
String rawName = method.findParameterNameByAlias(property.value0);
methodBuilder.addJavadoc("\t<dt>$L</dt>", "${" + property.value0 + "}");
methodBuilder.addJavadoc("<dd>is mapped to method's parameter <strong>$L</strong></dd>\n", rawName);
}
methodBuilder.addJavadoc("</dl>");
methodBuilder.addJavadoc("\n\n");
if (method.hasDynamicWhereConditions()) {
methodBuilder.addJavadoc("<dl>\n");
methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
methodBuilder.addJavadoc("\n</dl>");
methodBuilder.addJavadoc("\n\n");
}
// dynamic conditions
if (method.hasDynamicWhereConditions()) {
methodBuilder.addJavadoc("<h2>Method's parameters and associated dynamic parts:</h2>\n");
methodBuilder.addJavadoc("<dl>\n");
if (method.hasDynamicWhereConditions()) {
methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
}
methodBuilder.addJavadoc("</dl>");
methodBuilder.addJavadoc("\n\n");
}
// method parameters
if (methodParams.size() > 0) {
for (Pair<String, TypeName> param : methodParams) {
String resolvedName = method.findParameterAliasByName(param.value0);
methodBuilder.addJavadoc("@param $L", param.value0);
if (method.isThisDynamicWhereConditionsName(param.value0)) {
methodBuilder.addJavadoc("\n\tis used as dynamic where conditions\n");
} else if (where.value1.contains(new Pair<>(resolvedName, param.value1))) {
methodBuilder.addJavadoc("\n\tis used as where parameter <strong>$L</strong>\n", "${" + resolvedName + "}");
} else {
methodBuilder.addJavadoc("\n\tis used as updated field <strong>$L</strong>\n", resolvedName);
}
}
}
// if true, field must be associate to ben attributes
TypeName returnType = method.getReturnClass();
// define return value
if (returnType == TypeName.VOID) {
} else {
methodBuilder.addJavadoc("\n");
if (isIn(returnType, Boolean.TYPE, Boolean.class)) {
if (updateMode) {
methodBuilder.addJavadoc("@return <code>true</code> if record is updated, <code>false</code> otherwise");
} else {
methodBuilder.addJavadoc("@return <code>true</code> if record is deleted, <code>false</code> otherwise");
}
// methodBuilder.addCode("return result!=0;\n");
} else if (isIn(returnType, Long.TYPE, Long.class, Integer.TYPE, Integer.class, Short.TYPE, Short.class)) {
if (updateMode) {
methodBuilder.addJavadoc("@return number of updated records");
} else {
methodBuilder.addJavadoc("@return number of deleted records");
}
// methodBuilder.addCode("return result;\n");
} else {
// more than one listener found
throw (new InvalidMethodSignException(method, "invalid return type"));
}
methodBuilder.addJavadoc("\n");
}
}
Aggregations