コード例 #1
0
  void mapFields() {
    boolean byAnnotationsOnly = false;
    boolean inheritColumns = false;
    boolean strictTypeMapping = false;
    if (clazz.isAnnotationPresent(JQTable.class)) {
      JQTable tableAnnotation = clazz.getAnnotation(JQTable.class);
      byAnnotationsOnly = tableAnnotation.annotationsOnly();
      inheritColumns = tableAnnotation.inheritColumns();
      strictTypeMapping = tableAnnotation.strictTypeMapping();
    }

    List<Field> classFields = New.arrayList();
    classFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
    if (inheritColumns) {
      Class<?> superClass = clazz.getSuperclass();
      classFields.addAll(Arrays.asList(superClass.getDeclaredFields()));
    }

    for (Field f : classFields) {
      // default to field name
      String columnName = f.getName();
      boolean isAutoIncrement = false;
      boolean isPrimaryKey = false;
      int maxLength = 0;
      boolean trimString = false;
      boolean allowNull = true;
      String defaultValue = "";
      boolean hasAnnotation = f.isAnnotationPresent(JQColumn.class);
      if (hasAnnotation) {
        JQColumn col = f.getAnnotation(JQColumn.class);
        if (!StringUtils.isNullOrEmpty(col.name())) {
          columnName = col.name();
        }
        isAutoIncrement = col.autoIncrement();
        isPrimaryKey = col.primaryKey();
        maxLength = col.maxLength();
        trimString = col.trimString();
        allowNull = col.allowNull();
        defaultValue = col.defaultValue();
      }
      boolean isPublic = Modifier.isPublic(f.getModifiers());
      boolean reflectiveMatch = isPublic && !byAnnotationsOnly;
      if (reflectiveMatch || hasAnnotation) {
        FieldDefinition fieldDef = new FieldDefinition();
        fieldDef.field = f;
        fieldDef.columnName = columnName;
        fieldDef.isAutoIncrement = isAutoIncrement;
        fieldDef.isPrimaryKey = isPrimaryKey;
        fieldDef.maxLength = maxLength;
        fieldDef.trimString = trimString;
        fieldDef.allowNull = allowNull;
        fieldDef.defaultValue = defaultValue;
        fieldDef.dataType = ModelUtils.getDataType(fieldDef, strictTypeMapping);
        fields.add(fieldDef);
      }
    }
    List<String> primaryKey = New.arrayList();
    for (FieldDefinition fieldDef : fields) {
      if (fieldDef.isPrimaryKey) {
        primaryKey.add(fieldDef.columnName);
      }
    }
    if (primaryKey.size() > 0) {
      setPrimaryKey(primaryKey);
    }
  }