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:Main.java

public static boolean checkPermission(Context context, String permission) {
    boolean result = false;
    if (Build.VERSION.SDK_INT >= 23) {
        try {/*  w w w  . j av a  2 s  .c o m*/
            Class<?> clazz = Class.forName("android.content.Context");
            Method method = clazz.getMethod("checkSelfPermission", String.class);
            int rest = (Integer) method.invoke(context, permission);
            result = rest == PackageManager.PERMISSION_GRANTED;
        } catch (Exception e) {
            result = false;
        }
    } else {
        PackageManager pm = context.getPackageManager();
        if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
            result = true;
        }
    }
    return result;
}

From source file:Main.java

public static void forceConvertActivityFromTranslucent(Activity activity) {
    try {// w  ww  .j  a  v a  2  s.c o  m
        Method method = Activity.class.getDeclaredMethod("convertFromTranslucent", null);
        method.setAccessible(true);
        method.invoke(activity, null);
    } catch (Throwable ignored) {
    }
}

From source file:Main.java

private static void methodInvoker(Object receiver, String methodName, HashMap<String, Object> map) {
    for (Class<?> klass = receiver.getClass(); !(Object.class.equals(klass)); klass = klass.getSuperclass()) {
        try {// w  ww. j  av a2 s .c  o m
            Method method = klass.getDeclaredMethod(methodName, Map.class);
            method.setAccessible(true);
            method.invoke(receiver, map);
            break;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static Object invokeMethod(Method method, Object o, Object... args) {
    if (method == null)
        return null;

    try {//from w  w  w  .j  av a 2s.c  o  m
        return method.invoke(o, args);
    } catch (Throwable t) {
        //            XposedBridge.log(t);
    }
    return null;
}

From source file:Main.java

public static void setNextFocusForwardId(final View view, final int nextFocusForwardId) {
    try {/*www.jav a 2s . c o m*/
        final Method setNextFocusForwardId = TextView.class.getMethod("setNextFocusForwardId", Integer.TYPE);
        setNextFocusForwardId.invoke(view, nextFocusForwardId);
    } catch (final NoSuchMethodException x) {
        // expected on API levels below 11
    } catch (final Exception x) {
        throw new RuntimeException(x);
    }
}

From source file:Main.java

public static <T> T invokeInstanceMethod(final Object instance, final String methodName,
        final Class[] parameterTypes, final Object[] parameters) {

    final Class aClass = instance.getClass();
    try {//from ww w  .  jav a  2  s. c om
        final Method method = aClass.getMethod(methodName, parameterTypes);
        return (T) method.invoke(instance, parameters);
    } catch (NoSuchMethodException e) {
        Log.e("error", "can not find " + methodName + " in " + aClass.getName());
    } catch (IllegalAccessException e) {
        Log.e("error", methodName + " is not accessible");
    } catch (IllegalArgumentException e) {
        Log.e("error", "arguments are error when invoking " + methodName);
    } catch (InvocationTargetException e) {
        Log.e("error", "an exception was thrown by the invoked method when invoking " + methodName);
    }

    return null;
}

From source file:Main.java

/**
 * Build authorization url base on type of cloud service
 *
 * @param cloudApi type of cloud account
 * @return Uri//from   ww  w  . jav a  2 s.c  o m
 */
public static Uri buildAuthUri(String cloudApi, String stateString) {
    // use reflection for flexibility
    try {
        Class<?> clazz = Class.forName(cloudApi);
        Method buildAuthUri = clazz.getMethod("buildAuthUri", String.class);
        return (Uri) buildAuthUri.invoke(null, stateString);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

static public Object invokeStaticMethod(Class<?> claxx, String methodName, Object... args) throws Exception {
    Class<?>[] argsClass = null;

    if (args != null && args.length > 0) {
        argsClass = new Class<?>[args.length];

        for (int i = 0, j = args.length; i < j; i++) {
            argsClass[i] = args[i].getClass();
            if (argsClass[i] == Integer.class) {
                argsClass[i] = int.class;
            } else if (argsClass[i] == Boolean.class) {
                argsClass[i] = boolean.class;
            }//  ww  w . j av  a 2 s .com
        }
    }

    Method method = claxx.getDeclaredMethod(methodName, argsClass);
    method.setAccessible(true);
    return method.invoke(null, args);
}

From source file:com.basho.riak.client.api.convert.reflection.ClassUtil.java

public static <T> void setMethodValue(Method m, T obj, Object value) {
    try {// www.  j av  a  2  s.co m
        m.invoke(obj, value);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException("Unable to set Riak annotated method value", e);
    } catch (IllegalArgumentException e) {
        throw new IllegalStateException("Unable to set Riak annotated method value", e);
    } catch (InvocationTargetException e) {
        throw new IllegalStateException("Unable to set Riak annotated method value", e);
    }
}

From source file:Main.java

public static boolean checkDeviceHasNavigationBar(Context context) {
    boolean hasNavigationBar = false;
    Resources rs = context.getResources();
    int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
    if (id > 0) {
        hasNavigationBar = rs.getBoolean(id);
    }//from w  ww  .j a  va 2 s  . c o  m
    try {
        Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
        Method m = systemPropertiesClass.getMethod("get", String.class);
        String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
        if ("1".equals(navBarOverride)) {
            hasNavigationBar = false;
        } else if ("0".equals(navBarOverride)) {
            hasNavigationBar = true;
        }
    } catch (Exception e) {
    }
    return hasNavigationBar;
}