Example usage for java.lang Class getDeclaredMethod

List of usage examples for java.lang Class getDeclaredMethod

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:it.cnr.icar.eric.common.spi.LifeCycleManagerFactory.java

/**
* Creates the instance of the pluginClass
*//* w  ww .  j a v  a 2 s  .  co m*/
private Object createPluginInstance(String pluginClass) throws Exception {
    Object plugin = null;

    if (log.isDebugEnabled()) {
        log.debug("pluginClass = " + pluginClass);
    }

    Class<?> theClass = Class.forName(pluginClass);

    //try to invoke constructor using Reflection, 
    //if this fails then try invoking getInstance()
    try {
        Constructor<?> constructor = theClass.getConstructor((java.lang.Class[]) null);
        plugin = constructor.newInstance(new Object[0]);
    } catch (Exception e) {
        //log.warn("No accessible constructor. " +
        //    "invoking getInstance() instead.");

        Method factory = theClass.getDeclaredMethod("getInstance", (java.lang.Class[]) null);
        plugin = factory.invoke((java.lang.Class[]) null, new Object[0]);
    }

    return plugin;
}

From source file:it.cnr.icar.eric.common.spi.QueryManagerFactory.java

/**
* Creates the instance of the pluginClass
*//*from  w  w  w  .  jav  a2 s .co m*/
private Object createPluginInstance(String pluginClass) throws Exception {
    Object plugin = null;

    if (log.isDebugEnabled()) {
        log.debug("pluginClass = " + pluginClass);
    }

    Class<?> theClass = Class.forName(pluginClass);

    //try to invoke constructor using Reflection, 
    //if this fails then try invoking getInstance()
    try {
        Constructor<?> constructor = theClass.getConstructor((java.lang.Class[]) null);
        plugin = constructor.newInstance(new Object[0]);
    } catch (Exception e) {
        //log.warn("No accessible constructor. " +
        //    "invoking getInstance() instead.");

        Method factory = theClass.getDeclaredMethod("getInstance", (java.lang.Class[]) null);
        plugin = factory.invoke(null, new Object[0]);
    }

    return plugin;
}

From source file:com.google.flatbuffers.Table.java

@SuppressWarnings("unchecked")
@Override//w w w  .ja v  a 2 s  .c o m
public <Any> Any clone(Map<String, Object> mutate) throws Exception {
    Object oData = null;
    Class<?> cls = this.getClass();
    try {
        FlatBufferBuilder b = new FlatBufferBuilder();
        int root_table = this.clone(b, mutate);
        b.finish(root_table);
        Method m = cls.getDeclaredMethod("getRootAs" + cls.getSimpleName(),
                new Class<?>[] { ByteBuffer.class });
        oData = m.invoke(null, new Object[] { b.dataBuffer() });
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    return (Any) cls.cast(oData);
}

From source file:it.cnr.icar.eric.server.event.EventManagerFactory.java

/**
* Creates the instance of the pluginClass
*///from   ww  w . jav a  2  s.  c o m
private Object createPluginInstance(String pluginClass) throws Exception {
    Object plugin = null;

    if (log.isDebugEnabled()) {
        log.debug("pluginClass = " + pluginClass);
    }

    Class<?> theClass = Class.forName(pluginClass);

    //try to invoke constructor using Reflection, 
    //if this fails then try invoking getInstance()
    try {
        Constructor<?> constructor = theClass.getConstructor((java.lang.Class[]) null);
        plugin = constructor.newInstance(new Object[0]);
    } catch (Exception e) {
        //log.warn(ServerResourceBundle.getInstance().getString("message.NoAccessibleConstructorInvokingGetInstanceInstead"));

        Method factory = theClass.getDeclaredMethod("getInstance", (java.lang.Class[]) null);
        plugin = factory.invoke(null, new Object[0]);
    }

    return plugin;
}

From source file:com.example.administrator.mywebviewdrawsign.SysWebView.java

private void enableWorkers(WebSettings settings) {
    Class<? extends WebSettings> websettings = settings.getClass();
    Method setWorkersEnabled;/*from w  ww .j a  v  a2  s .  c o  m*/
    try {
        setWorkersEnabled = websettings.getDeclaredMethod("setWorkersEnabled", boolean.class);
        setWorkersEnabled.setAccessible(true);
        setWorkersEnabled.invoke(settings, true);
    } catch (Exception e) {
        e.printStackTrace();
        LogUtils.e("enableWorkers failed.");
    }
}

From source file:com.gsbabil.antitaintdroid.UtilityFunctions.java

@SuppressLint("NewApi")
public String runRemoteDex(String fullClassName, String methodName, String in) {

    String out = new String();

    @SuppressWarnings("unused")
    Class<?> noparams[] = {};

    Class<?>[] paramStr = new Class[1];
    paramStr[0] = String.class;

    Class<?>[] paramInt = new Class[1];
    paramInt[0] = Integer.TYPE;/*ww w  .  ja v  a 2 s  .  c om*/

    String path = MyApp.context.getFilesDir() + File.separator
            + MyApp.REMOTE_DEX_URL.substring(MyApp.REMOTE_DEX_URL.lastIndexOf('/') + 1);

    try {

        DexClassLoader classLoader = new DexClassLoader(path, MyApp.context.getFilesDir().getAbsolutePath(),
                null, MyApp.context.getClassLoader());

        Class<?> remoteClass = classLoader.loadClass("com.gsbabil.remotedex.UntaintTricks");
        Method remoteMethod = remoteClass.getDeclaredMethod("encodingTrick", paramStr);

        Object obj = remoteClass.newInstance();
        out = (String) remoteMethod.invoke(obj, in);

    } catch (Exception e) {
        Log.d(MyApp.TAG, e.getMessage().toString());
    }

    return out;
}

From source file:it.cnr.icar.eric.server.common.AbstractFactory.java

/**
 * Creates the instance of the pluginClass
 *
 * @param pluginClass class name of instance to create
 * @return an instance of the specified class
 * @exception Exception if an error occurs
 *//*from   w ww  .j  a  v a2 s  . co  m*/
protected Object createPluginInstance(String pluginClass) throws Exception {
    Object plugin = null;

    if (log.isDebugEnabled()) {
        log.debug("pluginClass = " + pluginClass);
    }

    Class<?> theClass = Class.forName(pluginClass);

    //try to invoke constructor using Reflection, 
    //if this fails then try invoking getInstance()
    try {
        Constructor<?> constructor = theClass.getConstructor((java.lang.Class[]) null);
        plugin = constructor.newInstance(new Object[0]);
    } catch (Exception e) {
        log.warn(ServerResourceBundle.getInstance()
                .getString("message.NoAccessibleConstructorInvokingGetInstanceInstead"));

        Method factory = theClass.getDeclaredMethod("getInstance", (java.lang.Class[]) null);
        plugin = factory.invoke(null, new Object[0]);
    }

    return plugin;
}

From source file:gov.llnl.lc.smt.command.SubnetMonitorTool.java

private boolean invokeSmtCommand(SmtCommandType s, String[] cmdArgs) {
    if (s != null) {
        String className = s.getCommandName();

        // attempt to invoke this command, using this class
        Class<?> smtClass = null;
        try {//from  w  w w.  ja v a2 s.com
            smtClass = Class.forName(className);
            Class[] argTypes = new Class[] { String[].class };
            Method main = smtClass.getDeclaredMethod("main", argTypes);

            // don't "invoke" with a null arg
            if (cmdArgs == null)
                cmdArgs = new String[] { "" };

            //        System.out.format("invoking %s.main()%n", smtClass.getName());
            //        System.out.println("with arguments " + Arrays.toString(cmdArgs));
            main.invoke(null, (Object) cmdArgs);
        } catch (Exception e) {
            logger.info("CMD NOT: " + className);
            System.err.println(className + " threw an exception while invoking main");
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
        return true;
    }
    return false;
}

From source file:com.opengsn.controller.client.ControllerClient.java

/**
 * Use the commandline args to invoke the proper service client and
 * operation using Reflection//from   ww  w  .j  av a 2 s  .c om
 * 
 * @param args
 */
private void process(String[] args) {

    Class<?> c = this.getClass();
    try {
        // Class<?> c =
        // Class.forName("com.greenstarnetwork.controller.client.ControllerWrapper");

        // Use reflection to call the service and operation that was given
        // from the command line
        Object obj = c.newInstance();
        @SuppressWarnings("rawtypes")
        Class[] argTypes = new Class[] { String[].class };
        Method m = c.getDeclaredMethod(args[0], argTypes);

        String[] methodArgs = Arrays.copyOfRange(args, 1, args.length);
        System.out.println("invoking Operation: " + c.getName() + "." + m.getName());
        m.invoke(obj, (Object) methodArgs);

    } catch (NoSuchMethodException e) {
        System.out.println("Invalid Operation: " + args[1]);
        System.out.println("Operations for the " + c.getName() + ": ");
        Method[] allMethods = c.getDeclaredMethods();
        for (Method m : allMethods) {
            System.out.println("  " + m.getName());
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:de.tudarmstadt.ukp.dkpro.lexsemresource.wordnet.WordNetResource.java

private PointerTargetNodeList getNodeListByRelationName(Synset synset, String relationType)
        throws LexicalSemanticResourceException {
    try {//from   w w w.ja va  2  s  . com

        Class<? extends PointerUtils> cls = pUtils.getClass();
        Class[] params = new Class[1];
        params[0] = Synset.class;
        String methodName = "get" + relationType + "s";

        Method method = cls.getDeclaredMethod(methodName, params);
        return (PointerTargetNodeList) method.invoke(pUtils, synset);

    } catch (Exception e) {
        // silently catch that - I do not know why JWNL throws NullPointerException exception here
        // if an Exception occured, we simply return a null as the result NodeList
        //         e.printStackTrace();
        //         throw new LexicalSemanticResourceException(e.getMessage());
    }
    return null;
}