commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1444932 - in /commons/sandbox/weaver/branches/mjb/processor: pom.xml src/main/java/org/apache/commons/weaver/Finder.java src/main/java/org/apache/commons/weaver/utils/Annotations.java
Date Mon, 11 Feb 2013 19:29:46 GMT
Author: mbenson
Date: Mon Feb 11 19:29:46 2013
New Revision: 1444932

URL: http://svn.apache.org/r1444932
Log:
use own code for annotation instances instead of full-blown proxy-stub

Added:
    commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/utils/Annotations.java
  (with props)
Modified:
    commons/sandbox/weaver/branches/mjb/processor/pom.xml
    commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/Finder.java

Modified: commons/sandbox/weaver/branches/mjb/processor/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/mjb/processor/pom.xml?rev=1444932&r1=1444931&r2=1444932&view=diff
==============================================================================
--- commons/sandbox/weaver/branches/mjb/processor/pom.xml (original)
+++ commons/sandbox/weaver/branches/mjb/processor/pom.xml Mon Feb 11 19:29:46 2013
@@ -41,8 +41,8 @@ under the License.
     </dependency>
     <dependency>
       <groupId>org.apache.commons</groupId>
-      <artifactId>commons-proxy2-stub</artifactId>
-      <version>2.0-SNAPSHOT</version>
+      <artifactId>commons-lang3</artifactId>
+      <version>3.1</version>
     </dependency>
 
   </dependencies>

Modified: commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/Finder.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/Finder.java?rev=1444932&r1=1444931&r2=1444932&view=diff
==============================================================================
--- commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/Finder.java
(original)
+++ commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/Finder.java
Mon Feb 11 19:29:46 2013
@@ -15,7 +15,7 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.lang3.Validate;
-import org.apache.commons.proxy.stub.AnnotationFactory;
+import org.apache.commons.weaver.utils.Annotations;
 import org.apache.xbean.asm.AnnotationVisitor;
 import org.apache.xbean.asm.Attribute;
 import org.apache.xbean.asm.ClassReader;
@@ -44,7 +44,7 @@ class Finder extends AnnotationFinder {
         }
 
         Annotation inflate() {
-            return AnnotationFactory.INSTANCE.create(annotationType, elements);
+            return Annotations.instanceOf(annotationType, elements);
         }
 
         @Override

Added: commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/utils/Annotations.java
URL: http://svn.apache.org/viewvc/commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/utils/Annotations.java?rev=1444932&view=auto
==============================================================================
--- commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/utils/Annotations.java
(added)
+++ commons/sandbox/weaver/branches/mjb/processor/src/main/java/org/apache/commons/weaver/utils/Annotations.java
Mon Feb 11 19:29:46 2013
@@ -0,0 +1,71 @@
+/*
+ * 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.utils;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Arrays;
+import java.util.Map;
+
+import org.apache.commons.lang3.AnnotationUtils;
+import org.apache.commons.lang3.Validate;
+
+/**
+ * Provide annotation-related utility methods.
+ */
+public class Annotations {
+    private Annotations() {
+    }
+
+    public static <A extends Annotation> A instanceOf(final Class<A> annotationType,
final Map<String, ?> elements) {
+        final ClassLoader proxyClassLoader = Validate.notNull(annotationType, "annotationType").getClassLoader();
+        final InvocationHandler invocationHandler = new InvocationHandler() {
+
+            @Override
+            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
+                if (method.getDeclaringClass().equals(annotationType)) {
+                    if (elements.containsKey(method.getName())) {
+                        return elements.get(method.getName());
+                    }
+                    return method.getDefaultValue();
+                }
+                if ("annotationType".equals(method.getName()) && method.getParameterTypes().length
== 0) {
+                    return annotationType;
+                }
+                if ("equals".equals(method.getName())
+                    && Arrays.equals(method.getParameterTypes(), new Class[] { Object.class
})) {
+                    return AnnotationUtils.equals((Annotation) proxy, (Annotation) args[0]);
+                }
+                if ("hashCode".equals(method.getName()) && method.getParameterTypes().length
== 0) {
+                    return AnnotationUtils.hashCode((Annotation) proxy);
+                }
+                if ("toString".equals(method.getName()) && method.getParameterTypes().length
== 0) {
+                    return AnnotationUtils.toString((Annotation) proxy);
+                }
+                throw new UnsupportedOperationException();
+            }
+        };
+        @SuppressWarnings("unchecked")
+        final A result =
+            (A) Proxy.newProxyInstance(proxyClassLoader, new Class[] { annotationType },
invocationHandler);
+        return result;
+    }
+}

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



Mime
View raw message