List of usage examples for java.lang.reflect Method setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:com.jk.util.JKObjectUtil.java
/** * Call method.// www . j a va2 s . c o m * * @param obj * the obj * @param methodName * the method name * @param includePrivateMehtods * the include private mehtods * @param args * the args * @throws InvocationTargetException * the invocation target exception */ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void callMethod(final Object obj, final String methodName, final boolean includePrivateMehtods, final Object... args) throws InvocationTargetException { final Class[] intArgsClass = initParamsClasses(args); try { Class<?> current = obj.getClass(); Method method = null; while (current != Object.class) { try { method = current.getDeclaredMethod(methodName, intArgsClass); break; } catch (final NoSuchMethodException ex) { current = current.getSuperclass(); } } if (method == null) { throw new NoSuchMethodException("Mehtod is not found in " + current); } method.setAccessible(true); method.invoke(obj, args); } catch (final InvocationTargetException e) { throw new InvocationTargetException(e.getCause()); } catch (final Exception e) { throw new InvocationTargetException(e); } }
From source file:com.glaf.core.util.ReflectUtils.java
public static Object invoke(Object target, String methodName, Object[] args) { try {/*w w w .j a v a2 s . c o m*/ Class<? extends Object> clazz = target.getClass(); Method method = findMethod(clazz, methodName, args); method.setAccessible(true); return method.invoke(target, args); } catch (Exception e) { throw new RuntimeException("couldn't invoke " + methodName + " on " + target, e); } }
From source file:ja.centre.util.io.nio.MappedByteBufferWrapper.java
private void clean(final MappedByteBuffer buffer) { AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { try { Method cleanerMethod = buffer.getClass().getMethod("cleaner"); cleanerMethod.setAccessible(true); sun.misc.Cleaner cleaner = (sun.misc.Cleaner) cleanerMethod.invoke(buffer); cleaner.clean();//w ww . ja va 2s. c o m } catch (IllegalAccessException e) { States.shouldNeverReachHere(e); } catch (NoSuchMethodException e) { States.shouldNeverReachHere(e); } catch (InvocationTargetException e) { States.shouldNeverReachHere(e); } return null; } }); }
From source file:com.intellij.plugins.firstspirit.languagesupport.FirstSpiritClassPathHack.java
public void addURL(UrlClassLoader classLoader, URL u) throws Exception { if (classLoader != null) { System.out.println("Classloader loaded successfully: " + classLoader); } else {//from w w w .ja v a 2 s . c o m System.err.println("Cannot load system classloader!"); } Class sysclass = UrlClassLoader.class; try { Method method = sysclass.getDeclaredMethod("addURL", parameters); method.setAccessible(true); method.invoke(classLoader, new Object[] { u }); } catch (Exception e) { System.err.println("Error, could not add URL " + u.getPath() + " to classloader!"); e.printStackTrace(); throw e; } }
From source file:io.github.seleniumquery.by.firstgen.css.pseudoclasses.SubmitPseudoClass.java
/** * Latest HtmlUnit returns @type=button when type is not set and browser is <=IE9. We don't want that, * we want null if it is not set, so we can decide if it is submit or not. Because if it is null, * then it is :submit. If type is "button", though, it is not. * * #Cross-Driver//from ww w . ja va2 s.co m * #HtmlUnit #reflection #hack */ private String getDeclaredTypeAttributeFromHtmlUnitButton(WebElement element) { try { if (element instanceof HtmlUnitWebElement) { Method getElementMethod = HtmlUnitWebElement.class.getDeclaredMethod("getElement"); getElementMethod.setAccessible(true); Object htmlUnitElement = getElementMethod.invoke(element); if (htmlUnitElement instanceof DomElement) { DomAttr domAttr = ((DomElement) htmlUnitElement).getAttributesMap().get("type"); return domAttr == null ? null : domAttr.getNodeValue(); } } } catch (Exception e) { LOGGER.debug("Unable to retrieve declared \"type\" attribute from HtmlUnitWebElement " + element + ".", e); } return element.getAttribute("type"); }
From source file:org.slc.sli.ingestion.validation.XsdErrorHandlerTest.java
@Test @SuppressWarnings("rawtypes") public void testGetErrorMessage() throws Exception { // Test conversion of a SAX to ingestion error message. // Make the XsdErrorHandler private method getErrorMessage accessible. try {/*w w w .j av a 2s. co m*/ Class[] argClasses = new Class[1]; argClasses[0] = org.xml.sax.SAXParseException.class; Method method = XsdErrorHandler.class.getDeclaredMethod("getErrorMessage", argClasses); method.setAccessible(true); Object[] argObjects = new Object[1]; String filePath = String.format("%1$shome%1$slandingzone%1$sTestFile.xml", File.separator); when(mockedSAXParseException.getSystemId()).thenReturn(filePath); when(mockedSAXParseException.getLineNumber()).thenReturn(12581); when(mockedSAXParseException.getColumnNumber()).thenReturn(36); when(mockedSAXParseException.getMessage()).thenReturn("SAXParseException fatal error"); argObjects[0] = mockedSAXParseException; String errorMessage = method.invoke(xsdErrorHandler, argObjects).toString(); assertEquals("File TestFile.xml, Line 12581, Column 36:\nSAXParseException fatal error", errorMessage); } catch (Exception e) { // Should never happen. throw e; } }
From source file:de.xaniox.heavyspleef.core.event.EventListenerMethod.java
@SuppressWarnings("unchecked") public EventListenerMethod(Object instance, Method method) { this.instance = instance; this.method = method; if (!method.isAccessible()) { method.setAccessible(true); }/*from w w w.j a v a 2 s .c o m*/ subscribe = method.getAnnotation(Subscribe.class); Class<?>[] parameters = method.getParameterTypes(); Validate.isTrue(parameters.length == 1, "method must have only one parameter which must be a subtype of Event"); Class<?> eventClass = parameters[0]; Validate.isTrue(Event.class.isAssignableFrom(eventClass), "First parameter of method must be a subtype of Event"); this.eventClass = (Class<? extends Event>) eventClass; }
From source file:nl.tue.gale.ae.GaleConfig.java
private void doAutoInit() { try {/* w w w . j av a 2s.c o m*/ ((EventBusClient) ac.getBean("eventBusClient")).event("initall", new LinkedList<String>()); if ("true".equals(GaleUtil.getProperty("useGEB"))) { Object geb = ac.getBean("gebManager"); Method initmethod = geb.getClass().getDeclaredMethod("init"); initmethod.setAccessible(true); initmethod.invoke(geb); } } catch (Exception e) { System.out.println("auto init failed: " + e.getMessage()); e.printStackTrace(); } }
From source file:com.thoughtworks.go.util.NestedJarClassLoader.java
private Method findNonPublicMethod(String name, Class klass, Class... args) { try {/*from w ww. j a v a 2s . co m*/ Method method = klass.getDeclaredMethod(name, args); method.setAccessible(true); return method; } catch (NoSuchMethodException e) { return findNonPublicMethod(name, klass.getSuperclass(), args); } }
From source file:io.github.zlika.reproducible.ZipStripper.java
private InputStream getRawInputStream(ZipFile zip, ZipArchiveEntry ze) throws IOException { try {/*from w w w . ja va2 s . c o m*/ // Call ZipFile.getRawInputStream(ZipArchiveEntry) by reflection // because it is a private method but we need it! final Method method = zip.getClass().getDeclaredMethod("getRawInputStream", ZipArchiveEntry.class); method.setAccessible(true); return (InputStream) method.invoke(zip, ze); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new IOException(e); } }