use of com.squareup.javapoet.ClassName in project butterknife by JakeWharton.
the class BindingSet method addFieldAndUnbindStatement.
private void addFieldAndUnbindStatement(TypeSpec.Builder result, MethodSpec.Builder unbindMethod, ViewBinding bindings) {
// Only add fields to the binding if there are method bindings.
Map<ListenerClass, Map<ListenerMethod, Set<MethodViewBinding>>> classMethodBindings = bindings.getMethodBindings();
if (classMethodBindings.isEmpty()) {
return;
}
String fieldName = bindings.isBoundToRoot() ? "viewSource" : "view" + bindings.getId().value;
result.addField(VIEW, fieldName, PRIVATE);
// We only need to emit the null check if there are zero required bindings.
boolean needsNullChecked = bindings.getRequiredBindings().isEmpty();
if (needsNullChecked) {
unbindMethod.beginControlFlow("if ($N != null)", fieldName);
}
for (ListenerClass listenerClass : classMethodBindings.keySet()) {
// We need to keep a reference to the listener
// in case we need to unbind it via a remove method.
boolean requiresRemoval = !"".equals(listenerClass.remover());
String listenerField = "null";
if (requiresRemoval) {
TypeName listenerClassName = bestGuess(listenerClass.type());
listenerField = fieldName + ((ClassName) listenerClassName).simpleName();
result.addField(listenerClassName, listenerField, PRIVATE);
}
if (!VIEW_TYPE.equals(listenerClass.targetType())) {
unbindMethod.addStatement("(($T) $N).$N($N)", bestGuess(listenerClass.targetType()), fieldName, removerOrSetter(listenerClass, requiresRemoval), listenerField);
} else {
unbindMethod.addStatement("$N.$N($N)", fieldName, removerOrSetter(listenerClass, requiresRemoval), listenerField);
}
if (requiresRemoval) {
unbindMethod.addStatement("$N = null", listenerField);
}
}
unbindMethod.addStatement("$N = null", fieldName);
if (needsNullChecked) {
unbindMethod.endControlFlow();
}
}
use of com.squareup.javapoet.ClassName in project butterknife by JakeWharton.
the class BindingSet method addMethodBindings.
private void addMethodBindings(MethodSpec.Builder result, ViewBinding binding) {
Map<ListenerClass, Map<ListenerMethod, Set<MethodViewBinding>>> classMethodBindings = binding.getMethodBindings();
if (classMethodBindings.isEmpty()) {
return;
}
// We only need to emit the null check if there are zero required bindings.
boolean needsNullChecked = binding.getRequiredBindings().isEmpty();
if (needsNullChecked) {
result.beginControlFlow("if (view != null)");
}
// Add the view reference to the binding.
String fieldName = "viewSource";
String bindName = "source";
if (!binding.isBoundToRoot()) {
fieldName = "view" + binding.getId().value;
bindName = "view";
}
result.addStatement("$L = $N", fieldName, bindName);
for (Map.Entry<ListenerClass, Map<ListenerMethod, Set<MethodViewBinding>>> e : classMethodBindings.entrySet()) {
ListenerClass listener = e.getKey();
Map<ListenerMethod, Set<MethodViewBinding>> methodBindings = e.getValue();
TypeSpec.Builder callback = TypeSpec.anonymousClassBuilder("").superclass(ClassName.bestGuess(listener.type()));
for (ListenerMethod method : getListenerMethods(listener)) {
MethodSpec.Builder callbackMethod = MethodSpec.methodBuilder(method.name()).addAnnotation(Override.class).addModifiers(PUBLIC).returns(bestGuess(method.returnType()));
String[] parameterTypes = method.parameters();
for (int i = 0, count = parameterTypes.length; i < count; i++) {
callbackMethod.addParameter(bestGuess(parameterTypes[i]), "p" + i);
}
boolean hasReturnType = !"void".equals(method.returnType());
CodeBlock.Builder builder = CodeBlock.builder();
if (hasReturnType) {
builder.add("return ");
}
if (methodBindings.containsKey(method)) {
for (MethodViewBinding methodBinding : methodBindings.get(method)) {
builder.add("target.$L(", methodBinding.getName());
List<Parameter> parameters = methodBinding.getParameters();
String[] listenerParameters = method.parameters();
for (int i = 0, count = parameters.size(); i < count; i++) {
if (i > 0) {
builder.add(", ");
}
Parameter parameter = parameters.get(i);
int listenerPosition = parameter.getListenerPosition();
if (parameter.requiresCast(listenerParameters[listenerPosition])) {
builder.add("$T.<$T>castParam(p$L, $S, $L, $S, $L)", UTILS, parameter.getType(), listenerPosition, method.name(), listenerPosition, methodBinding.getName(), i);
} else {
builder.add("p$L", listenerPosition);
}
}
builder.add(");\n");
}
} else if (hasReturnType) {
builder.add("$L;\n", method.defaultReturn());
}
callbackMethod.addCode(builder.build());
callback.addMethod(callbackMethod.build());
}
boolean requiresRemoval = listener.remover().length() != 0;
String listenerField = null;
if (requiresRemoval) {
TypeName listenerClassName = bestGuess(listener.type());
listenerField = fieldName + ((ClassName) listenerClassName).simpleName();
result.addStatement("$L = $L", listenerField, callback.build());
}
if (!VIEW_TYPE.equals(listener.targetType())) {
result.addStatement("(($T) $N).$L($L)", bestGuess(listener.targetType()), bindName, listener.setter(), requiresRemoval ? listenerField : callback.build());
} else {
result.addStatement("$N.$L($L)", bindName, listener.setter(), requiresRemoval ? listenerField : callback.build());
}
}
if (needsNullChecked) {
result.endControlFlow();
}
}
use of com.squareup.javapoet.ClassName in project butterknife by JakeWharton.
the class BindingSet method bestGuess.
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);
}
}
use of com.squareup.javapoet.ClassName in project requery by requery.
the class AndroidObservableExtension method addToSetter.
@Override
public void addToSetter(AttributeDescriptor member, MethodSpec.Builder builder) {
if (!observable || !isBindable(member)) {
return;
}
Elements elements = processingEnvironment.getElementUtils();
// data binding compiler will create a useful set of classes in /data-binding-info
String bindingInfo = BINDING_PACKAGE + ".layouts.DataBindingInfo";
TypeElement dataBindingType = elements.getTypeElement(bindingInfo);
ClassName BRclass = null;
if (dataBindingType != null) {
Optional<String> modulePackage = Mirrors.findAnnotationMirror(dataBindingType, BINDING_PACKAGE + ".BindingBuildInfo").map(mirror -> Mirrors.findAnnotationValue(mirror, "modulePackage")).filter(Optional::isPresent).map(Optional::get).map(AnnotationValue::toString);
if (modulePackage.isPresent()) {
// not actually checking the BR class exists since it maybe generated later
BRclass = ClassName.get(modulePackage.get().replaceAll("\"", ""), "BR");
}
}
if (BRclass == null) {
PackageElement packageElement = elements.getPackageOf(entity.element());
String packageName = packageElement.getQualifiedName().toString();
BRclass = ClassName.get(packageName, "BR");
}
// matching the way the BR property names are generated
// https://code.google.com/p/android/issues/detail?id=199436
String propertyName = member.setterName().replaceFirst("set", "");
/*
if (Names.isAllUpper(propertyName)) {
propertyName = propertyName.toLowerCase();
} else {
propertyName = Names.lowerCaseFirst(propertyName);
}*/
propertyName = Names.lowerCaseFirst(propertyName);
builder.addStatement("notifyPropertyChanged($L.$L)", BRclass, propertyName);
}
use of com.squareup.javapoet.ClassName in project CoreData by FangCloud-Android.
the class EntityProcessor method createEntityDao.
public void createEntityDao(TypeElement element) throws IOException {
EntityDetail entityDetail = EntityDetail.parse(processingEnv, element);
// 实体类的class
ClassName classEntity = ClassName.bestGuess(element.asType().toString());
if (entityDetail.getPrimaryKey() == null) {
throw new RuntimeException(classEntity.reflectionName() + " 没有主键");
}
List<Property> propertyList = entityDetail.getProperties(processingEnv);
// 1、找出tableName,PrimaryKeyName ok
// 2、找出所有的PropertyConverter,并生成局部变量,类似 __TagListConverter ok
// 3、找出所有关联对象 @Relation,并生成对应的dao 类似 __AuthorCoreDao ok
// 4、onCreate方法,初始化 关联对象对应的 dao ok
// 5、getInsertSql, 返回插入的sql语句 ok
// 6、getCreateTableSql,返回建表语句
// 7、getTableProperties,返回所有的表结构
// 8、绑定数据
// dao的java名字
String daoName = String.format("%sCoreDaoImpl", entityDetail.getEntityName());
TypeSpec.Builder daoTypeBuilder = TypeSpec.classBuilder(daoName).addModifiers(Modifier.PUBLIC, Modifier.FINAL).superclass(ParameterizedTypeName.get(classCoreDao, classEntity));
List<Element> convertElements = entityDetail.getConvertElements(processingEnv);
// static 代码块
CodeBlock convertStaticBlock = CreateConvertStatement.buildConvertStatic(convertElements);
if (convertStaticBlock != null) {
daoTypeBuilder.addStaticBlock(convertStaticBlock);
}
// 创建convert
List<FieldSpec> convertFieldSpecs = CreateConvertStatement.bindComvertFields(convertElements);
if (convertFieldSpecs != null) {
for (FieldSpec fieldSpec : convertFieldSpecs) {
daoTypeBuilder.addField(fieldSpec);
}
}
// onCreate方法
// 创建关联的dao
List<Element> relationElements = entityDetail.getRelationElements();
MethodSpec.Builder onCreateMethodBuilder = MethodSpec.methodBuilder("onCreate").addModifiers(Modifier.PROTECTED).returns(void.class).addParameter(classCoreData, "coreData").addStatement("super.onCreate($N)", "coreData");
for (Element relationElement : relationElements) {
TypeMirror typeMirror = relationElement.asType();
ClassName classRelation = ClassName.bestGuess(typeMirror.toString());
String daoFieldName = Utils.relationDaoName(classRelation);
FieldSpec fieldSpec = FieldSpec.builder(ParameterizedTypeName.get(classCoreDao, classRelation), daoFieldName, Modifier.PRIVATE).build();
daoTypeBuilder.addField(fieldSpec);
onCreateMethodBuilder.addStatement("$N = coreData.dao($T.class)", daoFieldName, classRelation);
}
MethodSpec onCreateMethod = onCreateMethodBuilder.build();
// getCreateTableSql 方法,用来获取建表语句
MethodSpec getCreateTableSqlMethod = MethodSpec.methodBuilder("getCreateTableSql").addModifiers(Modifier.PROTECTED).returns(String.class).addStatement("return $S", SqlBuilder.buildCreateSql(entityDetail.getTableName(), propertyList, true)).build();
// getInsertSql 方法,用来获取插入语句
MethodSpec getInsertSqlMethod = MethodSpec.methodBuilder("getInsertSql").addModifiers(Modifier.PROTECTED).returns(String.class).addStatement("return $S", Utils.getInsertSql(entityDetail.getTableName(), propertyList)).build();
// bindStatement 用来绑定数据
MethodSpec bindStatementMethod = new BindStatementMethod(processingEnv, entityDetail).build();
// replaceInternal 方法,用来处理关系型数据
MethodSpec replaceInternalMethod = new ReplaceInternalMethod(processingEnv, entityDetail).build();
// 创建 getTableName 方法,返回tableName
MethodSpec getTableNameMethod = MethodSpec.methodBuilder("getTableName").addModifiers(Modifier.PUBLIC).returns(String.class).addStatement("return $S", entityDetail.getTableName()).build();
// 创建 getPrimaryKeyName 方法,返回 主键的名字
MethodSpec getPrimaryKeyNameMethod = MethodSpec.methodBuilder("getPrimaryKeyName").addModifiers(Modifier.PUBLIC).returns(String.class).addStatement("return $S", Utils.getColumnName(entityDetail.getPrimaryKey())).build();
// 创建 bindCursor 方法,绑定游标数据到模型
MethodSpec bindCursorMethod = new BindCursorMethod(processingEnv, entityDetail).build();
// 创建 getTableProperties 方法,返回所有字段相关的 Property
ParameterizedTypeName listPropertyType = ParameterizedTypeName.get(ClassName.get(ArrayList.class), classCoreProperty);
MethodSpec.Builder getTablePropertiesBuilder = MethodSpec.methodBuilder("getTableProperties").addModifiers(Modifier.PUBLIC).returns(ParameterizedTypeName.get(ClassName.get(List.class), classCoreProperty));
getTablePropertiesBuilder.addStatement("$T list = new $T()", listPropertyType, listPropertyType);
for (Property property : propertyList) {
getTablePropertiesBuilder.addStatement("list.add(new $T($S, $T.class, $N))", classCoreProperty, property.name, Utils.getTypeNameByType(property.type), String.valueOf(property.primaryKey));
}
getTablePropertiesBuilder.addStatement("return list");
daoTypeBuilder.addMethod(onCreateMethod).addMethod(getTableNameMethod).addMethod(getPrimaryKeyNameMethod).addMethod(getTablePropertiesBuilder.build()).addMethod(getCreateTableSqlMethod).addMethod(getInsertSqlMethod).addMethod(bindStatementMethod).addMethod(replaceInternalMethod).addMethod(bindCursorMethod);
JavaFile javaFile = JavaFile.builder(entityDetail.getEntityPackageName(processingEnv), daoTypeBuilder.build()).build();
javaFile.writeTo(processingEnv.getFiler());
System.out.println(element.getSimpleName());
System.out.println(processingEnv.getElementUtils().getPackageOf(element).getQualifiedName());
}
Aggregations