Search in sources :

Example 86 with IntrospectionException

use of java.beans.IntrospectionException in project spring-framework by spring-projects.

the class PropertyDescriptorUtils method findPropertyType.

/**
	 * See {@link java.beans.PropertyDescriptor#findPropertyType}.
	 */
public static Class<?> findPropertyType(Method readMethod, Method writeMethod) throws IntrospectionException {
    Class<?> propertyType = null;
    if (readMethod != null) {
        Class<?>[] params = readMethod.getParameterTypes();
        if (params.length != 0) {
            throw new IntrospectionException("Bad read method arg count: " + readMethod);
        }
        propertyType = readMethod.getReturnType();
        if (propertyType == Void.TYPE) {
            throw new IntrospectionException("Read method returns void: " + readMethod);
        }
    }
    if (writeMethod != null) {
        Class<?>[] params = writeMethod.getParameterTypes();
        if (params.length != 1) {
            throw new IntrospectionException("Bad write method arg count: " + writeMethod);
        }
        if (propertyType != null) {
            if (propertyType.isAssignableFrom(params[0])) {
                // Write method's property type potentially more specific
                propertyType = params[0];
            } else if (params[0].isAssignableFrom(propertyType)) {
            // Proceed with read method's property type
            } else {
                throw new IntrospectionException("Type mismatch between read and write methods: " + readMethod + " - " + writeMethod);
            }
        } else {
            propertyType = params[0];
        }
    }
    return propertyType;
}
Also used : IntrospectionException(java.beans.IntrospectionException)

Example 87 with IntrospectionException

use of java.beans.IntrospectionException in project liquibase by liquibase.

the class FluentPropertyBeanIntrospector method introspect.

@Override
public void introspect(IntrospectionContext context) throws IntrospectionException {
    for (Method method : context.getTargetClass().getMethods()) {
        try {
            Class<?>[] argTypes = method.getParameterTypes();
            int argCount = argTypes.length;
            if (argCount == 1 && method.getName().startsWith("set")) {
                String propertyName = Introspector.decapitalize(method.getName().substring(3));
                if (!propertyName.equals("class")) {
                    PropertyDescriptor pd = context.getDescriptor(propertyName);
                    boolean setWriteMethod = false;
                    if (pd == null) {
                        pd = new PropertyDescriptor(propertyName, null, method);
                        context.addDescriptor(pd);
                        setWriteMethod = true;
                    } else if (pd.getWriteMethod() == null && pd.getReadMethod() != null && pd.getReadMethod().getReturnType() == argTypes[0]) {
                        pd.setWriteMethod(method);
                        setWriteMethod = true;
                    }
                    if (setWriteMethod) {
                        for (Class<?> type : method.getExceptionTypes()) {
                            if (type == PropertyVetoException.class) {
                                pd.setConstrained(true);
                                break;
                            }
                        }
                    }
                }
            }
        } catch (IntrospectionException ignored) {
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method)

Example 88 with IntrospectionException

use of java.beans.IntrospectionException in project goci by EBISPOT.

the class EmbeddableDocumentTest method testTraitIntrospection.

@Test
public void testTraitIntrospection() {
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(traitDoc.getClass());
        for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
            System.out.println("Display name: " + pd.getDisplayName());
            System.out.println("Name: " + pd.getName());
            System.out.println("Read method: " + pd.getReadMethod());
            System.out.println("\t" + pd);
        }
    } catch (IntrospectionException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Test(org.junit.Test)

Example 89 with IntrospectionException

use of java.beans.IntrospectionException in project intellij-community by JetBrains.

the class ListenerNavigateButton method buildNavigateActionGroup.

private static DefaultActionGroup buildNavigateActionGroup(RadComponent component, final PsiField boundField) {
    final DefaultActionGroup actionGroup = new DefaultActionGroup();
    final EventSetDescriptor[] eventSetDescriptors;
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(component.getComponentClass());
        eventSetDescriptors = beanInfo.getEventSetDescriptors();
    } catch (IntrospectionException e) {
        LOG.error(e);
        return null;
    }
    PsiFile boundClassFile = boundField.getContainingFile();
    if (boundClassFile == null) {
        return null;
    }
    final LocalSearchScope scope = new LocalSearchScope(boundClassFile);
    ReferencesSearch.search(boundField, scope).forEach(ref -> {
        final PsiElement element = ref.getElement();
        if (element.getParent() instanceof PsiReferenceExpression) {
            PsiReferenceExpression refExpr = (PsiReferenceExpression) element.getParent();
            if (refExpr.getParent() instanceof PsiMethodCallExpression) {
                PsiMethodCallExpression methodCall = (PsiMethodCallExpression) refExpr.getParent();
                final PsiElement psiElement = refExpr.resolve();
                if (psiElement instanceof PsiMethod) {
                    PsiMethod method = (PsiMethod) psiElement;
                    for (EventSetDescriptor eventSetDescriptor : eventSetDescriptors) {
                        if (Comparing.equal(eventSetDescriptor.getAddListenerMethod().getName(), method.getName())) {
                            final String eventName = eventSetDescriptor.getName();
                            final PsiExpression[] args = methodCall.getArgumentList().getExpressions();
                            if (args.length > 0) {
                                addListenerRef(actionGroup, eventName, args[0]);
                            }
                        }
                    }
                }
            }
        }
        return true;
    });
    return actionGroup;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) DefaultActionGroup(com.intellij.openapi.actionSystem.DefaultActionGroup) EventSetDescriptor(java.beans.EventSetDescriptor)

Example 90 with IntrospectionException

use of java.beans.IntrospectionException in project Activiti by Activiti.

the class BeanELResolver method getFeatureDescriptors.

/**
	 * If the base object is not null, returns an Iterator containing the set of JavaBeans
	 * properties available on the given object. Otherwise, returns null. The Iterator returned must
	 * contain zero or more instances of java.beans.FeatureDescriptor. Each info object contains
	 * information about a property in the bean, as obtained by calling the
	 * BeanInfo.getPropertyDescriptors method. The FeatureDescriptor is initialized using the same
	 * fields as are present in the PropertyDescriptor, with the additional required named
	 * attributes "type" and "resolvableAtDesignTime" set as follows:
	 * <ul>
	 * <li>{@link ELResolver#TYPE} - The runtime type of the property, from
	 * PropertyDescriptor.getPropertyType().</li>
	 * <li>{@link ELResolver#RESOLVABLE_AT_DESIGN_TIME} - true.</li>
	 * </ul>
	 * 
	 * @param context
	 *            The context of this evaluation.
	 * @param base
	 *            The bean to analyze.
	 * @return An Iterator containing zero or more FeatureDescriptor objects, each representing a
	 *         property on this bean, or null if the base object is null.
	 */
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
    if (isResolvable(base)) {
        final PropertyDescriptor[] properties;
        try {
            properties = Introspector.getBeanInfo(base.getClass()).getPropertyDescriptors();
        } catch (IntrospectionException e) {
            return Collections.<FeatureDescriptor>emptyList().iterator();
        }
        return new Iterator<FeatureDescriptor>() {

            int next = 0;

            public boolean hasNext() {
                return properties != null && next < properties.length;
            }

            public FeatureDescriptor next() {
                PropertyDescriptor property = properties[next++];
                FeatureDescriptor feature = new FeatureDescriptor();
                feature.setDisplayName(property.getDisplayName());
                feature.setName(property.getName());
                feature.setShortDescription(property.getShortDescription());
                feature.setExpert(property.isExpert());
                feature.setHidden(property.isHidden());
                feature.setPreferred(property.isPreferred());
                feature.setValue(TYPE, property.getPropertyType());
                feature.setValue(RESOLVABLE_AT_DESIGN_TIME, true);
                return feature;
            }

            public void remove() {
                throw new UnsupportedOperationException("cannot remove");
            }
        };
    }
    return null;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) FeatureDescriptor(java.beans.FeatureDescriptor) IntrospectionException(java.beans.IntrospectionException) Iterator(java.util.Iterator)

Aggregations

IntrospectionException (java.beans.IntrospectionException)111 PropertyDescriptor (java.beans.PropertyDescriptor)65 Method (java.lang.reflect.Method)46 BeanInfo (java.beans.BeanInfo)44 MockJavaBean (org.apache.harmony.beans.tests.support.mock.MockJavaBean)24 InvocationTargetException (java.lang.reflect.InvocationTargetException)18 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)17 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)10 Field (java.lang.reflect.Field)9 EventSetDescriptor (java.beans.EventSetDescriptor)7 List (java.util.List)6 Map (java.util.Map)6 MessageFormat (java.text.MessageFormat)5 UUID (java.util.UUID)5 Assert (org.springframework.util.Assert)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 Optional (java.util.Optional)4 RecursiveChildrenListManager (cern.gp.explorer.test.helpers.RecursiveChildrenListManager)3 SimpleDemoBean (cern.gp.explorer.test.helpers.SimpleDemoBean)3