commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1437189 - in /commons/sandbox/weaver/branches/mjb/processor/src: main/java/org/apache/commons/weaver/model/ test/java/org/apache/commons/weaver/test/weaver/
Date Tue, 22 Jan 2013 21:37:52 GMT
Author: mbenson
Date: Tue Jan 22 21:37:52 2013
New Revision: 1437189

URL: http://svn.apache.org/viewvc?rev=1437189&view=rev
Log:
further refinements to the generics interworkings, added AnnotatedElements iterable type to
allow more fluent filtering down to the desired scan results

Added:
    commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/AnnotatedElements.java
  (with props)
Modified:
    commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/ScanResult.java
    commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableConstructor.java
    commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableExecutable.java
    commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableMethod.java
    commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableParameter.java
    commons/sandbox/weaver/branches/mjb/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java

Added: commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/AnnotatedElements.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/AnnotatedElements.java?rev=1437189&view=auto
==============================================================================
--- commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/AnnotatedElements.java
(added)
+++ commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/AnnotatedElements.java
Tue Jan 22 21:37:52 2013
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.commons.weaver.model;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+
+public interface AnnotatedElements<T extends AnnotatedElement> extends Iterable<T>
{
+    AnnotatedElements<T> with(Class<? extends Annotation> annotationType);
+}

Propchange: commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/AnnotatedElements.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/ScanResult.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/ScanResult.java?rev=1437189&r1=1437188&r2=1437189&view=diff
==============================================================================
--- commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/ScanResult.java
(original)
+++ commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/ScanResult.java
Tue Jan 22 21:37:52 2013
@@ -19,6 +19,8 @@
 
 package org.apache.commons.weaver.model;
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
@@ -32,7 +34,7 @@ import java.util.concurrent.ConcurrentSk
  * Encapsulates the result of scanning based on a {@link ScanRequest}.
  */
 public class ScanResult {
-    private static abstract class Projection<PARENT, CHILD> implements Iterable<CHILD>
{
+    private static abstract class Projection<PARENT, CHILD extends AnnotatedElement>
implements AnnotatedElements<CHILD> {
         private final Iterable<PARENT> parents;
 
         Projection(Iterable<PARENT> parents) {
@@ -83,6 +85,68 @@ public class ScanResult {
                 }
             };
         }
+
+        @Override
+        public AnnotatedElements<CHILD> with(Class<? extends Annotation> annotationType)
{
+            return new AnnotatedWith<CHILD>(this, annotationType);
+        }
+    }
+
+    private static class AnnotatedWith<W extends AnnotatedElement> implements AnnotatedElements<W>
{
+        final Iterable<W> wrapped;
+        final Class<? extends Annotation> annotationType;
+
+        AnnotatedWith(Iterable<W> wrapped, Class<? extends Annotation> annotationType)
{
+            super();
+            this.wrapped = wrapped;
+            this.annotationType = annotationType;
+        }
+
+        @Override
+        public Iterator<W> iterator() {
+            final Iterator<W> iter = wrapped.iterator();
+            return new Iterator<W>() {
+                W next = read();
+
+                private W read() {
+                    while (iter.hasNext()) {
+                        W t = iter.next();
+                        if (t.isAnnotationPresent(annotationType)) {
+                            return t;
+                        }
+                    }
+                    return null;
+                }
+
+                @Override
+                public boolean hasNext() {
+                    return next != null;
+                }
+
+                @Override
+                public W next() {
+                    if (next == null) {
+                        throw new NoSuchElementException();
+                    }
+                    try {
+                        return next;
+                    } finally {
+                        next = read();
+                    }
+                }
+
+                @Override
+                public void remove() {
+                    throw new UnsupportedOperationException();
+                }
+            };
+        }
+
+        @Override
+        public AnnotatedElements<W> with(Class<? extends Annotation> annotationType)
{
+            return new AnnotatedWith<W>(this, annotationType);
+        }
+
     }
 
     private final ConcurrentNavigableMap<String, WeavablePackage> packages =
@@ -114,11 +178,16 @@ public class ScanResult {
         return getWeavable(ctor.getDeclaringClass()).getWeavable(ctor);
     }
 
-    public Iterable<WeavablePackage> getPackages() {
-        return Collections.unmodifiableCollection(packages.values());
+    public AnnotatedElements<WeavablePackage> getPackages() {
+        return new Projection<WeavablePackage, WeavablePackage>(packages.values())
{
+            @Override
+            protected Iterable<WeavablePackage> childrenOf(WeavablePackage parent)
{
+                return Collections.singleton(parent);
+            }
+        };
     }
 
-    public Iterable<WeavableClass<?>> getClasses() {
+    public AnnotatedElements<WeavableClass<?>> getClasses() {
         return new Projection<WeavablePackage, WeavableClass<?>>(getPackages())
{
 
             @Override
@@ -128,7 +197,7 @@ public class ScanResult {
         };
     }
 
-    public Iterable<WeavableField<?>> getFields() {
+    public AnnotatedElements<WeavableField<?>> getFields() {
         return new Projection<WeavableClass<?>, WeavableField<?>>(getClasses())
{
 
             @Override
@@ -140,7 +209,7 @@ public class ScanResult {
         };
     }
 
-    public Iterable<WeavableConstructor<?>> getConstructors() {
+    public AnnotatedElements<WeavableConstructor<?>> getConstructors() {
         return new Projection<WeavableClass<?>, WeavableConstructor<?>>(getClasses())
{
 
             @Override
@@ -152,7 +221,7 @@ public class ScanResult {
         };
     }
 
-    public Iterable<WeavableMethod<?>> getMethods() {
+    public AnnotatedElements<WeavableMethod<?>> getMethods() {
         return new Projection<WeavableClass<?>, WeavableMethod<?>>(getClasses())
{
 
             @Override
@@ -164,7 +233,7 @@ public class ScanResult {
         };
     }
 
-    public Iterable<WeavableMethodParameter<?>> getMethodParameters() {
+    public AnnotatedElements<WeavableMethodParameter<?>> getMethodParameters()
{
         return new Projection<WeavableMethod<?>, WeavableMethodParameter<?>>(getMethods())
{
 
             @Override
@@ -176,7 +245,7 @@ public class ScanResult {
         };
     }
 
-    public Iterable<WeavableConstructorParameter<?>> getConstructorParameters()
{
+    public AnnotatedElements<WeavableConstructorParameter<?>> getConstructorParameters()
{
 
         return new Projection<WeavableConstructor<?>, WeavableConstructorParameter<?>>(getConstructors())
{
 

Modified: commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableConstructor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableConstructor.java?rev=1437189&r1=1437188&r2=1437189&view=diff
==============================================================================
--- commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableConstructor.java
(original)
+++ commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableConstructor.java
Tue Jan 22 21:37:52 2013
@@ -20,7 +20,8 @@ package org.apache.commons.weaver.model;
 
 import java.lang.reflect.Constructor;
 
-public class WeavableConstructor<T> extends WeavableExecutable<WeavableConstructor<T>,
Constructor<T>, T> {
+public class WeavableConstructor<T> extends
+    WeavableExecutable<WeavableConstructor<T>, Constructor<T>, T, WeavableConstructorParameter<T>>
{
 
     public WeavableConstructor(Constructor<T> target, WeavableClass<T> parent)
{
         super(target, parent);
@@ -31,4 +32,9 @@ public class WeavableConstructor<T> exte
         return getTarget().getParameterTypes();
     }
 
+    @Override
+    protected WeavableConstructorParameter<T> createParameter(int index) {
+        return new WeavableConstructorParameter<T>(Integer.valueOf(index), this);
+    }
+
 }

Modified: commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableExecutable.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableExecutable.java?rev=1437189&r1=1437188&r2=1437189&view=diff
==============================================================================
--- commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableExecutable.java
(original)
+++ commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableExecutable.java
Tue Jan 22 21:37:52 2013
@@ -25,23 +25,22 @@ import java.util.List;
 
 import org.apache.commons.weaver.utils.Args;
 
-public abstract class WeavableExecutable<SELF extends WeavableExecutable<SELF, TARGET,
T>, TARGET extends Member, T>
+public abstract class WeavableExecutable<SELF extends WeavableExecutable<SELF, TARGET,
T, P>, TARGET extends Member, T, P extends WeavableParameter<P, SELF, TARGET, T>>
     extends NestedWeavable<SELF, TARGET, WeavableClass<T>, Class<T>> {
 
-    private final List<WeavableParameter<SELF, TARGET, T>> parameters;
+    private final List<P> parameters;
 
     protected WeavableExecutable(TARGET target, WeavableClass<T> parent) {
         super(target, parent);
-        final List<WeavableParameter<SELF, TARGET, T>> params = new ArrayList<WeavableParameter<SELF,
TARGET, T>>();
+        final List<P> params = new ArrayList<P>();
         for (int i = 0, sz = getParameterTypes().length; i < sz; i++) {
-            @SuppressWarnings("unchecked")
-            final WeavableParameter<SELF, TARGET, T> param =
-                new WeavableParameter<SELF, TARGET, T>(Integer.valueOf(i), (SELF) this);
-            params.add(param);
+            params.add(createParameter(i));
         }
         parameters = Collections.unmodifiableList(params);
     }
 
+    protected abstract P createParameter(int index);
+
     protected abstract Class<?>[] getParameterTypes();
 
     @Override
@@ -49,11 +48,11 @@ public abstract class WeavableExecutable
         return Args.compare(getParameterTypes(), o.getParameterTypes());
     }
 
-    public WeavableParameter<SELF, TARGET, T> getWeavableParameter(int index) {
+    public P getWeavableParameter(int index) {
         return parameters.get(index);
     }
 
-    public Iterable<WeavableParameter<SELF, TARGET, T>> getParameters() {
+    public Iterable<P> getParameters() {
         return parameters;
     }
 

Modified: commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableMethod.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableMethod.java?rev=1437189&r1=1437188&r2=1437189&view=diff
==============================================================================
--- commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableMethod.java
(original)
+++ commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableMethod.java
Tue Jan 22 21:37:52 2013
@@ -20,7 +20,7 @@ package org.apache.commons.weaver.model;
 
 import java.lang.reflect.Method;
 
-public class WeavableMethod<T> extends WeavableExecutable<WeavableMethod<T>,
Method, T> {
+public class WeavableMethod<T> extends WeavableExecutable<WeavableMethod<T>,
Method, T, WeavableMethodParameter<T>> {
 
     public WeavableMethod(Method target, WeavableClass<T> parent) {
         super(target, parent);
@@ -36,4 +36,9 @@ public class WeavableMethod<T> extends W
         int result = getTarget().getName().compareTo(o.getTarget().getName());
         return result == 0 ? super.localCompareTo(o) : result;
     }
+
+    @Override
+    protected WeavableMethodParameter<T> createParameter(int index) {
+        return new WeavableMethodParameter<T>(Integer.valueOf(index), this);
+    }
 }

Modified: commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableParameter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableParameter.java?rev=1437189&r1=1437188&r2=1437189&view=diff
==============================================================================
--- commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableParameter.java
(original)
+++ commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/model/WeavableParameter.java
Tue Jan 22 21:37:52 2013
@@ -20,7 +20,7 @@ package org.apache.commons.weaver.model;
 
 import java.lang.reflect.Member;
 
-public class WeavableParameter<SELF extends WeavableParameter<SELF, PARENT, PARENT_TARGET,
T>, PARENT extends WeavableExecutable<PARENT, PARENT_TARGET, T>, PARENT_TARGET extends
Member, T>
+public abstract class WeavableParameter<SELF extends WeavableParameter<SELF, PARENT,
PARENT_TARGET, T>, PARENT extends WeavableExecutable<PARENT, PARENT_TARGET, T, SELF>,
PARENT_TARGET extends Member, T>
     extends NestedWeavable<SELF, Integer, PARENT, PARENT_TARGET> {
 
     protected WeavableParameter(Integer target, PARENT parent) {

Modified: commons/sandbox/weaver/branches/mjb/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/mjb/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java?rev=1437189&r1=1437188&r2=1437189&view=diff
==============================================================================
--- commons/sandbox/weaver/branches/mjb/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java
(original)
+++ commons/sandbox/weaver/branches/mjb/processor/src/test/java/org/apache/commons/weaver/test/weaver/TestWeaver.java
Tue Jan 22 21:37:52 2013
@@ -58,14 +58,13 @@ public class TestWeaver implements Weave
     @Override
     public boolean process(ScanResult scanResult) {
         boolean result = false;
-        for (WeavableClass<?> weavableClass : scanResult.getClasses()) {
-            if (weavableClass.isAnnotationPresent(TestAnnotation.class) && wovenClasses.add(weavableClass.getTarget()))
{
+        for (WeavableClass<?> weavableClass : scanResult.getClasses().with(TestAnnotation.class))
{
+            if (wovenClasses.add(weavableClass.getTarget())) {
                 result = true;
             }
         }
-        for (WeavableMethod<?> weavableMethod : scanResult.getMethods()) {
-            if (weavableMethod.isAnnotationPresent(TestAnnotation.class)
-                && wovenMethods.add(weavableMethod.getTarget())) {
+        for (WeavableMethod<?> weavableMethod : scanResult.getMethods().with(TestAnnotation.class))
{
+            if (wovenMethods.add(weavableMethod.getTarget())) {
                 result = true;
             }
         }



Mime
View raw message