List of usage examples for java.beans PropertyDescriptor getReadMethod
public synchronized Method getReadMethod()
From source file:nl.ucan.navigate.NestedPath.java
private static Class getCollectionReturnType(String property, Class clasz) throws IntrospectionException { PropertyDescriptor propertyDescriptor = new PropertyDescriptor(property, clasz); Method method = propertyDescriptor.getReadMethod(); Class klass = GenericCollectionTypeResolver.getCollectionReturnType(method); if (klass == null) { klass = GenericTypeResolver.resolveReturnType(method, clasz); if (klass.isArray()) { return klass.getComponentType(); } else {/*from w w w. j ava 2 s .c o m*/ throw new IllegalStateException("unsupported return type"); } } else return klass; }
From source file:org.apache.myfaces.el.PropertyResolverImpl.java
public static Object getProperty(Object base, String name) { PropertyDescriptor propertyDescriptor = getPropertyDescriptor(base, name); Method m = propertyDescriptor.getReadMethod(); if (m == null) { throw new PropertyNotFoundException(getMessage(base, name)); }/*w w w. j a v a 2s. co m*/ // Check if the concrete class of this method is accessible and if not // search for a public interface that declares this method m = MethodUtils.getAccessibleMethod(m); if (m == null) { throw new PropertyNotFoundException(getMessage(base, name) + " (not accessible!)"); } try { return m.invoke(base, NO_ARGS); } catch (Throwable t) { throw new EvaluationException(getMessage(base, name), t); } }
From source file:org.codehaus.groovy.grails.commons.GrailsDomainConfigurationUtil.java
/** * Checks whether is property is configurational. * * @param descriptor The descriptor/* w ww .j av a 2 s. c o m*/ * @return true if it is configurational */ public static boolean isNotConfigurational(PropertyDescriptor descriptor) { final String name = descriptor.getName(); Method readMethod = descriptor.getReadMethod(); Method writeMethod = descriptor.getWriteMethod(); if ((readMethod != null && Modifier.isStatic(readMethod.getModifiers()) || (writeMethod != null && Modifier.isStatic(writeMethod.getModifiers())))) { return false; } return !Errors.class.isAssignableFrom(descriptor.getPropertyType()) && isNotConfigurational(name); }
From source file:org.bibsonomy.plugin.jabref.util.JabRefModelConverter.java
protected static void copyStringProperties(BibtexEntry entry, BibTex bibtex) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { /*/*from w w w.j a v a 2 s .c o m*/ * we use introspection to get all fields ... */ final BeanInfo info = Introspector.getBeanInfo(bibtex.getClass()); final PropertyDescriptor[] descriptors = info.getPropertyDescriptors(); /* * iterate over all properties */ for (final PropertyDescriptor pd : descriptors) { final Method getter = pd.getReadMethod(); // loop over all String attributes final Object o = getter.invoke(bibtex, (Object[]) null); if (String.class.equals(pd.getPropertyType()) && (o != null) && !JabRefModelConverter.EXCLUDE_FIELDS.contains(pd.getName())) { final String value = ((String) o); if (present(value)) { entry.setField(pd.getName().toLowerCase(), value); } } } }
From source file:es.logongas.ix3.util.ReflectionUtil.java
/** * * @param clazz/*from w ww .j a v a2s . c o m*/ * @param propertyName El nombre de la propiedad permite que sean varias * "nested" con puntos. Ej: "prop1.prop2.prop3" * @return */ static public boolean existsReadPropertyInClass(Class clazz, String propertyName) { PropertyDescriptor propertyDescriptor = getPropertyDescriptor(clazz, propertyName); if (propertyDescriptor.getReadMethod() != null) { return true; } else { return false; } }
From source file:org.dozer.util.ReflectionUtils.java
/** * There are some nasty bugs for introspection with generics. This method addresses those nasty bugs and tries to find proper methods if available * http://bugs.sun.com/view_bug.do?bug_id=6788525 * http://bugs.sun.com/view_bug.do?bug_id=6528714 * @param descriptor/* w w w . j av a 2s . c om*/ * @return */ private static PropertyDescriptor fixGenericDescriptor(Class<?> clazz, PropertyDescriptor descriptor) { Method readMethod = descriptor.getReadMethod(); Method writeMethod = descriptor.getWriteMethod(); if (readMethod != null && (readMethod.isBridge() || readMethod.isSynthetic())) { String propertyName = descriptor.getName(); //capitalize the first letter of the string; String baseName = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); String setMethodName = "set" + baseName; String getMethodName = "get" + baseName; Method[] methods = clazz.getMethods(); for (Method method : methods) { if (method.getName().equals(getMethodName) && !method.isBridge() && !method.isSynthetic()) { try { descriptor.setReadMethod(method); } catch (IntrospectionException e) { //move on } } if (method.getName().equals(setMethodName) && !method.isBridge() && !method.isSynthetic()) { try { descriptor.setWriteMethod(method); } catch (IntrospectionException e) { //move on } } } } return descriptor; }
From source file:org.jaxygen.converters.properties.PropertiesToBeanConverter.java
/** * Fill the field in bean by the value pointed by the name. Name format name=<(KEY([N])?)+> where KEY bean property name, N index in table (if bean field is * List of java array).// ww w . jav a2s . co m * * @param name * @param value * @param beanClass * @param baseBean * @param conversionReport * @return * @throws IntrospectionException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InvocationTargetException * @throws InstantiationException * @throws WrongProperyIndex */ private static Object fillBeanValueByName(final String name, Object value, Class<?> beanClass, Object baseBean) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException, WrongProperyIndex { // parse name x.y[i].z[n].v Object bean = baseBean; if (bean == null) { bean = beanClass.newInstance(); } Class<?> c = beanClass; BeanInfo beanInfo = Introspector.getBeanInfo(c, Object.class); final String childName = name.substring(name.indexOf(".") + 1); String path[] = name.split("\\."); final String fieldName = path[0]; // parse arrays [n] if (fieldName.endsWith("]")) { int bracketStart = fieldName.indexOf("["); int len = fieldName.length(); if (bracketStart > 0) { fillBeanArrayField(name, value, bean, beanInfo, path, fieldName, bracketStart, len); } else { throw new WrongProperyIndex(name); } } else { // parse non arrays for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { if (pd.getName().equals(fieldName)) { Method writter = pd.getWriteMethod(); Method reader = pd.getReadMethod(); if (writter != null && reader != null) { Class<?> valueType = reader.getReturnType(); if (path.length == 1) { Object valueObject = parsePropertyToValue(value, valueType); writter.invoke(bean, valueObject); } else { Object childBean = reader.invoke(bean); Object valueObject = fillBeanValueByName(childName, value, valueType, childBean); writter.invoke(bean, valueObject); } } } } } // Object bean = c.newInstance(); return bean; }
From source file:org.agnitas.service.csv.Toolkit.java
public static void setValueFromBean(Object bean, String fieldName, String value) { try {//w w w. jav a2s . com PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean, fieldName); // This should never be null as it would be caused by a node representing an unknown property being popped whereas it // should never have been pushed in the first place! if (propertyDescriptor.getName().equals("customFields")) { final Map<String, String> customCollumnMappig = (Map<String, String>) propertyDescriptor .getReadMethod().invoke(bean); customCollumnMappig.put(fieldName, value); } else { propertyDescriptor.getWriteMethod().invoke(bean, value); } } catch (Exception e) { AgnUtils.logger().warn(MessageFormat.format("Failed to set bean ({0}) property {1} with value {2}", bean, fieldName, value), e); } }
From source file:org.hellojavaer.testcase.generator.TestCaseGenerator.java
@SuppressWarnings({ "rawtypes", "unchecked" }) private static <T> void checkBeanValidity(Class<T> clazz, List<String> excludeFieldList, int recursiveCycleLimit) { PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(clazz); boolean validBean = false; for (PropertyDescriptor pd : pds) { if (pd.getWriteMethod() != null && pd.getReadMethod() != null && // (excludeFieldList != null && !excludeFieldList.contains(pd.getName()) || excludeFieldList == null) // ) {/*from ww w.j av a 2 s. c o m*/ validBean = true; // just set write ,read for user control if (!Modifier.isPublic(pd.getWriteMethod().getDeclaringClass().getModifiers())) { pd.getWriteMethod().setAccessible(true); } Class fieldClazz = pd.getPropertyType(); if (!TypeUtil.isBaseType(fieldClazz)) { try { // ???? if (recursiveCycleLimit == 0 && fieldClazz == clazz) { throw new RuntimeException("recursive cycle limit is 0! field[" + pd.getName() + "] may cause recursive, please add this field[" + pd.getName() + "] to exclude list or set recursiveCycleLimit more than 0 ."); } else if (!fieldClazz.isAssignableFrom(clazz)) { checkBeanValidity(fieldClazz, null, 999999999); } } catch (Exception e) { throw new RuntimeException("Unknown Class " + fieldClazz.getName() + " for field " + pd.getName() + " ! please add this field[" + pd.getName() + "] to exclude list.", e); } } } } if (!validBean) { throw new RuntimeException( "Invalid Bean Class[" + clazz.getName() + "], for it has not getter setter methods!"); } }
From source file:org.agnitas.service.csv.Toolkit.java
public static String getValueFromBean(Object bean, String fieldName) { try {// w w w . ja va2 s . c o m PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean, fieldName); // This should never be null as it would be caused by a node representing an unknown property being popped whereas it // should never have been pushed in the first place! if (propertyDescriptor.getName().equals("customFields")) { final Map<String, String> customCollumnMappig = (Map<String, String>) propertyDescriptor .getReadMethod().invoke(bean); return customCollumnMappig.get(fieldName); } else { return (String) propertyDescriptor.getReadMethod().invoke(bean); } } catch (Exception e) { AgnUtils.logger().warn( MessageFormat.format("Failed to get bean ({0}) property ({1}) value", bean, fieldName), e); return null; } }