Example usage for java.lang.reflect Method invoke

List of usage examples for java.lang.reflect Method invoke

Introduction

In this page you can find the example usage for java.lang.reflect Method invoke.

Prototype

@CallerSensitive
@ForceInline 
@HotSpotIntrinsicCandidate
public Object invoke(Object obj, Object... args)
        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException 

Source Link

Document

Invokes the underlying method represented by this Method object, on the specified object with the specified parameters.

Usage

From source file:net.jselby.pc.bukkit.BukkitLoader.java

public static void addToClasspath(File file) throws Exception {
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
    method.setAccessible(true);//  w w w  .ja  va  2s  . c  o m
    method.invoke(ClassLoader.getSystemClassLoader(), new Object[] { file.toURI().toURL() });
}

From source file:Main.java

public static Object invokeMethod(Object paramObject, String paramString, Class<?>[] paramArrayOfClass,
        Object[] paramArrayOfObject) {
    Class localClass = paramObject.getClass();

    try {/*from  w w  w  . j  a  v  a  2  s . c  o  m*/
        Method localException = localClass.getDeclaredMethod(paramString, paramArrayOfClass);
        localException.setAccessible(true);
        return localException.invoke(paramObject, paramArrayOfObject);
    } catch (Exception var9) {
        Method localMethod = null;

        try {
            if (localClass != null && localClass.getSuperclass() != null) {
                localMethod = localClass.getSuperclass().getDeclaredMethod(paramString, paramArrayOfClass);
                if (localMethod != null) {
                    localMethod.setAccessible(true);
                    return localMethod.invoke(paramObject, paramArrayOfObject);
                }
            }
        } catch (Exception var8) {
            ;
        }

        return null;
    }
}

From source file:com.prey.ReflectionsUtils.java

public static void setProperty(String nameProperty, Object target, Object value) throws Exception {
    Class[] parameterTypes = { value.getClass() };
    Method method = target.getClass().getMethod(nameProperty, parameterTypes);
    if (method != null) {
        Object[] paraneters = { value };
        method.invoke(target, paraneters);
    }//from  w  ww.  j  a  v  a  2 s .  co  m

}

From source file:Main.java

private static Object invoke(Class<?> cls, Object receiver, String methodname, Class<?>[] clsArr,
        Object[] objArr) throws Exception {
    Method method = null;
    if (objArr == null || objArr.length == 0) {
        method = cls.getMethod(methodname, new Class[0]);
        method.setAccessible(true);/*from   w  ww.  ja va  2s. c  om*/
        return method.invoke(receiver, new Object[0]);
    }
    method = cls.getMethod(methodname, clsArr);
    method.setAccessible(true);
    return method.invoke(receiver, objArr);
}

From source file:Main.java

public static String findJniLibrary(Context context, String libName) {
    String result = null;//from w ww . j  av  a 2  s  .  c o m
    ClassLoader classLoader = (context.getClassLoader());
    if (classLoader != null) {
        try {
            Method findLibraryMethod = classLoader.getClass().getMethod("findLibrary",
                    new Class<?>[] { String.class });
            if (findLibraryMethod != null) {
                Object objPath = findLibraryMethod.invoke(classLoader, new Object[] { libName });
                if (objPath != null && objPath instanceof String) {
                    result = (String) objPath;
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }

    return result;
}

From source file:gr.demokritos.iit.textforms.TextForm.java

/**
 * Get an instance of the correct subclass - the subclass that can parse the
 * JSON. If an error is encountered it throws an exception like specified
 * below.//from   w  w w  . java 2 s.  c o m
 *
 * @param data, the data representing the text object
 * @param classname, the class - subclass of TextForm to use to parse
 * the format. Will cal fromFormat of that class.
 * @return An instance of the class that parses the JSON
 * @throws gr.demokritos.iit.textforms.TextForm.InvalidFormatException
 */
public static TextForm fromFormat(String data, Class classname) throws InvalidFormatException {
    try {
        Class<? extends TextForm> textform = classname.asSubclass(TextForm.class);
        Method fromFormat = textform.getMethod("fromFormat", String.class);
        return textform.cast(fromFormat.invoke(null, data));
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
            | NoSuchMethodException | SecurityException ex) {
        if (!API.mapping.containsValue(classname)) {
            Logger.getLogger(TextForm.class.getName()).log(Level.SEVERE,
                    "fromFormat generics expects one of the " + "following values {0}",
                    API.mapping.values().toString());
        }
        Logger.getLogger(TextForm.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:com.jkoolcloud.tnt4j.streams.utils.LoggerUtils.java

private static Object invoke(Object obj, String className, String methodName, Class<?>[] paramTypes,
        Object... params) throws Exception {
    Class<?> cls = obj == null ? Class.forName(className) : obj.getClass();
    Method m = cls.getDeclaredMethod(methodName, paramTypes);
    return m.invoke(obj, params);
}

From source file:org.red5.server.Bootstrap.java

/**
 * Loads classloader with dependencies//from  ww  w. j  a v a 2 s.c o  m
 * 
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws ClassNotFoundException
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 */
private static void bootStrap() throws InstantiationException, IllegalAccessException, ClassNotFoundException,
        NoSuchMethodException, InvocationTargetException {

    // print the classpath
    //String classPath = System.getProperty("java.class.path");
    //System.out.printf("JVM classpath: %s\n", classPath);      

    System.setProperty("red5.deployment.type", "bootstrap");

    System.setProperty("sun.lang.ClassLoader.allowArraySyntax", "true");

    //check system property before forcing out selector
    if (System.getProperty("logback.ContextSelector") == null) {
        //set to use our logger
        System.setProperty("logback.ContextSelector", "org.red5.logging.LoggingContextSelector");
    }

    String policyFile = System.getProperty("java.security.policy");
    if (policyFile == null) {
        System.setProperty("java.security.debug", "all");
        System.setProperty("java.security.policy", "conf/red5.policy");
    }

    /*
     try {
         // Enable the security manager
         SecurityManager sm = new SecurityManager();
         System.setSecurityManager(sm);
     } catch (SecurityException se) {
        System.err.println("Security manager already set");
     }
    */

    // pass urls to the ClassLoader
    ClassLoader loader = ClassLoaderBuilder.build(null, ClassLoaderBuilder.USE_RED5_LIB, null);

    // set the classloader to the current thread
    Thread.currentThread().setContextClassLoader(loader);

    // create a new instance of this class using new classloader
    Object boot = Class.forName("org.red5.server.Bootstrap", true, loader).newInstance();

    Method m1 = boot.getClass().getMethod("launch", (Class[]) null);
    m1.invoke(boot, (Object[]) null);
}

From source file:com.microsoft.tfs.client.common.ui.helpers.WorkingSetHelper.java

public static boolean isAggregateWorkingSet(final IWorkingSet workingSet) {
    Check.notNull(workingSet, "workingSet"); //$NON-NLS-1$

    try {//from w w  w  . j  a  va 2 s. c  om
        final Method isAggregateMethod = workingSet.getClass().getMethod("isAggregateWorkingSet", new Class[0]); //$NON-NLS-1$
        final Object aggregateResult = isAggregateMethod.invoke(workingSet, new Object[0]);

        if (aggregateResult != null && aggregateResult instanceof Boolean) {
            return ((Boolean) aggregateResult).booleanValue();
        }
    } catch (final Exception e) {
        /* Suppress, Eclipse < 3.1 */
    }

    return false;
}

From source file:com.npower.dm.util.BeanHelper.java

/**
 * private/protected//ww w  .  j av  a 2s. c  o  m
 */
static public Object invokePrivateMethod(Object object, String methodName, Object[] params)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    assert object != null;
    assert StringUtils.isNotEmpty(methodName);
    Class<?>[] types = new Class[params.length];
    for (int i = 0; i < params.length; i++) {
        types[i] = params[i].getClass();
    }
    Method method = object.getClass().getDeclaredMethod(methodName, types);
    method.setAccessible(true);
    return method.invoke(object, params);
}