List of usage examples for java.lang Class getDeclaredMethod
@CallerSensitive public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
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 w w w .ja v a 2 s. c o m*/ Method method = targetClass.getDeclaredMethod(methodName, paramClasses); if (!method.isAccessible()) method.setAccessible(true); returnValue = (T) method.invoke(targetClass, params); return returnValue; }
From source file:Main.java
/** * Finds a method in the given class or any super class with the name {@code prefix + methodName} that accepts 0 * parameters.//from w w w. ja v a2s . com * * @param aClass * Class to search for method in * @param methodName * Camelcase'd method name to search for with any of the provided prefixes * @param parameterTypes * The parameter types the method signature must match. * @param prefixes * Prefixes to prepend to {@code methodName} when searching for method names, e.g. "get", "is" * @return The first method found to match the format {@code prefix + methodName} */ public static Method findMethod(Class<?> aClass, String methodName, Class<?>[] parameterTypes, String... prefixes) { for (String prefix : prefixes) { try { return aClass.getDeclaredMethod(prefix + methodName, parameterTypes); } catch (NoSuchMethodException ex) { // ignore, continue searching prefixes } } // If no method found with any prefixes search the super class aClass = aClass.getSuperclass(); return aClass == null ? null : findMethod(aClass, methodName, parameterTypes, prefixes); }
From source file:Main.java
public static <T> T runPrivateMethod(Class<?> targetClass, Object obj, String methodName, Class<?> paramClasses[], Object params[], Class<T> returnType) throws Exception { T returnValue = null;/* w w w .j a v a 2 s . c om*/ Method method = targetClass.getDeclaredMethod(methodName, paramClasses); if (!method.isAccessible()) method.setAccessible(true); returnValue = (T) method.invoke(obj, params); return returnValue; }
From source file:Main.java
public static Method getMethod(Class<?> targetClass, String name, Class<?>... parameterTypes) { if ((targetClass == null) || TextUtils.isEmpty(name)) { return null; }//from w ww .j a va 2 s.c o m try { return targetClass.getDeclaredMethod(name, parameterTypes); } catch (Exception e) { if (DEBUG) { e.printStackTrace(); } } return null; }
From source file:Main.java
public static Object invokeMethod(Class<?> methodClass, String methodName, Class<?>[] parameters, Object instance, Object... arguments) { try {// w w w.ja v a 2s . 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 boolean hasCustomEquals(Class c) { Class origClass = c;/*from ww w. j av a 2 s . c o m*/ if (_customEquals.containsKey(c)) { return _customEquals.get(c); } while (!Object.class.equals(c)) { try { c.getDeclaredMethod("equals", Object.class); _customEquals.put(origClass, true); return true; } catch (Exception ignored) { } c = c.getSuperclass(); } _customEquals.put(origClass, false); return false; }
From source file:com.fjn.helper.common.util.StreamUtil.java
public static void close(Object closeable) { if (closeable != null) { try {// w w w. j a v a 2 s.c o m if (closeable instanceof Closeable) { ((Closeable) closeable).close(); return; } } catch (Exception ex) { // ignore it; } // close(); Method closeMethod = null; Class<?> targetClass = closeable.getClass(); while (closeMethod == null && targetClass != null) { try { closeMethod = targetClass.getDeclaredMethod("close", new Class[0]); } catch (Exception ex) { targetClass = targetClass.getSuperclass(); } } if (closeMethod != null) { try { closeMethod.invoke(closeable, new Object[0]); } catch (Exception e) { // ignore it } } } }
From source file:dyco4j.instrumentation.internals.CLI.java
private static void extendClassPath(final CommandLine cmdLine) throws IOException { try {//from w ww .j a va 2 s . co m final URLClassLoader _urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); final Class<URLClassLoader> _urlClass = URLClassLoader.class; final Method _method = _urlClass.getDeclaredMethod("addURL", URL.class); _method.setAccessible(true); addEntryToClassPath(_urlClassLoader, _method, cmdLine.getOptionValue(IN_FOLDER_OPTION)); final String _classpathConfig = cmdLine.getOptionValue(CLASSPATH_CONFIG_OPTION); if (_classpathConfig != null) { for (final String _s : Files.readAllLines(Paths.get(_classpathConfig))) { addEntryToClassPath(_urlClassLoader, _method, _s); } } } catch (final NoSuchMethodException _e) { throw new RuntimeException(_e); } }
From source file:com.netflix.hystrix.contrib.javanica.utils.AopUtils.java
/** * Gets declared method from specified type by mame and parameters types. * * @param type the type/*w w w. j a v a 2 s . c o m*/ * @param methodName the name of the method * @param parameterTypes the parameter array * @return a {@link Method} object or null if method doesn't exist */ public static Method getDeclaredMethod(Class<?> type, String methodName, Class<?>... parameterTypes) { Method method = null; try { method = type.getDeclaredMethod(methodName, parameterTypes); if (method.isBridge()) { method = MethodProvider.getInstance().unbride(method, type); } } catch (NoSuchMethodException e) { Class<?> superclass = type.getSuperclass(); if (superclass != null) { method = getDeclaredMethod(superclass, methodName, parameterTypes); } } catch (ClassNotFoundException e) { Throwables.propagate(e); } catch (IOException e) { Throwables.propagate(e); } return method; }
From source file:io.hops.util.RMStorageFactory.java
private static void addToClassPath(String s) throws StorageInitializtionException { try {//w w w . ja v a 2 s .c o m File f = new File(s); URL u = f.toURI().toURL(); URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class urlClass = URLClassLoader.class; Method method = urlClass.getDeclaredMethod("addURL", new Class[] { URL.class }); method.setAccessible(true); method.invoke(urlClassLoader, new Object[] { u }); } catch (MalformedURLException ex) { throw new StorageInitializtionException(ex); } catch (IllegalAccessException ex) { throw new StorageInitializtionException(ex); } catch (IllegalArgumentException ex) { throw new StorageInitializtionException(ex); } catch (InvocationTargetException ex) { throw new StorageInitializtionException(ex); } catch (NoSuchMethodException ex) { throw new StorageInitializtionException(ex); } catch (SecurityException ex) { throw new StorageInitializtionException(ex); } }