Reflect
![SonarQube Coverage](https://camo.githubusercontent.com/0fd347b4975fbd1399eabb99aab8e775d18cc13de1316c92c28415ccaaa85c7f/68747470733a2f2f696d672e736869656c64732e696f2f736f6e61722f687474702f736f6e61722e636f646572656c6967696f6e2e636f6d2f636f6d2e636f646572656c6967696f6e3a636f646572656c6967696f6e2d6368657272792d7265666c6563742f636f7665726167652e7376673f7374796c653d706c6173746963)
This library is part of the cherry project, it provides convenience methods to do Java bean introspection. It's main functionality is to provide a very thin wrapper around the java.beans.Introspector
, which can not handle generic classes to well in Java version 1.6.
For more details have a look at the wiki.
Requirements
- Java 1.6
- dependencies see maven pom
Example code
package com.codereligion.example;
import com.codereligion.example.bean.User;
import com.codereligion.cherry.reflect.BeanIntrospections;
import com.codereligion.cherry.reflect.GenericTypeAwareBeanInfo;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.Set;
public class Example {
public void example() throws IntrospectionException {
// check if a class has a default constructor
boolean hasDefaultConstructor = BeanIntrospections.hasDefaultConstructor(User.class);
// get property descriptors, even for classes with generic members
Set<PropertyDescriptor> writeables = BeanIntrospections.getWriteableProperties(User.class);
Set<PropertyDescriptor> readables = BeanIntrospections.getReadableProperties(User.class);
Set<PropertyDescriptor> writeableAndReables = BeanIntrospections.getWriteableAndReadableProperties(User.class);
// decorate a BeanInfo so that it can handle generics
BeanInfo beanInfo = GenericTypeAwareBeanInfo.of(Introspector.getBeanInfo(User.class));
}
}