public void invoke(Object target) throws Throwable { ReflectionUtils.makeAccessible(this.method); this.method.invoke(target, (Object[]) null); }
/** * Obtain an accessible constructor for the given class and parameters. * @param clazz the clazz to check * @param parameterTypes the parameter types of the desired constructor * @return the constructor reference * @throws NoSuchMethodException if no such constructor exists * @since 5.0 */ public static <T> Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>... parameterTypes) throws NoSuchMethodException { Constructor<T> ctor = clazz.getDeclaredConstructor(parameterTypes); makeAccessible(ctor); return ctor; }
protected int getVarType(ReflectionVar v) { try { ReflectionUtils.makeAccessible(varTypeField); return (Integer) varTypeField.get(v); } catch (IllegalAccessException ex) { throw new IllegalStateException(ex); } } }
public RuntimeTestWalker(ShadowMatch shadowMatch) { try { ReflectionUtils.makeAccessible(residualTestField); this.runtimeTest = (Test) residualTestField.get(shadowMatch); } catch (IllegalAccessException ex) { throw new IllegalStateException(ex); } }
private <T> T getFieldValue(Object object, String fieldName, Class<T> fieldType) { Field field = ReflectionUtils.findField(object.getClass(), fieldName, fieldType); ReflectionUtils.makeAccessible(field); return (T) ReflectionUtils.getField(field, object); }
@Override protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable { Class<?> referenceClass = field.getType(); referenceBean = buildReferenceBean(reference, referenceClass); ReflectionUtils.makeAccessible(field); field.set(bean, referenceBean.getObject()); }
@Override @Nullable protected Object getRawAttributeValue(Method attributeMethod) { ReflectionUtils.makeAccessible(attributeMethod); return ReflectionUtils.invokeMethod(attributeMethod, getSource()); }
@Override protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable { Class<?> referenceClass = pd.getPropertyType(); referenceBean = buildReferenceBean(reference, referenceClass); ReflectionUtils.makeAccessible(method); method.invoke(bean, referenceBean.getObject()); }
@Override protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable { Class<?> referenceClass = pd.getPropertyType(); referenceBean = buildReferenceBean(reference, referenceClass); ReflectionUtils.makeAccessible(method); method.invoke(bean, referenceBean.getObject()); }
@Override protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable { Class<?> referenceClass = field.getType(); referenceBean = buildReferenceBean(reference, referenceClass); ReflectionUtils.makeAccessible(field); field.set(bean, referenceBean.getObject()); }
private <T> T getFieldValue(Object object, String fieldName, Class<T> fieldType) { Field field = ReflectionUtils.findField(object.getClass(), fieldName, fieldType); ReflectionUtils.makeAccessible(field); return (T) ReflectionUtils.getField(field, object); }
@Override public void run() { try { ReflectionUtils.makeAccessible(this.method); this.method.invoke(this.target); } catch (InvocationTargetException ex) { ReflectionUtils.rethrowRuntimeException(ex.getTargetException()); } catch (IllegalAccessException ex) { throw new UndeclaredThrowableException(ex); } }
@Override public void setValue(@Nullable Object value) throws Exception { try { ReflectionUtils.makeAccessible(this.field); this.field.set(getWrappedInstance(), value); } catch (IllegalAccessException ex) { throw new InvalidPropertyException(getWrappedClass(), this.field.getName(), "Field is not accessible", ex); } } }
@Override @Nullable public Object getValue() throws Exception { try { ReflectionUtils.makeAccessible(this.field); return this.field.get(getWrappedInstance()); } catch (IllegalAccessException ex) { throw new InvalidPropertyException(getWrappedClass(), this.field.getName(), "Field is not accessible", ex); } }
private static boolean isClassLoaded(String className) { ClassLoader cl = ClassUtils.getDefaultClassLoader(); Method findLoadedClassMethod = ReflectionUtils.findMethod(cl.getClass(), "findLoadedClass", String.class); ReflectionUtils.makeAccessible(findLoadedClassMethod); Class<?> loadedClass = (Class<?>) ReflectionUtils.invokeMethod(findLoadedClassMethod, cl, className); return loadedClass != null; }
@Test(expected = IllegalArgumentException.class) public void encodeHeaderFieldParamInvalidCharset() { Method encode = ReflectionUtils.findMethod(ContentDisposition.class, "encodeHeaderFieldParam", String.class, Charset.class); ReflectionUtils.makeAccessible(encode); ReflectionUtils.invokeMethod(encode, null, "test", StandardCharsets.UTF_16); }
@Test(expected = IllegalArgumentException.class) public void decodeHeaderFieldParamInvalidCharset() { Method decode = ReflectionUtils.findMethod(ContentDisposition.class, "decodeHeaderFieldParam", String.class); ReflectionUtils.makeAccessible(decode); ReflectionUtils.invokeMethod(decode, null, "UTF-16''test"); }
@Test // SPR-14547 public void encodeHeaderFieldParam() { Method encode = ReflectionUtils.findMethod(ContentDisposition.class, "encodeHeaderFieldParam", String.class, Charset.class); ReflectionUtils.makeAccessible(encode); String result = (String)ReflectionUtils.invokeMethod(encode, null, "test.txt", StandardCharsets.US_ASCII); assertEquals("test.txt", result); result = (String)ReflectionUtils.invokeMethod(encode, null, "中文.txt", StandardCharsets.UTF_8); assertEquals("UTF-8''%E4%B8%AD%E6%96%87.txt", result); }
@Test // SPR-14408 public void decodeHeaderFieldParam() { Method decode = ReflectionUtils.findMethod(ContentDisposition.class, "decodeHeaderFieldParam", String.class); ReflectionUtils.makeAccessible(decode); String result = (String)ReflectionUtils.invokeMethod(decode, null, "test.txt"); assertEquals("test.txt", result); result = (String)ReflectionUtils.invokeMethod(decode, null, "UTF-8''%E4%B8%AD%E6%96%87.txt"); assertEquals("中文.txt", result); }
@Test public void setField() { TestObjectSubclassWithNewField testBean = new TestObjectSubclassWithNewField(); Field field = ReflectionUtils.findField(TestObjectSubclassWithNewField.class, "name", String.class); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, testBean, "FooBar"); assertNotNull(testBean.getName()); assertEquals("FooBar", testBean.getName()); ReflectionUtils.setField(field, testBean, null); assertNull(testBean.getName()); }