Search in sources :

Example 46 with BeanInfo

use of java.beans.BeanInfo 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 47 with BeanInfo

use of java.beans.BeanInfo 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 48 with BeanInfo

use of java.beans.BeanInfo 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)

Example 49 with BeanInfo

use of java.beans.BeanInfo in project jbosstools-hibernate by jbosstools.

the class GenericPropertySource method buildPropertyDescriptors.

private IPropertyDescriptor[] buildPropertyDescriptors() {
    if (real == null)
        return new IPropertyDescriptor[0];
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(real.getClass(), Object.class);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        IPropertyDescriptor[] result = new IPropertyDescriptor[propertyDescriptors.length];
        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            result[i] = new BeanPropertyDescriptor(descriptor);
        }
        return result;
    } catch (IntrospectionException e) {
        return new IPropertyDescriptor[0];
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor)

Example 50 with BeanInfo

use of java.beans.BeanInfo in project synergic-developing by zeemood.

the class JSONWriter method bean.

private void bean(Object object) {
    add("{");
    BeanInfo info;
    boolean addedSomething = false;
    try {
        info = Introspector.getBeanInfo(object.getClass());
        PropertyDescriptor[] props = info.getPropertyDescriptors();
        for (int i = 0; i < props.length; ++i) {
            PropertyDescriptor prop = props[i];
            String name = prop.getName();
            Method accessor = prop.getReadMethod();
            if ((emitClassName || !"class".equals(name)) && accessor != null) {
                if (!accessor.isAccessible())
                    accessor.setAccessible(true);
                Object value = accessor.invoke(object, (Object[]) null);
                if (value == null)
                    continue;
                if (addedSomething)
                    add(',');
                add(name, value);
                addedSomething = true;
            }
        }
        Field[] ff = object.getClass().getFields();
        for (int i = 0; i < ff.length; ++i) {
            Field field = ff[i];
            Object value = field.get(object);
            if (value == null)
                continue;
            if (addedSomething)
                add(',');
            add(field.getName(), value);
            addedSomething = true;
        }
    } catch (IllegalAccessException iae) {
        iae.printStackTrace();
    } catch (InvocationTargetException ite) {
        ite.getCause().printStackTrace();
        ite.printStackTrace();
    } catch (IntrospectionException ie) {
        ie.printStackTrace();
    }
    add("}");
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ApiField(com.alipay.api.internal.mapping.ApiField) Field(java.lang.reflect.Field) ApiListField(com.alipay.api.internal.mapping.ApiListField)

Aggregations

BeanInfo (java.beans.BeanInfo)420 PropertyDescriptor (java.beans.PropertyDescriptor)328 Method (java.lang.reflect.Method)217 SimpleBeanInfo (java.beans.SimpleBeanInfo)167 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)161 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)148 IntrospectionException (java.beans.IntrospectionException)115 InvocationTargetException (java.lang.reflect.InvocationTargetException)38 HashMap (java.util.HashMap)33 Test (org.junit.jupiter.api.Test)33 ArrayList (java.util.ArrayList)30 Field (java.lang.reflect.Field)17 Map (java.util.Map)17 Test (org.junit.Test)13 EventSetDescriptor (java.beans.EventSetDescriptor)12 MethodDescriptor (java.beans.MethodDescriptor)12 List (java.util.List)11 BeanDescriptor (java.beans.BeanDescriptor)10 HashSet (java.util.HashSet)8 Introspector (java.beans.Introspector)7