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:org.objectweb.proactive.extensions.timitspmd.util.charts.Utilities.java

/**
 * Exports a JFreeChart to a SVG file.//w  w  w . j  a  v a2s . c  om
 *
 * @param chart
 *            JFreeChart to export
 * @param bounds
 *            the dimensions of the viewport
 * @param svgFile
 *            the output file.
 * @throws IOException
 *             if writing the svgFile fails.
 */
public static void saveChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException {
    try {
        Class<?> GDI = Class.forName("org.apache.batik.dom.GenericDOMImplementation");

        // Get a DOMImplementation and create an XML document
        Method getDOMImplementation = GDI.getMethod("getDOMImplementation", new Class<?>[0]);
        DOMImplementation domImpl = (DOMImplementation) getDOMImplementation.invoke(null, new Object[0]);

        org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null);

        // Create an instance of the SVG Generator
        Class<?> SG2D = Class.forName("org.apache.batik.svggen.SVGGraphics2D");
        Method streamMethod = SG2D.getMethod("stream", new Class<?>[] { Writer.class, boolean.class });
        Constructor<?> SG2DConstr = SG2D.getConstructor(new Class<?>[] { org.w3c.dom.Document.class });
        Object svgGenerator = SG2DConstr.newInstance(document);

        // draw the chart in the SVG generator
        chart.draw((Graphics2D) svgGenerator, bounds);

        // Write svg file
        OutputStream outputStream = new FileOutputStream(svgFile);
        Writer out = new OutputStreamWriter(outputStream, "UTF-8");
        streamMethod.invoke(svgGenerator, new Object[] { out, true /* use css */ });
        outputStream.flush();
        outputStream.close();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.bremersee.common.security.acls.model.CommonObjectIdentityRetrievalStrategy.java

public static String getId(final Object object) {
    Validate.notNull(object, "Object cannot be null.");

    Class<?> typeClass = ClassUtils.getUserClass(object.getClass());

    Object result;/*from w w w  .j  ava  2 s. c  o m*/

    try {
        @SuppressWarnings("RedundantArrayCreation")
        Method method = typeClass.getMethod("getId", new Class[] {});
        result = method.invoke(object);
    } catch (Exception e) {
        throw new IdentityUnavailableException("Could not extract identity from object " + object, e);
    }

    Assert.notNull(result, "getId() is required to return a non-null value");
    return result.toString();
}

From source file:Main.java

/**
 * Sets the thread policy.//from   w ww  .j  a v a 2s  .  co  m
 *
 * @param strictMode
 *            the strict mode to work with
 * @throws ClassNotFoundException
 *             on problem
 * @throws NoSuchMethodException
 *             on problem
 * @throws InstantiationException
 *             on problem
 * @throws IllegalAccessException
 *             on problem
 * @throws InvocationTargetException
 *             on problem
 */
private static void setThreadPolicy(final Class<?> strictMode) throws ClassNotFoundException,
        NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {

    Class<?> threadPolicyType = Class.forName("android.os.StrictMode$ThreadPolicy");
    Method setThreadPolicy = strictMode.getMethod("setThreadPolicy", threadPolicyType);
    Class<?> threadPolicyBuilder = Class.forName("android.os.StrictMode$ThreadPolicy$Builder");

    Object policy = buildPolicy(threadPolicyBuilder);
    setThreadPolicy.invoke(strictMode, policy);
}

From source file:hudson.plugins.trackplus.Updater.java

private static String getRevision(Entry entry) {
    // svn at least can get the revision
    try {// ww w.  j av  a 2 s.  c om
        Class<?> clazz = entry.getClass();
        Method method = clazz.getMethod("getRevision", (Class[]) null);
        if (method == null) {
            return null;
        }
        Object revObj = method.invoke(entry, (Object[]) null);
        return (revObj != null) ? revObj.toString() : null;
    } catch (Exception e) {
        return null;
    }
}

From source file:org.mstc.zmq.json.Decoder.java

static Object getNested(Class<?> nested, byte[] input)
        throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Method parseFrom = nested.getMethod("parseFrom", byte[].class);
    return parseFrom.invoke(null, input);

}

From source file:cn.loveapple.client.android.LoveappleHelper.java

private static Object invokeMethod(Object object, String methodName, Object[] params, Class... types)
        throws Exception {
    Object out = null;//from   ww  w  . j av a  2s.  c  om
    Class c = object instanceof Class ? (Class) object : object.getClass();
    if (types != null) {
        Method method = c.getMethod(methodName, types);
        out = method.invoke(object, params);
    } else {
        Method method = c.getMethod(methodName);
        out = method.invoke(object);
    }
    Log.d(LOG_TAG, object.getClass().getName() + "." + methodName + "() = " + out);
    return out;
}

From source file:ca.uhn.fhir.util.ReflectionUtil.java

public static LinkedHashSet<Method> getDeclaredMethods(Class<?> theClazz) {
    LinkedHashSet<Method> retVal = new LinkedHashSet<Method>();
    for (Method next : theClazz.getDeclaredMethods()) {
        try {/*  w w  w  .j a v  a2s .  co  m*/
            Method method = theClazz.getMethod(next.getName(), next.getParameterTypes());
            retVal.add(method);
        } catch (NoSuchMethodException e) {
            retVal.add(next);
        } catch (SecurityException e) {
            retVal.add(next);
        }
    }
    return retVal;
}

From source file:com.yahoo.xpathproto.ProtoBuilder.java

private static Message.Builder createMessageBuilder(final String className) {
    try {/*from  w  ww  .ja  v a 2s . c o  m*/
        Class messageClass = Class.forName(className);
        Method getDefaultInstanceMethod = messageClass.getMethod("getDefaultInstance", (Class[]) null);
        Message message = (Message) getDefaultInstanceMethod.invoke((Object[]) null, (Object[]) null);
        Message.Builder builder = message.newBuilderForType();
        return builder;
    } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
            | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:ca.psiphon.tunneledwebview.WebViewProxySettings.java

@SuppressWarnings("rawtypes")
private static Object invokeMethod(Object object, String methodName, Object[] params, Class... types)
        throws Exception {
    Object out = null;/*from  w w w. j a va  2  s .c o  m*/
    Class c = object instanceof Class ? (Class) object : object.getClass();

    if (types != null) {
        Method method = c.getMethod(methodName, types);
        out = method.invoke(object, params);
    } else {
        Method method = c.getMethod(methodName);
        out = method.invoke(object);
    }
    return out;
}

From source file:com.apporiented.hermesftp.PluginManager.java

/**
 * Need to be invoked from the application's main in order to utilize the dynamic class loader.
 * //from   w ww.j  a  v a2  s. c  o  m
 * @param mainClassName Main class.
 * @param startMethod Start method that accepts the main arguments.
 * @param args Array of optional arguments
 */
public static void startApplication(String mainClassName, String startMethod, String[] args) {
    try {
        classLoader.update();
        Class<?> clazz = classLoader.loadClass(mainClassName);
        Object instance = clazz.newInstance();
        Method startup = clazz.getMethod(startMethod, new Class[] { (new String[0]).getClass() });
        startup.invoke(instance, new Object[] { args });
    } catch (Exception e) {
        log.error(e, e);
    }
}