Example usage for java.beans BeanInfo getMethodDescriptors

List of usage examples for java.beans BeanInfo getMethodDescriptors

Introduction

In this page you can find the example usage for java.beans BeanInfo getMethodDescriptors.

Prototype

MethodDescriptor[] getMethodDescriptors();

Source Link

Document

Returns the method descriptors of the bean that define the externally visible methods supported by this bean.

Usage

From source file:org.psnively.scala.beans.ScalaBeanInfo.java

private static PropertyDescriptor[] initPropertyDescriptors(BeanInfo beanInfo) {
    Map<String, PropertyDescriptor> propertyDescriptors = new TreeMap<String, PropertyDescriptor>(
            new PropertyNameComparator());
    for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
        propertyDescriptors.put(pd.getName(), pd);
    }//from w  w  w. j av a 2  s  .co m

    for (MethodDescriptor md : beanInfo.getMethodDescriptors()) {
        Method method = md.getMethod();

        if (ReflectionUtils.isObjectMethod(method)) {
            continue;
        }
        if (isScalaSetter(method)) {
            addScalaSetter(propertyDescriptors, method);
        } else if (isScalaGetter(method)) {
            addScalaGetter(propertyDescriptors, method);
        }
    }
    return propertyDescriptors.values().toArray(new PropertyDescriptor[propertyDescriptors.size()]);
}

From source file:com.reignite.messaging.server.SpringInitializedDestination.java

@Override
public void afterPropertiesSet() throws Exception {
    if (target != null) {
        // introspect and cache methods
        BeanInfo info = Introspector.getBeanInfo(target.getClass(), Object.class);
        MethodDescriptor[] meths = info.getMethodDescriptors();
        for (MethodDescriptor meth : meths) {
            Method method = meth.getMethod();
            List<Method> methods = methodMap.get(method.getName());
            if (methods == null) {
                methods = new ArrayList<Method>();
                methodMap.put(method.getName(), methods);
            }/*from w  w  w.j ava2  s. c o m*/
            methods.add(method);
        }
    }
    initialized = true;
}

From source file:com.emc.ecs.sync.config.ConfigWrapper.java

public ConfigWrapper(Class<C> targetClass) {
    try {/*www.j  a v  a2  s . c o  m*/
        this.targetClass = targetClass;
        if (targetClass.isAnnotationPresent(StorageConfig.class))
            this.uriPrefix = targetClass.getAnnotation(StorageConfig.class).uriPrefix();
        if (targetClass.isAnnotationPresent(FilterConfig.class))
            this.cliName = targetClass.getAnnotation(FilterConfig.class).cliName();
        if (targetClass.isAnnotationPresent(Label.class))
            this.label = targetClass.getAnnotation(Label.class).value();
        if (targetClass.isAnnotationPresent(Documentation.class))
            this.documentation = targetClass.getAnnotation(Documentation.class).value();
        BeanInfo beanInfo = Introspector.getBeanInfo(targetClass);
        for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
            if (descriptor.getReadMethod().isAnnotationPresent(Option.class)) {
                propertyMap.put(descriptor.getName(), new ConfigPropertyWrapper(descriptor));
            }
        }
        for (MethodDescriptor descriptor : beanInfo.getMethodDescriptors()) {
            Method method = descriptor.getMethod();
            if (method.isAnnotationPresent(UriParser.class)) {
                if (method.getReturnType().equals(Void.TYPE) && method.getParameterTypes().length == 1
                        && method.getParameterTypes()[0].equals(String.class)) {
                    uriParser = method;
                } else {
                    log.warn("illegal signature for @UriParser method {}.{}", targetClass.getSimpleName(),
                            method.getName());
                }
            } else if (method.isAnnotationPresent(UriGenerator.class)) {
                if (method.getReturnType().equals(String.class) && method.getParameterTypes().length == 0) {
                    uriGenerator = method;
                } else {
                    log.warn("illegal signature for @UriGenerator method {}.{}", targetClass.getSimpleName(),
                            method.getName());
                }
            }
        }
        if (propertyMap.isEmpty())
            log.info("no @Option annotations found in {}", targetClass.getSimpleName());
    } catch (IntrospectionException e) {
        throw new RuntimeException(e);
    }
}

From source file:net.yasion.common.core.bean.wrapper.ExtendedBeanInfo.java

/**
 * Wrap the given {@link BeanInfo} instance; copy all its existing property descriptors locally, wrapping each in a custom {@link SimpleIndexedPropertyDescriptor indexed} or {@link SimplePropertyDescriptor non-indexed} {@code PropertyDescriptor} variant that bypasses default
 * JDK weak/soft reference management; then search through its method descriptors to find any non-void returning write methods and update or create the corresponding {@link PropertyDescriptor} for each one found.
 * // www.j  av  a2  s.co m
 * @param delegate
 *            the wrapped {@code BeanInfo}, which is never modified
 * @throws IntrospectionException
 *             if any problems occur creating and adding new property descriptors
 * @see #getPropertyDescriptors()
 */
public ExtendedBeanInfo(BeanInfo delegate) throws IntrospectionException {
    this.delegate = delegate;
    for (PropertyDescriptor pd : delegate.getPropertyDescriptors()) {
        try {
            this.propertyDescriptors.add(pd instanceof IndexedPropertyDescriptor
                    ? new SimpleIndexedPropertyDescriptor((IndexedPropertyDescriptor) pd)
                    : new SimplePropertyDescriptor(pd));
        } catch (IntrospectionException ex) {
            // Probably simply a method that wasn't meant to follow the JavaBeans pattern...
            if (logger.isDebugEnabled()) {
                logger.debug("Ignoring invalid bean property '" + pd.getName() + "': " + ex.getMessage());
            }
        }
    }
    MethodDescriptor[] methodDescriptors = delegate.getMethodDescriptors();
    if (methodDescriptors != null) {
        for (Method method : findCandidateWriteMethods(methodDescriptors)) {
            try {
                handleCandidateWriteMethod(method);
            } catch (IntrospectionException ex) {
                // We're only trying to find candidates, can easily ignore extra ones here...
                if (logger.isDebugEnabled()) {
                    logger.debug("Ignoring candidate write method [" + method + "]: " + ex.getMessage());
                }
            }
        }
    }
}

From source file:org.opendaylight.controller.cluster.datastore.DatastoreContextIntrospector.java

/**
 * Introspects the DataStoreProperties interface that is generated from the DataStoreProperties
 * yang grouping. We use the bean Introspector to find the types of all the properties defined
 * in the interface (this is the type returned from the getter method). For each type, we find
 * the appropriate constructor that we will use.
 *///ww w .  jav  a2s .co m
private static void introspectDataStoreProperties() throws IntrospectionException {
    BeanInfo beanInfo = Introspector.getBeanInfo(DataStoreProperties.class);
    for (PropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
        processDataStoreProperty(desc.getName(), desc.getPropertyType());
    }

    // Getter methods that return Boolean and start with "is" instead of "get" aren't recognized as
    // properties and thus aren't returned from getPropertyDescriptors. A getter starting with
    // "is" is only supported if it returns primitive boolean. So we'll check for these via
    // getMethodDescriptors.
    for (MethodDescriptor desc : beanInfo.getMethodDescriptors()) {
        String methodName = desc.getName();
        if (Boolean.class.equals(desc.getMethod().getReturnType()) && methodName.startsWith("is")) {
            String propertyName = WordUtils.uncapitalize(methodName.substring(2));
            processDataStoreProperty(propertyName, Boolean.class);
        }
    }
}

From source file:org.rhq.bindings.ScriptEngineFactory.java

/**
 * Goes through the methods of the object found in the <code>scriptEngine</code>'s ENGINE_SCOPE
 * and for each of them generates a top-level function that is called the same name and accepts the same
 * parameters.//from   w  w  w  .  ja v  a 2  s .  c o m
 * 
 * @param scriptEngine the script engine to generate the top-level functions in
 * @param bindingName the name of the object in the script engine to generate the functions from
 * 
 * @see ScriptEngineInitializer#generateIndirectionMethod(String, Method)
 * @see NoTopLevelIndirection
 */
public static void bindIndirectionMethods(ScriptEngine scriptEngine, String bindingName) {
    Object object = scriptEngine.get(bindingName);
    if (object == null) {
        LOG.debug("The script engine doesn't contain a binding called '" + bindingName
                + "'. No indirection functions will be generated.");
        return;
    }

    ScriptEngineInitializer initializer = getInitializer(scriptEngine.getFactory().getLanguageName());
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass(), Object.class);
        MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();

        Map<String, Set<Method>> overloadsPerMethodName = new HashMap<String, Set<Method>>();
        for (MethodDescriptor methodDescriptor : methodDescriptors) {
            Method method = methodDescriptor.getMethod();
            if (shouldIndirect(method)) {
                Set<Method> overloads = overloadsPerMethodName.get(method.getName());
                if (overloads == null) {
                    overloads = new HashSet<Method>();
                    overloadsPerMethodName.put(method.getName(), overloads);
                }
                overloads.add(method);
            }
        }

        for (Set<Method> overloads : overloadsPerMethodName.values()) {
            Set<String> methodDefs = initializer.generateIndirectionMethods(bindingName, overloads);
            for (String methodDef : methodDefs) {
                try {
                    scriptEngine.eval(methodDef);
                } catch (ScriptException e) {
                    LOG.warn("Unable to define global function declared as:\n" + methodDef, e);
                }
            }
        }
    } catch (IntrospectionException e) {
        LOG.debug("Could not inspect class " + object.getClass().getName()
                + ". No indirection methods for variable '" + bindingName + "' will be generated.", e);
    }
}

From source file:org.rhq.scripting.javascript.JavascriptCompletor.java

private Map<String, List<Object>> findJavaBeanContextMatches(Object baseObject, Class<?> baseObjectClass,
        String start) throws IntrospectionException {

    Map<String, List<Object>> found = new HashMap<String, List<Object>>();

    BeanInfo info = null;
    if (baseObjectClass.isInterface() || baseObjectClass.equals(Object.class)) {
        info = Introspector.getBeanInfo(baseObjectClass);
    } else {//from   w w  w . j a v  a2  s . com
        info = Introspector.getBeanInfo(baseObjectClass, Object.class);
    }

    Set<Method> methodsCovered = new HashSet<Method>();

    PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
    for (PropertyDescriptor desc : descriptors) {
        if (desc.getName().startsWith(start) && (!IGNORED_METHODS.contains(desc.getName()))) {

            List<Object> list = found.get(desc.getName());
            if (list == null) {
                list = new ArrayList<Object>();
                found.put(desc.getName(), list);
            }
            list.add(desc);

            methodsCovered.add(desc.getReadMethod());
            methodsCovered.add(desc.getWriteMethod());
        }
    }

    MethodDescriptor[] methods = info.getMethodDescriptors();
    for (MethodDescriptor desc : methods) {
        if (desc.getName().startsWith(start) && !methodsCovered.contains(desc.getMethod())
                && !desc.getName().startsWith("_d") && !IGNORED_METHODS.contains(desc.getName())) {

            Method m = desc.getMethod();

            List<Object> list = found.get(desc.getName());
            if (list == null) {
                list = new ArrayList<Object>();
                found.put(desc.getName(), list);
            }
            list.add(m);
        }
    }

    return found;
}

From source file:weka.classifiers.timeseries.eval.TSEvaluation.java

/**
 * Return the global info (if it exists) for the supplied forecaster.
 * //from w w w .  j  a v  a2  s. c o  m
 * @param forecaster the forecaster to get the global info for
 * @return the global info (synopsis) for the classifier
 * @throws Exception if there is a problem reflecting on the forecaster
 */
protected static String getGlobalInfo(TSForecaster forecaster) throws Exception {
    BeanInfo bi = Introspector.getBeanInfo(forecaster.getClass());
    MethodDescriptor[] methods;
    methods = bi.getMethodDescriptors();
    Object[] args = {};
    String result = "\nSynopsis for " + forecaster.getClass().getName() + ":\n\n";

    for (int i = 0; i < methods.length; i++) {
        String name = methods[i].getDisplayName();
        Method meth = methods[i].getMethod();
        if (name.equals("globalInfo")) {
            String globalInfo = (String) (meth.invoke(forecaster, args));
            result += globalInfo;
            break;
        }
    }

    return result;
}