List of usage examples for java.lang.reflect Method setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:app.commons.ReflectionUtils.java
/** * We cannot use <code>clazz.getMethod(String name, Class<?>... parameterTypes)</code> because it only returns public methods. *///ww w. j a v a 2s . c o m 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:com.sunway.cbm.util.web.ReflectionUtils.java
/** * ?, ?DeclaredMethod,?. ?Object?, null. * /*from www . j a v a 2s. c om*/ * ?. ?Method,?Method.invoke(Object obj, Object... * args) */ public static Method getAccessibleMethod(final Object obj, final String methodName, final Class<?>... parameterTypes) { AssertUtils.notNull(obj, "object?"); for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass .getSuperclass()) { try { Method method = superClass.getDeclaredMethod(methodName, parameterTypes); method.setAccessible(true); return method; } catch (NoSuchMethodException e) {// NOSONAR // Method??,? } } return null; }
From source file:io.pivotal.spring.xd.jdbcgpfdist.TestUtils.java
@SuppressWarnings("unchecked") public static <T> T callMethod(String name, Object target, Object[] args, Class<?>[] argsTypes) throws Exception { Class<?> clazz = target.getClass(); Method method = ReflectionUtils.findMethod(clazz, name, argsTypes); if (method == null) throw new IllegalArgumentException( "Cannot find method '" + method + "' in the class hierarchy of " + target.getClass()); method.setAccessible(true); return (T) ReflectionUtils.invokeMethod(method, target, args); }
From source file:com.xhsoft.framework.common.utils.ReflectionUtils.java
/** * <p>Description:?, ?DeclaredMethod,?. * ?Object?, null. * ?. ?Method,?Method.invoke(Object obj, Object... args) * </p>//from ww w .java2 s . com * @param obj * @param methodName * @param parameterTypes * @return Method */ public static Method getAccessibleMethod(final Object obj, final String methodName, final Class<?>... parameterTypes) { Assert.notNull(obj, "object?"); for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass .getSuperclass()) { try { Method method = superClass.getDeclaredMethod(methodName, parameterTypes); method.setAccessible(true); return method; } catch (NoSuchMethodException e) {//NOSONAR /* Method??,?*/ } } return null; }
From source file:com.atlassian.connector.eclipse.internal.crucible.ui.operations.CrucibleFileInfoCompareEditorInput.java
private static void hackGalileo(Viewer contentViewer, TextMergeViewer textMergeViewer, final MergeSourceViewer fLeft, final MergeSourceViewer fRight) { // FIXME: hack for e3.5 try {// w w w . ja va 2 s . co m Method getCompareConfiguration = ContentMergeViewer.class.getDeclaredMethod("getCompareConfiguration"); getCompareConfiguration.setAccessible(true); CompareConfiguration cc = (CompareConfiguration) getCompareConfiguration.invoke(textMergeViewer); Method getMergeContentProvider = ContentMergeViewer.class.getDeclaredMethod("getMergeContentProvider"); getMergeContentProvider.setAccessible(true); IMergeViewerContentProvider cp = (IMergeViewerContentProvider) getMergeContentProvider .invoke(textMergeViewer); Method getSourceViewer = MergeSourceViewer.class.getDeclaredMethod("getSourceViewer"); Method configureSourceViewer = TextMergeViewer.class.getDeclaredMethod("configureSourceViewer", SourceViewer.class, boolean.class); configureSourceViewer.setAccessible(true); configureSourceViewer.invoke(contentViewer, getSourceViewer.invoke(fLeft), cc.isLeftEditable() && cp.isLeftEditable(textMergeViewer.getInput())); configureSourceViewer.invoke(contentViewer, getSourceViewer.invoke(fRight), cc.isRightEditable() && cp.isRightEditable(textMergeViewer.getInput())); Field isConfiguredField = TextMergeViewer.class.getDeclaredField("isConfigured"); isConfiguredField.setAccessible(true); isConfiguredField.set(contentViewer, true); } catch (Throwable t) { // ignore as it may not exist in other versions } }
From source file:com.micmiu.mvc.ferriswheel.utils.ReflectionUtils.java
/** * ?, ?DeclaredMethod,?. ?Object?, null. * <p/>/*w w w . j av a 2 s .com*/ * ?. ?Method,?Method.invoke(Object obj, Object... * args) */ public static Method getAccessibleMethod(final Object obj, final String methodName, final Class<?>... parameterTypes) { Validate.notNull(obj, "object can't be null"); for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass .getSuperclass()) { try { Method method = superClass.getDeclaredMethod(methodName, parameterTypes); method.setAccessible(true); return method; } catch (NoSuchMethodException e) {// NOSONAR // Method??,? } } return null; }
From source file:Main.java
/** * invoke a setter method/* www .j av a 2s. c om*/ * * @param setter * @param object * @param propValue */ public static void invokeSetter(Method setter, Object object, Object propValue) { if (setter == null) { throw new IllegalArgumentException("The setter method cannot be null"); } if (object == null) { throw new IllegalArgumentException("The object cannot be null"); } try { setter.setAccessible(true); setter.invoke(object, new Object[] { propValue }); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } catch (InvocationTargetException e) { throw new IllegalStateException(e); } }
From source file:org.grails.datastore.mapping.reflect.ReflectionUtils.java
/** * Make the given method accessible, explicitly setting it accessible if necessary. * The <code>setAccessible(true)</code> method is only called when actually necessary, * to avoid unnecessary conflicts with a JVM SecurityManager (if active). * * Based on the same method in Spring core. * * @param method the method to make accessible * @see java.lang.reflect.Method#setAccessible */// w w w .ja va2s . co m public static void makeAccessible(Method method) { if (!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) { method.setAccessible(true); } }
From source file:com.meiah.core.util.ReflectionUtils.java
/** * ?, ?DeclaredMethod,?.//from ww w. java 2 s. c om * ?Object?, null. * * ?. ?Method,?Method.invoke(Object obj, Object... args) */ public static Method getAccessibleMethod(final Object obj, final String methodName, final Class<?>... parameterTypes) { for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass .getSuperclass()) { try { Method method = superClass.getDeclaredMethod(methodName, parameterTypes); method.setAccessible(true); return method; } catch (NoSuchMethodException e) {//NOSONAR // Method??,? } } return null; }
From source file:com.xhsoft.framework.common.utils.ReflectUtil.java
/** * <p>Description:setFieldValue</p> * @param target/* w w w . j ava2 s . c om*/ * @param fname * @param ftype * @param fvalue * @return void */ @SuppressWarnings("unchecked") public static void setFieldValue(Object target, String fname, Class ftype, Object fvalue) { if (target == null || fname == null || "".equals(fname) || (fvalue != null && !ftype.isAssignableFrom(fvalue.getClass()))) { return; } Class clazz = target.getClass(); try { Method method = clazz .getDeclaredMethod("set" + Character.toUpperCase(fname.charAt(0)) + fname.substring(1), ftype); if (!Modifier.isPublic(method.getModifiers())) { method.setAccessible(true); } method.invoke(target, fvalue); } catch (Exception me) { try { Field field = clazz.getDeclaredField(fname); if (!Modifier.isPublic(field.getModifiers())) { field.setAccessible(true); } field.set(target, fvalue); } catch (Exception fe) { if (logger.isDebugEnabled()) { logger.debug(fe); } } } }