use of jodd.introspector.PropertyDescriptor in project jodd by oblac.
the class ValidationContextTest method testCollectPropertyAnnotationChecks.
@Test
public void testCollectPropertyAnnotationChecks() {
ValidationContext context = spy(new ValidationContext());
PropertyDescriptor propertyDescriptor = mock(PropertyDescriptor.class);
List<Check> list = new ArrayList<>();
context.collectPropertyAnnotationChecks(list, propertyDescriptor);
verify(context, never()).collectAnnotationChecks(any(List.class), any(Class.class), any(String.class), any(Annotation[].class));
}
use of jodd.introspector.PropertyDescriptor in project jodd by oblac.
the class MapToBean method map2bean.
/**
* Converts map to target type.
*/
public Object map2bean(Map map, Class targetType) {
Object target = null;
// create targets type
String className = (String) map.get(classMetadataName);
if (className == null) {
if (targetType == null) {
// nothing to do, no information about target type found
target = map;
}
} else {
try {
targetType = ClassLoaderUtil.loadClass(className);
} catch (ClassNotFoundException cnfex) {
throw new JsonException(cnfex);
}
}
if (target == null) {
target = jsonParser.newObjectInstance(targetType);
}
ClassDescriptor cd = ClassIntrospector.lookup(target.getClass());
boolean targetIsMap = target instanceof Map;
for (Object key : map.keySet()) {
String keyName = key.toString();
if (classMetadataName != null) {
if (keyName.equals(classMetadataName)) {
continue;
}
}
PropertyDescriptor pd = cd.getPropertyDescriptor(keyName, declared);
if (!targetIsMap && pd == null) {
// target property does not exist, continue
continue;
}
// value is one of JSON basic types, like Number, Map, List...
Object value = map.get(key);
Class propertyType = pd == null ? null : pd.getType();
Class componentType = pd == null ? null : pd.resolveComponentType(true);
if (value != null) {
if (value instanceof List) {
if (componentType != null && componentType != String.class) {
value = generifyList((List) value, componentType);
}
} else if (value instanceof Map) {
// if the value we want to inject is a Map...
if (!ReflectUtil.isTypeOf(propertyType, Map.class)) {
// ... and if target is NOT a map
value = map2bean((Map) value, propertyType);
} else {
// target is also a Map, but we might need to generify it
Class keyType = pd == null ? null : pd.resolveKeyType(true);
if (keyType != String.class || componentType != String.class) {
// generify
value = generifyMap((Map) value, keyType, componentType);
}
}
}
}
if (targetIsMap) {
((Map) target).put(keyName, value);
} else {
try {
setValue(target, pd, value);
} catch (Exception ignore) {
ignore.printStackTrace();
}
}
}
return target;
}
use of jodd.introspector.PropertyDescriptor in project jodd by oblac.
the class TypeJsonVisitor method visit.
/**
* Visits a type.
*/
public void visit() {
ClassDescriptor classDescriptor = ClassIntrospector.lookup(type);
if (classMetadataName != null) {
// process first 'meta' fields 'class'
onProperty(classMetadataName, null, false);
}
PropertyDescriptor[] propertyDescriptors = classDescriptor.getAllPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
Getter getter = propertyDescriptor.getGetter(declared);
if (getter != null) {
String propertyName = propertyDescriptor.getName();
boolean isTransient = false;
// check for transient flag
FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor();
if (fieldDescriptor != null) {
isTransient = Modifier.isTransient(fieldDescriptor.getField().getModifiers());
}
onProperty(propertyName, propertyDescriptor, isTransient);
}
}
}
use of jodd.introspector.PropertyDescriptor in project jodd by oblac.
the class PetiteBeans method registerPetitePropertyInjectionPoint.
/**
* Registers property injection point.
*
* @param beanName bean name
* @param property property name
* @param reference explicit injection reference, may be <code>null</code>
*/
public void registerPetitePropertyInjectionPoint(String beanName, String property, String reference) {
BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName);
String[] references = reference == null ? null : new String[] { reference };
ClassDescriptor cd = ClassIntrospector.lookup(beanDefinition.type);
PropertyDescriptor propertyDescriptor = cd.getPropertyDescriptor(property, true);
if (propertyDescriptor == null) {
throw new PetiteException("Property not found: " + beanDefinition.type.getName() + '#' + property);
}
PropertyInjectionPoint pip = injectionPointFactory.createPropertyInjectionPoint(propertyDescriptor, references);
beanDefinition.addPropertyInjectionPoint(pip);
}
Aggregations