List of usage examples for java.lang Class getDeclaredMethod
@CallerSensitive public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:com.thoughtworks.go.util.NestedJarClassLoader.java
private Method findNonPublicMethod(String name, Class klass, Class... args) { try {// ww w . j a v a2s. c o m Method method = klass.getDeclaredMethod(name, args); method.setAccessible(true); return method; } catch (NoSuchMethodException e) { return findNonPublicMethod(name, klass.getSuperclass(), args); } }
From source file:com.ruesga.rview.TabFragmentActivity.java
@SuppressWarnings("ConfusingArgumentToVarargsMethod") private void openFragment(String fragmentName, ArrayList<String> args) throws IllegalArgumentException { Fragment fragment;//w w w . jav a 2 s. c o m try { Class<?> cls = Class.forName(fragmentName); Method m = cls.getDeclaredMethod("newFragment", ArrayList.class); fragment = (Fragment) m.invoke(null, args); } catch (Exception cause) { throw new IllegalArgumentException(cause); } FragmentTransaction tx = getSupportFragmentManager().beginTransaction().setReorderingAllowed(false); tx.replace(R.id.content, fragment, FRAGMENT_TAG_LIST); tx.commit(); }
From source file:com.springframework.core.annotation.AnnotationUtils.java
/** * Find a single {@link Annotation} of {@code annotationType} on the supplied * {@link Method}, traversing its super methods (i.e., from superclasses and * interfaces) if no annotation can be found on the given method itself. * <p>Correctly handles bridge {@link Method Methods} generated by the compiler. * <p>Meta-annotations will be searched if the annotation is not * <em>directly present</em> on the method. * <p>Annotations on methods are not inherited by default, so we need to handle * this explicitly./*from w ww .j a v a 2 s . c o m*/ * @param method the method to look for annotations on * @param annotationType the annotation type to look for * @return the matching annotation, or {@code null} if not found * @see #getAnnotation(Method, Class) */ @SuppressWarnings("unchecked") public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType) { AnnotationCacheKey cacheKey = new AnnotationCacheKey(method, annotationType); A result = (A) findAnnotationCache.get(cacheKey); if (result == null) { Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method); result = findAnnotation((AnnotatedElement) resolvedMethod, annotationType); if (result == null) { result = searchOnInterfaces(method, annotationType, method.getDeclaringClass().getInterfaces()); } Class<?> clazz = method.getDeclaringClass(); while (result == null) { clazz = clazz.getSuperclass(); if (clazz == null || clazz.equals(Object.class)) { break; } try { Method equivalentMethod = clazz.getDeclaredMethod(method.getName(), method.getParameterTypes()); Method resolvedEquivalentMethod = BridgeMethodResolver.findBridgedMethod(equivalentMethod); result = findAnnotation((AnnotatedElement) resolvedEquivalentMethod, annotationType); } catch (NoSuchMethodException ex) { // No equivalent method found } if (result == null) { result = searchOnInterfaces(method, annotationType, clazz.getInterfaces()); } } if (result != null) { findAnnotationCache.put(cacheKey, result); } } return result; }
From source file:es.caib.zkib.jxpath.util.ValueUtils.java
/** * Return an accessible method (that is, one that can be invoked via * reflection) that implements the specified Method. If no such method * can be found, return <code>null</code>. * * @param method The method that we wish to call * @return Method/*from w ww. j a v a 2 s. co m*/ */ public static Method getAccessibleMethod(Method method) { // Make sure we have a method to check if (method == null) { return (null); } // If the requested method is not public we cannot call it if (!Modifier.isPublic(method.getModifiers())) { return (null); } // If the declaring class is public, we are done Class clazz = method.getDeclaringClass(); if (Modifier.isPublic(clazz.getModifiers())) { return (method); } String name = method.getName(); Class[] parameterTypes = method.getParameterTypes(); while (clazz != null) { // Check the implemented interfaces and subinterfaces Method aMethod = getAccessibleMethodFromInterfaceNest(clazz, name, parameterTypes); if (aMethod != null) { return aMethod; } clazz = clazz.getSuperclass(); if (clazz != null && Modifier.isPublic(clazz.getModifiers())) { try { return clazz.getDeclaredMethod(name, parameterTypes); } catch (NoSuchMethodException e) { //NOPMD //ignore } } } return null; }
From source file:com.spectralogic.ds3client.metadata.WindowsMetadataRestore_Test.java
@Test public void restorePermissions_Test() throws NoSuchMethodException, IOException, URISyntaxException, InvocationTargetException, IllegalAccessException, InterruptedException { final ImmutableMap.Builder<String, String> mMetadataMap = new ImmutableMap.Builder<>(); final WindowsMetadataStore windowsMetadataStore = new WindowsMetadataStore(mMetadataMap); final Class aClass = WindowsMetadataStore.class; final Method method = aClass.getDeclaredMethod("saveFlagMetaData", new Class[] { Path.class }); method.setAccessible(true);/*w w w . jav a 2 s. c om*/ final File file = ResourceUtils.loadFileResource(FILE_NAME).toFile(); method.invoke(windowsMetadataStore, file.toPath()); final Method methodWindowsfilePermissions = aClass.getDeclaredMethod("saveWindowsfilePermissions", new Class[] { Path.class }); methodWindowsfilePermissions.setAccessible(true); methodWindowsfilePermissions.invoke(windowsMetadataStore, file.toPath()); final Set<String> mapKeys = mMetadataMap.build().keySet(); final int keySize = mapKeys.size(); final BasicHeader basicHeader[] = new BasicHeader[keySize + 1]; basicHeader[0] = new BasicHeader(METADATA_PREFIX + MetadataKeyConstants.KEY_OS, localOS); int count = 1; for (final String key : mapKeys) { basicHeader[count] = new BasicHeader(key, mMetadataMap.build().get(key)); count++; } final Metadata metadata = genMetadata(basicHeader); final WindowsMetadataRestore windowsMetadataRestore = new WindowsMetadataRestore(metadata, file.getPath(), MetaDataUtil.getOS()); windowsMetadataRestore.restoreOSName(); windowsMetadataRestore.restorePermissions(); }
From source file:org.apache.axis2.jaxws.message.databinding.impl.ClassFinderImpl.java
public void updateClassPath(final String filePath, final ClassLoader cl) throws Exception { if (filePath == null) { return;/*from w ww. j av a2s .c o m*/ } if (filePath.length() == 0) { return; } if (cl instanceof URLClassLoader) { //lets add the path to the classloader. try { AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws Exception { URLClassLoader ucl = (URLClassLoader) cl; //convert file path to URL. File file = new File(filePath); URL url = file.toURI().toURL(); Class uclClass = URLClassLoader.class; Method method = uclClass.getDeclaredMethod("addURL", new Class[] { URL.class }); method.setAccessible(true); method.invoke(ucl, new Object[] { url }); return ucl; } }); } catch (PrivilegedActionException e) { if (log.isDebugEnabled()) { log.debug("Exception thrown from AccessController: " + e); } throw ExceptionFactory.makeWebServiceException(e.getException()); } } }
From source file:org.cloudfoundry.practical.demo.web.controller.CompilerController.java
private void invokeMain(URLClassLoader classLoader) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { Class<?> main = Class.forName("Main", true, classLoader); Method method = main.getDeclaredMethod("main", String[].class); method.invoke(null, new Object[] { new String[] {} }); }
From source file:com.spectralogic.ds3client.metadata.WindowsMetadataRestore_Test.java
@Test public void restoreUserAndOwner_Test() throws Exception { final ImmutableMap.Builder<String, String> metadataMap = new ImmutableMap.Builder<>(); final WindowsMetadataStore windowsMetadataStore = new WindowsMetadataStore(metadataMap); final Class aClass = WindowsMetadataStore.class; final Method method = aClass.getDeclaredMethod("saveWindowsDescriptors", new Class[] { Path.class }); method.setAccessible(true);/*w w w. j a v a2s . c om*/ final String tempPathPrefix = null; final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix); final String fileName = "Gracie.txt"; try { final Path filePath = Files.createFile(Paths.get(tempDirectory.toString(), fileName)); method.invoke(windowsMetadataStore, filePath); final BasicHeader basicHeader[] = new BasicHeader[3]; basicHeader[0] = new BasicHeader(METADATA_PREFIX + KEY_OWNER, metadataMap.build().get(METADATA_PREFIX + KEY_OWNER)); basicHeader[1] = new BasicHeader(METADATA_PREFIX + KEY_GROUP, metadataMap.build().get(METADATA_PREFIX + KEY_GROUP)); basicHeader[2] = new BasicHeader(METADATA_PREFIX + MetadataKeyConstants.KEY_OS, localOS); final Metadata metadata = genMetadata(basicHeader); final WindowsMetadataRestore windowsMetadataRestore = new WindowsMetadataRestore(metadata, filePath.toString(), MetaDataUtil.getOS()); windowsMetadataRestore.restoreOSName(); windowsMetadataRestore.restoreUserAndOwner(); final ImmutableMap.Builder<String, String> mMetadataMapAfterRestore = new ImmutableMap.Builder<>(); final WindowsMetadataStore windowsMetadataStoreAfterRestore = new WindowsMetadataStore( mMetadataMapAfterRestore); final Class aClassAfterRestore = WindowsMetadataStore.class; final Method methodAfterRestore = aClassAfterRestore.getDeclaredMethod("saveWindowsDescriptors", new Class[] { Path.class }); methodAfterRestore.setAccessible(true); methodAfterRestore.invoke(windowsMetadataStoreAfterRestore, filePath); Assert.assertEquals(mMetadataMapAfterRestore.build().get(METADATA_PREFIX + KEY_OWNER), basicHeader[0].getValue()); Assert.assertEquals(mMetadataMapAfterRestore.build().get(METADATA_PREFIX + KEY_GROUP), basicHeader[1].getValue()); } finally { FileUtils.deleteDirectory(tempDirectory.toFile()); } }
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/* w w w. j ava 2 s .co 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:eu.europa.ejusticeportal.dss.applet.model.action.OpenPdfAction.java
/** * Tries to open file using java.awt.Desktop * @param url the file to open//from w w w.ja va 2 s . c o m * @throws Exception if desktop not available */ private void openDesktop(String url) throws Exception { final Class<?> d = Class.forName("java.awt.Desktop"); LOG.log(Level.FINE, "URI to open : " + "file:///{0}", url.replace("\\", "/")); d.getDeclaredMethod("browse", new Class[] { java.net.URI.class }).invoke( d.getDeclaredMethod("getDesktop").invoke(null), new Object[] { java.net.URI.create("file:///" + url.replace("\\", "/")) }); LOG.log(Level.FINE, "java.awt.Desktop.getDesktop().browse() available"); // above code mimics: java.awt.Desktop.getDesktop().browse() }