Example usage for java.lang Class getMethods

List of usage examples for java.lang Class getMethods

Introduction

In this page you can find the example usage for java.lang Class getMethods.

Prototype

@CallerSensitive
public Method[] getMethods() throws SecurityException 

Source Link

Document

Returns an array containing Method objects reflecting all the public methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.

Usage

From source file:com.jaliansystems.activeMQLite.impl.ObjectRepository.java

private Method findMethod(Object object, String methodName, Object[] args) {
    Class<? extends Object> klass = object.getClass();
    Method[] methods = klass.getMethods();
    for (Method method : methods) {
        if (methodName.equals(method.getName()) && paramMatches(method, args))
            return method;
    }/*from  www.j a va  2  s .  c  o m*/
    return null;
}

From source file:org.bytesoft.bytetcc.supports.dubbo.validator.ReferenceConfigValidator.java

public void validate() throws BeansException {
    MutablePropertyValues mpv = this.beanDefinition.getPropertyValues();
    PropertyValue retries = mpv.getPropertyValue("retries");
    PropertyValue loadbalance = mpv.getPropertyValue("loadbalance");
    PropertyValue cluster = mpv.getPropertyValue("cluster");
    PropertyValue filter = mpv.getPropertyValue("filter");

    if (retries == null || retries.getValue() == null || "0".equals(retries.getValue()) == false) {
        throw new FatalBeanException(
                String.format("The value of attr 'retries'(beanId= %s) should be '0'.", this.beanName));
    } else if (loadbalance == null || loadbalance.getValue() == null
            || "compensable".equals(loadbalance.getValue()) == false) {
        throw new FatalBeanException(String
                .format("The value of attr 'loadbalance'(beanId= %s) should be 'compensable'.", this.beanName));
    } else if (cluster == null || cluster.getValue() == null
            || "failfast".equals(cluster.getValue()) == false) {
        throw new FatalBeanException(String
                .format("The value of attribute 'cluster' (beanId= %s) must be 'failfast'.", this.beanName));
    } else if (filter == null || filter.getValue() == null
            || "compensable".equals(filter.getValue()) == false) {
        throw new FatalBeanException(String
                .format("The value of attr 'filter'(beanId= %s) should be 'compensable'.", this.beanName));
    }//from  w  w  w  .j a v  a  2 s .c  o  m

    PropertyValue pv = mpv.getPropertyValue("interface");
    String clazzName = String.valueOf(pv.getValue());
    ClassLoader cl = Thread.currentThread().getContextClassLoader();

    Class<?> clazz = null;
    try {
        clazz = cl.loadClass(clazzName);
    } catch (Exception ex) {
        throw new FatalBeanException(String.format("Cannot load class %s.", clazzName));
    }

    Method[] methodArray = clazz.getMethods();
    for (int i = 0; i < methodArray.length; i++) {
        Method method = methodArray[i];
        boolean declared = false;
        Class<?>[] exceptionTypeArray = method.getExceptionTypes();
        for (int j = 0; j < exceptionTypeArray.length; j++) {
            Class<?> exceptionType = exceptionTypeArray[j];
            if (RemotingException.class.isAssignableFrom(exceptionType)) {
                declared = true;
                break;
            }
        }

        if (declared == false) {
            // throw new FatalBeanException(String.format(
            // "The remote call method(%s) must be declared to throw a remote exception:
            // org.bytesoft.compensable.RemotingException!",
            // method));
            logger.warn("The remote call method({}) should be declared to throw a remote exception: {}!",
                    method, RemotingException.class.getName());
        }

    }

}

From source file:com.justcloud.osgifier.servlet.OsgifierServlet.java

private Method findRestMethod(RESTMethod method, Class<?> clazz, String url) {
    for (Method m : clazz.getMethods()) {
        REST rest = m.getAnnotation(REST.class);
        if (rest != null && rest.url().equals(url) && rest.method() == method) {
            return m;
        }/*from  w w w . ja  v  a 2  s.c o m*/
    }
    return null;
}

From source file:com.panet.imeta.core.config.KettleConfig.java

private Digester createDigester() {
    Digester digester = new Digester();
    digester.addObjectCreate(KETTLE_CONFIG, TempConfig.class);
    digester.addBeanPropertySetter(KETTLE_CONFIG_CLASS, "clazz");
    digester.addSetProperties(KETTLE_CONFIG, "id", "id");
    digester.addRule(KETTLE_CONFIG_PROPERTY, new SetPropertiesRule() {
        @Override// w  w w .  ja v a  2s  . c  o  m
        public void begin(String name, String namespace, Attributes attrs) throws Exception {
            ((TempConfig) digester.peek()).parms.put(attrs.getValue("name"), attrs.getValue("value"));
        }
    });

    digester.addRule(KETTLE_CONFIG, new SetNextRule("") {
        @SuppressWarnings("unchecked")
        public void end(String nameSpace, String name) throws Exception {
            TempConfig cfg = (TempConfig) digester.peek();

            // do the conversion here.
            Class<? extends ConfigManager> cfclass = Class.forName(cfg.clazz).asSubclass(ConfigManager.class);

            ConfigManager parms = cfclass.newInstance();
            // method injection
            inject(cfclass.getMethods(), cfg, parms);
            // field injection
            inject(cfclass.getDeclaredFields(), cfg, parms);
            KettleConfig.this.configs.put(cfg.id, parms);
        }
    });

    return digester;
}

From source file:edu.txstate.dmlab.clusteringwiki.util.StaticInitializerBeanFactoryPostProcessor.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory)
        throws BeansException {
    for (Iterator<String> classIterator = classes.keySet().iterator(); classIterator.hasNext();) {
        String className = (String) classIterator.next();
        //System.out.println("Class " + className + ":");
        Map<String, Object> vars = classes.get(className);
        Class<?> c = null;
        try {//from www  .j  a v a 2  s  . c om
            c = Class.forName(className);
        } catch (ClassNotFoundException e) {
            throw new StaticInitializerBeansException("Class not found for " + className, e);
        }
        Method[] methods = c.getMethods();
        for (Iterator<String> fieldIterator = vars.keySet().iterator(); fieldIterator.hasNext();) {
            String fieldName = (String) fieldIterator.next();
            Object value = vars.get(fieldName);
            Method method = findStaticSetter(methods, fieldName);
            if (method == null) {
                throw new StaticInitializerBeansException(
                        "No static setter method found for class " + className + ", field " + fieldName);
            }
            //System.out.println("\tFound method " + method.getName() + " for field " + fieldName + ", value " + value);
            try {
                method.invoke(null, new Object[] { value });
            } catch (Exception e) {
                throw new StaticInitializerBeansException("Invocation of method " + method.getName()
                        + " on class " + className + " with value " + value + " failed.", e);
            }
        }
    }
}

From source file:io.github.moosbusch.lumpi.application.spi.AbstractLumpiApplicationContext.java

protected String getBeanCreationMethodName(Class<?> type) {
    Collection<Class<?>> configClasses = getApplication().getBeanConfigurationClasses();

    if (configClasses != null) {
        if (!configClasses.isEmpty()) {
            for (Class<?> configClass : configClasses) {
                Method[] methods = configClass.getMethods();

                for (Method method : methods) {
                    Class<?> returnType = method.getReturnType();
                    if (returnType == type) {
                        return method.getName();
                    }/*from   w w w.  j av a2s  .  c om*/
                }
            }
        }
    }

    throw new BeanCreationException("No qualifying bean of type '" + type.getName() + "' is defined!");
}

From source file:com.utest.dao.AuditTrailInterceptor.java

@SuppressWarnings("unchecked")
private Field[] getAllFields(final Class objectClass) {

    final List<Field> fields = new ArrayList<Field>();
    for (final Method method : objectClass.getMethods()) {
        if (method.isAnnotationPresent(Audit.class)) {
            try {
                final Field methodField = objectClass.getDeclaredField(
                        method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4));
                if (methodField != null) {
                    fields.add(methodField);
                }/*from  w w  w  .j  a v a2  s. com*/
            } catch (final Exception e) {
            }
        }
    }
    for (final Field field : objectClass.getDeclaredFields()) {
        if (field.isAnnotationPresent(Audit.class)) {
            fields.add(field);
        }
    }

    return fields.toArray(new Field[fields.size()]);

}

From source file:de.iteratec.iteraplan.MockTestDataFactory.java

public <T> T generateTestObject(Class<T> clazz) {
    T obj = null;//from  w w w .ja va2s. c  o m
    try {
        obj = clazz.newInstance();

        for (Method m : clazz.getMethods()) {
            if (m.getName().startsWith("set") && !m.getName().endsWith("Id")
                    && (m.getParameterTypes().length == 1)) {
                int rn = random.nextInt() * 10000;
                Class<?> param = m.getParameterTypes()[0];
                String nm = param.getSimpleName();
                if (("String").equals(nm)) {
                    m.invoke(obj,
                            new Object[] {
                                    clazz.getSimpleName() + "." + m.getName().substring(3, 4).toLowerCase()
                                            + m.getName().substring(4) + "." + rn });
                } else if (("long").equals(nm) || ("int").equals(nm) || ("double").equals(nm)) {
                    m.invoke(obj, new Object[] { Integer.valueOf(rn) });
                } else if (("Long").equals(nm)) {
                    m.invoke(obj, new Object[] { Long.valueOf(rn) });
                } else if (("Integer").equals(nm)) {
                    m.invoke(obj, new Object[] { Integer.valueOf(rn) });
                } else if (("boolean").equals(nm)) {
                    m.invoke(obj, new Object[] { Boolean.TRUE });
                } else if (("Boolean").equals(nm)) {
                    m.invoke(obj, new Object[] { Boolean.TRUE });
                } else if (("Locale").equals(nm)) {
                    m.invoke(obj, new Object[] { Locale.GERMAN });
                } else if (("RuntimePeriod").equals(nm)) {
                    m.invoke(obj, new Object[] { new RuntimePeriod(new Date(), new Date()) });
                }
            }
        }
    } catch (InvocationTargetException e) {
        LOGGER.error(e);
    } catch (IllegalArgumentException e) {
        LOGGER.error(e);
    } catch (IllegalAccessException e) {
        LOGGER.error(e);
    } catch (InstantiationException e) {
        LOGGER.error(e);
    }
    return obj;
}

From source file:de.bund.bva.pliscommon.serviceapi.core.serviceimpl.ServiceExceptionFassade.java

/**
 * {@inheritDoc}/*from   ww w. ja v  a2 s . c  o  m*/
 */
@Override
public void validateConfiguration(Class<?> remoteBeanInterface, Object target) {

    Class<?> targetClass = AopUtils.getTargetClass(target);

    for (Method remoteBeanMethod : remoteBeanInterface.getMethods()) {
        Class<? extends PlisTechnicalToException> genericTechnicalToExceptionClass = this.exceptionMappingSource
                .getGenericTechnicalToException(remoteBeanMethod);
        if (genericTechnicalToExceptionClass == null) {
            throw new IllegalStateException("Fehler in der statischen Konfiguration der Exception-Fassade fr "
                    + remoteBeanInterface.getName() + ": Keine generische TO-Exception definiert");
        }
        if (!ArrayUtils.contains(remoteBeanMethod.getExceptionTypes(), genericTechnicalToExceptionClass)) {
            throw new IllegalStateException("Fehler in der statischen Konfiguration der Exception-Fassade fr "
                    + getMethodSignatureString(remoteBeanMethod) + ": Die generische TO-Exception "
                    + genericTechnicalToExceptionClass.getSimpleName()
                    + " ist nicht im RemoteBean-Interface deklariert");
        }

        Method coreMethod = this.methodMappingSource.getTargetMethod(remoteBeanMethod, targetClass);

        for (Class<?> exceptionClass : coreMethod.getExceptionTypes()) {
            if (PlisException.class.isAssignableFrom(exceptionClass)) {
                @SuppressWarnings("unchecked")
                Class<? extends PlisException> plisExceptionClass = (Class<? extends PlisException>) exceptionClass;
                Class<? extends PlisToException> plisToExceptionClass = this.exceptionMappingSource
                        .getToExceptionClass(remoteBeanMethod, plisExceptionClass);
                if (plisToExceptionClass == null) {
                    throw new IllegalStateException(
                            "Fehler in der statischen Konfiguration der Exception-Fassade fr "
                                    + getMethodSignatureString(remoteBeanMethod)
                                    + ": Keine TO-Exception fr AWK-Exception "
                                    + plisExceptionClass.getSimpleName() + " definiert");
                }
                if (!ArrayUtils.contains(remoteBeanMethod.getExceptionTypes(), plisToExceptionClass)) {
                    throw new IllegalStateException(
                            "Fehler in der statischen Konfiguration der Exception-Fassade fr "
                                    + getMethodSignatureString(remoteBeanMethod) + ": Die TO-Exception "
                                    + plisToExceptionClass.getSimpleName()
                                    + " ist nicht im RemoteBean-Interface deklariert");
                }
            }
        }
    }
}

From source file:com.github.dactiv.fear.service.RemoteApiCaller.java

/**
 * ??//from ww  w . java  2  s  .c  o  m
 *
 * @param targetClass  class
 * @param method      ??
 * @param paramTypes  ?
 *
 * @return 
 */
private Method findParamMethod(Class<?> targetClass, String method, Class<?>[] paramTypes) {

    // ?
    Method[] methods = (targetClass.isInterface() ? targetClass.getMethods()
            : ReflectionUtils.getAllDeclaredMethods(targetClass));

    // ?
    for (Method m : methods) {
        // ??
        Class<?>[] methodParamTypes = m.getParameterTypes();
        // ??????
        if (m.getName().equals(method) && methodParamTypes.length == paramTypes.length) {
            // ??
            Boolean flag = Boolean.TRUE;
            // ??flag  true
            for (int i = 0; i < methodParamTypes.length; i++) {

                Class<?> paramTarget = paramTypes[i];
                Class<?> paramSource = methodParamTypes[i];

                if (paramTarget != null && paramTarget != paramSource
                        && !paramSource.isAssignableFrom(paramTarget)) {
                    flag = Boolean.FALSE;
                }

            }

            if (flag) {
                cacheService(targetClass, m, paramTypes);
                return m;
            }

        }

    }

    return null;
}