List of usage examples for java.lang Class getDeclaredMethod
@CallerSensitive public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:org.apache.bval.util.reflection.Reflection.java
/** * Get the declared method from {@code clazz}. * @param clazz/*from w w w.j a va 2 s . c om*/ * @param name * @param parameters * @return {@link Method} or {@code null} */ public static Method getDeclaredMethod(final Class<?> clazz, final String name, final Class<?>... parameters) { try { return clazz.getDeclaredMethod(name, parameters); } catch (final NoSuchMethodException e) { return null; } }
From source file:org.apache.kylin.monitor.ConfigUtils.java
public static void addClasspath(String path) throws Exception { File file = new File(path); if (file.exists()) { URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class<URLClassLoader> urlClass = URLClassLoader.class; Method method = urlClass.getDeclaredMethod("addURL", new Class[] { URL.class }); method.setAccessible(true);/*w w w .jav a2 s . com*/ method.invoke(urlClassLoader, new Object[] { file.toURI().toURL() }); } }
From source file:com.googlecode.struts2gwtplugin.interceptor.GWTServlet.java
/** * Find the invoked method on either the specified interface or any super. *//*w w w. j a v a 2 s . c o m*/ private static Method findInterfaceMethod(Class<?> intf, String methodName, Class<?>[] paramTypes, boolean includeInherited) { try { return intf.getDeclaredMethod(methodName, paramTypes); } catch (NoSuchMethodException e) { if (includeInherited) { Class<?>[] superintfs = intf.getInterfaces(); for (int i = 0; i < superintfs.length; i++) { Method method = findInterfaceMethod(superintfs[i], methodName, paramTypes, true); if (method != null) { return method; } } } return null; } }
From source file:Main.java
static void initReflection() { try {//from w ww .j a va2 s. c o m // lightweight dispatcher dispatcherField = Container.class.getDeclaredField("dispatcher"); dispatcherField.setAccessible(true); Class<?> dispatcherCls = Class.forName("java.awt.LightweightDispatcher"); newLightweightDispatcher = dispatcherCls.getDeclaredConstructor(new Class[] { Container.class }); newLightweightDispatcher.setAccessible(true); dispatchMethod = dispatcherCls.getDeclaredMethod("dispatchEvent", AWTEvent.class); dispatchMethod.setAccessible(true); enableEvents = dispatcherCls.getDeclaredMethod("enableEvents", new Class[] { long.class }); enableEvents.setAccessible(true); } catch (Exception ex) { System.err.println(ex); InternalError err = new InternalError(); err.initCause(ex); throw err; } }
From source file:com.haulmont.bali.util.ReflectionHelper.java
/** * Searches for a method by its name and arguments. * @param c class/*from w w w . j a v a 2 s .com*/ * @param name method name * @param params method arguments * @return method reference or null if a suitable method not found */ @Nullable public static Method findMethod(Class c, String name, Object... params) { Class[] paramTypes = getParamTypes(params); Method method = null; try { method = c.getDeclaredMethod(name, paramTypes); } catch (NoSuchMethodException e) { try { method = c.getMethod(name, paramTypes); } catch (NoSuchMethodException e1) { // } } if (method != null) method.setAccessible(true); return method; }
From source file:com.xlson.standalonewar.Starter.java
/** * Add url to the system class loader./* w w w.j a va2 s . c o m*/ */ public static void appendClasspath(URL url) throws IOException { URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class sysclass = URLClassLoader.class; try { Method method = sysclass.getDeclaredMethod("addURL", parameters); method.setAccessible(true); method.invoke(sysloader, new Object[] { url }); } catch (Throwable t) { t.printStackTrace(); throw new IOException("Error, could not add URL to system classloader"); } }
From source file:app.commons.ReflectionUtils.java
/** * We cannot use <code>clazz.getMethod(String name, Class<?>... parameterTypes)</code> because it only returns public methods. *///from ww w .j a v a 2 s . c om private static Method getMethod(Class clazz, String methodName, Class<?>... parameterTypes) { Class currentClass = clazz; while (currentClass != null) { try { Method method = currentClass.getDeclaredMethod(methodName, parameterTypes); method.setAccessible(true); return method; } catch (NoSuchMethodException e) { currentClass = currentClass.getSuperclass(); } } throw new RuntimeException(new NoSuchMethodException(methodName)); }
From source file:Main.java
public static <T> T getPrivateMethod(Class<?> outerClass, String innerClassName, Object obj, String methodName, Class<?> parameterTypes[], Object parameters[], Class<T> returnType) throws Exception { // Get all inner classes Class<?> innerClasses[] = outerClass.getDeclaredClasses(); // find the inner class that matches the order Class<?> innerClass = null; for (int index = 0; index < innerClasses.length; index++) { if (innerClassName.equals(innerClasses[index].getSimpleName())) { innerClass = innerClasses[index]; }/*from w w w . jav a 2 s. com*/ } T returnValue = null; if (innerClass != null) { Method method = innerClass.getDeclaredMethod(methodName, parameterTypes); method.setAccessible(true); returnValue = (T) method.invoke(obj, parameters); } return returnValue; }
From source file:it.geosolutions.geobatch.action.scripting.ScriptingAction.java
/** * /*from ww w . ja v a2 s. c o m*/ * @param moduleFile * @throws IOException */ private static void addFile(File moduleFile) throws IOException { URL moduleURL = moduleFile.toURI().toURL(); final Class[] parameters = new Class[] { URL.class }; URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class sysclass = URLClassLoader.class; try { Method method = sysclass.getDeclaredMethod("addURL", parameters); method.setAccessible(true); method.invoke(sysloader, new Object[] { moduleURL }); } catch (Throwable t) { LOGGER.error("Error, could not add URL to system classloader", t); throw new IOException("Error, could not add URL to system classloader"); } }
From source file:Main.java
public static Object setObjectFileValue(Object obj, Map<String, String> data) throws Exception { Class<?> cls = obj.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { Class<?> clsType = field.getType(); String name = field.getName(); String strSet = "set" + name.substring(0, 1).toUpperCase() + name.substring(1, name.length()); Method methodSet = cls.getDeclaredMethod(strSet, clsType); if (data.containsKey(name)) { Object objValue = typeConversion(clsType, data.get(name)); methodSet.invoke(obj, objValue); }/*w w w . j a v a 2 s .co m*/ } return obj; }