use of java.lang.reflect.Field in project nutz by nutzam.
the class JsonEntityTest method test_entity_field_maker.
@Test
public void test_entity_field_maker() {
Json.clearEntityCache();
JENObj obj = new JENObj();
obj.setAge(100);
obj.setName("name");
obj.setObjId(9l);
Json.setDefaultFieldMaker(new AbstractJsonEntityFieldMaker() {
@Override
public JsonEntityField make(Mirror<?> mirror, Method method) {
return null;
}
@Override
public JsonEntityField make(Mirror<?> mirror, Field field) {
return null;
}
});
assertEquals("{}", Json.toJson(obj, JsonFormat.compact()));
Json.clearEntityCache();
Json.setDefaultFieldMaker(new AbstractJsonEntityFieldMaker() {
@Override
public JsonEntityField make(Mirror<?> mirror, Method method) {
if (method.getName().equals("setName")) {
String fn = Strings.lowerFirst(method.getName().substring(3));
return JsonEntityField.eval("another_name", method.getParameterTypes()[0], mirror.getEjecting(fn), new InjectBySetter(method));
}
return null;
}
@Override
public JsonEntityField make(Mirror<?> mirror, Field field) {
return JsonEntityField.eval("test_" + field.getName(), field.getType(), mirror.getEjecting(field), mirror.getInjecting(field.getName()));
}
});
String json = Json.toJson(obj, JsonFormat.compact());
assertTrue(json.contains("\"test_objId\":9"));
assertTrue(json.contains("\"test_name\":\"name\""));
assertTrue(json.contains("\"test_age\":100"));
assertTrue(json.contains("\"another_name\":\"name\""));
Json.clearEntityCache();
Json.setDefaultFieldMaker(new JsonEntityFieldMakerImpl());
}
use of java.lang.reflect.Field in project nutz by nutzam.
the class MirrorTest method test_getFields.
@Test
public void test_getFields() {
Field[] fs = Mirror.me(MTGSF.class).getFields();
assertEquals(2, fs.length);
for (Field f : fs) assertTrue(f.getName().startsWith("m_"));
fs = Mirror.me(MTGSF.class).getStaticField(true);
assertEquals(3, fs.length);
for (Field f : fs) {
assertFalse(f.getName().startsWith("m_"));
assertFalse(f.getName().startsWith("F_"));
}
fs = Mirror.me(MTGSF.class).getStaticField(false);
List<Field> tmp = new ArrayList<Field>();
for (Field field : fs) {
if (field.getName().equals("$jacocoData"))
continue;
tmp.add(field);
}
if (fs.length != tmp.size())
fs = tmp.toArray(new Field[tmp.size()]);
assertEquals(5, fs.length);
for (Field f : fs) {
assertFalse(f.getName().startsWith("m_"));
}
}
use of java.lang.reflect.Field in project jodd by oblac.
the class Properties method inspectProperties.
/**
* Inspects all properties of target type.
*/
protected HashMap<String, PropertyDescriptor> inspectProperties() {
boolean scanAccessible = classDescriptor.isScanAccessible();
Class type = classDescriptor.getType();
HashMap<String, PropertyDescriptor> map = new HashMap<>();
Method[] methods = scanAccessible ? ReflectUtil.getAccessibleMethods(type) : ReflectUtil.getSupportedMethods(type);
for (int iteration = 0; iteration < 2; iteration++) {
// first find the getters, and then the setters!
for (Method method : methods) {
if (Modifier.isStatic(method.getModifiers())) {
// ignore static methods
continue;
}
boolean add = false;
boolean issetter = false;
String propertyName;
if (iteration == 0) {
propertyName = ReflectUtil.getBeanPropertyGetterName(method);
if (propertyName != null) {
add = true;
issetter = false;
}
} else {
propertyName = ReflectUtil.getBeanPropertySetterName(method);
if (propertyName != null) {
add = true;
issetter = true;
}
}
if (add) {
MethodDescriptor methodDescriptor = classDescriptor.getMethodDescriptor(method.getName(), method.getParameterTypes(), true);
addProperty(map, propertyName, methodDescriptor, issetter);
}
}
}
if (classDescriptor.isIncludeFieldsAsProperties()) {
FieldDescriptor[] fieldDescriptors = classDescriptor.getAllFieldDescriptors();
String[] prefix = classDescriptor.getPropertyFieldPrefix();
for (FieldDescriptor fieldDescriptor : fieldDescriptors) {
Field field = fieldDescriptor.getField();
if (Modifier.isStatic(field.getModifiers())) {
// ignore static fields
continue;
}
String name = field.getName();
if (prefix != null) {
for (String p : prefix) {
if (!name.startsWith(p)) {
continue;
}
name = name.substring(p.length());
break;
}
}
if (!map.containsKey(name)) {
// add missing field as a potential property
map.put(name, createPropertyDescriptor(name, fieldDescriptor));
}
}
}
return map;
}
use of java.lang.reflect.Field in project orientdb by orientechnologies.
the class OObjectSerializerHelperDocument method analyzeClass.
protected void analyzeClass(final Class<?> iClass) {
classes.add(iClass.getName());
int fieldModifier;
for (Class<?> currentClass = iClass; currentClass != Object.class; ) {
for (Field f : currentClass.getDeclaredFields()) {
fieldModifier = f.getModifiers();
if (Modifier.isStatic(fieldModifier) || Modifier.isNative(fieldModifier) || Modifier.isTransient(fieldModifier))
continue;
if (f.getName().equals("this$0"))
continue;
// CHECK FOR AUTO-BINDING
if (f.getAnnotation(ODocumentInstance.class) != null)
// BOUND DOCUMENT ON IT
boundDocumentFields.put(iClass, f);
}
currentClass = currentClass.getSuperclass();
if (currentClass.equals(ODocument.class))
// ODOCUMENT FIELDS
break;
}
}
use of java.lang.reflect.Field in project quasar by puniverse.
the class InitialSizeTest method getStackSize.
private int getStackSize(Fiber c) {
try {
Field stackField = Fiber.class.getDeclaredField("stack");
stackField.setAccessible(true);
Object stack = stackField.get(c);
Field dataObjectField = Stack.class.getDeclaredField("dataObject");
dataObjectField.setAccessible(true);
Object[] dataObject = (Object[]) dataObjectField.get(stack);
return dataObject.length;
} catch (Throwable ex) {
throw new AssertionError(ex);
}
}
Aggregations