aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject svn commit: r1724454 - in /aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model: Bean.java Context.java FieldFinder.java Introspector.java
Date Wed, 13 Jan 2016 16:25:23 GMT
Author: cschneider
Date: Wed Jan 13 16:25:23 2016
New Revision: 1724454

URL: http://svn.apache.org/viewvc?rev=1724454&view=rev
Log:
[ARIES-1481] Refactor to an Introspector class

Added:
    aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Introspector.java
      - copied, changed from r1724453, aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/FieldFinder.java
Removed:
    aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/FieldFinder.java
Modified:
    aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
    aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java?rev=1724454&r1=1724453&r2=1724454&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java
Wed Jan 13 16:25:23 2016
@@ -18,10 +18,8 @@
  */
 package org.apache.aries.blueprint.plugin.model;
 
-import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
-import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -39,9 +37,6 @@ import org.springframework.beans.factory
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Iterables;
-
 public class Bean extends BeanRef {
     public String initMethod;
     public String destroyMethod;
@@ -52,15 +47,16 @@ public class Bean extends BeanRef {
 
     public Bean(Class<?> clazz) {
         super(clazz, BeanRef.getBeanName(clazz));
+        Introspector introspector = new Introspector(clazz);
 
         // Init method
-        Method initMethod = getMethodWithAnnotation(clazz, PostConstruct.class);
+        Method initMethod = introspector.methodWith(PostConstruct.class);
         if (initMethod != null) {
             this.initMethod = initMethod.getName();
         }
 
         // Destroy method
-        Method destroyMethod = getMethodWithAnnotation(clazz, PreDestroy.class);
+        Method destroyMethod = introspector.methodWith(PreDestroy.class);
         if (destroyMethod != null) {
             this.destroyMethod = destroyMethod.getName();
         }
@@ -69,7 +65,7 @@ public class Bean extends BeanRef {
         transactionDefs.addAll(new JavaxTransactionFactory().create(clazz));
         transactionDefs.addAll(new SpringTransactionFactory().create(clazz));
         this.isPrototype = isPrototype(clazz);
-        this.persistenceFields = new FieldFinder(PersistenceContext.class, PersistenceUnit.class).findFields(clazz);
+        this.persistenceFields = introspector.fieldsWith(PersistenceContext.class, PersistenceUnit.class);
         properties = new TreeSet<Property>();
     }
 
@@ -79,7 +75,7 @@ public class Bean extends BeanRef {
     }
 
     public void resolve(Matcher matcher) {
-        for (Field field : new FieldFinder(Value.class, Autowired.class, Inject.class).findFields(clazz))
{
+        for (Field field : new Introspector(clazz).fieldsWith(Value.class, Autowired.class,
Inject.class)) {
             Property prop = Property.create(matcher, field);
             if (prop != null) {
                 properties.add(prop);
@@ -87,26 +83,7 @@ public class Bean extends BeanRef {
         }
     }
 
-    private static <T extends Annotation> Method getMethodWithAnnotation(Class<?>
classToSearch,
-                                                                         Class<T> annotationClass)
{
-        List<Method> methods = getMethodsWithAnnotation(classToSearch, annotationClass);
-        Preconditions.checkArgument(methods.size() <= 1,
-                                    "Found %d methods annotated with %s in class %s, but
only 1 allowed",
-                                    methods.size(), annotationClass.getName(), classToSearch.getName());
-        return Iterables.getOnlyElement(methods, null);
-    }
 
-    private static <T extends Annotation> List<Method> getMethodsWithAnnotation(Class<?>
classToSearch,
-                                                                                Class<T>
annotationClass) {
-        List<Method> methods = new ArrayList<>();
-        for (Method method : classToSearch.getMethods()) {
-            T annotation = method.getAnnotation(annotationClass);
-            if (annotation != null) {
-                methods.add(method);
-            }
-        }
-        return methods;
-    }
 
     @Override
     public int hashCode() {

Modified: aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java?rev=1724454&r1=1724453&r2=1724454&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Context.java
Wed Jan 13 16:25:23 2016
@@ -79,7 +79,7 @@ public class Context implements Matcher
     }
 
     private void addServiceRefs(Class<?> clazz) {
-        for (Field field : new FieldFinder(OsgiService.class).findFields(clazz)) {
+        for (Field field : new Introspector(clazz).fieldsWith(OsgiService.class)) {
             reg.add(new OsgiServiceRef(field));
         }
     }

Copied: aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Introspector.java
(from r1724453, aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/FieldFinder.java)
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Introspector.java?p2=aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Introspector.java&p1=aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/FieldFinder.java&r1=1724453&r2=1724454&rev=1724454&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/FieldFinder.java
(original)
+++ aries/trunk/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Introspector.java
Wed Jan 13 16:25:23 2016
@@ -20,11 +20,15 @@ package org.apache.aries.blueprint.plugi
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Set;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Sets;
@@ -32,24 +36,24 @@ import com.google.common.collect.Sets;
 /**
  * Class to find uniquely-named fields declared in a class hierarchy with specified annotations.
  */
-public final class FieldFinder {
-    private final Class<? extends Annotation>[] requiredAnnotations;
+public final class Introspector {
+    private Class<?> originalClazz;
 
     /**
-     * @param requiredAnnotations if not empty, a field must have one of these annotations
for it to be found.
+     * @param clazz the class to introspect (including those defined in parent classes).
      */
-    @SafeVarargs
-    public FieldFinder(Class<? extends Annotation>... requiredAnnotations) {
-        this.requiredAnnotations = requiredAnnotations;
+    public Introspector(Class<?> clazz) {
+        this.originalClazz = clazz;
     }
 
     /**
-     * @param originalClazz the class in which to find fields (including those defined in
parent classes).
+     * @param 
      * @return fields in the given class (including parent classes) that match this finder's
annotations requirements.
      * @throws UnsupportedOperationException if any field matching the annotations requirement
shares its name with a
      * field declared elsewhere in the class hierarchy.
      */
-    public List<Field> findFields(Class<?> originalClazz) {
+    @SafeVarargs
+    public final List<Field> fieldsWith(Class<? extends Annotation>... requiredAnnotations)
{
         Multimap<String, Field> fieldsByName = HashMultimap.create();
         Set<String> acceptedFieldNames = Sets.newHashSet();
         Class<?> clazz = originalClazz;
@@ -61,7 +65,7 @@ public final class FieldFinder {
                 fieldsByName.put(field.getName(), field);
 
                 // ...and if it meets the annotation requirement, add the field name to the
set of accepted field names
-                if (hasAnyRequiredAnnotation(field)) {
+                if (hasAnyRequiredAnnotation(field, requiredAnnotations)) {
                     acceptedFieldNames.add(field.getName());
                 }
             }
@@ -72,7 +76,7 @@ public final class FieldFinder {
         List<Field> acceptedFields = Lists.newArrayList();
         for (String fieldName : acceptedFieldNames) {
             Collection<Field> fields = fieldsByName.get(fieldName);
-            validateOnlyOneFieldWithName(originalClazz, fieldName, fields);
+            validateOnlyOneFieldWithName(fieldName, fields);
             acceptedFields.addAll(fields);
         }
         return acceptedFields;
@@ -84,7 +88,7 @@ public final class FieldFinder {
      * @param acceptedFieldName
      * @param acceptedFieldsWithSameName
      */
-    private void validateOnlyOneFieldWithName(Class<?> originalClazz, String acceptedFieldName,
+    private void validateOnlyOneFieldWithName(String acceptedFieldName,
                                               Collection<Field> acceptedFieldsWithSameName)
{
         if (acceptedFieldsWithSameName.size() > 1) {
             String header = String.format("Field '%s' in bean class '%s' has been defined
multiple times in:",
@@ -97,9 +101,10 @@ public final class FieldFinder {
         }
     }
 
-    private boolean hasAnyRequiredAnnotation(Field field) {
+    @SafeVarargs
+    private final boolean hasAnyRequiredAnnotation(Field field, Class<? extends Annotation>...
requiredAnnotations) {
         if (requiredAnnotations.length == 0) {
-            return true;
+            throw new IllegalArgumentException("Must specify at least one annotation");
         }
         for (Class<? extends Annotation> requiredAnnotation : requiredAnnotations)
{
             if (field.getAnnotation(requiredAnnotation) != null) {
@@ -108,4 +113,23 @@ public final class FieldFinder {
         }
         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);
+    }
+
+    public <T extends Annotation> List<Method> methodsWith(Class<T> annotationClass)
{
+        List<Method> methods = new ArrayList<>();
+        for (Method method : originalClazz.getMethods()) {
+            T annotation = method.getAnnotation(annotationClass);
+            if (annotation != null) {
+                methods.add(method);
+            }
+        }
+        return methods;
+    }
 }



Mime
View raw message