List of usage examples for java.lang Class getMethod
@CallerSensitive public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:Main.java
public static String getId(Context context) { String id = null;/*from w ww. j a v a 2 s . c om*/ try { Class aicclass = Class.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient"); Method m1 = aicclass.getMethod("getAdvertisingIdInfo", Context.class); Object oInfo = m1.invoke(null, context); Class infoclass = Class.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient$Info"); Method m2 = infoclass.getMethod("getId"); id = (String) m2.invoke(oInfo); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } return id; }
From source file:Main.java
public static Object clone(final Object obj) throws CloneNotSupportedException { if (obj == null) { return null; }/*from w ww . j a v a 2 s . c o m*/ if (obj instanceof Cloneable) { Class<?> clazz = obj.getClass(); Method m; try { m = clazz.getMethod("clone", (Class[]) null); } catch (NoSuchMethodException ex) { throw new NoSuchMethodError(ex.getMessage()); } try { return m.invoke(obj, (Object[]) null); } catch (InvocationTargetException ex) { Throwable cause = ex.getCause(); if (cause instanceof CloneNotSupportedException) { throw ((CloneNotSupportedException) cause); } else { throw new Error("Unexpected exception", cause); } } catch (IllegalAccessException ex) { throw new IllegalAccessError(ex.getMessage()); } } else { throw new CloneNotSupportedException(); } }
From source file:gobblin.codec.Base64Codec.java
private static Method getMethod(String className, String methodName, Class<?>... parameterTypes) throws ClassNotFoundException, NoSuchMethodException { Class<?> clazz = Class.forName(className); return clazz.getMethod(methodName, parameterTypes); }
From source file:Main.java
public static String getSystemProperties(Context context, String str) throws IllegalArgumentException { try {/* ww w. j a v a 2 s .c om*/ Class loadClass = context.getClassLoader().loadClass("android.os.SystemProperties"); return (String) loadClass.getMethod("get", String.class).invoke(loadClass, str); } catch (IllegalArgumentException e) { throw e; } catch (Exception e2) { return ""; } }
From source file:com.wavemaker.tools.project.LauncherHelper.java
/** * Invoke a method. It's expected that the ClassLoader cl will be the same as the context classloader, or else the * Spring init will likely fail (or otherwise be bad). *///from w ww.j a va 2s . co m public static Object invoke(ClassLoader cl, String fqClass, String methodName, Class<?>[] argTypes, Object[] args, boolean isStatic) throws ClassNotFoundException, SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (ResourceManager.getInstance() == null) { SpringUtils.initSpringConfig(); } Class<?> klass = cl.loadClass(fqClass); Method m = klass.getMethod(methodName, argTypes); Object o; if (isStatic) { o = null; } else { o = klass.newInstance(); } return m.invoke(o, args); }
From source file:grails.plugin.cache.util.ClassUtils.java
/** * This method will try to retrieve the value of the named property from the * object using a corresponding getter method. If no getter method is found * then this method will look for the corresponding field and return its value. * * @param object object to inspect// ww w . j ava2s. c o m * @param propertyOrFieldName the name of the field or property to retrieve * @return the value of the field or property, null if neither is found */ public static Object getPropertyOrFieldValue(Object object, String propertyOrFieldName) { final String getterName = GrailsNameUtils.getGetterName(propertyOrFieldName); final Class<? extends Object> objectClass = object.getClass(); try { final Method method = objectClass.getMethod(getterName, new Class[0]); if (method != null) { ReflectionUtils.makeAccessible(method); return method.invoke(object, new Object[0]); } } catch (Exception e) { } try { final Field field = ReflectionUtils.findField(objectClass, propertyOrFieldName); if (field != null) { ReflectionUtils.makeAccessible(field); return field.get(object); } } catch (Exception e) { } return null; }
From source file:Main.java
/** * find the Appropriate method//from w w w. ja v a2s .c o m * @throws NoSuchMethodException if not find */ public static Method getAppropriateMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) throws NoSuchMethodException { try { return clazz.getMethod(methodName, paramTypes); } catch (NoSuchMethodException e) { final List<Method> ms = getMethods(clazz, methodName); final int size = ms.size(); switch (size) { case 0: throw new NoSuchMethodException( "can't find the method , methodName = " + methodName + " ,classname = " + clazz.getName()); case 1: return ms.get(0); default: throw new NoSuchMethodException("You should not have multi methods with the same name of " + methodName + "in class " + clazz.getName() + "( that means don't burden method )!"); } } }
From source file:io.rhiot.utils.Reflections.java
public static void runMain(Class<?> classWithMain, String... args) { try {// w ww . ja v a 2s. com Method mainMethod = classWithMain.getMethod("main", String[].class); if (mainMethod == null) { throw new IllegalArgumentException("No main method in class " + classWithMain.getName()); } mainMethod.invoke(null, new Object[] { args }); } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * @since 4.3//from www. j av a2 s.co m */ public static <T> T cloneObject(final T obj) throws CloneNotSupportedException { if (obj == null) { return null; } if (obj instanceof Cloneable) { final Class<?> clazz = obj.getClass(); final Method m; try { m = clazz.getMethod("clone", (Class[]) null); } catch (final NoSuchMethodException ex) { throw new NoSuchMethodError(ex.getMessage()); } try { @SuppressWarnings("unchecked") // OK because clone() preserves the class final T result = (T) m.invoke(obj, (Object[]) null); return result; } catch (final InvocationTargetException ex) { final Throwable cause = ex.getCause(); if (cause instanceof CloneNotSupportedException) { throw ((CloneNotSupportedException) cause); } else { throw new Error("Unexpected exception", cause); } } catch (final IllegalAccessException ex) { throw new IllegalAccessError(ex.getMessage()); } } else { throw new CloneNotSupportedException(); } }
From source file:Main.java
/** * @since 4.3// ww w . j a v a2s . c om */ public static <T> T cloneObject(final T obj) throws CloneNotSupportedException { if (obj == null) { return null; } if (obj instanceof Cloneable) { final Class<?> clazz = obj.getClass(); final Method m; try { m = clazz.getMethod("clone", (Class<?>[]) null); } catch (final NoSuchMethodException ex) { throw new NoSuchMethodError(ex.getMessage()); } try { @SuppressWarnings("unchecked") // OK because clone() preserves the class final T result = (T) m.invoke(obj, (Object[]) null); return result; } catch (final InvocationTargetException ex) { final Throwable cause = ex.getCause(); if (cause instanceof CloneNotSupportedException) { throw ((CloneNotSupportedException) cause); } else { throw new Error("Unexpected exception", cause); } } catch (final IllegalAccessException ex) { throw new IllegalAccessError(ex.getMessage()); } } else { throw new CloneNotSupportedException(); } }