Search in sources :

Example 71 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project qiuyj-code by qiuyuanjun.

the class BeanWrapperImpl method doGetPropertyValue.

@Override
protected Object doGetPropertyValue(String property) {
    PropertyDescriptor pd = getPropertyDescriptor(property);
    Object getterValue = null;
    if (Objects.isNull(pd)) {
        throw new ReflectionException("Can not found property: " + property + " in class: " + wrappedClass);
    } else if (pd instanceof NoGetterSetterPropertyDescriptor) {
        Field propertyField = ((NoGetterSetterPropertyDescriptor) pd).getPropertyField();
        ReflectionUtils.makeAccessible(propertyField);
        try {
            getterValue = propertyField.get(wrappedInstance);
        } catch (IllegalAccessException e) {
        // ignore
        }
    } else {
        Method readMethod = pd.getReadMethod();
        if (Objects.isNull(readMethod)) {
            if (fieldOperationSupport) {
                Field propertyField = ReflectionUtils.getDeclaredField(wrappedClass, property);
                ReflectionUtils.makeAccessible(propertyField);
                try {
                    getterValue = propertyField.get(wrappedInstance);
                } catch (IllegalAccessException e) {
                // ignore
                }
            } else {
                throw new ReflectionException("Property '" + property + "' is an reading invisible property.");
            }
        } else {
            ReflectionUtils.makeAccessible(readMethod);
            getterValue = ReflectionUtils.invokeMethod(wrappedInstance, readMethod);
        }
    }
    return getterValue;
}
Also used : ReflectionException(com.qiuyj.commons.bean.exception.ReflectionException) Field(java.lang.reflect.Field) PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method)

Example 72 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project liferay-ide by liferay.

the class NameFragment method getMethodCompletionProposals.

public ICompletionProposal[] getMethodCompletionProposals(int subOffset, int offset, Class parentClass, IResource file) {
    if (instanceOf(parentClass, String.class) || instanceOf(parentClass, Number.class) || instanceOf(parentClass, Date.class) || instanceOf(parentClass, Collection.class) || instanceOf(parentClass, List.class) || instanceOf(parentClass, Map.class))
        return null;
    String prefix = getContent().substring(1, subOffset);
    List proposals = new ArrayList();
    String pUpper = prefix.toUpperCase();
    try {
        BeanInfo bi = Introspector.getBeanInfo(parentClass);
        PropertyDescriptor[] pds = bi.getPropertyDescriptors();
        for (int i = 0; i < pds.length; i++) {
            PropertyDescriptor pd = pds[i];
            String propertyName = pd.getName();
            if (!propertyName.equals("class") && propertyName.toUpperCase().startsWith(pUpper)) {
                // $NON-NLS-1$
                proposals.add(new CompletionProposal(propertyName, offset - subOffset + 1, getContent().length() - 1, propertyName.length(), null, propertyName + " - " + pd.getReadMethod().getReturnType().getName(), null, // $NON-NLS-1$
                null));
            }
        }
        for (int i = 0; i < parentClass.getMethods().length; i++) {
            Method m = parentClass.getMethods()[i];
            String mName = m.getName();
            if (m.getParameterTypes().length > 0 && mName.startsWith("get") && mName.toUpperCase().startsWith(pUpper)) {
                // $NON-NLS-1$
                StringBuffer display = new StringBuffer();
                display.append(mName);
                // $NON-NLS-1$
                display.append("(");
                for (int j = 0; j < m.getParameterTypes().length; j++) {
                    // $NON-NLS-1$
                    if (j > 0)
                        display.append(", ");
                    display.append(m.getParameterTypes()[j].getName());
                }
                // $NON-NLS-1$
                display.append(")");
                // $NON-NLS-1$
                String actual = mName + "()";
                int tLength = actual.length();
                if (m.getParameterTypes().length > 0)
                    tLength--;
                proposals.add(new CompletionProposal(actual, offset - subOffset + 1, getContent().length() - 1, tLength, null, display.toString() + " - " + m.getReturnType().getName(), null, // $NON-NLS-1$
                null));
            }
        }
        return completionProposals(proposals);
    } catch (IntrospectionException e) {
        return null;
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) BeanInfo(java.beans.BeanInfo) ArrayList(java.util.ArrayList) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 73 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project cu-kfs by CU-CommunityApps.

the class AssetSeparatePaymentDistributor method applyRatioToPaymentAmounts.

/**
 * Utility method which can take one payment and distribute its amount by ratio to the target payments
 *
 * @param source  Source Payment
 * @param targets Target Payment
 * @param ratios  Ratio to be applied for each target
 */
private void applyRatioToPaymentAmounts(AssetPayment source, AssetPayment[] targets, double[] ratios) {
    try {
        for (PropertyDescriptor propertyDescriptor : assetPaymentProperties) {
            Method readMethod = propertyDescriptor.getReadMethod();
            if (readMethod != null && propertyDescriptor.getPropertyType() != null && KualiDecimal.class.isAssignableFrom(propertyDescriptor.getPropertyType())) {
                KualiDecimal amount = (KualiDecimal) readMethod.invoke(source);
                if (amount != null && amount.isNonZero()) {
                    KualiDecimal[] ratioAmounts = KualiDecimalUtils.allocateByRatio(amount, ratios);
                    Method writeMethod = propertyDescriptor.getWriteMethod();
                    if (writeMethod != null) {
                        for (int i = 0; i < ratioAmounts.length; i++) {
                            writeMethod.invoke(targets[i], ratioAmounts[i]);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) KualiDecimal(org.kuali.rice.core.api.util.type.KualiDecimal) Method(java.lang.reflect.Method)

Example 74 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project knox by apache.

the class XmlUrlRewriteRulesExporter method createElement.

private Element createElement(Document document, String name, Object bean) throws IntrospectionException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
    Element element = document.createElement(name);
    BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(), Object.class);
    for (PropertyDescriptor propInfo : beanInfo.getPropertyDescriptors()) {
        String propName = propInfo.getName();
        if (propInfo.getReadMethod() != null && String.class.isAssignableFrom(propInfo.getPropertyType())) {
            String propValue = BeanUtils.getProperty(bean, propName);
            if (propValue != null && !propValue.isEmpty()) {
                // Doing it the hard way to avoid having the &'s in the query string escaped at &amp;
                Attr attr = document.createAttribute(propName);
                attr.setValue(propValue);
                element.setAttributeNode(attr);
            // element.setAttribute( propName, propValue );
            }
        }
    }
    return element;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Element(org.w3c.dom.Element) BeanInfo(java.beans.BeanInfo) Attr(org.w3c.dom.Attr)

Example 75 with PropertyDescriptor

use of java.beans.PropertyDescriptor in project fabric8 by jboss-fuse.

the class JolokiaFabricController method getPropertyDescriptors.

public static Map<String, PropertyDescriptor> getPropertyDescriptors(Class<?> aClass) throws IntrospectionException {
    Map<String, PropertyDescriptor> answer = new HashMap<>();
    if (aClass != null) {
        BeanInfo beanInfo = java.beans.Introspector.getBeanInfo(aClass);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            // ignore the class property
            String name = propertyDescriptor.getName();
            if (name.equals("class")) {
                continue;
            }
            answer.put(name, propertyDescriptor);
        }
    }
    return answer;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) BeanInfo(java.beans.BeanInfo)

Aggregations

PropertyDescriptor (java.beans.PropertyDescriptor)836 Method (java.lang.reflect.Method)396 BeanInfo (java.beans.BeanInfo)339 IntrospectionException (java.beans.IntrospectionException)188 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)171 SimpleBeanInfo (java.beans.SimpleBeanInfo)142 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)141 InvocationTargetException (java.lang.reflect.InvocationTargetException)108 ArrayList (java.util.ArrayList)90 HashMap (java.util.HashMap)66 Field (java.lang.reflect.Field)59 Map (java.util.Map)52 Test (org.junit.Test)39 IOException (java.io.IOException)34 MockJavaBean (org.apache.harmony.beans.tests.support.mock.MockJavaBean)34 List (java.util.List)33 HashSet (java.util.HashSet)21 Type (java.lang.reflect.Type)20 Set (java.util.Set)20 LinkedHashMap (java.util.LinkedHashMap)19