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.qmetry.qaf.automation.step.JavaStepFinder.java

private static Set<Method> getAllMethodsWithAnnotation(Collection<Class<?>> classes,
        Class<? extends Annotation> annotation) {

    Set<Method> methods = new HashSet<Method>();
    for (Class<?> cls : classes) {

        if (cls.isInterface() || Modifier.isAbstract(cls.getModifiers()))
            continue;

        boolean isStepProvider = cls.isAnnotationPresent(QAFTestStepProvider.class);

        for (Method method : cls.getMethods()) {
            if (isStepProvider || ClassUtil.hasAnnotation(method, annotation)) {
                methods.add(method);//from   w  w w. j  a va2  s. co  m
            }
        }

    }

    return methods;
}

From source file:com.aw.support.reflection.MethodInvoker.java

public static boolean existsMethod(Object target, String method) {
    Class cls = target.getClass();
    Method[] methods = cls.getMethods();
    boolean existMethod = false;
    for (int i = 0; i < methods.length; i++) {
        if (methods[i].toString().contains(method)) {
            existMethod = true;//from ww w.j  a v  a 2 s .c  o  m
            break;
        }
    }
    return existMethod;

}

From source file:com.aw.support.reflection.MethodInvoker.java

public static boolean validateIfClassImplMethod(Object target, String method) {
    String reflexionMethod = target.getClass().getName() + "." + method + "()";
    logger.info("searching method " + reflexionMethod);
    Class cls = target.getClass();
    Method[] methods = cls.getMethods();
    boolean existMethod = false;
    for (int i = 0; i < methods.length; i++) {
        if (methods[i].toString().contains(reflexionMethod)) {
            existMethod = true;/*from www .j  a v  a  2s.  c o m*/
            break;
        }
    }
    return existMethod && reflexionMethod.contains("$");
}

From source file:com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBReflector.java

static List<Method> findRelevantGetters(Class<?> clazz) {
    List<Method> relevantGetters = new LinkedList<Method>();
    for (Method m : clazz.getMethods()) {
        if (isRelevantGetter(m)) {
            relevantGetters.add(m);/*  www .j a v a2s .c o  m*/
        }
    }
    return relevantGetters;
}

From source file:com.aw.support.reflection.MethodInvoker.java

/**
 * Invoke specific method on the target and returns the value that this method returns
 *
 * @param target/*from  w w  w .j a  v  a 2 s. co  m*/
 * @return
 */
public static Object invokeMethodWithPrefix(Object target, String prefix) throws Throwable {
    List results = new ArrayList();
    try {
        Class cls = target.getClass();
        Method[] methods = cls.getMethods();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().startsWith(prefix)) {
                results.add(methods[i].invoke(target, null));
            }
        }
    } catch (Throwable e) {
        e.printStackTrace();
        throw e;
    }
    return results;
}

From source file:org.fusesource.meshkeeper.util.internal.IntrospectionSupport.java

public static boolean getProperties(Object target, Map<String, Object> props, String optionPrefix) {

    boolean rc = false;
    if (target == null) {
        throw new IllegalArgumentException("target was null.");
    }/*  ww  w.  j  a va 2  s .  c  o m*/
    if (props == null) {
        throw new IllegalArgumentException("props was null.");
    }

    if (optionPrefix == null) {
        optionPrefix = "";
    }

    Class<?> clazz = target.getClass();
    Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        String name = method.getName();
        Class<?> type = method.getReturnType();
        Class<?> params[] = method.getParameterTypes();
        if ((name.startsWith("is") || name.startsWith("get")) && params.length == 0 && type != null
                && isSettableType(type)) {

            try {

                Object value = method.invoke(target, new Object[] {});
                if (value == null) {
                    continue;
                }

                String strValue = convertToString(value, type);
                if (strValue == null) {
                    continue;
                }
                if (name.startsWith("get")) {
                    name = name.substring(3, 4).toLowerCase() + name.substring(4);
                } else {
                    name = name.substring(2, 3).toLowerCase() + name.substring(3);
                }
                props.put(optionPrefix + name, strValue);
                rc = true;

            } catch (Throwable ignore) {
            }

        }
    }

    return rc;
}

From source file:com.enonic.cms.business.portal.datasource.methodcall.MethodCallFactory.java

private static Method resolveMethod(Class targetClass, String methodName, int numParams, boolean useContext) {
    final String localMethodName = resolveLocalMethodName(methodName);

    for (Method method : targetClass.getMethods()) {
        if (localMethodName.equals(method.getName()) && (method.getParameterTypes().length == numParams)) {
            return method;
        }/*from ww  w. ja va 2 s.  co m*/
    }
    throw new VerticalRenderException("Method [" + localMethodName + "] with ["
            + (useContext ? numParams - 1 : numParams) + "] parameters does not exist");
}

From source file:com.espertech.esper.epl.annotation.AnnotationUtil.java

private static List<AnnotationAttribute> getAttributes(Class annotationClass) {
    List<AnnotationAttribute> props = new ArrayList<AnnotationAttribute>();
    Method[] methods = annotationClass.getMethods();
    if (methods == null) {
        return Collections.EMPTY_LIST;
    }/*from  w w w  . j a  v  a 2  s.co m*/

    for (int i = 0; i < methods.length; i++) {
        if (methods[i].getReturnType() == void.class) {
            continue;
        }
        if (methods[i].getParameterTypes().length > 0) {
            continue;
        }
        if ((methods[i].getName().equals("class")) || (methods[i].getName().equals("getClass"))
                || (methods[i].getName().equals("toString")) || (methods[i].getName().equals("annotationType"))
                || (methods[i].getName().equals("hashCode"))) {
            continue;
        }

        props.add(new AnnotationAttribute(methods[i].getName(), methods[i].getReturnType(),
                methods[i].getDefaultValue()));
    }

    Collections.sort(props, new Comparator<AnnotationAttribute>() {
        public int compare(AnnotationAttribute o1, AnnotationAttribute o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    return props;
}

From source file:com.appleframework.jmx.connector.framework.ConnectorRegistry.java

@SuppressWarnings("deprecation")
private static ConnectorRegistry create(ApplicationConfig config) throws Exception {

    Map<String, String> paramValues = config.getParamValues();
    String appId = config.getApplicationId();
    String connectorId = (String) paramValues.get("connectorId");

    File file1 = new File(CoreUtils.getConnectorDir() + File.separatorChar + connectorId);

    URL[] urls = new URL[] { file1.toURL() };
    ClassLoader cl = ClassLoaderRepository.getClassLoader(urls, true);

    //URLClassLoader cl = new URLClassLoader(urls,
    //        ConnectorMBeanRegistry.class.getClassLoader());

    Registry.MODELER_MANIFEST = MBEANS_DESCRIPTOR;
    URL res = cl.getResource(MBEANS_DESCRIPTOR);

    logger.info("Application ID   : " + appId);
    logger.info("Connector Archive: " + file1.getAbsoluteFile());
    logger.info("MBean Descriptor : " + res.toString());

    //Thread.currentThread().setContextClassLoader(cl);
    //Registry.setUseContextClassLoader(true);

    ConnectorRegistry registry = new ConnectorRegistry();
    registry.loadMetadata(cl);//from www  .j av a  2 s. c  om

    String[] mbeans = registry.findManagedBeans();

    MBeanServer server = MBeanServerFactory.newMBeanServer(DOMAIN_CONNECTOR);

    for (int i = 0; i < mbeans.length; i++) {
        ManagedBean managed = registry.findManagedBean(mbeans[i]);
        String clsName = managed.getType();
        String domain = managed.getDomain();
        if (domain == null) {
            domain = DOMAIN_CONNECTOR;
        }

        Class<?> cls = Class.forName(clsName, true, cl);
        Object objMBean = null;

        // Use the factory method when it is defined.
        Method[] methods = cls.getMethods();
        for (Method method : methods) {
            if (method.getName().equals(FACTORY_METHOD) && Modifier.isStatic(method.getModifiers())) {
                objMBean = method.invoke(null);
                logger.info("Create MBean using factory method.");
                break;
            }
        }

        if (objMBean == null) {
            objMBean = cls.newInstance();
        }

        // Call the initialize method if the MBean extends ConnectorSupport class
        if (objMBean instanceof ConnectorSupport) {
            Method method = cls.getMethod("initialize", new Class[] { Map.class });
            Map<String, String> props = config.getParamValues();
            method.invoke(objMBean, new Object[] { props });
        }
        ModelMBean mm = managed.createMBean(objMBean);

        String beanObjName = domain + ":name=" + mbeans[i];
        server.registerMBean(mm, new ObjectName(beanObjName));
    }

    registry.setMBeanServer(server);
    entries.put(appId, registry);
    return registry;
}

From source file:autocorrelator.apps.SDFGroovy.java

private static void exitWithHelp(String msg, Options options) {
    System.err.println(msg);/*from w w w .j  a va 2  s .c  om*/
    HelpFormatter formatter = new HelpFormatter();
    formatter.setWidth(120);
    formatter.printHelp(INTROText, options);

    StringBuilder sb = new StringBuilder();
    Class<SDFGroovyHelper> helper = SDFGroovyHelper.class;
    for (Method m : helper.getMethods()) {
        if ((m.getModifiers() & Modifier.STATIC) == 0 || (m.getModifiers() & Modifier.PUBLIC) == 0)
            continue;

        String meth = m.toString();
        meth = meth.replaceAll("public static final ", "");
        meth = meth.replaceAll("java\\.lang\\.", "");
        meth = meth.replaceAll("java\\.math\\.", "");
        meth = meth.replaceAll("autocorrelator\\.apps\\.SDFGroovyHelper.", "");
        sb.append('\t').append(meth).append('\n');
    }
    System.out.println("Imported internal functions:");
    System.out.println(sb);
    System.exit(1);

}