Example usage for java.beans PropertyDescriptor getReadMethod

List of usage examples for java.beans PropertyDescriptor getReadMethod

Introduction

In this page you can find the example usage for java.beans PropertyDescriptor getReadMethod.

Prototype

public synchronized Method getReadMethod() 

Source Link

Document

Gets the method that should be used to read the property value.

Usage

From source file:com.complexible.pinto.RDFMapper.java

private static boolean isIgnored(final PropertyDescriptor thePropertyDescriptor) {
    // we'll ignore getClass() on the bean
    if (thePropertyDescriptor.getName().equals("class")
            && thePropertyDescriptor.getReadMethod().getDeclaringClass() == Object.class
            && thePropertyDescriptor.getReadMethod().getReturnType().equals(Class.class)) {
        return true;
    }/*from www .jav a2  s  .co  m*/

    return false;
}

From source file:org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldIntrospectorImpl.java

private Object getValueFrom(PropertyDescriptor pd, Object target) {
    if (target == null)
        return null;
    Method m = pd.getReadMethod();
    if (m != null) {
        try {/* w w w.j a v  a 2  s.  co  m*/
            return m.invoke(ProxyHelper.getRealObject(target), null);
        } catch (Throwable e) {
            logProblem(pd, target, null, "Can't read value from given object");
            throw new MetadataException(
                    "Error invoking method:" + m.getName() + " in object " + target.getClass().getName(), e);
        }
    } else {
        throw new MetadataException("Can't get ReadMethod for property:" + pd.getName() + " in object "
                + target.getClass().getName());
    }
}

From source file:com.sf.ddao.crud.param.CRUDBeanPropsParameter.java

private synchronized void init(Context ctx) {
    if (descriptors != null) {
        return;//from   ww w  . j a v  a2 s.co  m
    }
    Class<?> beanClass = CRUDParameterService.getCRUDDaoBean(ctx, argNum);
    final PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass);
    List<PropertyDescriptor> res = new ArrayList<PropertyDescriptor>(descriptors.length);
    for (PropertyDescriptor descriptor : descriptors) {
        if (CRUDDao.IGNORED_PROPS.contains(descriptor.getName())) {
            continue;
        }
        final Method readMethod = descriptor.getReadMethod();
        if (readMethod == null) {
            continue;
        }
        if (readMethod.getAnnotation(CRUDIgnore.class) != null) {
            continue;
        }
        res.add(descriptor);
    }
    this.descriptors = res;
}

From source file:org.brushingbits.jnap.persistence.hsearch.FullTextDao.java

protected String[] getIndexedFields() {
    if (this.indexedFields == null) {
        PropertyDescriptor[] beanProperties = BeanUtils.getPropertyDescriptors(getEntityClass());
        List<String> fields = new ArrayList<String>();
        for (PropertyDescriptor propertyDescriptor : beanProperties) {
            Field field = ReflectionUtils.getAnnotation(Field.class, propertyDescriptor.getReadMethod());
            if (field == null) {
                java.lang.reflect.Field propertyField = FieldUtils.getField(getEntityClass(),
                        propertyDescriptor.getName());
                if (propertyField != null && propertyField.isAnnotationPresent(Field.class)) {
                    field = propertyField.getAnnotation(Field.class);
                }/*  ww  w. j  a  va2  s .  c  o  m*/
            }
            if (field != null) {
                String fieldName = propertyDescriptor.getName();
                if (StringUtils.isNotBlank(fieldName)) {
                    fieldName = field.name();
                }
                fields.add(fieldName);
            }
        }
    }
    return this.indexedFields;
}

From source file:org.jnap.core.persistence.hsearch.FullTextDaoSupport.java

protected String[] getIndexedFields() {
    if (this.indexedFields == null) {
        PropertyDescriptor[] beanProperties = BeanUtils.getPropertyDescriptors(getEntityClass());
        List<String> fields = new ArrayList<String>();
        for (PropertyDescriptor propertyDescriptor : beanProperties) {
            Field field = AnnotationUtils.findAnnotation(propertyDescriptor.getReadMethod(), Field.class);
            if (field == null) {
                java.lang.reflect.Field propertyField = FieldUtils.getField(getEntityClass(),
                        propertyDescriptor.getName(), true);
                if (propertyField != null && propertyField.isAnnotationPresent(Field.class)) {
                    field = propertyField.getAnnotation(Field.class);
                }//from   w ww  . j  ava2 s  .  c o  m
            }
            if (field != null) {
                fields.add(propertyDescriptor.getName());
            }
        }
        if (fields.isEmpty()) {
            throw new HSearchQueryException(""); // TODO ex msg
        }
        this.indexedFields = fields.toArray(new String[] {});
    }
    return this.indexedFields;
}

From source file:gr.interamerican.bo2.utils.JavaBeanUtils.java

/**
 * Copies a property from a source object to a target object given two
 * respective property descriptors./*from  w w w.  j a  va2s  . c o  m*/
 * The target's property type has to be a super-type of the source's
 * property type. If both are sub-types of {@link Number}, we use a 
 * converter to convert the source property to a type compatible with
 * the target property. This may lead to loss of precision.
 * 
 * TODO: We could also support other conversions, e.g. Character -> String
 * 
 * @param source
 * @param target
 * @param sourcePd
 * @param targetPd
 */
public static void copyProperty(Object source, Object target, PropertyDescriptor sourcePd,
        PropertyDescriptor targetPd) {
    if (source == null || target == null || sourcePd == null || targetPd == null) {
        return;
    }
    Class<?> targetPdType = targetPd.getPropertyType();
    Class<?> sourcePdType = sourcePd.getPropertyType();

    boolean assignable = targetPdType.isAssignableFrom(sourcePdType);
    boolean differentNumberTypes = Number.class.isAssignableFrom(targetPdType)
            && Number.class.isAssignableFrom(sourcePdType) && !assignable;

    if (assignable || differentNumberTypes) {
        Method getter = sourcePd.getReadMethod();
        if (getter != null) {
            Object[] getterArgs = null;
            Object value = ReflectionUtils.invoke(getter, source, getterArgs);
            Method setter = targetPd.getWriteMethod();
            if (setter != null) {
                if (differentNumberTypes) {
                    @SuppressWarnings("unchecked")
                    NumberConverter converter = new NumberConverter((Class<? extends Number>) targetPdType);
                    value = converter.execute((Number) value);
                }
                Object[] setterArgs = { value };
                ReflectionUtils.invoke(setter, target, setterArgs);
            }
        }
    }
}

From source file:com.zero.service.impl.BaseServiceImpl.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private void copyProperties(Object source, Object target) throws BeansException {
    Assert.notNull(source, "Source must not be null");
    Assert.notNull(target, "Target must not be null");

    PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass());
    for (PropertyDescriptor targetPd : targetPds) {
        if (targetPd.getWriteMethod() != null) {
            PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(),
                    targetPd.getName());
            if (sourcePd != null && sourcePd.getReadMethod() != null) {
                try {
                    Method readMethod = sourcePd.getReadMethod();
                    if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                        readMethod.setAccessible(true);
                    }/*ww  w  .  jav  a2 s . co m*/
                    Object sourceValue = readMethod.invoke(source);
                    Object targetValue = readMethod.invoke(target);
                    if (sourceValue != null && targetValue != null && targetValue instanceof Collection) {
                        Collection collection = (Collection) targetValue;
                        collection.clear();
                        collection.addAll((Collection) sourceValue);
                    } else {
                        Method writeMethod = targetPd.getWriteMethod();
                        if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                            writeMethod.setAccessible(true);
                        }
                        writeMethod.invoke(target, sourceValue);
                    }
                } catch (Throwable ex) {
                    throw new FatalBeanException("Could not copy properties from source to target", ex);
                }
            }
        }
    }
}

From source file:net.cpollet.jixture.hibernate3.helper.Hibernate3MappingDefinitionHolder.java

private Column getColumnFromGetter(Field field) {
    BeanInfo beanInfo = getBeanInfo(field);

    for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
        if (isGetterForField(pd, field)) {
            return pd.getReadMethod().getAnnotation(Column.class);
        }/* w w w  . java  2  s.  c  o  m*/
    }

    return null;
}

From source file:de.knightsoftnet.validators.rebind.GwtReflectGetterGenerator.java

@Override
public final String generate(final TreeLogger plogger, final GeneratorContext pcontext, final String ptypeName)
        throws UnableToCompleteException {
    try {/*from w  w w .  j  a  v a 2 s  .  c  om*/
        plogger.log(TreeLogger.DEBUG, "Start generating for " + ptypeName + ".");

        final JClassType classType = pcontext.getTypeOracle().getType(ptypeName);
        final TypeOracle typeOracle = pcontext.getTypeOracle();
        assert typeOracle != null;
        final JClassType reflectorType = this.findType(plogger, typeOracle, ptypeName);

        // here you would retrieve the metadata based on typeName for this class
        final SourceWriter src = this.getSourceWriter(classType, pcontext, plogger);

        // generator is called more then once in a build, with second call, we don't get
        // a writer and needn't generate the class once again
        if (src != null) {
            final Class<?>[] classes = this.getBeans(plogger, reflectorType);

            src.println("@Override");
            src.println("public final Object getProperty(final Object pbean, final String pname)"
                    + " throws NoSuchMethodException, ReflectiveOperationException {");

            src.println("  if (pbean == null) {");
            src.println("    throw new NoSuchMethodException(\"A null object has no getters\");");
            src.println("  }");
            src.println("  if (pname == null) {");
            src.println("    throw new NoSuchMethodException(\"No method to get property for null\");");
            src.println("  }");
            src.println(StringUtils.EMPTY);

            for (final Class<?> clazz : classes) {
                final String className = clazz.getName();
                plogger.log(TreeLogger.DEBUG, "Generating getter reflections for class " + className);

                // Describe the bean properties
                final PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(clazz);

                src.println("  if (pbean.getClass() == " + className + ".class) {");
                src.println("    switch (pname) {");

                // for all getters generate a case and return entry
                for (final PropertyDescriptor property : properties) {

                    final Method readMethod = property.getReadMethod();
                    final String name = property.getName();
                    if (readMethod == null) {
                        continue; // If the property cannot be read
                    }
                    plogger.log(TreeLogger.DEBUG, "Add getter for property " + name);

                    // Invoke the getter on the bean
                    src.println("      case \"" + name + "\":");
                    src.println("        return ((" + className + ") pbean)." + readMethod.getName() + "();");
                }

                src.println("      default:");
                src.println("        throw new NoSuchMethodException(\"Class " + className
                        + " has no getter for porperty \" + pname);");
                src.println("    }");
                src.println("  }");
            }
            src.println("  throw new ReflectiveOperationException(\"Class \" + "
                    + "pbean.getClass().getName() + \" is not reflected\");");
            src.println("}");

            plogger.log(TreeLogger.DEBUG, "End of generating reached");

            src.commit(plogger);
        }
        return this.getClassPackage(classType) + "." + this.getClassName(classType);
    } catch (final NotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.heren.turtle.server.utils.TransUtils.java

public Map<String, Object> beanToMap(Object obj) {
    Map<String, Object> resultMap = new HashMap<>();
    if (obj == null) {
        return null;
    }//from  w  w  w  .j ava 2  s.c om
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (!key.equals("class")) {
                Method getter = property.getReadMethod();
                Object value = getter.invoke(obj);
                resultMap.put(key, value);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("transBean2Map Error " + e);
    }
    return resultMap;
}