View Javadoc

    1   package sharin.sql.generator;
    2
    3   import java.lang.reflect.Field;
    4   import java.lang.reflect.Modifier;
    5   import java.util.Arrays;
    6   import java.util.HashMap;
    7   import java.util.Map;
    8
    9   import javax.persistence.Column;
    10  import javax.persistence.Id;
    11  import javax.persistence.Table;
    12
    13  public class EntityInfo {
    14
    15      private final Class<?> entityClass;
    16
    17      private final String tableName;
    18
    19      private final Map<String, String> columnNameMap;
    20
    21      private final String idColumnName;
    22
    23      private final String[] propertyNames;
    24
    25      public EntityInfo(Class<?> entityClass) {
    26          this(entityClass, null, null, null);
    27      }
    28
    29      public EntityInfo(Class<?> entityClass, String tableName,
    30              Map<String, String> columnNameMap, String idColumnName) {
    31
    32          this.entityClass = entityClass;
    33
    34          /* tableName */
    35
    36          if (tableName == null || tableName.length() == 0) {
    37              Table table = entityClass.getAnnotation(Table.class);
    38
    39              if (table != null) {
    40                  tableName = table.name();
    41
    42              } else {
    43                  tableName = entityClass.getSimpleName();
    44              }
    45          }
    46
    47          this.tableName = tableName;
    48
    49          /* columnNameMap */
    50
    51          if (columnNameMap == null) {
    52              columnNameMap = new HashMap<String, String>();
    53
    54          } else {
    55              columnNameMap = new HashMap<String, String>(columnNameMap);
    56          }
    57
    58          Class<?> clazz = entityClass;
    59
    60          while (true) {
    61
    62              if (clazz == Object.class) {
    63                  break;
    64              }
    65
    66              for (Field field : clazz.getDeclaredFields()) {
    67
    68                  if ((field.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) != 0) {
    69                      continue;
    70                  }
    71
    72                  String fieldName = field.getName();
    73                  Column column = field.getAnnotation(Column.class);
    74
    75                  if (!columnNameMap.containsKey(fieldName)) {
    76
    77                      if (column != null) {
    78                          columnNameMap.put(fieldName, column.name());
    79
    80                      } else {
    81                          columnNameMap.put(fieldName, fieldName);
    82                      }
    83                  }
    84
    85                  if (idColumnName == null) {
    86                      Id id = field.getAnnotation(Id.class);
    87
    88                      if (id != null) {
    89                          idColumnName = (column == null) ? fieldName : column
    90                                  .name();
    91                      }
    92                  }
    93              }
    94
    95              clazz = clazz.getSuperclass();
    96          }
    97
    98          this.columnNameMap = columnNameMap;
    99
    100         /* idColumnName */
    101
    102         if (idColumnName == null || idColumnName.length() == 0) {
    103             idColumnName = "id";
    104         }
    105
    106         this.idColumnName = idColumnName;
    107
    108         /* propertyNames */
    109
    110         propertyNames = columnNameMap.keySet().toArray(
    111                 new String[columnNameMap.size()]);
    112         Arrays.sort(propertyNames);
    113     }
    114
    115     public boolean hasColumnName(String propertyName) {
    116         return columnNameMap.containsKey(propertyName);
    117     }
    118
    119     public String getColumnName(String propertyName) {
    120         return columnNameMap.get(propertyName);
    121     }
    122
    123     public Class<?> getEntityClass() {
    124         return entityClass;
    125     }
    126
    127     public String getTableName() {
    128         return tableName;
    129     }
    130
    131     public String getIdColumnName() {
    132         return idColumnName;
    133     }
    134
    135     public String[] getPropertyNames() {
    136         return propertyNames;
    137     }
    138 }