[jboss-cvs] JBossAS SVN: r80310 - in projects/jboss-mdr/trunk/src: test/java/org/jboss/test and 2 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Oct 31 06:38:18 EDT 2008
Author: alesj
Date: 2008-10-31 06:38:18 -0400 (Fri, 31 Oct 2008)
New Revision: 80310
Added:
projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/support/NameImpl.java
Modified:
projects/jboss-mdr/trunk/src/main/java/org/jboss/annotation/factory/AnnotationProxy.java
projects/jboss-mdr/trunk/src/test/java/org/jboss/test/ContainerTest.java
projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/support/Complex.java
projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/test/AnnotationCreatorTest.java
Log:
[JBMDR-51]; fix AnnotationProxy::doEquals.
Modified: projects/jboss-mdr/trunk/src/main/java/org/jboss/annotation/factory/AnnotationProxy.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/java/org/jboss/annotation/factory/AnnotationProxy.java 2008-10-31 10:32:43 UTC (rev 80309)
+++ projects/jboss-mdr/trunk/src/main/java/org/jboss/annotation/factory/AnnotationProxy.java 2008-10-31 10:38:18 UTC (rev 80310)
@@ -22,35 +22,72 @@
package org.jboss.annotation.factory;
import java.io.Serializable;
+import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
+import java.lang.reflect.UndeclaredThrowableException;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Collections;
import java.util.Map;
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.config.plugins.property.PropertyConfiguration;
+import org.jboss.config.spi.Configuration;
+import org.jboss.util.JBossObject;
+import org.jboss.util.Objects;
+
/**
* InvocationHandler implementation for creating an annotation proxy.
*
* @author <a href="mailto:bill at jboss.org">Bill Burke</a>
* @author <a href="mailto:kabir.khan at jboss.org">Kabir Khan</a>
- * @version $Revision$
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
*/
public class AnnotationProxy implements InvocationHandler, Serializable
{
private static final long serialVersionUID = 1;
- @SuppressWarnings("unchecked")
- private Map map;
+ private static Configuration configuration;
+
+ static
+ {
+ configuration = AccessController.doPrivileged(new PrivilegedAction<Configuration>()
+ {
+ public Configuration run()
+ {
+ return new PropertyConfiguration();
+ }
+ });
+ }
+
private Class<?> annotationType;
+ private transient BeanInfo beanInfo;
+ private Map<String, Object> map;
@SuppressWarnings("unchecked")
public AnnotationProxy(Class<?> annotationType, Map valueMap)
{
+ if (annotationType == null)
+ throw new IllegalArgumentException("Null annotation type.");
this.annotationType = annotationType;
- map = valueMap;
+ this.map = valueMap != null ? valueMap : Collections.<String, Object>emptyMap();
}
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable
+ /**
+ * Get bean info.
+ *
+ * @return the annotation's bean info
+ */
+ protected BeanInfo getBeanInfo()
{
+ if (beanInfo == null)
+ beanInfo = configuration.getBeanInfo(annotationType);
+
+ return beanInfo;
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+ {
String name = method.getName();
if ("equals".equals(name))
{
@@ -68,15 +105,6 @@
{
return annotationType;
}
-
- /*
- Object obj = map.get(method.getName());
- if (!method.getReturnType().equals(obj.getClass()))
- {
- System.err.println("***** " + method.toString() + " has bad return type: " + obj.getClass().getName());
- }
- return obj;
- */
return map.get(name);
}
@@ -93,25 +121,47 @@
if (obj == null)
return Boolean.FALSE;
- Class[] intfs = proxy.getClass().getInterfaces();
- if (intfs[0].isAssignableFrom(obj.getClass()) == false)
+ if (annotationType.isInstance(obj) == false)
{
return Boolean.FALSE;
}
+
try
{
- Proxy.getInvocationHandler(obj);
+ for (String key : map.keySet())
+ {
+ Object value = getValue(key);
+ Object other = getBeanInfo().getProperty(obj, key);
+ if (Objects.isArray(value))
+ {
+ if (Objects.isArray(other) == false)
+ return Boolean.FALSE;
+
+ int length = Array.getLength(value);
+ if (length != Array.getLength(other))
+ return Boolean.FALSE;
+
+ for (int i = 0; i < length; i++)
+ {
+ if (JBossObject.equals(Array.get(value, i), Array.get(other, i)) == false)
+ return Boolean.FALSE;
+ }
+ }
+ else if (JBossObject.equals(value, other) == false)
+ return Boolean.FALSE;
+ }
}
- catch (Exception ex)
+ catch (Throwable t)
{
- return Boolean.FALSE;
+ throw new UndeclaredThrowableException(t);
}
+
return Boolean.TRUE;
}
private Object doHashCode()
{
- return new Integer(map.hashCode());
+ return map.hashCode();
}
/**
@@ -121,8 +171,7 @@
* @return an instance implementing the annotation
* @throws Exception for any error
*/
- @SuppressWarnings("unchecked")
- public static Object createProxy(Map map, Class<?> annotation) throws Exception
+ public static Object createProxy(Map<String, Object> map, Class<?> annotation) throws Exception
{
AnnotationProxy proxyHandler = new AnnotationProxy(annotation, map);
return java.lang.reflect.Proxy.newProxyInstance(annotation.getClassLoader(), new Class[]{annotation}, proxyHandler);
Modified: projects/jboss-mdr/trunk/src/test/java/org/jboss/test/ContainerTest.java
===================================================================
--- projects/jboss-mdr/trunk/src/test/java/org/jboss/test/ContainerTest.java 2008-10-31 10:32:43 UTC (rev 80309)
+++ projects/jboss-mdr/trunk/src/test/java/org/jboss/test/ContainerTest.java 2008-10-31 10:38:18 UTC (rev 80310)
@@ -21,6 +21,11 @@
*/
package org.jboss.test;
+import java.lang.reflect.Array;
+
+import org.jboss.util.Objects;
+import org.jboss.util.JBossObject;
+
/**
* A ContainerTest.
*
@@ -52,4 +57,29 @@
//delegate.enableSecurity = true;
return delegate;
}
+
+ protected static void assertArrays(Object value, Object other)
+ {
+ assertTrue(isArrayEqual(value, other));
+ }
+
+ protected static boolean isArrayEqual(Object value, Object other)
+ {
+ if (Objects.isArray(value))
+ {
+ if (Objects.isArray(other) == false)
+ return false;
+
+ int length = Array.getLength(value);
+ if (length != Array.getLength(other))
+ return false;
+
+ for (int i = 0; i < length; i++)
+ {
+ if (JBossObject.equals(Array.get(value, i), Array.get(other, i)) == false)
+ return false;
+ }
+ }
+ return true;
+ }
}
Modified: projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/support/Complex.java
===================================================================
--- projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/support/Complex.java 2008-10-31 10:32:43 UTC (rev 80309)
+++ projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/support/Complex.java 2008-10-31 10:38:18 UTC (rev 80310)
@@ -51,4 +51,6 @@
Class<?> clazz();
MyEnum enumVal();
+
+ int[] primitiveArray();
}
Added: projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/support/NameImpl.java
===================================================================
--- projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/support/NameImpl.java (rev 0)
+++ projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/support/NameImpl.java 2008-10-31 10:38:18 UTC (rev 80310)
@@ -0,0 +1,81 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.annotation.factory.support;
+
+import java.lang.annotation.Annotation;
+
+import org.jboss.util.JBossObject;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class NameImpl implements Name
+{
+ private String type = "";
+ private String subtype = "";
+
+ public NameImpl()
+ {
+ }
+
+ public NameImpl(String type, String subtype)
+ {
+ if (type != null)
+ this.type = type;
+ if (subtype != null)
+ this.subtype = subtype;
+ }
+
+ public String type()
+ {
+ return type;
+ }
+
+ public String subtype()
+ {
+ return subtype;
+ }
+
+ public Class<? extends Annotation> annotationType()
+ {
+ return Name.class;
+ }
+
+ public int hashCode()
+ {
+ return type.hashCode() + subtype.hashCode();
+ }
+
+ public boolean equals(Object obj)
+ {
+ if (obj instanceof Name == false)
+ return false;
+
+ Name name = (Name)obj;
+ return JBossObject.equals(type, name.type()) && JBossObject.equals(subtype, name.subtype());
+ }
+
+ public String toString()
+ {
+ return "@" + annotationType().getName() + "(type=" + type + ", subtype=" + subtype + ")";
+ }
+}
Modified: projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/test/AnnotationCreatorTest.java
===================================================================
--- projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/test/AnnotationCreatorTest.java 2008-10-31 10:32:43 UTC (rev 80309)
+++ projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/test/AnnotationCreatorTest.java 2008-10-31 10:38:18 UTC (rev 80310)
@@ -33,6 +33,7 @@
import org.jboss.test.annotation.factory.support.NameDefaults;
import org.jboss.test.annotation.factory.support.Simple;
import org.jboss.test.annotation.factory.support.SimpleValue;
+import org.jboss.test.annotation.factory.support.NameImpl;
/**
*
@@ -82,7 +83,7 @@
public void testComplex() throws Exception
{
- String expr = "@org.jboss.test.annotation.factory.support.Complex(ch='a', string=\"Test123\", flt=9.9, dbl=123456789.99, shrt=1, lng=987654321, integer=123, bool=true, annotation=@org.jboss.test.annotation.factory.support.SimpleValue(\"Yes\"), array={\"Test\", \"123\"}, clazz=java.lang.Long.class, enumVal=org.jboss.test.annotation.factory.support.MyEnum.TWO)";
+ String expr = "@org.jboss.test.annotation.factory.support.Complex(ch='a', string=\"Test123\", flt=9.9, dbl=123456789.99, shrt=1, lng=987654321, integer=123, bool=true, annotation=@org.jboss.test.annotation.factory.support.SimpleValue(\"Yes\"), array={\"Test\", \"123\"}, clazz=java.lang.Long.class, enumVal=org.jboss.test.annotation.factory.support.MyEnum.TWO, primitiveArray={1,2,3})";
Annotation annotation = (Annotation)AnnotationCreator.createAnnotation(expr, Complex.class);
assertEquals(Complex.class, annotation.annotationType());
Complex complex = (Complex)annotation;
@@ -98,12 +99,13 @@
assertEquals("Yes", complex.annotation().value());
assertEquals(new String[]{"Test", "123"}, complex.array());
assertEquals(MyEnum.TWO, complex.enumVal());
+ assertArrays(new int[]{1, 2, 3}, complex.primitiveArray());
}
public void testComplexWithPropertyReplacement() throws Exception
{
String expr = "@org.jboss.test.annotation.factory.support.Complex(ch='${ch}', string=\"${string}\", flt=${flt}, dbl=${dbl}, shrt=${shrt}, " +
- "lng=${lng}, integer=${integer}, bool=${bool}, annotation=${annotation}, array=${array}, clazz=${clazz}, enumVal=${enumVal})";
+ "lng=${lng}, integer=${integer}, bool=${bool}, annotation=${annotation}, array=${array}, clazz=${clazz}, enumVal=${enumVal}, primitiveArray=${primitiveArray})";
System.setProperty("ch", "a");
System.setProperty("string", "Test123");
@@ -117,8 +119,8 @@
System.setProperty("array", "{\"Test\", \"123\"}");
System.setProperty("clazz", "java.lang.Long.class");
System.setProperty("enumVal", "org.jboss.test.annotation.factory.support.MyEnum.TWO");
+ System.setProperty("primitiveArray", "{1, 2, 3}");
-
Annotation annotation = (Annotation)AnnotationCreator.createAnnotation(expr, Complex.class, true);
assertEquals(Complex.class, annotation.annotationType());
Complex complex = (Complex)annotation;
@@ -134,11 +136,12 @@
assertEquals("Yes", complex.annotation().value());
assertEquals(new String[]{"Test", "123"}, complex.array());
assertEquals(MyEnum.TWO, complex.enumVal());
+ assertArrays(new int[]{1, 2, 3}, complex.primitiveArray());
}
public void testSerializable() throws Exception
{
- String expr = "@org.jboss.test.annotation.factory.support.Complex(ch='a', string=\"Test123\", flt=9.9, dbl=123456789.99, shrt=1, lng=987654321, integer=123, bool=true, annotation=@org.jboss.test.annotation.factory.support.SimpleValue(\"Yes\"), array={\"Test\", \"123\"}, clazz=java.lang.Long.class, enumVal=org.jboss.test.annotation.factory.support.MyEnum.TWO)";
+ String expr = "@org.jboss.test.annotation.factory.support.Complex(ch='a', string=\"Test123\", flt=9.9, dbl=123456789.99, shrt=1, lng=987654321, integer=123, bool=true, annotation=@org.jboss.test.annotation.factory.support.SimpleValue(\"Yes\"), array={\"Test\", \"123\"}, clazz=java.lang.Long.class, enumVal=org.jboss.test.annotation.factory.support.MyEnum.TWO, primitiveArray={1,2,3})";
Annotation annotation = (Annotation)AnnotationCreator.createAnnotation(expr, Complex.class);
assertInstanceOf(annotation, Serializable.class);
byte[] bytes = serialize((Serializable)annotation);
@@ -168,12 +171,28 @@
Name n1 = NameDefaults.class.getAnnotation(Name.class);
getLog().debug("n1: "+n1);
assertEquals(n0, n1);
+ assertEquals(n1, n0);
Name defaultName = NameDefaults.defaultNameType();
getLog().debug("defaultName: "+defaultName);
// The jdk annotation should not equal the defaultName
assertFalse(n1+" != "+defaultName, n1.equals(defaultName));
- // The AnnotationCreator annotation should not equal the defaultName
+ assertFalse(defaultName+" != "+n1, defaultName.equals(n1));
+ // The AnnotationCreator annotation should not equal the defaultName
assertFalse(n0+" != "+defaultName, n0.equals(defaultName));
+ assertFalse(defaultName+" != "+n0, defaultName.equals(n0));
+
+ Name n2 = new NameImpl("type", "subtype");
+ getLog().debug("n2: " + n2);
+ assertEquals(n0, n2);
+ assertEquals(n2, n0);
+ assertEquals(n1, n2);
+ assertEquals(n2, n1);
+
+ Name n3 = new NameImpl();
+ assertFalse(n0 + "!=" + n3, n0.equals(n3));
+ assertFalse(n3 + "!=" + n0, n3.equals(n0));
+ assertFalse(n1 + "!=" + n3, n1.equals(n3));
+ assertFalse(n3 + "!=" + n1, n3.equals(n1));
}
}
More information about the jboss-cvs-commits
mailing list