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;
}
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();
}
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();
}
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);
}
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);
}
Aggregations