/** * Return the class or interface to expose for the given bean class. * This is the class that will be searched for attributes and operations * (for example, checked for annotations). * <p>This implementation returns the superclass for a CGLIB proxy and * the class of the given bean else (for a JDK proxy or a plain bean class). * @param clazz the bean class (might be an AOP proxy class) * @return the bean class to expose * @see org.springframework.util.ClassUtils#getUserClass(Class) */ public static Class<?> getClassToExpose(Class<?> clazz) { return ClassUtils.getUserClass(clazz); }
/** * Return the class or interface to expose for the given bean. * This is the class that will be searched for attributes and operations * (for example, checked for annotations). * <p>This implementation returns the superclass for a CGLIB proxy and * the class of the given bean else (for a JDK proxy or a plain bean class). * @param managedBean the bean instance (might be an AOP proxy) * @return the bean class to expose * @see org.springframework.util.ClassUtils#getUserClass(Object) */ public static Class<?> getClassToExpose(Object managedBean) { return ClassUtils.getUserClass(managedBean); }
/** * Determine the default bean name for the specified bean instance. * <p>The default implementation returns the superclass name for a CGLIB * proxy and the name of the plain bean class else. * @param beanInstance the bean instance to build a default name for * @return the default bean name to use * @see org.springframework.util.ClassUtils#getUserClass(Class) */ protected String getDefaultBeanName(Object beanInstance) { return ClassUtils.getUserClass(beanInstance).getName(); }
/** * Return the user-defined class for the given instance: usually simply * the class of the given instance, but the original class in case of a * CGLIB-generated subclass. * @param instance the instance to check * @return the user-defined class */ public static Class<?> getUserClass(Object instance) { Assert.notNull(instance, "Instance must not be null"); return getUserClass(instance.getClass()); }
protected Constructor<?> getUserDeclaredConstructor(Constructor<?> constructor) { Class<?> declaringClass = constructor.getDeclaringClass(); Class<?> userClass = ClassUtils.getUserClass(declaringClass); if (userClass != declaringClass) { try { return userClass.getDeclaredConstructor(constructor.getParameterTypes()); } catch (NoSuchMethodException ex) { // No equivalent constructor on user class (superclass)... // Let's proceed with the given constructor as we usually would. } } return constructor; }
@Override public BeanWiringInfo resolveWiringInfo(Object beanInstance) { Assert.notNull(beanInstance, "Bean instance must not be null"); return new BeanWiringInfo(ClassUtils.getUserClass(beanInstance).getName(), true); }
/** * Determine whether the given class is an {@code org.springframework} * bean class that is not annotated as a user or test {@link Component}... * which indicates that there is no {@link EventListener} to be found there. * @since 5.1 */ private static boolean isSpringContainerClass(Class<?> clazz) { return (clazz.getName().startsWith("org.springframework.") && !AnnotatedElementUtils.isAnnotated(ClassUtils.getUserClass(clazz), Component.class)); }
@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; }
/** * Return the type of the contained bean. * <p>If the bean type is a CGLIB-generated class, the original * user-defined class is returned. */ @Nullable public Class<?> getBeanType() { Class<?> beanType = (this.bean instanceof String ? obtainBeanFactory().getType((String) this.bean) : this.bean.getClass()); return (beanType != null ? ClassUtils.getUserClass(beanType) : null); }
/** * Create an instance from a bean instance, method name, and parameter types. * @throws NoSuchMethodException when the method cannot be found */ public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException { Assert.notNull(bean, "Bean is required"); Assert.notNull(methodName, "Method name is required"); this.bean = bean; this.beanFactory = null; this.beanType = ClassUtils.getUserClass(bean); this.method = bean.getClass().getMethod(methodName, parameterTypes); this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(this.method); this.parameters = initMethodParameters(); }
/** * Create an instance from a bean instance and a method. */ public HandlerMethod(Object bean, Method method) { Assert.notNull(bean, "Bean is required"); Assert.notNull(method, "Method is required"); this.bean = bean; this.beanFactory = null; this.beanType = ClassUtils.getUserClass(bean); this.method = method; this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); this.parameters = initMethodParameters(); }
/** * Create an instance from a bean instance, method name, and parameter types. * @throws NoSuchMethodException when the method cannot be found */ public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException { Assert.notNull(bean, "Bean is required"); Assert.notNull(methodName, "Method name is required"); this.bean = bean; this.beanFactory = null; this.beanType = ClassUtils.getUserClass(bean); this.method = bean.getClass().getMethod(methodName, parameterTypes); this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(this.method); this.parameters = initMethodParameters(); evaluateResponseStatus(); }
@Override public void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck) throws BeansException { if (autowireMode == AUTOWIRE_CONSTRUCTOR) { throw new IllegalArgumentException("AUTOWIRE_CONSTRUCTOR not supported for existing bean instance"); } // Use non-singleton bean definition, to avoid registering bean as dependent bean. RootBeanDefinition bd = new RootBeanDefinition(ClassUtils.getUserClass(existingBean), autowireMode, dependencyCheck); bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); BeanWrapper bw = new BeanWrapperImpl(existingBean); initBeanWrapper(bw); populateBean(bd.getBeanClass().getName(), bd, bw); }
/** * Create an instance from a bean instance and a method. */ public HandlerMethod(Object bean, Method method) { Assert.notNull(bean, "Bean is required"); Assert.notNull(method, "Method is required"); this.bean = bean; this.beanFactory = null; this.beanType = ClassUtils.getUserClass(bean); this.method = method; this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); this.parameters = initMethodParameters(); evaluateResponseStatus(); }
/** * Create an instance from a bean name, a method, and a {@code BeanFactory}. * The method {@link #createWithResolvedBean()} may be used later to * re-create the {@code HandlerMethod} with an initialized bean. */ public HandlerMethod(String beanName, BeanFactory beanFactory, Method method) { Assert.hasText(beanName, "Bean name is required"); Assert.notNull(beanFactory, "BeanFactory is required"); Assert.notNull(method, "Method is required"); this.bean = beanName; this.beanFactory = beanFactory; Class<?> beanType = beanFactory.getType(beanName); if (beanType == null) { throw new IllegalStateException("Cannot resolve bean type for bean with name '" + beanName + "'"); } this.beanType = ClassUtils.getUserClass(beanType); this.method = method; this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); this.parameters = initMethodParameters(); }
@Override protected void writeToResult(Object o, HttpHeaders headers, Result result) throws Exception { try { Class<?> clazz = ClassUtils.getUserClass(o); Marshaller marshaller = createMarshaller(clazz); setCharset(headers.getContentType(), marshaller); marshaller.marshal(o, result); } catch (MarshalException ex) { throw ex; } catch (JAXBException ex) { throw new HttpMessageConversionException("Invalid JAXB setup: " + ex.getMessage(), ex); } }
/** * Assert the type of the handler that processed the request. */ public ResultMatcher handlerType(final Class<?> type) { return result -> { Object handler = result.getHandler(); assertTrue("No handler", handler != null); if (handler != null) { Class<?> actual = handler.getClass(); if (HandlerMethod.class.isInstance(handler)) { actual = ((HandlerMethod) handler).getBeanType(); } assertEquals("Handler type", type, ClassUtils.getUserClass(actual)); } }; }
@Override public void autowireBean(Object existingBean) { // Use non-singleton bean definition, to avoid registering bean as dependent bean. RootBeanDefinition bd = new RootBeanDefinition(ClassUtils.getUserClass(existingBean)); bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd.allowCaching = ClassUtils.isCacheSafe(bd.getBeanClass(), getBeanClassLoader()); BeanWrapper bw = new BeanWrapperImpl(existingBean); initBeanWrapper(bw); populateBean(bd.getBeanClass().getName(), bd, bw); }
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { if (isLogEnabled()) { String[] beanNames = beanFactory.getBeanDefinitionNames(); for (String beanName : beanNames) { String nameToLookup = beanName; if (beanFactory.isFactoryBean(beanName)) { nameToLookup = BeanFactory.FACTORY_BEAN_PREFIX + beanName; } Class<?> beanType = beanFactory.getType(nameToLookup); if (beanType != null) { Class<?> userClass = ClassUtils.getUserClass(beanType); if (userClass.isAnnotationPresent(Deprecated.class)) { BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); logDeprecatedBean(beanName, beanType, beanDefinition); } } } } }
/** * Look for handler methods in a handler. * @param handler the bean name of a handler or a handler instance */ protected void detectHandlerMethods(final Object handler) { Class<?> handlerType = (handler instanceof String ? obtainApplicationContext().getType((String) handler) : handler.getClass()); if (handlerType != null) { final Class<?> userType = ClassUtils.getUserClass(handlerType); Map<Method, T> methods = MethodIntrospector.selectMethods(userType, (MethodIntrospector.MetadataLookup<T>) method -> getMappingForMethod(method, userType)); if (logger.isTraceEnabled()) { logger.trace(formatMappings(userType, methods)); } methods.forEach((key, mapping) -> { Method invocableMethod = AopUtils.selectInvocableMethod(key, userType); registerHandlerMethod(handler, invocableMethod, mapping); }); } }