Refine search
/** * Given the source object and the destination, which must be the same class * or a subclass, copy all fields, including inherited fields. Designed to * work on objects with public no-arg constructors. * @throws IllegalStateException if introspection fails */ public static void shallowCopyFieldState(final Object src, final Object dest) { Assert.notNull(src, "Source for field copy cannot be null"); Assert.notNull(dest, "Destination for field copy cannot be null"); if (!src.getClass().isAssignableFrom(dest.getClass())) { throw new IllegalArgumentException("Destination class [" + dest.getClass().getName() + "] must be same or subclass as source class [" + src.getClass().getName() + "]"); } doWithFields(src.getClass(), field -> { makeAccessible(field); Object srcValue = field.get(src); field.set(dest, srcValue); }, COPYABLE_FIELDS); }
@Override public void addTransformer(ClassFileTransformer transformer) { Assert.notNull(transformer, "Transformer must not be null"); ReflectionUtils.invokeMethod(this.addTransformerMethod, this.classLoader, transformer); }
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; }
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); }
private ConfigurableBeanFactory getBeanFactory(Object enhancedConfigInstance) { Field field = ReflectionUtils.findField(enhancedConfigInstance.getClass(), BEAN_FACTORY_FIELD); Assert.state(field != null, "Unable to find generated bean factory field"); Object beanFactory = ReflectionUtils.getField(field, enhancedConfigInstance); Assert.state(beanFactory != null, "BeanFactory has not been injected into @Configuration class"); Assert.state(beanFactory instanceof ConfigurableBeanFactory, "Injected BeanFactory is not a ConfigurableBeanFactory"); return (ConfigurableBeanFactory) beanFactory; }
@Override @Nullable protected Object getRawAttributeValue(Method attributeMethod) { ReflectionUtils.makeAccessible(attributeMethod); return ReflectionUtils.invokeMethod(attributeMethod, getSource()); }
@Nullable public static Object invokeGetterMethod(Object target, String name) { Assert.notNull(target, "Target object must not be null"); Assert.hasText(name, "Method name must not be empty"); getterMethodName = GETTER_PREFIX + StringUtils.capitalize(name); Method method = ReflectionUtils.findMethod(target.getClass(), getterMethodName); if (method == null && !getterMethodName.equals(name)) { getterMethodName = name; method = ReflectionUtils.findMethod(target.getClass(), getterMethodName); logger.debug(String.format("Invoking getter method '%s' on %s", getterMethodName, safeToString(target))); ReflectionUtils.makeAccessible(method); return ReflectionUtils.invokeMethod(method, target);
public JBossLoadTimeWeaver(@Nullable ClassLoader classLoader) { Assert.notNull(classLoader, "ClassLoader must not be null"); this.classLoader = classLoader; Field transformer = ReflectionUtils.findField(classLoader.getClass(), "transformer"); if (transformer == null) { throw new IllegalArgumentException("Could not find 'transformer' field on JBoss ClassLoader: " + Method addTransformer = ReflectionUtils.findMethod(this.delegatingTransformer.getClass(), "addTransformer", ClassFileTransformer.class); if (addTransformer == null) {
AuthenticationManagerDelegator(AuthenticationManagerBuilder delegateBuilder, ApplicationContext context) { Assert.notNull(delegateBuilder, "delegateBuilder cannot be null"); Field parentAuthMgrField = ReflectionUtils.findField( AuthenticationManagerBuilder.class, "parentAuthenticationManager"); ReflectionUtils.makeAccessible(parentAuthMgrField); beanNames = getAuthenticationManagerBeanNames(context); validateBeanCycle( ReflectionUtils.getField(parentAuthMgrField, delegateBuilder), beanNames); this.delegateBuilder = delegateBuilder; }
private static void setField(Object target, String name, Object value) { Assert.notNull(target, "Target object must not be null"); Field field = ReflectionUtils.findField(target.getClass(), name); if (field == null) { throw new IllegalStateException("Could not find field [" + name + "] on target [" + target + "]"); } if (logger.isDebugEnabled()) { logger.debug("Getting field [" + name + "] from target [" + target + "]"); } ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, target, value); }
private JCacheOperation<?> getCacheOperation(Class<?> targetType, String methodName, Class<?>... parameterTypes) { Method method = ReflectionUtils.findMethod(targetType, methodName, parameterTypes); Assert.notNull(method, "requested method '" + methodName + "'does not exist"); return source.getCacheOperation(method, targetType); }
@Override @Nullable public SessionFactory getObject() { EntityManagerFactory emf = getEntityManagerFactory(); Assert.state(emf != null, "EntityManagerFactory must not be null"); try { Method getSessionFactory = emf.getClass().getMethod("getSessionFactory"); return (SessionFactory) ReflectionUtils.invokeMethod(getSessionFactory, emf); } catch (NoSuchMethodException ex) { throw new IllegalStateException("No compatible Hibernate EntityManagerFactory found: " + ex); } }
@Nullable public static Object getField(@Nullable Object targetObject, @Nullable Class<?> targetClass, String name) { Assert.isTrue(targetObject != null || targetClass != null, "Either targetObject or targetClass for the field must be specified"); Field field = ReflectionUtils.findField(targetClass, name); if (field == null) { throw new IllegalArgumentException(String.format("Could not find field '%s' on %s or target class [%s]", safeToString(targetObject), targetClass)); ReflectionUtils.makeAccessible(field); return ReflectionUtils.getField(field, targetObject);
public static void setField(@Nullable Object targetObject, @Nullable Class<?> targetClass, @Nullable String name, @Nullable Object value, @Nullable Class<?> type) { Assert.isTrue(targetObject != null || targetClass != null, "Either targetObject or targetClass for the field must be specified"); Field field = ReflectionUtils.findField(targetClass, name, type); if (field == null) { throw new IllegalArgumentException(String.format( safeToString(targetObject), targetClass, value)); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, targetObject, value);
@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); }
@Override @Nullable public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { Field field = ReflectionUtils.findField(obj.getClass(), BEAN_FACTORY_FIELD); Assert.state(field != null, "Unable to find generated BeanFactory field"); field.set(obj, args[0]); // Does the actual (non-CGLIB) superclass implement BeanFactoryAware? // If so, call its setBeanFactory() method. If not, just exit. if (BeanFactoryAware.class.isAssignableFrom(ClassUtils.getUserClass(obj.getClass().getSuperclass()))) { return proxy.invokeSuper(obj, args); } return null; }
/** * Get the native Hibernate FlushMode, adapting between Hibernate 5.0/5.1 and 5.2+. * @param session the Hibernate Session to get the flush mode from * @return the FlushMode (never {@code null}) * @since 4.3 */ static FlushMode getFlushMode(Session session) { FlushMode flushMode = (FlushMode) ReflectionUtils.invokeMethod(getFlushMode, session); Assert.state(flushMode != null, "No FlushMode from Session"); return flushMode; }
@Override protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable { Field field = (Field) this.member; Object value; desc.setContainingClass(bean.getClass()); Set<String> autowiredBeanNames = new LinkedHashSet<>(1); Assert.state(beanFactory != null, "No BeanFactory available"); TypeConverter typeConverter = beanFactory.getTypeConverter(); try { ReflectionUtils.makeAccessible(field); field.set(bean, value);
private static HttpHeaders createTomcatHttpHeaders(HttpServletResponse response) { ResponseFacade responseFacade = getResponseFacade(response); org.apache.catalina.connector.Response connectorResponse = (org.apache.catalina.connector.Response) ReflectionUtils.getField(COYOTE_RESPONSE_FIELD, responseFacade); Assert.state(connectorResponse != null, "No Tomcat connector response"); Response tomcatResponse = connectorResponse.getCoyoteResponse(); TomcatHeadersAdapter headers = new TomcatHeadersAdapter(tomcatResponse.getMimeHeaders()); return new HttpHeaders(headers); }
protected void setFieldValue(String fieldname, Object value, Object object) { Field f = ReflectionUtils.findField(object.getClass(), fieldname); ReflectionUtils.makeAccessible(f); ReflectionUtils.setField(f, object, value); }