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

public static boolean checkPermission(Context context, String permission) {
    boolean result = false;
    if (Build.VERSION.SDK_INT >= 23) {
        try {/*from  w w  w . j a  v a  2s . 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

/**
 * Build authorization url base on type of cloud service
 *
 * @param cloudApi type of cloud account
 * @return Uri/* www .  j a  v  a 2 s .c  om*/
 */
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

public static boolean setPairingConfirmation(Class btClass, BluetoothDevice device, boolean confirm)
        throws Exception {
    Method setPairiConfirmMethod = btClass.getMethod("setPairingConfirmation", boolean.class);
    Boolean retVal = (Boolean) setPairiConfirmMethod.invoke(device, confirm);
    return retVal.booleanValue();
}

From source file:Main.java

public static boolean checkPermission(Context context, String permission) {
    boolean result = false;
    if (Build.VERSION.SDK_INT >= 23) {
        try {/*from   ww  w. ja  va 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);
            if (rest == PackageManager.PERMISSION_GRANTED) {
                result = true;
            } else {
                result = false;
            }
        } 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 String getSizeMethod(Class<?> cls, String[] sizers) {
    for (String sizer : sizers) {
        try {//from  w  w w  .  j  av  a 2 s.co m
            return cls.getMethod(sizer, new Class<?>[0]).getName() + "()";
        } catch (NoSuchMethodException e) {
        }
    }
    return null;
}

From source file:cn.teamlab.wg.framework.struts2.breadcrumb.Utils.java

/**
 * Find a method with no arguments with the name <var>name</var>.
 * /*w ww.j a va  2 s. c o m*/
 * It is just a wrapper of the <code>class.getMethod()</code>.
 * 
 * @param clazz the class from which are searching.
 * @param name the method name we are looking for.
 * @return the method or
 *         <code>null</null> if such a method cannot be found.
 * 
 */
public static Method findMethod(Class<?> clazz, String name) {

    try {
        return clazz.getMethod(name, (Class[]) null);
    } catch (SecurityException e) {
        return null;
    } catch (NoSuchMethodException e) {
        return null;
    }
}

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);
    }//w w  w.  j  a  v a  2s .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;
}

From source file:Main.java

static boolean isSuperMethodAnnotated(Class<?> superClass, Method method,
        Class<? extends Annotation> annotationClass) {
    try {//from   w  ww  .ja v  a  2s  .  c  o  m
        return superClass.getMethod(method.getName(), method.getParameterTypes())
                .isAnnotationPresent(annotationClass);
    } catch (NoSuchMethodException ignored) {
        return false;
    }
}

From source file:Main.java

/**
 * @param clz class name/*w  w  w  . j a v  a 2  s. com*/
 * @param methodName method name
 * @return true if the method is in the class, else false
 */
public static Method getMethod(Class clz, String methodName, Class... parameterTypes) {
    if (null != clz) {
        try {
            return clz.getMethod(methodName, parameterTypes);
        } catch (Exception e) {
        }
        return (clz == Object.class ? null : getMethod(clz.getSuperclass(), methodName, parameterTypes));
    }
    return null;
}

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 {/*  w  w w  .  j  ava2  s .co m*/
        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;
}