use of com.squareup.javapoet.ClassName in project CoreData by FangCloud-Android.
the class BindCursorMethod method bind.
private void bind(MethodSpec.Builder builder) {
// final int cursorIndexOfId = cursor.getColumnIndexOrThrow("id");
// final int cursorIndexOfName = cursor.getColumnIndexOrThrow("name");
// final int cursorIndexOfTags = cursor.getColumnIndexOrThrow("tags");
// final int cursorIndexOfAuthor = cursor.getColumnIndexOrThrow("author_id");
// final int cursorIndexOfContent = cursor.getColumnIndexOrThrow("desc_content");
// final int cursorIndexOfEmail = cursor.getColumnIndexOrThrow("desc_email");
// List<Book> bookList = new ArrayList<>();
// Map<Integer, Book> authorIdWithBookMap = new HashMap<>();
// while (cursor.moveToNext()) {
// Book book = new Book();
// book.id = cursor.getLong(cursorIndexOfId);
// book.name = cursor.getString(cursorIndexOfName);
// book.tags = __TagListConverter.convertToValue(cursor.getString(cursorIndexOfTags));
// int authorId = cursor.getInt(cursorIndexOfAuthor);
// book.desc = new Desc();
// book.desc.content = cursor.getString(cursorIndexOfContent);
// book.desc.email = cursor.getString(cursorIndexOfEmail);
// bookList.add(book);
// authorIdWithBookMap.put(authorId, book);
// }
// List<Author> authorList = __authorCoreDao.queryByKeys(authorIdWithBookMap.keySet().toArray(new Integer[]{}));
// for (Author author : authorList) {
// Book book = authorIdWithBookMap.get(author.getId());
// if (book != null) {
// book.author = author;
// }
// }
// return bookList;
TypeName typeNameEntity = ClassName.get(entityDetail.getEntityElement().asType());
ParameterizedTypeName typeListEntity = ParameterizedTypeName.get(ClassName.get(ArrayList.class), typeNameEntity);
List<Element> elementsForDb = entityDetail.getDbElements();
List<Property> properties = entityDetail.getProperties(processingEnv);
List<Element> relationElements = entityDetail.getRelationElements();
for (Property property : properties) {
builder.addStatement("int $N = cursor.getColumnIndexOrThrow($S)", "cursorIndexOf" + property.name, property.name);
}
// 创建自己的列表
builder.addStatement("$T list = new $T()", typeListEntity, typeListEntity);
// 创建多个关联数据的hashMap,主键类型为key
for (Element relationElement : relationElements) {
TypeElement typeRelation = (TypeElement) processingEnv.getTypeUtils().asElement(relationElement.asType());
EntityDetail relationEntityDetail = EntityDetail.parse(processingEnv, typeRelation);
Element primaryKeyElement = relationEntityDetail.getPrimaryKey();
if (primaryKeyElement != null) {
TypeMirror typeMirror = primaryKeyElement.asType();
ParameterizedTypeName hashMapType = ParameterizedTypeName.get(ClassName.get(HashMap.class), ClassName.get(typeMirror).box(), typeNameEntity);
builder.addStatement("$T __$NMap = new $T()", hashMapType, Utils.getColumnName(relationElement), hashMapType);
}
}
builder.addCode("while (cursor.moveToNext()) {\n ");
// 创建一个对象
String itemName = "entity";
builder.addStatement("$T $N = new $T()", typeNameEntity, itemName, typeNameEntity);
for (Element element : elementsForDb) {
bindCursorToField(builder, element, itemName);
}
builder.addStatement("list.add(entity)");
// 将对象放入相应关联对象对应的map
builder.addCode("}\n");
// 循环关联对象,赋值给主对象
for (Element relationElement : relationElements) {
ClassName classNameRelation = ClassName.bestGuess(relationElement.asType().toString());
TypeElement typeRelationElement = (TypeElement) processingEnv.getTypeUtils().asElement(relationElement.asType());
EntityDetail relationEntityDetail = EntityDetail.parse(processingEnv, typeRelationElement);
Element primaryKeyElement = relationEntityDetail.getPrimaryKey();
if (primaryKeyElement != null) {
// List<Author> authorList = __authorCoreDao.queryByKeys(authorIdWithBookMap.keySet().toArray(new Integer[]{}));
TypeName primaryTypeName = ClassName.get(primaryKeyElement.asType());
ParameterizedTypeName listRelationType = ParameterizedTypeName.get(ClassName.get(List.class), classNameRelation);
String listName = String.format("__%sList", typeRelationElement.getSimpleName());
String mapName = String.format("__%sMap", Utils.getColumnName(relationElement));
builder.addStatement("$T $N = $N.queryByKeys($N.keySet().toArray(new $T[]{}))", listRelationType, listName, Utils.relationDaoName(classNameRelation), mapName, primaryTypeName.box());
builder.addCode("for($T item : $N){\n", classNameRelation, listName);
builder.addStatement(" $T entity = $N.get($N)", typeNameEntity, mapName, Utils.methodGet(primaryKeyElement, "item"));
builder.addCode(" if(entity != null){\n");
builder.addStatement(Utils.methodSetFormat(relationElement, "entity"), "item");
builder.addCode(" }\n");
builder.addCode("}\n");
}
}
builder.addStatement("return list");
}
use of com.squareup.javapoet.ClassName in project CoreData by FangCloud-Android.
the class BindStatementMethod method bindStatementMethodInternal.
private int bindStatementMethodInternal(MethodSpec.Builder builder, List<Element> elements, int index, String prefix, boolean carePrefix) {
for (Element element : elements) {
TypeName typeName = ClassName.get(element.asType());
String fieldGetMethod = Utils.methodGet(element, prefix);
if (typeName.isPrimitive() || typeName.isBoxedPrimitive()) {
bindTo(builder, typeName, index, fieldGetMethod, prefix);
} else {
// throw exception
if (typeName.equals(ClassName.get(String.class))) {
bindTo(builder, typeName, index, fieldGetMethod, prefix);
} else {
Embedded embedded = element.getAnnotation(Embedded.class);
if (embedded != null) {
Element elementEmbedded = processingEnv.getTypeUtils().asElement(element.asType());
List<Element> elementsForDbEmbedded = new ArrayList<>();
Utils.fillElementsForDbAndReturnPrimaryKey(processingEnv, elementsForDbEmbedded, (TypeElement) elementEmbedded);
index = bindStatementMethodInternal(builder, elementsForDbEmbedded, index, fieldGetMethod, true);
continue;
}
Convert convert = element.getAnnotation(Convert.class);
if (convert != null) {
// 转换
ClassName classConverter = ClassName.bestGuess(Utils.getConverterType(convert).toString());
ClassName dbClassName = ClassName.bestGuess(Utils.getConvertDbType(convert).toString());
if (!carePrefix || TextUtils.isEmpty(prefix)) {
builder.addStatement("$T __temp_$N = $N", dbClassName, String.valueOf(index), formatBindMethod(fieldGetMethod, Utils.converterName(element, classConverter)));
} else {
builder.addStatement("$T __temp_$N = $N == null ? $N : $N", dbClassName, String.valueOf(index), prefix, getDefaultValue(dbClassName), formatBindMethod(fieldGetMethod, Utils.converterName(element, classConverter)));
}
bindTo(builder, dbClassName, index, "__temp_" + index, null);
} else {
Relation relation = element.getAnnotation(Relation.class);
if (relation != null) {
TypeElement relationType = (TypeElement) processingEnv.getTypeUtils().asElement(element.asType());
EntityDetail relationEntityDetail = EntityDetail.parse(processingEnv, relationType);
Element relationEntityDetailPrimaryKey = relationEntityDetail.getPrimaryKey();
if (relationEntityDetailPrimaryKey == null) {
throw new RuntimeException(element.getSimpleName() + "#" + relationType.getSimpleName() + "must add has primaryKey");
}
TypeName dbClassName = ClassName.get(relationEntityDetailPrimaryKey.asType());
builder.addCode("$T __primaryKey_$N = ", dbClassName, String.valueOf(index));
if (carePrefix && !TextUtils.isEmpty(prefix)) {
builder.addCode("$N == null ? $N : ", prefix, getDefaultValue(dbClassName));
}
// 拼接当前数据判断为空
builder.addCode("($N == null ? $N : ", fieldGetMethod, getDefaultValue(dbClassName));
builder.addStatement("$N)", Utils.methodGet(relationEntityDetailPrimaryKey, fieldGetMethod));
bindTo(builder, dbClassName, index, "__primaryKey_" + index, null);
} else {
throw new RuntimeException(element.getSimpleName() + "must add @Relation or @Convert");
}
}
}
}
index++;
}
return index;
}
use of com.squareup.javapoet.ClassName in project CoreData by FangCloud-Android.
the class CreateConvertStatement method bindComvertFields.
public static List<FieldSpec> bindComvertFields(List<Element> convertElements) {
if (convertElements == null) {
return null;
}
List<FieldSpec> list = new ArrayList<>();
for (Element convertElement : convertElements) {
Convert convert = convertElement.getAnnotation(Convert.class);
ClassName classConverter = ClassName.bestGuess(Utils.getConverterType(convert).toString());
String packageName = classConverter.packageName();
String name = classConverter.simpleName();
System.out.println("BindConvertStatement----packageName:" + packageName);
System.out.println("BindConvertStatement----name:" + name);
FieldSpec fieldSpec;
if (SERIALIZABLE_CLASS_NAME.equals(classConverter)) {
fieldSpec = createSerializableField(convertElement);
} else if (SERIALIZABLE_LIST_CLASS_NAME.equals(classConverter)) {
fieldSpec = createSerializableListField(convertElement);
} else {
fieldSpec = createCommonField(convertElement, classConverter);
}
list.add(fieldSpec);
}
return list;
}
use of com.squareup.javapoet.ClassName in project CoreData by FangCloud-Android.
the class CreateConvertStatement method buildConvertStatic.
public static CodeBlock buildConvertStatic(List<Element> convertElements) {
CodeBlock.Builder builder = CodeBlock.builder();
if (convertElements == null) {
return null;
}
for (Element convertElement : convertElements) {
Convert convert = convertElement.getAnnotation(Convert.class);
ClassName classConverter = ClassName.bestGuess(Utils.getConverterType(convert).toString());
String packageName = classConverter.packageName();
String name = classConverter.simpleName();
System.out.println("BindConvertStatement----packageName:" + packageName);
System.out.println("BindConvertStatement----name:" + name);
if (SERIALIZABLE_CLASS_NAME.equals(classConverter) || SERIALIZABLE_LIST_CLASS_NAME.equals(classConverter)) {
TypeName typeName1 = ClassName.get(convertElement.asType());
System.out.println("BindConvertStatement----typeName:" + typeName1.toString());
TypeName typeName = findSerializableType(typeName1);
if (typeName != null) {
builder.addStatement("$T.registerSerializableClass($T.class)", classCoreData, typeName);
}
}
}
return builder.build();
}
use of com.squareup.javapoet.ClassName in project CoreData by FangCloud-Android.
the class ReplaceInternalMethod method bind.
private void bind(MethodSpec.Builder builder) {
Types typeUtils = processingEnv.getTypeUtils();
TypeName typeEntity = ClassName.get(entityDetail.getEntityElement().asType());
ParameterizedTypeName typeListEntity = ParameterizedTypeName.get(ClassName.get(ArrayList.class), typeEntity);
builder.addStatement("$T $N = new $T()", typeListEntity, entityDetail.getEntityElement().getSimpleName() + "List", typeListEntity);
List<Element> relationElements = entityDetail.getRelationElements();
for (Element relationElement : relationElements) {
Element typeRelationElement = typeUtils.asElement(relationElement.asType());
TypeName typeRelation = ClassName.get(relationElement.asType());
ParameterizedTypeName typeListRelation = ParameterizedTypeName.get(ClassName.get(ArrayList.class), typeRelation);
builder.addStatement("$T $N = new $T()", typeListRelation, typeRelationElement.getSimpleName() + "List", typeListRelation);
}
builder.addCode("for ($T item : collection) {\n ", typeEntity);
builder.addStatement("$N.add($N)", entityDetail.getEntityElement().getSimpleName() + "List", "item");
for (Element relationElement : relationElements) {
Element typeRelationElement = typeUtils.asElement(relationElement.asType());
TypeName typeRelation = ClassName.get(relationElement.asType());
relationElement.getSimpleName();
String methodGet = Utils.methodGet(relationElement, "item");
builder.addCode("if($N != null){\n ", methodGet);
builder.addStatement("$N.add($N)", typeRelationElement.getSimpleName() + "List", methodGet);
builder.addCode("}\n");
}
builder.addCode("}\n");
builder.addStatement("executeInsert($N, db)", entityDetail.getEntityElement().getSimpleName() + "List");
for (Element relationElement : relationElements) {
Element typeRelationElement = typeUtils.asElement(relationElement.asType());
ClassName classNameRelation = ClassName.bestGuess(relationElement.asType().toString());
String relationDaoName = Utils.relationDaoName(classNameRelation);
builder.addStatement("$N.replace($N, db)", relationDaoName, typeRelationElement.getSimpleName() + "List");
}
}
Aggregations