Java Code Examples for com.esotericsoftware.reflectasm.FieldAccess#get()

The following examples show how to use com.esotericsoftware.reflectasm.FieldAccess#get() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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");
}
 
Example 10
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");
}