Search in sources :

Example 1 with Entity

use of com.coredata.annotation.Entity in project CoreData by FangCloud-Android.

the class EntityDetail method getTableName.

/**
 * 获取数据库表名
 *
 * @return
 */
public String getTableName() {
    Entity entity = entityElement.getAnnotation(Entity.class);
    String tableName = entity.tableName();
    if (TextUtils.isEmpty(tableName)) {
        tableName = getEntityName();
    }
    return tableName;
}
Also used : Entity(com.coredata.annotation.Entity)

Example 2 with Entity

use of com.coredata.annotation.Entity in project CoreData by FangCloud-Android.

the class Utils method fillElementsForDbAndReturnPrimaryKey.

public static Element fillElementsForDbAndReturnPrimaryKey(ProcessingEnvironment env, List<Element> elementList, TypeElement element) {
    Entity entity = element.getAnnotation(Entity.class);
    String primaryKeyStr = null;
    if (entity != null) {
        primaryKeyStr = entity.primaryKey();
    }
    return fillElementsForDbAndReturnPrimaryKey(env, elementList, element, primaryKeyStr);
}
Also used : Entity(com.coredata.annotation.Entity)

Example 3 with Entity

use of com.coredata.annotation.Entity in project CoreData by FangCloud-Android.

the class Utils method getProperties.

public static List<Property> getProperties(ProcessingEnvironment env, List<Element> elements, Element primaryKey) {
    Elements elementUtils = env.getElementUtils();
    Types typeUtils = env.getTypeUtils();
    List<Property> propertyList = new ArrayList<>();
    for (Element element : elements) {
        String columnName = Utils.getColumnName(element);
        TypeName dbBaseType;
        TypeMirror typeMirror = element.asType();
        TypeName typeNameElement = ClassName.get(typeMirror);
        if (Utils.isBaseType(typeNameElement)) {
            dbBaseType = typeNameElement;
        } else {
            TypeElement elementFieldType = (TypeElement) typeUtils.asElement(typeMirror);
            Relation annotationRelation = element.getAnnotation(Relation.class);
            if (annotationRelation != null) {
                // 关联数据
                Entity relationEntity = elementFieldType.getAnnotation(Entity.class);
                if (relationEntity == null) {
                    throw new IllegalStateException("@Relation 添加的属性必须是 @Entity 的类");
                }
                EntityDetail relationEntityDetail = EntityDetail.parse(env, elementFieldType);
                Element relationEntityPrimaryKey = relationEntityDetail.getPrimaryKey();
                if (relationEntityPrimaryKey == null) {
                    throw new IllegalStateException(relationEntityDetail.getEntityName() + "是关联类型,@Relation 属性必须有主键");
                }
                dbBaseType = ClassName.get(relationEntityPrimaryKey.asType());
            } else {
                Embedded embedded = element.getAnnotation(Embedded.class);
                if (embedded != null) {
                    // 内嵌数据, 循环解析内嵌结构的字段
                    List<Element> embeddedEleList = new ArrayList<>();
                    fillElementsForDbAndReturnPrimaryKey(env, embeddedEleList, (TypeElement) typeUtils.asElement(typeMirror));
                    propertyList.addAll(getProperties(env, embeddedEleList, null));
                    continue;
                } else {
                    Convert convert = element.getAnnotation(Convert.class);
                    if (convert != null) {
                        TypeName classConvertDb = Utils.getConvertDbType(convert);
                        if (Utils.isBaseType(classConvertDb)) {
                            dbBaseType = classConvertDb;
                        } else {
                            throw new IllegalStateException(element.getSimpleName() + "converter dbtype is not a base type");
                        }
                    } else {
                        throw new IllegalStateException(element.getSimpleName() + " is a complex structure field," + "must indicate it as @Relation or @Embedded, " + "or supply a @Convert for it");
                    }
                }
            }
        }
        propertyList.add(new Property(columnName, Utils.getTypeByTypeName(dbBaseType), element == primaryKey));
    }
    return propertyList;
}
Also used : Types(javax.lang.model.util.Types) Entity(com.coredata.annotation.Entity) TypeName(com.squareup.javapoet.TypeName) Convert(com.coredata.annotation.Convert) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) Elements(javax.lang.model.util.Elements) Relation(com.coredata.annotation.Relation) TypeMirror(javax.lang.model.type.TypeMirror) Embedded(com.coredata.annotation.Embedded) EntityDetail(com.coredata.compiler.EntityDetail) Property(com.coredata.db.Property)

Aggregations

Entity (com.coredata.annotation.Entity)3 Convert (com.coredata.annotation.Convert)1 Embedded (com.coredata.annotation.Embedded)1 Relation (com.coredata.annotation.Relation)1 EntityDetail (com.coredata.compiler.EntityDetail)1 Property (com.coredata.db.Property)1 TypeName (com.squareup.javapoet.TypeName)1 ArrayList (java.util.ArrayList)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1 Elements (javax.lang.model.util.Elements)1 Types (javax.lang.model.util.Types)1