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

The following examples show how to use com.esotericsoftware.reflectasm.FieldAccess#set() . 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: 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 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: 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 6
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 7
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 8
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 9
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 10
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 11
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 12
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");
}