use of jodd.introspector.FieldDescriptor in project jodd by oblac.
the class JsonAnnotationManager method scanClassForAnnotations.
/**
* Scans class for annotations and returns {@link jodd.json.meta.JsonAnnotationManager.TypeData}.
*/
private TypeData scanClassForAnnotations(Class type) {
ClassDescriptor cd = ClassIntrospector.lookup(type);
PropertyDescriptor[] pds = cd.getAllPropertyDescriptors();
ArrayList<String> includedList = new ArrayList<>();
ArrayList<String> excludedList = new ArrayList<>();
ArrayList<String> jsonNames = new ArrayList<>();
ArrayList<String> realNames = new ArrayList<>();
JSONAnnotation jsonAnnotation = new JSONAnnotation(JoddJson.jsonAnnotation);
for (PropertyDescriptor pd : pds) {
JSONAnnotationData data = null;
{
MethodDescriptor md = pd.getReadMethodDescriptor();
if (md != null) {
Method method = md.getMethod();
data = jsonAnnotation.readAnnotationData(method);
}
}
if (data == null) {
MethodDescriptor md = pd.getWriteMethodDescriptor();
if (md != null) {
Method method = md.getMethod();
data = jsonAnnotation.readAnnotationData(method);
}
}
if (data == null) {
FieldDescriptor fd = pd.getFieldDescriptor();
if (fd != null) {
Field field = fd.getField();
data = jsonAnnotation.readAnnotationData(field);
}
}
if (data != null) {
// annotation found
String propertyName = pd.getName();
String newPropertyName = data.getName();
if (newPropertyName != null) {
realNames.add(propertyName);
jsonNames.add(newPropertyName);
propertyName = newPropertyName;
}
if (data.isIncluded()) {
includedList.add(propertyName);
} else {
excludedList.add(propertyName);
}
}
}
String[] reals = null;
if (!realNames.isEmpty()) {
reals = realNames.toArray(new String[realNames.size()]);
}
String[] jsons = null;
if (!jsonNames.isEmpty()) {
jsons = jsonNames.toArray(new String[jsonNames.size()]);
}
// type
JSONAnnotationData data = (JSONAnnotationData) jsonAnnotation.readAnnotationData(type);
return new TypeData(includedList, excludedList, data != null && data.isStrict(), jsons, reals);
}
use of jodd.introspector.FieldDescriptor in project jodd by oblac.
the class PropertyResolver method resolve.
/**
* Resolves all properties for given type.
*/
public PropertyInjectionPoint[] resolve(Class type, boolean autowire) {
// lookup fields
ClassDescriptor cd = ClassIntrospector.lookup(type);
List<PropertyInjectionPoint> list = new ArrayList<>();
PropertyDescriptor[] allPropertyDescriptors = cd.getAllPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : allPropertyDescriptors) {
if (propertyDescriptor.isGetterOnly()) {
continue;
}
Class propertyType = propertyDescriptor.getType();
if (ReflectUtil.isTypeOf(propertyType, Collection.class)) {
continue;
}
MethodDescriptor writeMethodDescriptor = propertyDescriptor.getWriteMethodDescriptor();
FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor();
PetiteInject ref = null;
if (writeMethodDescriptor != null) {
ref = writeMethodDescriptor.getMethod().getAnnotation(PetiteInject.class);
}
if (ref == null && fieldDescriptor != null) {
ref = fieldDescriptor.getField().getAnnotation(PetiteInject.class);
}
if ((!autowire) && (ref == null)) {
continue;
}
String[] refName = null;
if (ref != null) {
String name = ref.value().trim();
if (name.length() != 0) {
refName = new String[] { name };
}
}
list.add(injectionPointFactory.createPropertyInjectionPoint(propertyDescriptor, refName));
}
PropertyInjectionPoint[] fields;
if (list.isEmpty()) {
fields = PropertyInjectionPoint.EMPTY;
} else {
fields = list.toArray(new PropertyInjectionPoint[list.size()]);
}
return fields;
}
use of jodd.introspector.FieldDescriptor in project jodd by oblac.
the class SetResolver method resolve.
/**
* Resolves all collections for given type.
*/
public SetInjectionPoint[] resolve(Class type, boolean autowire) {
ClassDescriptor cd = ClassIntrospector.lookup(type);
List<SetInjectionPoint> list = new ArrayList<>();
PropertyDescriptor[] allProperties = cd.getAllPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : allProperties) {
if (propertyDescriptor.isGetterOnly()) {
continue;
}
Class propertyType = propertyDescriptor.getType();
if (!ReflectUtil.isTypeOf(propertyType, Collection.class)) {
continue;
}
MethodDescriptor writeMethodDescriptor = propertyDescriptor.getWriteMethodDescriptor();
FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor();
PetiteInject ref = null;
if (writeMethodDescriptor != null) {
ref = writeMethodDescriptor.getMethod().getAnnotation(PetiteInject.class);
}
if (ref == null && fieldDescriptor != null) {
ref = fieldDescriptor.getField().getAnnotation(PetiteInject.class);
}
if ((!autowire) && (ref == null)) {
continue;
}
list.add(injectionPointFactory.createSetInjectionPoint(propertyDescriptor));
}
SetInjectionPoint[] fields;
if (list.isEmpty()) {
fields = SetInjectionPoint.EMPTY;
} else {
fields = list.toArray(new SetInjectionPoint[list.size()]);
}
return fields;
}
use of jodd.introspector.FieldDescriptor in project jodd by oblac.
the class BeanVisitor method getAllBeanPropertyNames.
// ---------------------------------------------------------------- util
/**
* Returns all bean property names.
*/
protected String[] getAllBeanPropertyNames(Class type, boolean declared) {
ClassDescriptor classDescriptor = ClassIntrospector.lookup(type);
PropertyDescriptor[] propertyDescriptors = classDescriptor.getAllPropertyDescriptors();
ArrayList<String> names = new ArrayList<>(propertyDescriptors.length);
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
MethodDescriptor getter = propertyDescriptor.getReadMethodDescriptor();
if (getter != null) {
if (getter.matchDeclared(declared)) {
names.add(propertyDescriptor.getName());
}
} else if (includeFields) {
FieldDescriptor field = propertyDescriptor.getFieldDescriptor();
if (field != null) {
if (field.matchDeclared(declared)) {
names.add(field.getName());
}
}
}
}
return names.toArray(new String[names.size()]);
}
use of jodd.introspector.FieldDescriptor in project jodd by oblac.
the class ValidationContext method collectPropertyAnnotationChecks.
/**
* Process all annotations of provided properties.
*/
protected void collectPropertyAnnotationChecks(final List<Check> annChecks, final PropertyDescriptor propertyDescriptor) {
final FieldDescriptor fd = propertyDescriptor.getFieldDescriptor();
if (fd != null) {
final Annotation[] annotations = fd.getField().getAnnotations();
collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations);
}
MethodDescriptor md = propertyDescriptor.getReadMethodDescriptor();
if (md != null) {
final Annotation[] annotations = md.getMethod().getAnnotations();
collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations);
}
md = propertyDescriptor.getWriteMethodDescriptor();
if (md != null) {
final Annotation[] annotations = md.getMethod().getAnnotations();
collectAnnotationChecks(annChecks, propertyDescriptor.getType(), propertyDescriptor.getName(), annotations);
}
}
Aggregations