You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by al...@apache.org on 2017/02/06 21:18:56 UTC
svn commit: r1781955 -
/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/
Author: alien11689
Date: Mon Feb 6 21:18:56 2017
New Revision: 1781955
URL: http://svn.apache.org/viewvc?rev=1781955&view=rev
Log:
[REFACTOR] Refactor bean name generation
Added:
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/NamingHelper.java
- copied, changed from r1781793, aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/QualifierHelper.java
Modified:
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/AnnotationHelper.java
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Argument.java
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanFromFactory.java
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Blueprint.java
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Introspector.java
aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Property.java
Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/AnnotationHelper.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/AnnotationHelper.java?rev=1781955&r1=1781954&r2=1781955&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/AnnotationHelper.java (original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/AnnotationHelper.java Mon Feb 6 21:18:56 2017
@@ -85,4 +85,13 @@ class AnnotationHelper {
}
return false;
}
+
+ static boolean findSingleton(Class clazz) {
+ for (Class<?> singletonAnnotation : Handlers.SINGLETONS) {
+ if (clazz.getAnnotation(singletonAnnotation) != null) {
+ return true;
+ }
+ }
+ return false;
+ }
}
Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Argument.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Argument.java?rev=1781955&r1=1781954&r2=1781955&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Argument.java (original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Argument.java Mon Feb 6 21:18:56 2017
@@ -18,18 +18,53 @@
*/
package org.apache.aries.blueprint.plugin.model;
+import org.apache.aries.blueprint.plugin.handlers.Handlers;
+import org.apache.aries.blueprint.plugin.spi.CustomDependencyAnnotationHandler;
import org.apache.aries.blueprint.plugin.spi.XmlWriter;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
+import java.lang.annotation.Annotation;
+
+import static org.apache.aries.blueprint.plugin.model.AnnotationHelper.findName;
+import static org.apache.aries.blueprint.plugin.model.AnnotationHelper.findValue;
+import static org.apache.aries.blueprint.plugin.model.NamingHelper.getBeanName;
class Argument implements XmlWriter{
private final String ref;
private final String value;
- Argument(String ref, String value) {
- this.ref = ref;
+ Argument(BlueprintRegistry blueprintRegistry, Class<?> argumentClass, Annotation[] annotations) {
+ String value = findValue(annotations);
+ String ref = findName(annotations);
+
+ for (CustomDependencyAnnotationHandler customDependencyAnnotationHandler : Handlers.CUSTOM_DEPENDENCY_ANNOTATION_HANDLERS) {
+ Annotation annotation = (Annotation) AnnotationHelper.findAnnotation(annotations, customDependencyAnnotationHandler.getAnnotation());
+ if (annotation != null) {
+ String generatedRef = customDependencyAnnotationHandler.handleDependencyAnnotation(argumentClass, annotation, ref, blueprintRegistry);
+ if (generatedRef != null) {
+ ref = generatedRef;
+ break;
+ }
+ }
+ }
+
+ if (ref == null && value == null) {
+ BeanTemplate template = new BeanTemplate(argumentClass, annotations);
+ BeanRef bean = blueprintRegistry.getMatching(template);
+ if (bean != null) {
+ ref = bean.id;
+ } else {
+ String name = findName(annotations);
+ if (name != null) {
+ ref = name;
+ } else {
+ ref = getBeanName(argumentClass);
+ }
+ }
+ }
this.value = value;
+ this.ref = ref;
}
String getRef() {
Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java?rev=1781955&r1=1781954&r2=1781955&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java (original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java Mon Feb 6 21:18:56 2017
@@ -26,7 +26,6 @@ import org.apache.aries.blueprint.plugin
import org.apache.aries.blueprint.plugin.spi.FieldAnnotationHandler;
import org.apache.aries.blueprint.plugin.spi.InjectLikeHandler;
import org.apache.aries.blueprint.plugin.spi.MethodAnnotationHandler;
-import org.apache.aries.blueprint.plugin.spi.NamedLikeHandler;
import org.apache.aries.blueprint.plugin.spi.XmlWriter;
import javax.xml.stream.XMLStreamException;
@@ -44,7 +43,9 @@ import java.util.SortedSet;
import java.util.TreeSet;
import static org.apache.aries.blueprint.plugin.model.AnnotationHelper.findName;
+import static org.apache.aries.blueprint.plugin.model.AnnotationHelper.findSingleton;
import static org.apache.aries.blueprint.plugin.model.AnnotationHelper.findValue;
+import static org.apache.aries.blueprint.plugin.model.NamingHelper.getBeanName;
class Bean implements BeanEnricher, XmlWriter {
@@ -57,19 +58,17 @@ class Bean implements BeanEnricher, XmlW
final Map<String, String> attributes = new HashMap<>();
final Map<String, XmlWriter> beanContentWriters = new HashMap<>();
protected final ContextEnricher contextEnricher;
+ private final Introspector introspector;
Bean(Class<?> clazz, ContextEnricher contextEnricher) {
this.clazz = clazz;
this.id = getBeanName(clazz);
this.contextEnricher = contextEnricher;
- Introspector introspector = new Introspector(clazz);
+ introspector = new Introspector(clazz);
setScope(clazz);
-
handleCustomBeanAnnotations();
-
handleFieldsAnnotation(introspector);
-
handleMethodsAnnotation(introspector);
}
@@ -110,18 +109,8 @@ class Bean implements BeanEnricher, XmlW
}
}
- private boolean findSingleton(Class clazz) {
- for (Class<?> singletonAnnotation : Handlers.SINGLETONS) {
- if (clazz.getAnnotation(singletonAnnotation) != null) {
- return true;
- }
- }
- return false;
- }
-
-
private void resolveMethods(BlueprintRegistry blueprintRegistry) {
- for (Method method : new Introspector(clazz).methodsWith(AnnotationHelper.injectDependencyAnnotations)) {
+ for (Method method : introspector.methodsWith(AnnotationHelper.injectDependencyAnnotations)) {
Property prop = Property.create(blueprintRegistry, method);
if (prop != null) {
properties.add(prop);
@@ -130,7 +119,7 @@ class Bean implements BeanEnricher, XmlW
}
private void resolveFields(BlueprintRegistry matcher) {
- for (Field field : new Introspector(clazz).fieldsWith(AnnotationHelper.injectDependencyAnnotations)) {
+ for (Field field : introspector.fieldsWith(AnnotationHelper.injectDependencyAnnotations)) {
Property prop = Property.create(matcher, field);
if (prop != null) {
properties.add(prop);
@@ -159,45 +148,10 @@ class Bean implements BeanEnricher, XmlW
void resolveArguments(BlueprintRegistry blueprintRegistry, Class[] parameterTypes, Annotation[][] parameterAnnotations) {
for (int i = 0; i < parameterTypes.length; ++i) {
- Annotation[] annotations = parameterAnnotations[i];
- String value = findValue(annotations);
- String ref = findName(annotations);
-
- for (CustomDependencyAnnotationHandler customDependencyAnnotationHandler : Handlers.CUSTOM_DEPENDENCY_ANNOTATION_HANDLERS) {
- Annotation annotation = (Annotation) AnnotationHelper.findAnnotation(annotations, customDependencyAnnotationHandler.getAnnotation());
- if (annotation != null) {
- String generatedRef = customDependencyAnnotationHandler.handleDependencyAnnotation(parameterTypes[i], annotation, ref, blueprintRegistry);
- if (generatedRef != null) {
- ref = generatedRef;
- break;
- }
- }
- }
-
- if (ref == null && value == null) {
- BeanTemplate template = new BeanTemplate(parameterTypes[i], annotations);
- BeanRef bean = blueprintRegistry.getMatching(template);
- if (bean != null) {
- ref = bean.id;
- } else {
- String name = findName(annotations);
- if (name != null) {
- ref = name;
- } else {
- ref = getBeanName(parameterTypes[i]);
- }
- }
- }
-
- constructorArguments.add(new Argument(ref, value));
+ constructorArguments.add(new Argument(blueprintRegistry, parameterTypes[i], parameterAnnotations[i]));
}
}
- @Override
- public String toString() {
- return clazz.getName();
- }
-
private void writeProperties(XMLStreamWriter writer) throws XMLStreamException {
for (Property property : properties) {
property.write(writer);
@@ -267,25 +221,4 @@ class Bean implements BeanEnricher, XmlW
BeanRef toBeanRef() {
return new BeanRef(clazz, id, clazz.getAnnotations());
}
-
- static String getBeanName(Class<?> clazz) {
- return getBeanName(clazz, clazz);
- }
-
- private static String getBeanName(Class<?> clazz, AnnotatedElement annotatedElement) {
- for (NamedLikeHandler namedLikeHandler : Handlers.NAMED_LIKE_HANDLERS) {
- if (annotatedElement.getAnnotation(namedLikeHandler.getAnnotation()) != null) {
- String name = namedLikeHandler.getName(clazz, annotatedElement);
- if (name != null) {
- return name;
- }
- }
- }
- String name = clazz.getSimpleName();
- return getBeanNameFromSimpleName(name);
- }
-
- private static String getBeanNameFromSimpleName(String name) {
- return name.substring(0, 1).toLowerCase() + name.substring(1, name.length());
- }
}
Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanFromFactory.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanFromFactory.java?rev=1781955&r1=1781954&r2=1781955&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanFromFactory.java (original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/BeanFromFactory.java Mon Feb 6 21:18:56 2017
@@ -28,21 +28,25 @@ import java.lang.reflect.Method;
class BeanFromFactory extends Bean {
private final Method producingMethod;
- BeanFromFactory(Class<?> clazz, Bean factoryBean, Method factoryMethod, ContextEnricher contextEnricher) {
- this(clazz, null, factoryBean, factoryMethod, contextEnricher);
- }
-
- BeanFromFactory(Class<?> clazz, String id, Bean factoryBean, Method factoryMethod, ContextEnricher contextEnricher) {
- super(clazz, contextEnricher);
- if (id != null) {
- this.id = id;
+ BeanFromFactory(Bean factoryBean, Method factoryMethod, ContextEnricher contextEnricher) {
+ super(factoryMethod.getReturnType(), contextEnricher);
+ String forcedId = AnnotationHelper.findName(factoryMethod.getAnnotations());
+ if (forcedId != null) {
+ this.id = forcedId;
}
this.producingMethod = factoryMethod;
+ setScope(factoryMethod);
handleCustomBeanAnnotations();
attributes.put("factory-ref", factoryBean.id);
attributes.put("factory-method", producingMethod.getName());
}
+ private void setScope(Method factoryMethod) {
+ if (AnnotationHelper.findSingletons(factoryMethod.getAnnotations())) {
+ attributes.put("scope", "singleton");
+ }
+ }
+
private void handleCustomBeanAnnotations() {
for (BeanAnnotationHandler beanAnnotationHandler : Handlers.BEAN_ANNOTATION_HANDLERS) {
Object annotation = AnnotationHelper.findAnnotation(producingMethod.getAnnotations(), beanAnnotationHandler.getAnnotation());
@@ -52,10 +56,6 @@ class BeanFromFactory extends Bean {
}
}
- void setSingleton() {
- attributes.put("scope", "singleton");
- }
-
@Override
protected void resolveArguments(BlueprintRegistry matcher) {
resolveArguments(matcher, producingMethod.getParameterTypes(), producingMethod.getParameterAnnotations());
Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Blueprint.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Blueprint.java?rev=1781955&r1=1781954&r2=1781955&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Blueprint.java (original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Blueprint.java Mon Feb 6 21:18:56 2017
@@ -80,17 +80,7 @@ public class Blueprint implements Bluepr
if (!isFactoryMethod(method)) {
continue;
}
- String name = AnnotationHelper.findName(method.getAnnotations());
- Class<?> beanClass = method.getReturnType();
- BeanFromFactory beanFromFactory;
- if (name == null) {
- beanFromFactory = new BeanFromFactory(beanClass, factoryBean, method, this);
- } else {
- beanFromFactory = new BeanFromFactory(beanClass, name, factoryBean, method, this);
- }
- if (AnnotationHelper.findSingletons(method.getAnnotations())) {
- beanFromFactory.setSingleton();
- }
+ BeanFromFactory beanFromFactory = new BeanFromFactory(factoryBean, method, this);
beanRefStore.addBean(beanFromFactory.toBeanRef());
generatedBeans.add(beanFromFactory);
}
Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Introspector.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Introspector.java?rev=1781955&r1=1781954&r2=1781955&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Introspector.java (original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Introspector.java Mon Feb 6 21:18:56 2017
@@ -37,7 +37,7 @@ import java.util.Set;
* Class to find uniquely-named fields declared in a class hierarchy with specified annotations.
*/
final class Introspector {
- private Class<?> originalClazz;
+ private final Class<?> originalClazz;
/**
* @param clazz the class to introspect (including those defined in parent classes).
@@ -112,14 +112,6 @@ final class Introspector {
}
return false;
}
-
- public <T extends Annotation> Method methodWith(Class<T> annotationClass) {
- List<Method> methods = methodsWith(annotationClass);
- Preconditions.checkArgument(methods.size() <= 1,
- "Found %d methods annotated with %s in class %s, but only 1 allowed",
- methods.size(), annotationClass.getName(), originalClazz.getName());
- return Iterables.getOnlyElement(methods, null);
- }
@SafeVarargs
final List<Method> methodsWith(Class<? extends Annotation>... annotationClasses) {
Copied: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/NamingHelper.java (from r1781793, aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/QualifierHelper.java)
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/NamingHelper.java?p2=aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/NamingHelper.java&p1=aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/QualifierHelper.java&r1=1781793&r2=1781955&rev=1781955&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/QualifierHelper.java (original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/NamingHelper.java Mon Feb 6 21:18:56 2017
@@ -18,31 +18,23 @@
*/
package org.apache.aries.blueprint.plugin.model;
-import org.apache.aries.blueprint.plugin.handlers.Handlers;
+import java.lang.reflect.AnnotatedElement;
-import java.lang.annotation.Annotation;
-import java.util.HashSet;
-import java.util.Set;
+class NamingHelper {
+ static String getBeanName(Class<?> clazz) {
+ return getBeanName(clazz, clazz);
+ }
-class QualifierHelper {
- static Set<Annotation> getQualifiers(Annotation[] annotations) {
- final Set<Annotation> qualifiers = new HashSet<>();
- for (Annotation ann : annotations) {
- if (isQualifier(ann) != null) {
- qualifiers.add(ann);
- }
+ private static String getBeanName(Class<?> clazz, AnnotatedElement annotatedElement) {
+ String name = AnnotationHelper.findName(annotatedElement.getAnnotations());
+ if (name != null) {
+ return name;
}
- return qualifiers;
+ return getBeanNameFromSimpleName(clazz.getSimpleName());
}
- private static Object isQualifier(Annotation ann) {
- for (Class<? extends Annotation> qualifingAnnotationClass : Handlers.QUALIFING_ANNOTATION_CLASSES) {
- Object annotation = ann.annotationType().getAnnotation(qualifingAnnotationClass);
- if (annotation != null) {
- return annotation;
- }
- }
- return null;
+ private static String getBeanNameFromSimpleName(String name) {
+ return name.substring(0, 1).toLowerCase() + name.substring(1, name.length());
}
}
Modified: aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Property.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Property.java?rev=1781955&r1=1781954&r2=1781955&view=diff
==============================================================================
--- aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Property.java (original)
+++ aries/trunk/blueprint/plugin/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Property.java Mon Feb 6 21:18:56 2017
@@ -31,6 +31,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import static org.apache.aries.blueprint.plugin.model.AnnotationHelper.findName;
+import static org.apache.aries.blueprint.plugin.model.NamingHelper.getBeanName;
class Property implements Comparable<Property>, XmlWriter {
public final String name;
@@ -120,7 +121,7 @@ class Property implements Comparable<Pro
BeanTemplate template = new BeanTemplate(method);
BeanRef matching = blueprintRegistry.getMatching(template);
- ref = (matching == null) ? Bean.getBeanName(method.getParameterTypes()[0]) : matching.id;
+ ref = (matching == null) ? getBeanName(method.getParameterTypes()[0]) : matching.id;
return new Property(propertyName, ref, null, false);
}
@@ -142,25 +143,21 @@ class Property implements Comparable<Pro
* @return
*/
private static String getDefaultRefName(Field field) {
- return Bean.getBeanName(field.getType());
+ return getBeanName(field.getType());
}
private static String getForcedRefName(Field field) {
- for (NamedLikeHandler namedLikeHandler : Handlers.NAMED_LIKE_HANDLERS) {
- if (field.getAnnotation(namedLikeHandler.getAnnotation()) != null) {
- String name = namedLikeHandler.getName(field.getType(), field);
- if (name != null) {
- return name;
- }
- }
- }
- return null;
+ return getForcedRefName(field.getType(), field);
}
private static String getForcedRefName(Method method) {
+ return getForcedRefName(method.getParameterTypes()[0], method);
+ }
+
+ private static String getForcedRefName(Class<?> clazz, AnnotatedElement annotatedElement) {
for (NamedLikeHandler namedLikeHandler : Handlers.NAMED_LIKE_HANDLERS) {
- if (method.getAnnotation(namedLikeHandler.getAnnotation()) != null) {
- String name = namedLikeHandler.getName(method.getParameterTypes()[0], method);
+ if (annotatedElement.getAnnotation(namedLikeHandler.getAnnotation()) != null) {
+ String name = namedLikeHandler.getName(clazz, annotatedElement);
if (name != null) {
return name;
}