use of java.beans.IntrospectionException in project Gargoyle by callakrsos.
the class ObjectUtil method toMap.
/**
* @작성자 : KYJ
* @작성일 : 2017. 3. 31.
* @param obj
* @param filter
* @return
* @throws IntrospectionException
*/
@SuppressWarnings("rawtypes")
public static Map toMap(Object t, Predicate<String> fieldNameFilter) {
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(t.getClass());
} catch (IntrospectionException e1) {
throw new RuntimeException(e1);
}
Map<String, Object> hashMap = new HashMap<String, Object>();
// Iterate over all the attributes
for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
// write메소드와 read메소드가 존재할때만.
Method writeMethod = descriptor.getWriteMethod();
Method readMethod = descriptor.getReadMethod();
if (ValueUtil.isEmpty(writeMethod) || ValueUtil.isEmpty(readMethod)) {
continue;
}
// Class<?> returnType = readMethod.getReturnType();
String methodName = ValueUtil.getSimpleMethodName(readMethod.getName());
if (fieldNameFilter != null) {
if (fieldNameFilter.test(methodName))
continue;
}
Object originalValue = null;
try {
originalValue = readMethod.invoke(t);
} catch (Exception e) {
}
if (ValueUtil.isNotEmpty(originalValue)) {
hashMap.put(methodName, originalValue);
} else {
hashMap.put(methodName, null);
}
}
return hashMap;
}
use of java.beans.IntrospectionException in project jdk8u_jdk by JetBrains.
the class GenSwingBeanInfo method genBeanInfo.
/**
* Generates the BeanInfo source file using instrospection and a
* Hashtable full of hints. This the only public method in this class.
*
* @param classname Root name of the class. i.e., JButton
* @param dochash A hashtable containing the DocBeanInfo.
*/
public void genBeanInfo(String packageName, String classname, Hashtable dochash) {
// The following initial values are just examples. All of these
// fields are initialized below.
String beanClassName = "JInternalFrame";
String beanClassObject = "javax.swing.JInternalFrame.class";
String beanDescription = "<A description of this component>.";
String beanPropertyDescriptors = "<createSwingPropertyDescriptor code>";
String classPropertyDescriptors = "<createSwingClassPropertyDescriptor code>";
Class cls = getClass(packageName, classname);
if (cls == null) {
messageAndExit("Can't find class: " + classname);
}
// Get the output stream.
PrintStream out = initOutputFile(classname);
// Run the Introspector and initialize the variables
BeanInfo beanInfo = null;
BeanDescriptor beanDescriptor = null;
try {
if (cls == javax.swing.JComponent.class) {
// Go all the way up the heirarchy for JComponent
beanInfo = Introspector.getBeanInfo(cls);
} else {
beanInfo = Introspector.getBeanInfo(cls, cls.getSuperclass());
}
beanDescriptor = beanInfo.getBeanDescriptor();
beanDescription = beanDescriptor.getShortDescription();
} catch (IntrospectionException e) {
messageAndExit("Introspection failed for " + cls.getName() + " " + e);
}
beanClassName = beanDescriptor.getName();
beanClassObject = cls.getName() + ".class";
if (DEBUG) {
System.out.println(">>>>GenSwingBeanInfo class: " + beanClassName);
}
// Generate the Class BeanDescriptor information first
if (dochash.size() > 0) {
if (dochash.containsKey(beanClassName)) {
DocBeanInfo dbi = (DocBeanInfo) dochash.remove(beanClassName);
classPropertyDescriptors = genBeanDescriptor(dbi);
if (DEBUG)
System.out.println("ClassPropertyDescriptors: " + classPropertyDescriptors);
if (!(dbi.desc.equals("null")))
beanDescription = dbi.desc;
} else
beanDescription = beanDescriptor.getShortDescription();
} else
beanDescription = beanDescriptor.getShortDescription();
// Generate the Property descriptors
beanPropertyDescriptors = genPropertyDescriptors(beanInfo, dochash);
// Dump the template to out, substituting values for
// @(token) tokens as they're encountered.
int currentIndex = 0;
// not loading this to get around build issue for now
String template = loadTemplate();
// current class.
while (currentIndex < template.length()) {
// Find the Token
int tokenStart = template.indexOf("@(", currentIndex);
if (tokenStart != -1) {
out.print(template.substring(currentIndex, tokenStart));
int tokenEnd = template.indexOf(")", tokenStart);
if (tokenEnd == -1) {
messageAndExit("Bad @(<token>) beginning at " + tokenStart);
}
String token = template.substring(tokenStart + 2, tokenEnd);
if (token.equals(TOK_BEANCLASS)) {
out.print(beanClassName);
} else if (token.equals(TOK_CLASSDESC)) {
if (!(classPropertyDescriptors.equals("<createSwingClassPropertyDescriptor code>"))) {
printDescriptors(out, classPropertyDescriptors, template, tokenStart);
}
} else if (token.equals(TOK_BEANPACKAGE)) {
out.print(packageName);
} else if (token.equals(TOK_BEANOBJECT)) {
out.print(beanClassObject);
} else if (token.equals(TOK_BEANDESC)) {
out.print(beanDescription);
} else if (token.equals(TOK_ENUMVARS)) {
out.print(enumcode);
} else if (token.equals(TOK_PROPDESC)) {
printDescriptors(out, beanPropertyDescriptors, template, tokenStart);
} else if (token.equals("#")) {
// Ignore the @(#) Version Control tag if it exists.
} else {
messageAndExit("Unrecognized token @(" + token + ")");
}
currentIndex = tokenEnd + 1;
} else {
// tokenStart == -1 - We are finsihed.
out.print(template.substring(currentIndex, template.length()));
break;
}
}
out.close();
}
use of java.beans.IntrospectionException in project opennms by OpenNMS.
the class OnmsRestService method applyQueryFilters.
protected static void applyQueryFilters(final MultivaluedMap<String, String> p, final CriteriaBuilder builder, final Integer defaultLimit) {
final MultivaluedMap<String, String> params = new MultivaluedMapImpl();
params.putAll(p);
builder.distinct();
builder.limit(defaultLimit);
if (params.containsKey("limit")) {
builder.limit(Integer.valueOf(params.getFirst("limit")));
params.remove("limit");
}
if (params.containsKey("offset")) {
builder.offset(Integer.valueOf(params.getFirst("offset")));
params.remove("offset");
}
if (params.containsKey("orderBy")) {
builder.clearOrder();
builder.orderBy(params.getFirst("orderBy"));
params.remove("orderBy");
if (params.containsKey("order")) {
if ("desc".equalsIgnoreCase(params.getFirst("order"))) {
builder.desc();
} else {
builder.asc();
}
params.remove("order");
}
}
if (Boolean.getBoolean("org.opennms.web.rest.enableQuery")) {
final String query = removeParameter(params, "query");
if (query != null)
builder.sql(query);
}
final String matchType;
final String match = removeParameter(params, "match");
if (match == null) {
matchType = "all";
} else {
matchType = match;
}
builder.match(matchType);
final Class<?> criteriaClass = builder.toCriteria().getCriteriaClass();
final BeanWrapper wrapper = getBeanWrapperForClass(criteriaClass);
final String comparatorParam = removeParameter(params, "comparator", "eq").toLowerCase();
final Criteria currentCriteria = builder.toCriteria();
for (final String key : params.keySet()) {
for (final String paramValue : params.get(key)) {
// the actual implementation com.sun.jersey.core.util.MultivaluedMapImpl returns a String, so this is fine in some way ...
if ("null".equalsIgnoreCase(paramValue)) {
builder.isNull(key);
} else if ("notnull".equalsIgnoreCase(paramValue)) {
builder.isNotNull(key);
} else {
Object value;
Class<?> type = Object.class;
try {
type = currentCriteria.getType(key);
} catch (final IntrospectionException e) {
LOG.debug("Unable to determine type for key {}", key);
}
if (type == null) {
type = Object.class;
}
LOG.debug("comparator = {}, key = {}, propertyType = {}", comparatorParam, key, type);
if (comparatorParam.equals("contains") || comparatorParam.equals("iplike") || comparatorParam.equals("ilike") || comparatorParam.equals("like")) {
value = paramValue;
} else {
LOG.debug("convertIfNecessary({}, {})", key, paramValue);
try {
value = wrapper.convertIfNecessary(paramValue, type);
} catch (final Throwable t) {
LOG.debug("failed to introspect (key = {}, value = {})", key, paramValue, t);
value = paramValue;
}
}
try {
final Method m = builder.getClass().getMethod(comparatorParam, String.class, Object.class);
m.invoke(builder, new Object[] { key, value });
} catch (final Throwable t) {
LOG.warn("Unable to find method for comparator: {}, key: {}, value: {}", comparatorParam, key, value, t);
}
}
}
}
}
use of java.beans.IntrospectionException in project jo-client-platform by jo-source.
the class BeanPropertyBuilderImpl method setPropertyDefaults.
private static boolean setPropertyDefaults(final Class<?> beanType, final String propertyName, final PropertyBuilder propertyBuilder) {
try {
final BeanInfo beanInfo = Introspector.getBeanInfo(beanType);
boolean isDefaultSet = setPropertyDefaults(beanInfo, propertyName, propertyBuilder);
if (!isDefaultSet) {
if (beanType.isInterface()) {
for (final Class<?> implementedInterface : beanType.getInterfaces()) {
isDefaultSet = setPropertyDefaults(implementedInterface, propertyName, propertyBuilder);
if (isDefaultSet) {
return true;
}
}
return false;
} else {
return false;
}
} else {
return true;
}
} catch (final IntrospectionException e) {
throw new RuntimeException(e);
}
}
use of java.beans.IntrospectionException in project rabbitmq-java-client by rabbitmq.
the class JSONWriter method writeLimited.
/**
* Write only a certain subset of the object's properties and fields.
* @param klass the class to look up properties etc in
* @param object the object
* @param properties explicit list of property/field names to include - may be null for "all"
*/
public void writeLimited(Class<?> klass, Object object, String[] properties) {
Set<String> propertiesSet = null;
if (properties != null) {
propertiesSet = new HashSet<String>();
for (String p : properties) {
propertiesSet.add(p);
}
}
add('{');
indentLevel += 2;
newline();
boolean needComma = false;
BeanInfo info;
try {
info = Introspector.getBeanInfo(klass);
} catch (IntrospectionException ie) {
info = null;
}
if (info != null) {
PropertyDescriptor[] props = info.getPropertyDescriptors();
for (int i = 0; i < props.length; ++i) {
PropertyDescriptor prop = props[i];
String name = prop.getName();
if (propertiesSet == null && name.equals("class")) {
// We usually don't want the class in there.
continue;
}
if (propertiesSet == null || propertiesSet.contains(name)) {
Method accessor = prop.getReadMethod();
if (accessor != null && !Modifier.isStatic(accessor.getModifiers())) {
try {
Object value = accessor.invoke(object, (Object[]) null);
if (needComma) {
add(',');
newline();
}
needComma = true;
add(name, value);
} catch (Exception e) {
// Ignore it.
}
}
}
}
}
Field[] ff = object.getClass().getDeclaredFields();
for (int i = 0; i < ff.length; ++i) {
Field field = ff[i];
int fieldMod = field.getModifiers();
String name = field.getName();
if (propertiesSet == null || propertiesSet.contains(name)) {
if (!Modifier.isStatic(fieldMod)) {
try {
Object v = field.get(object);
if (needComma) {
add(',');
newline();
}
needComma = true;
add(name, v);
} catch (Exception e) {
// Ignore it.
}
}
}
}
indentLevel -= 2;
newline();
add('}');
}
Aggregations