List of usage examples for java.lang.reflect Method setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:com.krawler.runtime.utils.URLClassLoaderUtil.java
public void addJarURL(URL u) throws Exception { try {/* w w w . ja va 2s .c o m*/ URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); URL urls[] = sysLoader.getURLs(); for (int i = 0; i < urls.length; i++) { if (StringUtils.equalsIgnoreCase(urls[i].toString(), u.toString())) { if (log.isDebugEnabled()) { log.debug("URL " + u + " is already in the CLASSPATH"); } return; } } Class sysclass = URLClassLoader.class; Method method = sysclass.getDeclaredMethod("addURL", parameters); method.setAccessible(true); method.invoke(sysLoader, new Object[] { u }); } catch (Exception e) { e.printStackTrace(); throw new Exception("Error, could not add URL to system classloader" + e.getMessage()); } }
From source file:de.elomagic.vaadin.addon.speechrecognition.SpeechRecognitionEventDataTest.java
private Gson getGson() throws Exception { Method method = SpeechRecognition.class.getDeclaredMethod("createGson"); method.setAccessible(true); return (Gson) method.invoke(null); }
From source file:org.jtwig.unit.mvc.JtwigViewTest.java
@Test(expected = BeansException.class) public void ensureAppCtxInitializationCapturesServletException() throws Throwable { JtwigView underTest = new JtwigView() { @Override/*ww w.j a v a 2s . c om*/ protected GenericServlet getGenericServlet() { GenericServlet servlet = spy(super.getGenericServlet()); try { doThrow(ServletException.class).when(servlet).init(any(ServletConfig.class)); } catch (ServletException ex) { } return servlet; } }; // Let's just use reflection to call the method try { Method meth = JtwigView.class.getDeclaredMethod("initApplicationContext"); meth.setAccessible(true); meth.invoke(underTest); } catch (InvocationTargetException ex) { throw ex.getCause(); } }
From source file:com.hc.wx.server.common.bytecode.ReflectUtils.java
public static Map<String, Method> getBeanPropertyReadMethods(Class cl) { Map<String, Method> properties = new HashMap<String, Method>(); for (; cl != null; cl = cl.getSuperclass()) { Method[] methods = cl.getDeclaredMethods(); for (Method method : methods) { if (isBeanPropertyReadMethod(method)) { method.setAccessible(true); String property = getPropertyNameFromBeanReadMethod(method); properties.put(property, method); }/* ww w. j ava 2 s .c o m*/ } } return properties; }
From source file:org.vaadin.spring.boot.CustomServletOverrideTest.java
@Test public void customServletIsInjected() throws Exception { Method getServlet = ServletRegistrationBean.class.getDeclaredMethod("getServlet"); getServlet.setAccessible(true); Servlet servlet = (Servlet) getServlet.invoke(servletRegistrationBean); assertTrue("expected MyCustomVaadinServlet, was " + servlet.getClass().getSimpleName(), servlet instanceof MyCustomVaadinServlet); }
From source file:Main.java
public static <T extends Object, Result> Result callMethodCast(T receiver, Class<Result> resultClass, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { if (receiver == null || methodName == null) { return null; }/* w w w. ja va 2 s . c om*/ Class<?> cls = receiver.getClass(); Method toInvoke = null; do { Method[] methods = cls.getDeclaredMethods(); methodLoop: for (Method method : methods) { if (!methodName.equals(method.getName())) { continue; } Class<?>[] paramTypes = method.getParameterTypes(); if (args == null && paramTypes == null) { toInvoke = method; break; } else if (args == null || paramTypes == null || paramTypes.length != args.length) { continue; } for (int i = 0; i < args.length; ++i) { if (!paramTypes[i].isAssignableFrom(args[i].getClass())) { continue methodLoop; } } toInvoke = method; } } while (toInvoke == null && (cls = cls.getSuperclass()) != null); Result t; if (toInvoke != null) { boolean accessible = toInvoke.isAccessible(); toInvoke.setAccessible(true); if (resultClass != null) { t = resultClass.cast(toInvoke.invoke(receiver, args)); } else { toInvoke.invoke(receiver, args); t = null; } toInvoke.setAccessible(accessible); return t; } else { throw new NoSuchMethodException("Method " + methodName + " not found"); } }
From source file:org.openmrs.module.metadatasharing.reflection.ReplaceMethodInovker.java
public Method getMethod(Class<?> type, String name, Class<?>... parameterTypes) { try {// w ww . ja va 2 s. c o m Method result = type.getDeclaredMethod(name, parameterTypes); result.setAccessible(true); return result; } catch (NoSuchMethodException e) { return null; } }
From source file:com.amazonaws.http.conn.ssl.privileged.PrivilegedMasterSecretValidator.java
private Object getMasterSecret(SSLSession session, String className) throws Exception { Class<?> clazz = Class.forName(className); Method method = clazz.getDeclaredMethod("getMasterSecret"); method.setAccessible(true); return method.invoke(session); }
From source file:com.aurel.track.util.PluginUtils.java
/** * Add class folders and jar files in the lib directories for all plugins. * @param tpHome the Genji home directory which includes the plugin directory * @return/*from ww w . ja v a 2 s . c o m*/ */ public static void addPluginLocationsToClassPath(String tpHome) { File[] pluginDirs = new File[0]; File directory = new File(tpHome + File.separator + HandleHome.PLUGINS_DIR); File resources = new File(tpHome + File.separator + HandleHome.XRESOURCES_DIR); File logos = new File(tpHome + File.separator + HandleHome.LOGOS_DIR); if (directory != null && directory.exists() && directory.isDirectory()) { pluginDirs = directory.listFiles(); } else { return; } // Expand Genji plugins first for (File f : pluginDirs) { if (!f.isDirectory() && f.getName().endsWith(".tpx")) { String targetDir = f.getAbsolutePath().substring(0, f.getAbsolutePath().length() - 4); File td = new File(targetDir); if (!td.exists() || (td.lastModified() < f.lastModified()) || !td.isDirectory()) { try { unzipFileIntoDirectory(f, directory); } catch (Exception e) { LOGGER.error("Problem unzipping archive: " + e.getMessage()); } } } } pluginDirs = directory.listFiles(); // Now process the directories in <TRACKPLUS_HOME>/plugins ArrayList<File> files = new ArrayList<File>(); ArrayList<File> jsdirs = new ArrayList<File>(); ArrayList<String> bundles = new ArrayList<String>(); for (File f : pluginDirs) { if (f != null && f.exists() && f.isDirectory()) { files.add(f); File classes = new File(f.getAbsolutePath() + File.separator + "classes"); if (classes != null && classes.exists() && classes.isDirectory()) { files.add(classes); } File libs = new File(f.getAbsolutePath() + File.separator + "lib"); File[] jars = new File[0]; if (libs != null && libs.exists() && libs.isDirectory()) { jars = libs.listFiles(); } for (File fj : jars) { if (fj.exists() && !fj.isDirectory() && fj.getAbsolutePath().endsWith(".jar")) { files.add(fj); } } File conf = new File(f.getAbsolutePath() + File.separator + "conf"); if (conf != null && conf.exists() && conf.isDirectory()) { files.add(conf); } File js = new File(f.getAbsolutePath() + File.separator + "js"); if (js != null && js.exists() && js.isDirectory()) { jsdirs.add(f); } bundles.add("resources." + f.getName()); } } URLClassLoader sysloader = null; try { sysloader = (URLClassLoader) StartServlet.class.getClassLoader(); Class sysclass = URLClassLoader.class; for (File file : files) { try { LOGGER.info("Adding " + file.getAbsolutePath() + " to classpath."); Method method = sysclass.getDeclaredMethod("addURL", parameters); method.setAccessible(true); method.invoke(sysloader, new Object[] { file.toURI().toURL() }); } catch (Exception t) { LOGGER.error(ExceptionUtils.getStackTrace(t)); } } try { LOGGER.info("Trying to add " + resources.getAbsolutePath() + " to classpath."); Method method = sysclass.getDeclaredMethod("addURL", parameters); method.setAccessible(true); method.invoke(sysloader, new Object[] { resources.toURI().toURL() }); } catch (Exception t) { LOGGER.info("No custom resources found, okay."); } try { LOGGER.info("Trying to add " + logos.getAbsolutePath() + " to classpath."); Method method = sysclass.getDeclaredMethod("addURL", parameters); method.setAccessible(true); method.invoke(sysloader, new Object[] { logos.toURI().toURL() }); } catch (Exception t) { LOGGER.info("No custom logos found, okay."); } } catch (Exception e) { LOGGER.warn( "URLClassloader not supported. You have to add your plugins to the Java classpath manually"); } setJavaScriptExtensionDirs(jsdirs); setBundles(bundles); return; }
From source file:hivemall.dataset.LogisticRegressionDataGeneratorUDTFWrapper.java
@Override protected Options getOptions() { Options options = null;/* w w w. j a v a 2s . co m*/ try { Method m = udtf.getClass().getDeclaredMethod("getOptions"); m.setAccessible(true); options = (Options) m.invoke(udtf); } catch (Exception e) { e.printStackTrace(); } return options; }