Search in sources :

Example 76 with Field

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());
}
Also used : JsonEntityField(org.nutz.json.entity.JsonEntityField) Field(java.lang.reflect.Field) JENObj(org.nutz.json.meta.JENObj) JsonEntityField(org.nutz.json.entity.JsonEntityField) InjectBySetter(org.nutz.lang.inject.InjectBySetter) JsonEntityFieldMakerImpl(org.nutz.json.impl.JsonEntityFieldMakerImpl) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 77 with Field

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_"));
    }
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 78 with Field

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;
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field)

Example 79 with Field

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;
    }
}
Also used : Field(java.lang.reflect.Field) ODocumentInstance(com.orientechnologies.orient.core.annotation.ODocumentInstance)

Example 80 with Field

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);
    }
}
Also used : Field(java.lang.reflect.Field)

Aggregations

Field (java.lang.reflect.Field)5144 Method (java.lang.reflect.Method)613 Test (org.junit.Test)538 ArrayList (java.util.ArrayList)490 IOException (java.io.IOException)318 HashMap (java.util.HashMap)318 Map (java.util.Map)296 InvocationTargetException (java.lang.reflect.InvocationTargetException)176 List (java.util.List)171 Pattern (java.util.regex.Pattern)122 Matcher (java.util.regex.Matcher)117 HashSet (java.util.HashSet)109 File (java.io.File)107 Annotation (java.lang.annotation.Annotation)98 Support_Field (tests.support.Support_Field)82 Collection (java.util.Collection)81 Test (org.testng.annotations.Test)68 Type (java.lang.reflect.Type)67 SienaException (siena.SienaException)65 Before (org.junit.Before)62