List of usage examples for java.beans Introspector getBeanInfo
public static BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException
From source file:com.github.hateoas.forms.spring.uber.UberUtils.java
private static PropertyDescriptor[] getPropertyDescriptors(Object bean) { try {/*from w w w .j av a 2 s. c o m*/ return Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); } catch (IntrospectionException e) { throw new RuntimeException("failed to get property descriptors of bean " + bean, e); } }
From source file:BeanUtility.java
public static Class getPropertyType(Object o, String propertyName) throws Exception { PropertyDescriptor[] pds = Introspector.getBeanInfo(o.getClass()).getPropertyDescriptors(); for (int i = 0; i < pds.length; i++) { if (pds[i].getName().equals(propertyName)) { return pds[i].getPropertyType(); }// www . j a v a 2 s. c o m } throw new Exception("Property not found."); }
From source file:org.dphibernate.serialization.HibernateDeserializer.java
private Object readBean(Object obj) { try {/* www. j ava2 s . c o m*/ BeanInfo info = Introspector.getBeanInfo(obj.getClass()); for (PropertyDescriptor pd : info.getPropertyDescriptors()) { String propName = pd.getName(); if (!"class".equals(propName) && !"annotations".equals(propName) && !"hibernateLazyInitializer".equals(propName)) { Object val = pd.getReadMethod().invoke(obj, null); if (val != null) { Object newVal = translate(val, pd.getPropertyType()); try { Method writeMethod = pd.getWriteMethod(); if (writeMethod != null) { writeMethod.invoke(obj, newVal); } } catch (IllegalArgumentException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (NullPointerException npe) { throw npe; } } } } } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } return obj; }
From source file:org.geotools.filter.function.PropertyExistsFunction.java
/** * @return {@link Boolean#TRUE} if the Class of the object passed as * argument defines a property names as the property name passed as * this function argument, following the standard Java Beans naming * conventions for getters. {@link Boolean#FALSE} otherwise. *///from w w w .j a va2s . co m public Object evaluate(Object bean) { if (bean instanceof SimpleFeature) { return evaluate((SimpleFeature) bean); } final String propName = getPropertyName(); try { Class type = bean.getClass(); //quick 1 // try { // String getName = "get"+propName.substring(0,1).toUpperCase()+propName.substring(1); // if (type.getMethod(getName, new Class[0]) != null) { // return true; // } // } catch (Exception ignore) { // } // // quick 2 // try { // String isName = "is"+propName.substring(0,1).toUpperCase()+propName.substring(1); // if (type.getMethod(isName, new Class[0]) != null) { // return true; // } // } catch (Exception ignore) { // } // okay go for real BeanInfo info = Introspector.getBeanInfo(type); for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) { if (descriptor.getName().equals(propName)) { if (descriptor.getReadMethod() != null) { return true; } else { return false; // property found but not writable } } } //PropertyUtils.getProperty(bean, propName); //return true; } catch (IntrospectionException ignore) { } return false; }
From source file:com.ebay.pulsar.analytics.dao.service.BaseDBService.java
public static Map<String, Object> describe(Object obj) { if (obj == null) { return null; }/* ww w .j a va2 s.c o m*/ Map<String, Object> map = new HashMap<String, Object>(); try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); if (!key.equals("class")) { try { Method getter = property.getReadMethod(); Object value = getter.invoke(obj); if (value != null) { if ("properties".equalsIgnoreCase(key) || "config".equalsIgnoreCase(key)) { value = new SerialBlob(((String) value).getBytes()); } map.put(key, value); } } catch (Exception e) { logger.error("transBean1Map Error " + e); } } } } catch (Exception e) { logger.error("transBean2Map Error " + e); } return map; }
From source file:org.theospi.portfolio.shared.model.impl.GenericXmlRenderer.java
protected void addObjectNodeInfo(Element parentNode, Object object, Element structure, String container, String site, String context) throws IntrospectionException { if (object == null) return;/*from ww w . j a v a 2 s .co m*/ // go through each property... put each one in... logger.debug("adding object of class " + object.getClass()); BeanInfo info = Introspector.getBeanInfo(object.getClass()); PropertyDescriptor[] props = info.getPropertyDescriptors(); for (int i = 0; i < props.length; i++) { PropertyDescriptor property = props[i]; logger.debug("examining property: " + property.getName()); if (isTraversableType(property, structure)) { if (isCollection(property, structure)) { addCollectionItems(parentNode, property, object, structure, container, site, context); } else if (isArtifact(property, structure)) { addArtifactItem(parentNode, property, object, container, site, context); } else { addItem(parentNode, property, object, structure, container, site, context); } } else { addItemToXml(parentNode, property, object); } } }
From source file:com.hubspot.jinjava.lib.tag.ForTag.java
@SuppressWarnings("unchecked") @Override//www . j a va 2s.co m public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) { List<String> helper = new HelperStringTokenizer(tagNode.getHelpers()).splitComma(true).allTokens(); List<String> loopVars = Lists.newArrayList(); int inPos = 0; while (inPos < helper.size()) { String val = helper.get(inPos); if ("in".equals(val)) { break; } else { loopVars.add(val); inPos++; } } if (inPos >= helper.size()) { throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'for' expects valid 'in' clause, got: " + tagNode.getHelpers(), tagNode.getLineNumber()); } String loopExpr = StringUtils.join(helper.subList(inPos + 1, helper.size()), ","); Object collection = interpreter.resolveELExpression(loopExpr, tagNode.getLineNumber()); ForLoop loop = ObjectIterator.getLoop(collection); try (InterpreterScopeClosable c = interpreter.enterScope()) { interpreter.getContext().put(LOOP, loop); StringBuilder buff = new StringBuilder(); while (loop.hasNext()) { Object val = loop.next(); // set item variables if (loopVars.size() == 1) { interpreter.getContext().put(loopVars.get(0), val); } else { for (String loopVar : loopVars) { if (Map.Entry.class.isAssignableFrom(val.getClass())) { Map.Entry<String, Object> entry = (Entry<String, Object>) val; Object entryVal = null; if ("key".equals(loopVar)) { entryVal = entry.getKey(); } else if ("value".equals(loopVar)) { entryVal = entry.getValue(); } interpreter.getContext().put(loopVar, entryVal); } else { try { PropertyDescriptor[] valProps = Introspector.getBeanInfo(val.getClass()) .getPropertyDescriptors(); for (PropertyDescriptor valProp : valProps) { if (loopVar.equals(valProp.getName())) { interpreter.getContext().put(loopVar, valProp.getReadMethod().invoke(val)); break; } } } catch (Exception e) { throw new InterpretException(e.getMessage(), e, tagNode.getLineNumber()); } } } } for (Node node : tagNode.getChildren()) { buff.append(node.render(interpreter)); } } return buff.toString(); } }
From source file:ch.ifocusit.plantuml.utils.ClassUtils.java
public static boolean isSetter(Method method) { try {/*w w w . j a v a 2 s.c om*/ return Stream.of(Introspector.getBeanInfo(method.getDeclaringClass()).getPropertyDescriptors()) .map(desc -> desc.getWriteMethod()).filter(Objects::nonNull) .anyMatch(setter -> setter.equals(method)); } catch (IntrospectionException e) { throw new IllegalStateException(e); } }
From source file:no.sesat.search.datamodel.DataModelFactoryImplTest.java
/** * * @throws java.lang.Exception//from ww w.ja va 2 s . c om */ @Test public void testDataObjectGetters() throws Exception { LOG.info("testDataObjectGetters()"); scan(DataObject.class, DataModel.class, new Command() { public void execute(Object... args) { try { final Class<?> cls = (Class<?>) args[0]; final Object dataObject = testInstantiate(cls); final PropertyDescriptor[] properties = Introspector.getBeanInfo(cls).getPropertyDescriptors(); for (PropertyDescriptor property : properties) { if (null != property.getReadMethod()) { final Object value = invoke(property.getReadMethod(), dataObject, new Object[0]); LOG.info(" Getter on " + property.getName() + " returned " + value); } if (property instanceof MappedPropertyDescriptor) { final MappedPropertyDescriptor mappedProperty = (MappedPropertyDescriptor) property; if (null != mappedProperty.getReadMethod()) { final Object value = invoke(mappedProperty.getMappedReadMethod(), dataObject, ""); LOG.info(" Getter on " + mappedProperty.getName() + " returned " + value); } } } } catch (IntrospectionException ie) { LOG.info(ie.getMessage(), ie); throw new RuntimeException(ie.getMessage(), ie); } } }); }
From source file:de.micromata.genome.db.jpa.logging.BaseJpaLoggingImpl.java
/** * Inits the.//from ww w . j a va2 s . com */ protected void initProps() { final BeanInfo bi; try { bi = Introspector.getBeanInfo(getMasterClass()); } catch (IntrospectionException e) { log.error("unable to introspect hibernate DO for logging -> no searchable fields will be available", e); return; } for (PropertyDescriptor pd : bi.getPropertyDescriptors()) { if (pd.getReadMethod() == null || pd.getWriteMethod() == null) { continue; } EntityLogSearchAttribute ent = pd.getReadMethod().getAnnotation(EntityLogSearchAttribute.class); if (ent == null) { continue; } Column col = pd.getReadMethod().getAnnotation(Column.class); if (col == null) { log.warn("Found EntityLogSearchAttribute but no Column: " + pd); continue; } for (String en : ent.enumName()) { searchableAttributeProperties.put(en, new SearchColumnDesc(pd, col.length())); } } }