Search in sources :

Example 11 with PropertyDescriptor

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

the class JsonParser method parseObjectContent.

// ---------------------------------------------------------------- object
/**
	 * Parses object, once when open bracket has been consumed.
	 */
protected Object parseObjectContent(Class targetType, Class valueKeyType, Class valueType) {
    if (targetType == Object.class) {
        targetType = Map.class;
    }
    // continue
    targetType = replaceWithMappedTypeForPath(targetType);
    Object target;
    boolean isTargetTypeMap = true;
    boolean isTargetRealTypeMap = true;
    ClassDescriptor targetTypeClassDescriptor = null;
    JsonAnnotationManager.TypeData typeData = null;
    if (targetType != null) {
        targetTypeClassDescriptor = ClassIntrospector.lookup(targetType);
        // find if the target is really a map
        // because when classMetadataName != null we are forcing
        // map usage locally in this method
        isTargetRealTypeMap = targetTypeClassDescriptor.isMap();
        typeData = JoddJson.annotationManager.lookupTypeData(targetType);
    }
    if (isTargetRealTypeMap) {
        // resolve keys only for real maps
        path.push(KEYS);
        valueKeyType = replaceWithMappedTypeForPath(valueKeyType);
        path.pop();
    }
    if (classMetadataName == null) {
        // create instance of target type, no 'class' information
        target = newObjectInstance(targetType);
        isTargetTypeMap = isTargetRealTypeMap;
    } else {
        // all beans will be created first as a map
        target = new HashMap();
    }
    boolean koma = false;
    mainloop: while (true) {
        skipWhiteSpaces();
        char c = input[ndx];
        if (c == '}') {
            if (koma) {
                syntaxError("Trailing comma");
            }
            ndx++;
            break;
        }
        koma = false;
        String key = parseString();
        String keyOriginal = key;
        skipWhiteSpaces();
        consume(':');
        skipWhiteSpaces();
        // read the type of the simple property
        PropertyDescriptor pd = null;
        Class propertyType = null;
        Class keyType = null;
        Class componentType = null;
        if (!isTargetRealTypeMap) {
            // replace key with real property value
            key = JoddJson.annotationManager.resolveRealName(targetType, key);
        }
        if (!isTargetTypeMap) {
            pd = targetTypeClassDescriptor.getPropertyDescriptor(key, true);
            if (pd != null) {
                propertyType = pd.getType();
                keyType = pd.resolveKeyType(true);
                componentType = pd.resolveComponentType(true);
            }
        }
        Object value;
        if (!isTargetTypeMap) {
            // *** inject into bean
            path.push(key);
            value = parseValue(propertyType, keyType, componentType);
            path.pop();
            if (typeData.rules.match(keyOriginal, !typeData.strict)) {
                if (pd != null) {
                    // only inject values if target property exist
                    injectValueIntoObject(target, pd, value);
                }
            }
        } else {
            Object keyValue = key;
            if (valueKeyType != null) {
                keyValue = convertType(key, valueKeyType);
            }
            // *** add to map
            if (isTargetRealTypeMap) {
                path.push(VALUES, key);
                valueType = replaceWithMappedTypeForPath(valueType);
            } else {
                path.push(key);
            }
            value = parseValue(valueType, null, null);
            path.pop();
            ((Map) target).put(keyValue, value);
        }
        skipWhiteSpaces();
        c = input[ndx];
        switch(c) {
            case '}':
                ndx++;
                break mainloop;
            case ',':
                ndx++;
                koma = true;
                break;
            default:
                syntaxError("Invalid char: expected } or ,");
        }
    }
    // convert Map to target type
    if (classMetadataName != null) {
        target = mapToBean.map2bean((Map) target, targetType);
    }
    return target;
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) JsonAnnotationManager(jodd.json.meta.JsonAnnotationManager)

Example 12 with PropertyDescriptor

use of jodd.introspector.PropertyDescriptor 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 13 with PropertyDescriptor

use of jodd.introspector.PropertyDescriptor 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 14 with PropertyDescriptor

use of jodd.introspector.PropertyDescriptor 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 15 with PropertyDescriptor

use of jodd.introspector.PropertyDescriptor 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

PropertyDescriptor (jodd.introspector.PropertyDescriptor)19 ClassDescriptor (jodd.introspector.ClassDescriptor)16 ArrayList (java.util.ArrayList)8 FieldDescriptor (jodd.introspector.FieldDescriptor)5 MethodDescriptor (jodd.introspector.MethodDescriptor)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 LifeBean (jodd.bean.data.LifeBean)2 CachingIntrospector (jodd.introspector.CachingIntrospector)2 PetiteInject (jodd.petite.meta.PetiteInject)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Woof (jodd.bean.data.Woof)1 Getter (jodd.introspector.Getter)1