Search in sources :

Example 16 with ClassDescriptor

use of jodd.introspector.ClassDescriptor in project jodd by oblac.

the class JsonParserBase method newObjectInstance.

/**
	 * Creates new object or a <code>HashMap</code> if type is not specified.
	 */
protected Object newObjectInstance(Class targetType) {
    if (targetType == null || targetType == Map.class) {
        return new HashMap();
    }
    ClassDescriptor cd = ClassIntrospector.lookup(targetType);
    CtorDescriptor ctorDescriptor = cd.getDefaultCtorDescriptor(true);
    if (ctorDescriptor == null) {
        throw new JsonException("Default ctor not found for: " + targetType.getName());
    }
    try {
        return ctorDescriptor.getConstructor().newInstance();
    } catch (Exception e) {
        throw new JsonException(e);
    }
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) HashMap(java.util.HashMap) CtorDescriptor(jodd.introspector.CtorDescriptor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with ClassDescriptor

use of jodd.introspector.ClassDescriptor in project jodd by oblac.

the class BeanPrefixTest method testFieldPrefix1.

@Test
public void testFieldPrefix1() {
    LifeBean lifeBean = new LifeBean();
    String foo = BeanUtil.pojo.getProperty(lifeBean, "foo").toString();
    assertEquals("foo", foo);
    JoddIntrospector.introspector = new CachingIntrospector(true, true, true, new String[] { "_" });
    foo = BeanUtil.pojo.getProperty(lifeBean, "foo").toString();
    assertEquals("foo", foo);
    ClassDescriptor cd = JoddIntrospector.introspector.lookup(LifeBean.class);
    PropertyDescriptor[] pds = cd.getAllPropertyDescriptors();
    assertEquals(3, pds.length);
    assertEquals("bar", pds[0].getName());
    assertEquals("_bar", pds[0].getFieldDescriptor().getName());
    assertEquals("www", pds[2].getName());
    assertEquals(null, pds[2].getFieldDescriptor());
    JoddIntrospector.introspector = new CachingIntrospector();
}
Also used : CachingIntrospector(jodd.introspector.CachingIntrospector) ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) LifeBean(jodd.bean.data.LifeBean) Test(org.junit.Test)

Example 18 with ClassDescriptor

use of jodd.introspector.ClassDescriptor in project jodd by oblac.

the class BeanPrefixTest method testFieldPrefix1withEmpty.

@Test
public void testFieldPrefix1withEmpty() {
    LifeBean lifeBean = new LifeBean();
    String foo = BeanUtil.pojo.getProperty(lifeBean, "foo").toString();
    assertEquals("foo", foo);
    JoddIntrospector.introspector = new CachingIntrospector(true, true, true, new String[] { "_", "" });
    foo = BeanUtil.pojo.getProperty(lifeBean, "foo").toString();
    assertEquals("foo", foo);
    ClassDescriptor cd = JoddIntrospector.introspector.lookup(LifeBean.class);
    PropertyDescriptor[] pds = cd.getAllPropertyDescriptors();
    assertEquals(3, pds.length);
    assertEquals("bar", pds[0].getName());
    assertEquals("_bar", pds[0].getFieldDescriptor().getName());
    assertEquals("www", pds[2].getName());
    assertEquals("www", pds[2].getFieldDescriptor().getName());
    JoddIntrospector.introspector = new CachingIntrospector();
}
Also used : CachingIntrospector(jodd.introspector.CachingIntrospector) ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) LifeBean(jodd.bean.data.LifeBean) Test(org.junit.Test)

Example 19 with ClassDescriptor

use of jodd.introspector.ClassDescriptor in project jodd by oblac.

the class BeanUtilGenericsTest method testAllBeanSetters.

@Test
public void testAllBeanSetters() {
    Woof woof = new Woof();
    Class type = woof.getClass();
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    PropertyDescriptor[] properties = cd.getAllPropertyDescriptors();
    assertNotNull(properties);
    assertEquals(7, properties.length);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) Woof(jodd.bean.data.Woof) Test(org.junit.Test)

Example 20 with ClassDescriptor

use of jodd.introspector.ClassDescriptor in project jodd by oblac.

the class ValidationContext method addClassChecks.

/**
	 * Parses class annotations and adds all checks.
	 * @see #resolveFor(Class)
	 */
public void addClassChecks(Class target) {
    List<Check> list = cache.get(target);
    if (list == null) {
        list = new ArrayList<>();
        ClassDescriptor cd = ClassIntrospector.lookup(target);
        PropertyDescriptor[] allProperties = cd.getAllPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : allProperties) {
            collectPropertyAnnotationChecks(list, propertyDescriptor);
        }
        cache.put(target, list);
    }
    addAll(list);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor)

Aggregations

ClassDescriptor (jodd.introspector.ClassDescriptor)33 PropertyDescriptor (jodd.introspector.PropertyDescriptor)16 MethodDescriptor (jodd.introspector.MethodDescriptor)15 ArrayList (java.util.ArrayList)10 Method (java.lang.reflect.Method)8 FieldDescriptor (jodd.introspector.FieldDescriptor)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 PetiteInject (jodd.petite.meta.PetiteInject)4 Map (java.util.Map)3 CtorDescriptor (jodd.introspector.CtorDescriptor)3 PetiteException (jodd.petite.PetiteException)3 Annotation (java.lang.annotation.Annotation)2 Constructor (java.lang.reflect.Constructor)2 Collection (java.util.Collection)2 LifeBean (jodd.bean.data.LifeBean)2 CachingIntrospector (jodd.introspector.CachingIntrospector)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 List (java.util.List)1