com.esotericsoftware.reflectasm.FieldAccess Java Examples

The following examples show how to use com.esotericsoftware.reflectasm.FieldAccess. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TableInfo.java    From litchi with Apache License 2.0 6 votes vote down vote up
public static TableInfo valueOf(Class<? extends Table> clazz, TableName tableName) {
    TableInfo tableInfo = new TableInfo();
    tableInfo.clazz = clazz;
    tableInfo.tableName = tableName;

    tableInfo.classAccess = ConstructorAccess.get(clazz);
    tableInfo.fieldAccess = FieldAccess.get(clazz);

    //reflect column
    tableInfo.reflectColumn(clazz.getDeclaredFields());

    if (StringUtils.isBlank(tableInfo.pkName)) {
        LOGGER.error(tableName.tableName() + "实体缺少主键");
    }
    return tableInfo;
}
 
Example #2
Source File: ListColumn.java    From litchi with Apache License 2.0 6 votes vote down vote up
@Override
public void readColumn(Table<?> instance, TableInfo.TableColumnInfo columnInfo, ResultSet rs) throws SQLException {
    FieldAccess fieldAccess = instance.getTableInfo().fieldAccess;

    byte[] jsonBytes = rs.getBytes(columnInfo.aliasName);
    if (jsonBytes != null && jsonBytes.length > 0) {
        Class<?> valueType = columnInfo.getColumnType(1);
        List<?> value = JsonEntityParser.parseArray(jsonBytes, valueType);
        @SuppressWarnings("unchecked")
        List<Object> list = (List<Object>) fieldAccess.get(instance, columnInfo.fieldName);
        if (list != null) {
            list.addAll(value);
        } else {
            fieldAccess.set(instance, columnInfo.fieldName, value);
        }
    }
}
 
Example #3
Source File: Table.java    From litchi with Apache License 2.0 6 votes vote down vote up
/**
 * 获取所有字段的值,,续承类可以重写自定义读取方式
 * @return
 */
public Object[] writeData() {
	TableInfo tableInfo = getTableInfo(getClass());
	FieldAccess fieldAccess = tableInfo.fieldAccess;

	ArrayList<Object> writeList = new ArrayList<>();
	for (TableColumnInfo columnInfo : tableInfo.columnInfoList) {
		Class<?> columnType = columnInfo.getColumnType(0);
		AbstractColumnParser parser = ColumnContext.getParser(columnType);
		if (parser == null) {
			LOGGER.error("Read data error.column type {} not implemented.", columnInfo);
			continue;
		}
		writeBefore();
		Object fieldValue = null;
		try {
			fieldValue = fieldAccess.get(this, columnInfo.fieldName);
			parser.writeColumn(writeList, columnInfo, fieldValue);
		} catch (Exception ex) {
			LOGGER.error("write game error! className={} fieldName={} value={}", tableInfo.clazz().getName(), columnInfo.fieldName, fieldValue);
			LOGGER.error("{}", ex);
		}
	}

	return writeList.toArray();
}
 
Example #4
Source File: SetColumn.java    From litchi with Apache License 2.0 6 votes vote down vote up
@Override
public void readColumn(Table<?> instance, TableInfo.TableColumnInfo columnInfo, ResultSet rs) throws SQLException {
    FieldAccess fieldAccess = instance.getTableInfo().fieldAccess;

    byte[] bytes = rs.getBytes(columnInfo.aliasName);
    if (bytes != null && bytes.length > 0) {
    	Class<?> valueType = columnInfo.getColumnType(1);
        List<?> value = JsonEntityParser.parseArray(bytes, valueType);
        @SuppressWarnings("unchecked")
        Set<Object> set = (Set<Object>) fieldAccess.get(instance, columnInfo.fieldName);
        if (set != null) {
            set.addAll(value);
        } else {
        	set = new CopyOnWriteArraySet<Object>();
        	set.addAll(value);
            fieldAccess.set(instance, columnInfo.fieldName, value);
        }
    }
}
 
Example #5
Source File: StringColumn.java    From litchi with Apache License 2.0 6 votes vote down vote up
@Override
public void readColumn(Table<?> instance, TableInfo.TableColumnInfo columnInfo, ResultSet rs) throws SQLException {
    String value = null;
    for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
        String columnName = rs.getMetaData().getColumnName(i);
        if (columnName.equals(columnInfo.aliasName)) {
            String columnTypeName = rs.getMetaData().getColumnTypeName(i);
            // BLOB/TEXT二进制存储结构都存在中文编码问题
            // BLOB/TEXT等二进制存储结构不能以UTF-8进行编码,需要将字节数组取出后进行UTF-8的编码转换
            if (columnTypeName.equals("BLOB") || columnTypeName.equals("TEXT")) {
                byte[] bytes = rs.getBytes(columnInfo.aliasName);
                value = new String(bytes, Charset.forName("UTF-8"));
            } else {
                value = rs.getString(columnInfo.aliasName);
            }
            break;
        }
    }
    FieldAccess fieldAccess = instance.getTableInfo().fieldAccess;
    fieldAccess.set(instance, columnInfo.fieldName, value);
}
 
Example #6
Source File: JsonColumn.java    From litchi with Apache License 2.0 6 votes vote down vote up
@Override
public void readColumn(Table<?> instance, TableInfo.TableColumnInfo columnInfo, ResultSet rs) throws SQLException {
    FieldAccess fieldAccess = instance.getTableInfo().fieldAccess;

    byte[] byteJson = rs.getBytes(columnInfo.aliasName);
    if (byteJson == null) {
        return;
    }

    Object obj = JsonEntityParser.parseJson(byteJson, columnInfo.getColumnType(0));
    fieldAccess.set(instance, columnInfo.fieldName, obj);

    JsonEntity jsonEntity = (JsonEntity) fieldAccess.get(instance, columnInfo.fieldName);
    if (jsonEntity != null) {
        jsonEntity.afterRead(obj);
    }
}
 
Example #7
Source File: DecimalColumn.java    From litchi with Apache License 2.0 5 votes vote down vote up
@Override
  public void readColumn(Table<?> instance, TableInfo.TableColumnInfo columnInfo, ResultSet rs) throws SQLException {
      FieldAccess fieldAccess = instance.getTableInfo().fieldAccess;
      Object obj = rs.getObject(columnInfo.aliasName);
      if (obj != null) {
      	fieldAccess.set(instance, columnInfo.fieldName, obj);
}
      // 数据库中该字段为null时,使用对象中该字段的默认值
  }
 
Example #8
Source File: ASMCallerField.java    From MyTown2 with The Unlicense 5 votes vote down vote up
@Override
public void setClass(Class<?> clazz) {
    super.setClass(clazz);

    // Setup ReflectASM stuff
    try {
        access = FieldAccess.get(clazz);
        index = access.getIndex(name);
    } catch(Exception ex) {
        MyTown.instance.LOG.debug(name + " - Falling back to reflection based field caller", ex);
        index = -1;
    }
}
 
Example #9
Source File: DoubleColumn.java    From litchi with Apache License 2.0 5 votes vote down vote up
@Override
  public void readColumn(Table<?> instance, TableInfo.TableColumnInfo columnInfo, ResultSet rs) throws SQLException {
      FieldAccess fieldAccess = instance.getTableInfo().fieldAccess;
      Object x = rs.getObject(columnInfo.aliasName);
      if (x != null) {
      	fieldAccess.set(instance, columnInfo.fieldName, x);
}
      // 数据库中该字段为null时,使用对象中该字段的默认值
  }
 
Example #10
Source File: AtomicLongColumn.java    From litchi with Apache License 2.0 5 votes vote down vote up
@Override
public void readColumn(Table<?> instance, TableInfo.TableColumnInfo columnInfo, ResultSet rs) throws SQLException {
    FieldAccess fieldAccess = instance.getTableInfo().fieldAccess;

    AtomicLong atomLong = (AtomicLong) fieldAccess.get(instance, columnInfo.fieldName);
    atomLong.set(rs.getLong(columnInfo.aliasName));
}
 
Example #11
Source File: NumberColumn.java    From litchi with Apache License 2.0 5 votes vote down vote up
@Override
 public void readColumn(Table<?> instance, TableInfo.TableColumnInfo columnInfo, ResultSet rs) throws SQLException {
     try {
         FieldAccess fieldAccess = instance.getTableInfo().fieldAccess;
         Object x = rs.getObject(columnInfo.aliasName);
         if (x != null) {
	fieldAccess.set(instance, columnInfo.fieldName, x);
}
         // 数据库中该字段为null时,使用对象中该字段的默认值
     } catch (Exception e) {
         e.printStackTrace();
     }
 }
 
Example #12
Source File: ByteArrayColumn.java    From litchi with Apache License 2.0 4 votes vote down vote up
@Override
public void readColumn(Table<?> instance, TableInfo.TableColumnInfo columnInfo, ResultSet rs) throws SQLException {
    FieldAccess fieldAccess = instance.getTableInfo().fieldAccess;
    fieldAccess.set(instance, columnInfo.fieldName, rs.getBytes(columnInfo.aliasName));
}
 
Example #13
Source File: BooleanColumn.java    From litchi with Apache License 2.0 4 votes vote down vote up
@Override
public void readColumn(Table<?> instance, TableInfo.TableColumnInfo columnInfo, ResultSet rs) throws SQLException {
    FieldAccess fieldAccess = instance.getTableInfo().fieldAccess;
    fieldAccess.set(instance, columnInfo.fieldName, rs.getInt(columnInfo.aliasName) > 0 ? true : false);
}
 
Example #14
Source File: MapColumn.java    From litchi with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void readColumn(Table<?> instance, TableInfo.TableColumnInfo columnInfo, ResultSet rs) throws SQLException {
    FieldAccess fieldAccess = instance.getTableInfo().fieldAccess;

    Map<Object, Object> originMaps = (Map<Object, Object>) fieldAccess.get(instance, columnInfo.fieldName);
    if (originMaps == null) {
        originMaps = new ConcurrentHashMap<>();
        fieldAccess.set(instance, columnInfo.fieldName, originMaps);
    }

    Class<?> keyType = columnInfo.columnTypeList.get(1);
    Class<?> valueType = columnInfo.columnTypeList.get(2);
    byte[] jsonBytes = rs.getBytes(columnInfo.aliasName);
    if (jsonBytes != null && jsonBytes.length > 0) {
        try {
            JSONObject jsonObject = JSON.parseObject(new String(jsonBytes));
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                Object key;
                Object obj = entry.getKey();
                if (obj instanceof Number) {
                    Number number = (Number) obj;
                    if (keyType == Integer.class) {
                        key = number.intValue();
                    } else if (keyType == Long.class) {
                        key = number.longValue();
                    } else {
                        LOGGER.error("不支持的map数据类型:{}", keyType);
                        continue;
                    }

                } else {
                    key = obj;
                }
                try {
                    Object value = JsonEntityParser.parseJson(entry.getValue().toString(), valueType);
                    originMaps.put(key, value);
                } catch (Exception e) {
                    LOGGER.error("read column error. class={} field={}", instance.getTableInfo().clazz().getSimpleName(), columnInfo.fieldName);
                    LOGGER.error("", e);
                }
            }
        } catch (Exception ex) {
            LOGGER.info("{}", ex);
        }
    }
}
 
Example #15
Source File: CacheableAccessors.java    From godaddy-logger with MIT License 4 votes vote down vote up
/**
 * Builds the field sorted cache. Sorts the fields alphabetically by name.
 */
private static void buildFieldCache(Class<?> clazz, FieldAccess fieldAccess) {

    if(!_fieldSortCache.contains(clazz)) {
        LogCache[] sortedLogCache = new LogCache[fieldAccess.getFieldCount()];

        String[] sortedFieldNames = Arrays.copyOf(fieldAccess.getFieldNames(), sortedLogCache.length);

        Arrays.sort(sortedFieldNames);

        for(int i = 0; i < sortedLogCache.length; i++) {
            sortedLogCache[i] = new LogCache(fieldAccess.getIndex(sortedFieldNames[i]),
                                             getLoggingScope(clazz, sortedFieldNames[i]));
        }

        _fieldSortCache.put(clazz, sortedLogCache);
    }

}
 
Example #16
Source File: CacheableAccessors.java    From godaddy-logger with MIT License 4 votes vote down vote up
public static LogCache[] getFieldIndexes(Class<?> clazz, FieldAccess fieldAccess) {
    buildFieldCache(clazz, fieldAccess);

    return _fieldSortCache.get(clazz);
}
 
Example #17
Source File: DateColumn.java    From litchi with Apache License 2.0 4 votes vote down vote up
@Override
public void readColumn(Table<?> instance, TableInfo.TableColumnInfo columnInfo, ResultSet rs) throws SQLException {
    FieldAccess fieldAccess = instance.getTableInfo().fieldAccess;
    Date x = rs.getDate(columnInfo.aliasName);
    fieldAccess.set(instance, columnInfo.fieldName, x);
}
 
Example #18
Source File: FieldAccessBenchmark.java    From reflectasm with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public FieldAccessBenchmark () throws Exception {
	int count = 1000000;
	Object[] dontCompileMeAway = new Object[count];

	FieldAccess access = FieldAccess.get(SomeClass.class);
	SomeClass someObject = new SomeClass();
	int index = access.getIndex("name");

	Field field = SomeClass.class.getField("name");

	for (int i = 0; i < 100; i++) {
		for (int ii = 0; ii < count; ii++) {
			access.set(someObject, index, "first");
			dontCompileMeAway[ii] = access.get(someObject, index);
		}
		for (int ii = 0; ii < count; ii++) {
			field.set(someObject, "first");
			dontCompileMeAway[ii] = field.get(someObject);
		}
	}
	warmup = false;

	for (int i = 0; i < 100; i++) {
		start();
		for (int ii = 0; ii < count; ii++) {
			access.set(someObject, index, "first");
			dontCompileMeAway[ii] = access.get(someObject, index);
		}
		end("FieldAccess");
	}
	for (int i = 0; i < 100; i++) {
		start();
		for (int ii = 0; ii < count; ii++) {
			field.set(someObject, "first");
			dontCompileMeAway[ii] = field.get(someObject);
		}
		end("Reflection");
	}

	chart("Field Set/Get");
}