Example usage for java.lang Class getMethod

List of usage examples for java.lang Class getMethod

Introduction

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

Prototype

@CallerSensitive
public Method getMethod(String name, Class<?>... parameterTypes)
        throws NoSuchMethodException, SecurityException 

Source Link

Document

Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

Usage

From source file:Main.java

public static Method getMethod(Class<?> targetClass, String name, Class<?>... parameterTypes) {
    if (targetClass == null || TextUtils.isEmpty(name))
        return null;
    try {//w  w  w  .  ja  v  a 2s .  c o m
        return targetClass.getMethod(name, parameterTypes);
    } catch (SecurityException e) {
        // ignore
    } catch (NoSuchMethodException e) {
        // ignore
    }
    return null;
}

From source file:SampleInvoke.java

public static String append(String firstWord, String secondWord) {
    String result = null;/*from  w w  w .  jav  a2s. com*/
    Class c = String.class;
    Class[] parameterTypes = new Class[] { String.class };
    Method concatMethod;
    Object[] arguments = new Object[] { secondWord };
    try {
        concatMethod = c.getMethod("concat", parameterTypes);
        result = (String) concatMethod.invoke(firstWord, arguments);
    } catch (NoSuchMethodException e) {
        System.out.println(e);
    } catch (IllegalAccessException e) {
        System.out.println(e);
    } catch (InvocationTargetException e) {
        System.out.println(e);
    }
    return result;
}

From source file:dk.netarkivet.common.distribute.RemoteFileFactory.java

/**
 * Returns true iff the defined RemoteFile class has a factory method with
 * signature public static RemoteFile getInstance(ArchiveRecord record)
 * @return true if using an extended remote file.
 *//*ww w.  ja v  a  2  s .  co  m*/
public static boolean isExtendedRemoteFile() {
    String remoteFileClass = Settings.get(CommonSettings.REMOTE_FILE_CLASS);
    try {
        Class theClass = Class.forName(remoteFileClass);
        try {
            theClass.getMethod("getInstance", ArchiveRecord.class);
            return true;
        } catch (NoSuchMethodException e) {
            return false;
        }
    } catch (ClassNotFoundException e) {
        log.error("Unknown RemoteFile class :" + remoteFileClass);
        throw new ArgumentNotValid("Unknown RemoteFile class :" + remoteFileClass);
    }
}

From source file:Main.java

public static Method findGetterMethodForField(Field field, Class<?> objectClass) throws NoSuchMethodException {
    String getterMethodName = "get" + field.getName().toUpperCase().substring(0, 1)
            + field.getName().substring(1);
    return objectClass.getMethod(getterMethodName, (java.lang.Class[]) null);
}

From source file:com.mirth.connect.client.core.api.util.OperationUtil.java

public static Operation getOperation(Class<?> servletInterface, String methodName, Class<?>... parameterTypes) {
    try {/*from  w  ww  . j  av  a  2 s  . c  o  m*/
        Method matchingMethod = servletInterface.getMethod(methodName, parameterTypes);
        MirthOperation annotation = matchingMethod.getAnnotation(MirthOperation.class);
        if (annotation != null) {
            return new Operation(annotation.name(), annotation.display(), annotation.type(),
                    annotation.auditable());
        }
    } catch (Exception e) {
    }

    return null;
}

From source file:com.ery.ertc.estorm.util.Methods.java

public static <T> Object call(Class<T> clazz, T instance, String methodName, Class[] types, Object[] args)
        throws Exception {
    try {/*from   w  w w  .  j a  va 2s.  c  o  m*/
        Method m = clazz.getMethod(methodName, types);
        return m.invoke(instance, args);
    } catch (IllegalArgumentException arge) {
        LOG.fatal("Constructed invalid call. class=" + clazz.getName() + " method=" + methodName + " types="
                + Classes.stringify(types), arge);
        throw arge;
    } catch (NoSuchMethodException nsme) {
        throw new IllegalArgumentException("Can't find method " + methodName + " in " + clazz.getName() + "!",
                nsme);
    } catch (InvocationTargetException ite) {
        // unwrap the underlying exception and rethrow
        if (ite.getTargetException() != null) {
            if (ite.getTargetException() instanceof Exception) {
                throw (Exception) ite.getTargetException();
            } else if (ite.getTargetException() instanceof Error) {
                throw (Error) ite.getTargetException();
            }
        }
        throw new UndeclaredThrowableException(ite,
                "Unknown exception invoking " + clazz.getName() + "." + methodName + "()");
    } catch (IllegalAccessException iae) {
        throw new IllegalArgumentException("Denied access calling " + clazz.getName() + "." + methodName + "()",
                iae);
    } catch (SecurityException se) {
        LOG.fatal("SecurityException calling method. class=" + clazz.getName() + " method=" + methodName
                + " types=" + Classes.stringify(types), se);
        throw se;
    }
}

From source file:com.prepaird.objectgraphdb.utils.Utils.java

public static Method getMethod(Class cl, String methodName, Class[] params) {
    try {//from www .  j a  va  2s  . com
        return cl.getMethod(methodName, params);
    } catch (NoSuchMethodException ex) {
        //never mind
    } catch (SecurityException ex) {
        Log.error(ex);
    }
    return null;
}

From source file:Main.java

/**
 * Attempts to find a method with the given name and argument types on the specified class. Other than Class's
 * getMethod(String, Class...) method, this method will also return protected and private methods.
 *
 * @param clazz         Class supposed to offer the method being searched.
 * @param methodName    Name of the method.
 * @param argumentTypes The arguments found in the method signature, if any.
 * @return a method with the given name and argument types on the specified class, if any.
 *///from  w  w w. j a va2 s . c  o  m
public static synchronized Method getMethod(final Class clazz, final String methodName,
        final Class... argumentTypes) {
    try {
        return clazz.getMethod(methodName, argumentTypes);
    } catch (NoSuchMethodException e) {
        /* Ok, so there's no public method... */
    }

    Class runner = clazz;
    while (runner != null) {
        try {
            return runner.getDeclaredMethod(methodName, argumentTypes);
        } catch (NoSuchMethodException e) {
            /* No luck here either */
        }
        runner = runner.getSuperclass();
    }

    /* Still no luck, means there is no suitable method */
    return null;
}

From source file:com.weibo.api.motan.protocol.grpc.GrpcUtil.java

public static ServiceDescriptor getServiceDesc(String clazzName) throws Exception {
    Class<?> clazz = Class.forName(clazzName);
    return (ServiceDescriptor) clazz.getMethod("getServiceDescriptor", null).invoke(null, null);
}

From source file:io.albraga.namus.OverlaypdfUI.java

private static void load() throws Exception {
    String[] args = new String[3];
    args[0] = "input.pdf";
    args[1] = "overlay.pdf";
    args[2] = "output.pdf";
    URL[] classLoaderUrls = new URL[] { new URL("file:///pdfbox-tools-2.0.4") };
    URLClassLoader urlClassLoader = new URLClassLoader(classLoaderUrls);
    Class<?> overlaypdfClass = urlClassLoader.loadClass("org.apache.pdfbox.tools.OverlayPDF");
    Method method = overlaypdfClass.getMethod("main", new Class[] { args.getClass() });
    method.invoke(null, new Object[] { args });
}