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

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static int getScreenRealH(Context context) {
    int h;// w  w w. j  av a  2  s .  c  om
    WindowManager winMgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = winMgr.getDefaultDisplay();
    DisplayMetrics dm = new DisplayMetrics();
    if (Build.VERSION.SDK_INT >= 17) {
        display.getRealMetrics(dm);
        h = dm.heightPixels;
    } else {
        try {
            Method method = Class.forName("android.view.Display").getMethod("getRealMetrics",
                    DisplayMetrics.class);
            method.invoke(display, dm);
            h = dm.heightPixels;
        } catch (Exception e) {
            display.getMetrics(dm);
            h = dm.heightPixels;
        }
    }
    return h;
}

From source file:gobblin.compaction.mapreduce.avro.AvroKeyCompactorOutputCommitter.java

private static long getRecordCountFromCounter(TaskAttemptContext context, Enum<?> counterName) {
    try {/*  ww w . j  a v a 2 s  . c o  m*/
        Method getCounterMethod = context.getClass().getMethod("getCounter", Enum.class);
        return ((Counter) getCounterMethod.invoke(context, counterName)).getValue();
    } catch (Exception e) {
        throw new RuntimeException("Error reading record count counter", e);
    }
}

From source file:Main.java

private static synchronized BluetoothSocket getInsecureRfcommSocketByReflection(BluetoothDevice device)
        throws Exception {
    if (device == null)
        return null;
    Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class });

    return (BluetoothSocket) m.invoke(device, 1);
}

From source file:Main.java

public static ArrayList<ParcelUuid> getDeviceUuids(BluetoothDevice device) {
    ArrayList<ParcelUuid> result = new ArrayList<ParcelUuid>();

    try {//w w w . j a  v  a2s. c om
        Method method = device.getClass().getMethod("getUuids", null);
        ParcelUuid[] phoneUuids = (ParcelUuid[]) method.invoke(device, null);
        if (phoneUuids != null) {
            for (ParcelUuid uuid : phoneUuids) {
                if (D)
                    Log.d(TAG, device.getName() + ": " + uuid.toString());
                result.add(uuid);
            }
        }
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
        if (D)
            Log.e(TAG, "getDeviceUuids() failed", e);
    } catch (InvocationTargetException e) {
        e.printStackTrace();
        if (D)
            Log.e(TAG, "getDeviceUuids() failed", e);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        if (D)
            Log.e(TAG, "getDeviceUuids() failed", e);
    }

    return result;
}

From source file:Main.java

public static void convertActivityToTranslucent(Activity activity) {
    try {//from ww  w  . ja v  a2  s .c  o m
        Class<?>[] classes = Activity.class.getDeclaredClasses();
        Class<?> translucentConversionListenerClazz = null;
        for (Class clazz : classes) {
            if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
                translucentConversionListenerClazz = clazz;
            }
        }
        Method method = Activity.class.getDeclaredMethod("convertToTranslucent",
                translucentConversionListenerClazz);
        method.setAccessible(true);
        method.invoke(activity, new Object[] { null });
    } catch (Throwable t) {
    }
}

From source file:Main.java

public static Object invokeMethod(Class<?> methodClass, String methodName, Class<?>[] parameters,
        Object instance, Object... arguments) {
    try {/*w  w w .ja  va 2  s  . c  o m*/
        Method e = methodClass.getDeclaredMethod(methodName, parameters);
        e.setAccessible(true);
        return e.invoke(instance, arguments);
    } catch (Exception var6) {
        var6.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void disableChangeTransition(LayoutTransition layoutTransition) {
    if (layoutTransition == null) {
        return;/*from ww w .j  a  v  a 2  s  .c  o m*/
    }
    try {
        Method method = LayoutTransition.class.getMethod("disableTransitionType", new Class[] { int.class });
        Field field = LayoutTransition.class.getField("CHANGING");
        method.invoke(layoutTransition, field.get(null));
    } catch (Exception e) {
        sLayoutTransitions.remove(layoutTransition);
    }
}

From source file:net.minecrell.serverlistplus.canary.SnakeYAML.java

@SneakyThrows
private static void loadJAR(Path path) {
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    method.setAccessible(true);//from w w  w.  j a  va2s. c  o m
    method.invoke(SnakeYAML.class.getClassLoader(), path.toUri().toURL());
}

From source file:Main.java

static Point getDisplaySizeLT11(Display d) {
    try {//from w w  w  . j a v  a2  s. c o  m
        Method getWidth = Display.class.getMethod("getWidth", new Class[] {});
        Method getHeight = Display.class.getMethod("getHeight", new Class[] {});
        return new Point(((Integer) getWidth.invoke(d, (Object[]) null)).intValue(),
                ((Integer) getHeight.invoke(d, (Object[]) null)).intValue());
    } catch (Exception ex) { // None of these exceptions should ever occur.
        return new Point(-1, -1);
    }
}

From source file:Main.java

public static <T> T runPrivateMethod(Class<?> targetClass, String methodName, Class<?> paramClasses[],
        Object params[], Class<T> returnType) throws Exception {
    T returnValue = null;/*from www  .  j  av a2 s  .c o  m*/
    Method method = targetClass.getDeclaredMethod(methodName, paramClasses);
    if (!method.isAccessible())
        method.setAccessible(true);
    returnValue = (T) method.invoke(targetClass, params);
    return returnValue;
}