From e3153f705ffd70157deffb2935e3db5b2a2a38a1 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sat, 28 Feb 2009 06:57:52 +0000 Subject: [PATCH] + Renamed @Factory -> @FactoryMethod + callbackType -> interceptor + registrarType -> registrar + validatorTypes -> validators --- .../config/java/BeanDefinitionRegistrar.java | 2 +- .../springframework/config/java/Factory.java | 55 --------------- .../config/java/FactoryMethod.java | 67 +++++++++++++++++++ .../config/java/ModelMethod.java | 15 ++--- .../config/java/NoOpInterceptor.java | 32 +++++++++ .../org/springframework/config/java/Util.java | 2 +- .../springframework/config/java/ext/Bean.java | 10 +-- .../enhancement/ConfigurationEnhancer.java | 4 +- .../ConfigurationClassMethodVisitor.java | 8 +-- ...onfigurationModelBeanDefinitionReader.java | 6 +- 10 files changed, 122 insertions(+), 79 deletions(-) delete mode 100644 org.springframework.config.java/src/main/java/org/springframework/config/java/Factory.java create mode 100644 org.springframework.config.java/src/main/java/org/springframework/config/java/FactoryMethod.java create mode 100644 org.springframework.config.java/src/main/java/org/springframework/config/java/NoOpInterceptor.java diff --git a/org.springframework.config.java/src/main/java/org/springframework/config/java/BeanDefinitionRegistrar.java b/org.springframework.config.java/src/main/java/org/springframework/config/java/BeanDefinitionRegistrar.java index 0b7f9604798..02bb0cf47fd 100644 --- a/org.springframework.config.java/src/main/java/org/springframework/config/java/BeanDefinitionRegistrar.java +++ b/org.springframework.config.java/src/main/java/org/springframework/config/java/BeanDefinitionRegistrar.java @@ -12,7 +12,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; *

Constraints

Implementations must have only a default constructor, or explicitly * declare a no-arg constructor. * - * @see Factory + * @see FactoryMethod * @see ModelMethod * * @author Chris Beams diff --git a/org.springframework.config.java/src/main/java/org/springframework/config/java/Factory.java b/org.springframework.config.java/src/main/java/org/springframework/config/java/Factory.java deleted file mode 100644 index 6b0bd567708..00000000000 --- a/org.springframework.config.java/src/main/java/org/springframework/config/java/Factory.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2002-2008 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.config.java; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import net.sf.cglib.proxy.Callback; -import net.sf.cglib.proxy.NoOp; - - -/** - * Meta-annotation used to identify annotations as producers of beans and/or values. - * - * @author Chris Beams - */ -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.ANNOTATION_TYPE) -@Documented -public @interface Factory { - - /** - * Specifies which registrar (if any) should be used to register bean definitions for - * this {@link Factory} method. - */ - Class registrarType(); - - /** - * Specifies what (if any) callback should be used when processing this {@link Factory} - * method. Defaults to CGLIB's {@link NoOp}, which does nothing. TODO: rename - * (interceptorType)? to keep with the -or|-ar nomenclature - */ - Class callbackType() default NoOp.class; - - /** - * TODO: document TODO: rename - */ - Class[] validatorTypes() default {}; -} diff --git a/org.springframework.config.java/src/main/java/org/springframework/config/java/FactoryMethod.java b/org.springframework.config.java/src/main/java/org/springframework/config/java/FactoryMethod.java new file mode 100644 index 00000000000..ab1e7107855 --- /dev/null +++ b/org.springframework.config.java/src/main/java/org/springframework/config/java/FactoryMethod.java @@ -0,0 +1,67 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.config.java; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import net.sf.cglib.proxy.MethodInterceptor; + +import org.springframework.config.java.ext.AbstractMethodInterceptor; +import org.springframework.config.java.ext.Bean; + + +/** + * Meta-annotation used to identify method annotations as producers of beans and/or values. + * Provides a model that's open for extension. i.e.: The {@link Bean} annotation is + * annotated as a {@link FactoryMethod}. In this same fashion, any custom annotation can be + * devised with its own semantics. It need only provide a custom registrar, interceptor and + * optionally, validators. + * + * @see Bean + * @see BeanDefinitionRegistrar + * @see AbstractMethodInterceptor + * @see Validator + * + * @author Chris Beams + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.ANNOTATION_TYPE) +@Documented +public @interface FactoryMethod { + + /** + * Specifies which registrar should be used to register bean definitions when processing + * this {@link FactoryMethod}. + */ + Class registrar(); + + /** + * Specifies what interceptor should be used when processing this {@link FactoryMethod}. + * Defaults to {@link NoOpInterceptor} which does nothing. + */ + Class interceptor() default NoOpInterceptor.class; + + /** + * Optionally specifies any {@link Validator} types capable of validating the syntax of + * this {@link FactoryMethod}. Usually used when a factory method may have multiple + * annotations such as {@link Bean} and {@link ScopedProxy}. + */ + Class[] validators() default {}; +} diff --git a/org.springframework.config.java/src/main/java/org/springframework/config/java/ModelMethod.java b/org.springframework.config.java/src/main/java/org/springframework/config/java/ModelMethod.java index 2f53c73ac68..a8b50a7caa8 100644 --- a/org.springframework.config.java/src/main/java/org/springframework/config/java/ModelMethod.java +++ b/org.springframework.config.java/src/main/java/org/springframework/config/java/ModelMethod.java @@ -26,7 +26,6 @@ import java.util.List; import java.util.Set; import net.sf.cglib.proxy.Callback; -import net.sf.cglib.proxy.NoOp; import org.springframework.util.Assert; @@ -40,7 +39,7 @@ public final class ModelMethod implements Validatable { private final List annotations = new ArrayList(); private transient ConfigurationClass declaringClass; private transient int lineNumber; - private transient Factory factoryAnno; + private transient FactoryMethod factoryAnno; private transient final List validators = new ArrayList(); public ModelMethod(String name, int modifiers, ModelClass returnType, Annotation... annotations) { @@ -51,7 +50,7 @@ public final class ModelMethod implements Validatable { for (Annotation annotation : annotations) { this.annotations.add(annotation); if (factoryAnno == null) - factoryAnno = annotation.annotationType().getAnnotation(Factory.class); + factoryAnno = annotation.annotationType().getAnnotation(FactoryMethod.class); } Assert.isTrue(modifiers >= 0, "modifiers must be non-negative: " + modifiers); @@ -144,23 +143,23 @@ public final class ModelMethod implements Validatable { } public BeanDefinitionRegistrar getRegistrar() { - return getInstance(factoryAnno.registrarType()); + return getInstance(factoryAnno.registrar()); } public Set getValidators() { HashSet validator = new HashSet(); - for (Class validatorType : factoryAnno.validatorTypes()) + for (Class validatorType : factoryAnno.validators()) validator.add(getInstance(validatorType)); return validator; } public Callback getCallback() { - Class callbackType = factoryAnno.callbackType(); + Class callbackType = factoryAnno.interceptor(); - if (callbackType.equals(NoOp.class)) - return NoOp.INSTANCE; + if (callbackType.equals(NoOpInterceptor.class)) + return NoOpInterceptor.INSTANCE; return getInstance(callbackType); } diff --git a/org.springframework.config.java/src/main/java/org/springframework/config/java/NoOpInterceptor.java b/org.springframework.config.java/src/main/java/org/springframework/config/java/NoOpInterceptor.java new file mode 100644 index 00000000000..c6a579619fe --- /dev/null +++ b/org.springframework.config.java/src/main/java/org/springframework/config/java/NoOpInterceptor.java @@ -0,0 +1,32 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.config.java; + +import java.lang.reflect.Method; + +import net.sf.cglib.proxy.MethodInterceptor; +import net.sf.cglib.proxy.MethodProxy; + + +public class NoOpInterceptor implements MethodInterceptor { + + public static final NoOpInterceptor INSTANCE = new NoOpInterceptor(); + + public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { + return null; + } + +} diff --git a/org.springframework.config.java/src/main/java/org/springframework/config/java/Util.java b/org.springframework.config.java/src/main/java/org/springframework/config/java/Util.java index d974a920089..09424039dab 100644 --- a/org.springframework.config.java/src/main/java/org/springframework/config/java/Util.java +++ b/org.springframework.config.java/src/main/java/org/springframework/config/java/Util.java @@ -106,7 +106,7 @@ public class Util { *

* ASM class reading is used throughout JavaConfig, but there are certain cases where * classloading cannot be avoided - specifically in cases where users define their own - * {@link Extension} or {@link Factory} annotations. This method should therefore be + * {@link Extension} or {@link FactoryMethod} annotations. This method should therefore be * used sparingly but consistently where required. *

* Because {@link ClassNotFoundException} is compensated for by returning null, callers diff --git a/org.springframework.config.java/src/main/java/org/springframework/config/java/ext/Bean.java b/org.springframework.config.java/src/main/java/org/springframework/config/java/ext/Bean.java index a7eb53ac26b..a3f5ab699bc 100644 --- a/org.springframework.config.java/src/main/java/org/springframework/config/java/ext/Bean.java +++ b/org.springframework.config.java/src/main/java/org/springframework/config/java/ext/Bean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,7 +29,7 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.config.java.Configuration; import org.springframework.config.java.ConfigurationClass; import org.springframework.config.java.ConfigurationModel; -import org.springframework.config.java.Factory; +import org.springframework.config.java.FactoryMethod; import org.springframework.config.java.ModelMethod; import org.springframework.config.java.Scopes; import org.springframework.config.java.UsageError; @@ -62,7 +62,6 @@ import org.springframework.config.java.Validator; *

* * @see Configuration - * @see BeanNamingStrategy * * @author Rod Johnson * @author Costin Leau @@ -72,8 +71,9 @@ import org.springframework.config.java.Validator; @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented -@Factory(registrarType = BeanRegistrar.class, callbackType = BeanMethodInterceptor.class, validatorTypes = { - BeanValidator.class, IllegalBeanOverrideValidator.class }) +@FactoryMethod(registrar = BeanRegistrar.class, + interceptor = BeanMethodInterceptor.class, + validators = { BeanValidator.class, IllegalBeanOverrideValidator.class }) public @interface Bean { /** diff --git a/org.springframework.config.java/src/main/java/org/springframework/config/java/internal/enhancement/ConfigurationEnhancer.java b/org.springframework.config.java/src/main/java/org/springframework/config/java/internal/enhancement/ConfigurationEnhancer.java index 399035aa3d3..d7ac8284d3a 100644 --- a/org.springframework.config.java/src/main/java/org/springframework/config/java/internal/enhancement/ConfigurationEnhancer.java +++ b/org.springframework.config.java/src/main/java/org/springframework/config/java/internal/enhancement/ConfigurationEnhancer.java @@ -29,7 +29,6 @@ import net.sf.cglib.core.DefaultGeneratorStrategy; import net.sf.cglib.proxy.Callback; import net.sf.cglib.proxy.CallbackFilter; import net.sf.cglib.proxy.Enhancer; -import net.sf.cglib.proxy.NoOp; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -44,6 +43,7 @@ import org.springframework.config.java.Configuration; import org.springframework.config.java.ConfigurationClass; import org.springframework.config.java.ConfigurationModel; import org.springframework.config.java.ModelMethod; +import org.springframework.config.java.NoOpInterceptor; /** @@ -122,7 +122,7 @@ public class ConfigurationEnhancer { // no-op } }); - callbackInstances.add(NoOp.INSTANCE); + callbackInstances.add(NoOpInterceptor.INSTANCE); for (Callback callback : callbackInstances) callbackTypes.add(callback.getClass()); diff --git a/org.springframework.config.java/src/main/java/org/springframework/config/java/internal/parsing/ConfigurationClassMethodVisitor.java b/org.springframework.config.java/src/main/java/org/springframework/config/java/internal/parsing/ConfigurationClassMethodVisitor.java index d4d81861ae6..d5afedcd708 100644 --- a/org.springframework.config.java/src/main/java/org/springframework/config/java/internal/parsing/ConfigurationClassMethodVisitor.java +++ b/org.springframework.config.java/src/main/java/org/springframework/config/java/internal/parsing/ConfigurationClassMethodVisitor.java @@ -30,14 +30,14 @@ import org.objectweb.asm.MethodAdapter; import org.objectweb.asm.Opcodes; import org.springframework.config.java.Configuration; import org.springframework.config.java.ConfigurationClass; -import org.springframework.config.java.Factory; +import org.springframework.config.java.FactoryMethod; import org.springframework.config.java.ModelClass; import org.springframework.config.java.ModelMethod; /** * Visits a single method declared in a given {@link Configuration} class. Determines - * whether the method is a {@link Factory} method and if so, adds it to the + * whether the method is a {@link FactoryMethod} method and if so, adds it to the * {@link ConfigurationClass}. * * @author Chris Beams @@ -105,13 +105,13 @@ class ConfigurationClassMethodVisitor extends MethodAdapter { /** * Parses through all {@link #annotations} on this method in order to determine whether - * it is a {@link Factory} method or not and if so adds it to the enclosing + * it is a {@link FactoryMethod} method or not and if so adds it to the enclosing * {@link #configClass}. */ @Override public void visitEnd() { for (Annotation anno : annotations) { - if (anno.annotationType().getAnnotation(Factory.class) != null) { + if (anno.annotationType().getAnnotation(FactoryMethod.class) != null) { isModelMethod = true; break; } diff --git a/org.springframework.config.java/src/main/java/org/springframework/config/java/support/ConfigurationModelBeanDefinitionReader.java b/org.springframework.config.java/src/main/java/org/springframework/config/java/support/ConfigurationModelBeanDefinitionReader.java index a30a59f5d72..dc7d3d88edb 100644 --- a/org.springframework.config.java/src/main/java/org/springframework/config/java/support/ConfigurationModelBeanDefinitionReader.java +++ b/org.springframework.config.java/src/main/java/org/springframework/config/java/support/ConfigurationModelBeanDefinitionReader.java @@ -34,7 +34,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.config.java.Configuration; import org.springframework.config.java.ConfigurationClass; import org.springframework.config.java.ConfigurationModel; -import org.springframework.config.java.Factory; +import org.springframework.config.java.FactoryMethod; import org.springframework.config.java.ModelMethod; import org.springframework.config.java.plugin.Extension; import org.springframework.config.java.plugin.ExtensionAnnotationBeanDefinitionRegistrar; @@ -84,7 +84,7 @@ class ConfigurationModelBeanDefinitionReader { /** * Reads a particular {@link ConfigurationClass}, registering bean definitions for the - * class itself, all its {@link Factory} methods and all its {@link Extension} + * class itself, all its {@link FactoryMethod} methods and all its {@link Extension} * annotations. */ private void loadBeanDefinitionsForConfigurationClass(ConfigurationClass configClass) { @@ -145,7 +145,7 @@ class ConfigurationModelBeanDefinitionReader { * Reads a particular {@link ModelMethod}, registering bean definitions with * {@link #beanFactory} based on its contents. * - * @see Factory + * @see FactoryMethod */ private void loadBeanDefinitionsForModelMethod(ModelMethod method) { method.getRegistrar().register(method, beanFactory);