private boolean matchesBeanType(Class<?> targetType, String beanName, BeanFactory beanFactory) { Class<?> beanType = beanFactory.getType(beanName); return (beanType != null && targetType.isAssignableFrom(beanType)); }
private boolean matchesBeanType(Class<?> targetType, String beanName, BeanFactory beanFactory) { Class<?> beanType = beanFactory.getType(beanName); return (beanType != null && targetType.isAssignableFrom(beanType)); }
/** * Overridden to support the {@link #setTargetBeanName "targetBeanName"} feature. */ @Override public Class<?> getTargetClass() { Class<?> targetClass = super.getTargetClass(); if (targetClass == null && this.targetBeanName != null) { Assert.state(this.beanFactory != null, "BeanFactory must be set when using 'targetBeanName'"); targetClass = this.beanFactory.getType(this.targetBeanName); } return targetClass; }
/** * Look at bean factory metadata to work out whether this bean name, * which concludes the interceptorNames list, is an Advisor or Advice, * or may be a target. * @param beanName bean name to check * @return {@code true} if it's an Advisor or Advice */ private boolean isNamedBeanAnAdvisorOrAdvice(String beanName) { Assert.state(this.beanFactory != null, "No BeanFactory set"); Class<?> namedBeanClass = this.beanFactory.getType(beanName); if (namedBeanClass != null) { return (Advisor.class.isAssignableFrom(namedBeanClass) || Advice.class.isAssignableFrom(namedBeanClass)); } // Treat it as an target bean if we can't tell. if (logger.isDebugEnabled()) { logger.debug("Could not determine type of bean with name '" + beanName + "' - assuming it is neither an Advisor nor an Advice"); } return false; }
@Override public Class<?> getTargetClass() { Class<?> targetClass = this.targetClass; if (targetClass != null) { return targetClass; } synchronized (this) { // Full check within synchronization, entering the BeanFactory interaction algorithm only once... targetClass = this.targetClass; if (targetClass == null && this.beanFactory != null) { // Determine type of the target bean. targetClass = this.beanFactory.getType(this.targetBeanName); if (targetClass == null) { if (logger.isTraceEnabled()) { logger.trace("Getting bean with name '" + this.targetBeanName + "' for type determination"); } Object beanInstance = this.beanFactory.getBean(this.targetBeanName); targetClass = beanInstance.getClass(); } this.targetClass = targetClass; } return targetClass; } }
@Override public void setBeanFactory(BeanFactory beanFactory) { if (!StringUtils.hasText(this.targetBeanName)) { throw new IllegalArgumentException("Property 'targetBeanName' is required"); } if (!StringUtils.hasText(this.methodName)) { throw new IllegalArgumentException("Property 'methodName' is required"); } Class<?> beanClass = beanFactory.getType(this.targetBeanName); if (beanClass == null) { throw new IllegalArgumentException("Can't determine type of bean with name '" + this.targetBeanName + "'"); } this.method = BeanUtils.resolveSignature(this.methodName, beanClass); if (this.method == null) { throw new IllegalArgumentException("Unable to locate method [" + this.methodName + "] on bean [" + this.targetBeanName + "]"); } }
/** * 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 a BeanFactoryAspectInstanceFactory, providing a type that AspectJ should * introspect to create AJType metadata. Use if the BeanFactory may consider the type * to be a subclass (as when using CGLIB), and the information should relate to a superclass. * @param beanFactory the BeanFactory to obtain instance(s) from * @param name the name of the bean * @param type the type that should be introspected by AspectJ * ({@code null} indicates resolution through {@link BeanFactory#getType} via the bean name) */ public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name, @Nullable Class<?> type) { Assert.notNull(beanFactory, "BeanFactory must not be null"); Assert.notNull(name, "Bean name must not be null"); this.beanFactory = beanFactory; this.name = name; Class<?> resolvedType = type; if (type == null) { resolvedType = beanFactory.getType(name); Assert.notNull(resolvedType, "Unresolvable bean type - explicitly specify the aspect class"); } this.aspectMetadata = new AspectMetadata(resolvedType, name); }
/** * 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(); }
/** * Determine the order for this factory's target aspect, either * an instance-specific order expressed through implementing the * {@link org.springframework.core.Ordered} interface (only * checked for singleton beans), or an order expressed through the * {@link org.springframework.core.annotation.Order} annotation * at the class level. * @see org.springframework.core.Ordered * @see org.springframework.core.annotation.Order */ @Override public int getOrder() { Class<?> type = this.beanFactory.getType(this.name); if (type != null) { if (Ordered.class.isAssignableFrom(type) && this.beanFactory.isSingleton(this.name)) { return ((Ordered) this.beanFactory.getBean(this.name)).getOrder(); } return OrderUtils.getOrder(type, Ordered.LOWEST_PRECEDENCE); } return Ordered.LOWEST_PRECEDENCE; }
/** * 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(); evaluateResponseStatus(); }
/** * Return the type of the proxy. Will check the singleton instance if * already created, else fall back to the proxy interface (in case of just * a single one), the target bean type, or the TargetSource's target class. * @see org.springframework.aop.TargetSource#getTargetClass */ @Override public Class<?> getObjectType() { synchronized (this) { if (this.singletonInstance != null) { return this.singletonInstance.getClass(); } } Class<?>[] ifcs = getProxiedInterfaces(); if (ifcs.length == 1) { return ifcs[0]; } else if (ifcs.length > 1) { return createCompositeInterface(ifcs); } else if (this.targetName != null && this.beanFactory != null) { return this.beanFactory.getType(this.targetName); } else { return getTargetClass(); } }
@Test public void testPrototypeInstancesAreNotEqual() { assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(factory.getType("prototype"))); ITestBean test2 = (ITestBean) factory.getBean("prototype"); ITestBean test2_1 = (ITestBean) factory.getBean("prototype"); assertTrue("Prototype instances !=", test2 != test2_1); assertTrue("Prototype instances equal", test2.equals(test2_1)); assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(factory.getType("prototype"))); }
@Override public void setBeanFactory(BeanFactory beanFactory) { if (!(beanFactory instanceof ConfigurableBeanFactory)) { throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory); } ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory; this.scopedTargetSource.setBeanFactory(beanFactory); ProxyFactory pf = new ProxyFactory(); pf.copyFrom(this); pf.setTargetSource(this.scopedTargetSource); Assert.notNull(this.targetBeanName, "Property 'targetBeanName' is required"); Class<?> beanType = beanFactory.getType(this.targetBeanName); if (beanType == null) { throw new IllegalStateException("Cannot create scoped proxy for bean '" + this.targetBeanName + "': Target type could not be determined at the time of proxy creation."); } if (!isProxyTargetClass() || beanType.isInterface() || Modifier.isPrivate(beanType.getModifiers())) { pf.setInterfaces(ClassUtils.getAllInterfacesForClass(beanType, cbf.getBeanClassLoader())); } // Add an introduction that implements only the methods on ScopedObject. ScopedObject scopedObject = new DefaultScopedObject(cbf, this.scopedTargetSource.getTargetBeanName()); pf.addAdvice(new DelegatingIntroductionInterceptor(scopedObject)); // Add the AopInfrastructureBean marker to indicate that the scoped proxy // itself is not subject to auto-proxying! Only its target bean is. pf.addInterface(AopInfrastructureBean.class); this.proxy = pf.getProxy(cbf.getBeanClassLoader()); }
"] does not contain specified controller advice bean '" + beanName + "'"); beanType = this.beanFactory.getType(beanName); this.order = initOrderFromBeanType(beanType);
@Test public void configWithObjectReturnType() { BeanFactory factory = initBeanFactory(ConfigWithNonSpecificReturnTypes.class); assertEquals(Object.class, factory.getType("stringBean")); assertFalse(factory.isTypeMatch("stringBean", String.class)); String stringBean = factory.getBean("stringBean", String.class); assertEquals("foo", stringBean); }
ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test1"); assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(config.getObjectType())); assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(factory.getType("test1"))); assertTrue("Has correct object type", ITestBean.class.isAssignableFrom(factory.getType("test1")));
@Test(expected = IllegalArgumentException.class) public void testWhenTargetBeanClassCannotBeResolved() { factory.setTargetBeanName(BEAN_NAME); factory.setMethodName("toString()"); factory.setBeanFactory(beanFactory); verify(beanFactory).getType(BEAN_NAME); }
@Test(expected = IllegalArgumentException.class) @SuppressWarnings("unchecked") public void testWhereMethodCannotBeResolved() { given(beanFactory.getType(BEAN_NAME)).willReturn((Class)String.class); factory.setTargetBeanName(BEAN_NAME); factory.setMethodName("loadOfOld()"); factory.setBeanFactory(beanFactory); }
@Test @SuppressWarnings("unchecked") public void testSunnyDayPath() throws Exception { given(beanFactory.getType(BEAN_NAME)).willReturn((Class)String.class); factory.setTargetBeanName(BEAN_NAME); factory.setMethodName("toString()"); factory.setBeanFactory(beanFactory); Object result = factory.getObject(); assertNotNull(result); assertTrue(result instanceof Method); Method method = (Method) result; assertEquals("Bingo", method.invoke("Bingo")); }