[jboss-cvs] JBossAS SVN: r79151 - in projects/jboss-mdr/trunk/src: test/java/org/jboss/test/annotation/factory/test and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Oct 6 09:09:20 EDT 2008


Author: alesj
Date: 2008-10-06 09:09:19 -0400 (Mon, 06 Oct 2008)
New Revision: 79151

Modified:
   projects/jboss-mdr/trunk/src/main/java/org/jboss/annotation/factory/AnnotationCreator.java
   projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/test/AnnotationCreatorTest.java
Log:
[JBMDR-45]; fix annotation creator.

Modified: projects/jboss-mdr/trunk/src/main/java/org/jboss/annotation/factory/AnnotationCreator.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/java/org/jboss/annotation/factory/AnnotationCreator.java	2008-10-06 12:57:33 UTC (rev 79150)
+++ projects/jboss-mdr/trunk/src/main/java/org/jboss/annotation/factory/AnnotationCreator.java	2008-10-06 13:09:19 UTC (rev 79151)
@@ -30,6 +30,7 @@
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.util.HashMap;
+import java.util.Properties;
 
 import org.jboss.annotation.factory.ast.ASTAnnotation;
 import org.jboss.annotation.factory.ast.ASTChar;
@@ -323,32 +324,58 @@
    
    private static ASTAnnotation getRootExpr(final String annotationExpr) throws Exception
    {
-      final StringBuffer expr = new StringBuffer();
+      return getRootExpr(annotationExpr, null, false);
+   }
+
+   private static ASTAnnotation getRootExpr(final String annotationExpr, final Properties properties) throws Exception
+   {
+      return getRootExpr(annotationExpr, properties, null);
+   }
+
+   private static ASTAnnotation getRootExpr(final String annotationExpr, final Properties properties, final Boolean replace) throws Exception
+   {
       try
       {
-
          return AccessController.doPrivileged(new PrivilegedExceptionAction<ASTAnnotation>()
          {
-           public ASTAnnotation run() throws Exception
-           {
-              String expression = StringPropertyReplacer.replaceProperties(annotationExpr);
-              expr.append(expression);
-              AnnotationParser parser = new AnnotationParser(new StringReader(expression));
-              ASTStart start = parser.Start();
-              return (ASTAnnotation) start.jjtGetChild(0);
-           }
+            public ASTAnnotation run() throws Exception
+            {
+               String expr;
+               if (properties != null && properties.isEmpty() == false)
+                  expr = StringPropertyReplacer.replaceProperties(annotationExpr, properties);
+               else if (replace != null && replace)
+                  expr = StringPropertyReplacer.replaceProperties(annotationExpr);
+               else
+                  expr = annotationExpr;
+               AnnotationParser parser = new AnnotationParser(new StringReader(expr));
+               ASTStart start = parser.Start();
+               return (ASTAnnotation)start.jjtGetChild(0);
+            }
          });
       }
       catch (PrivilegedActionException e)
       {
-         throw new RuntimeException("Error getting root expression " + expr.toString(), e.getException());
+         throw new RuntimeException("Error getting root expression " + annotationExpr, e.getException());
       }
    }
-   
+
    public static Object createAnnotation(ASTAnnotation node, Class<?> annotation, ClassLoader cl) throws Exception
    {
       HashMap<String, Object> map = new HashMap<String, Object>();
-      ClassLoader loader = (cl != null) ? cl : Thread.currentThread().getContextClassLoader();
+      ClassLoader loader;
+      if (cl != null)
+      {
+         loader = cl;
+      }
+      else if (annotation != null)
+      {
+         loader = annotation.getClassLoader();
+      }
+      else
+      {
+         loader = Thread.currentThread().getContextClassLoader();
+      }
+      
       if (annotation == null)
       {
          annotation = loader.loadClass(node.getIdentifier());
@@ -382,20 +409,98 @@
       return AnnotationProxy.createProxy(map, annotation);
    }
 
+   /**
+    * Create annotation.
+    *
+    * @param node the ast annotation node
+    * @param annotation the annotation class
+    * @return new annotation instance
+    * @throws Exception for any error
+    */
    public static Object createAnnotation(ASTAnnotation node, Class<?> annotation) throws Exception
    {
       return createAnnotation(node, annotation, null);
    }
-   
+
+   /**
+    * Create annotation.
+    *
+    * @param annotationExpr the annotation expression
+    * @param annotation the annotation class
+    * @return new annotation instance
+    * @throws Exception for any error
+    */
    public static Object createAnnotation(final String annotationExpr, final Class<?> annotation) throws Exception
    {
       return createAnnotation(getRootExpr(annotationExpr), annotation, null);
    }
 
+   /**
+    * Create annotation.
+    *
+    * @param annotationExpr the annotation expression
+    * @param cl the classloader to use
+    * @return new annotation instance
+    * @throws Exception for any error
+    */
    public static Object createAnnotation(String annotationExpr, ClassLoader cl) throws Exception
    {
       return createAnnotation(getRootExpr(annotationExpr), null, cl);
    }
 
-   
+   /**
+    * Create annotation.
+    *
+    * @param annotationExpr the annotation expression
+    * @param annotation the annotation class
+    * @param replace should we replace possible properties in expression
+    * @return new annotation instance
+    * @throws Exception for any error
+    */
+   public static Object createAnnotation(final String annotationExpr, final Class<?> annotation, boolean replace) throws Exception
+   {
+      return createAnnotation(getRootExpr(annotationExpr, null, replace), annotation, null);
+   }
+
+   /**
+    * Create annotation.
+    *
+    * @param annotationExpr the annotation expression
+    * @param cl the classloader to use
+    * @param replace should we replace possible properties in expression
+    * @return new annotation instance
+    * @throws Exception for any error
+    */
+   public static Object createAnnotation(String annotationExpr, ClassLoader cl, boolean replace) throws Exception
+   {
+      return createAnnotation(getRootExpr(annotationExpr, null, replace), null, cl);
+   }
+
+   /**
+    * Create annotation.
+    *
+    * @param annotationExpr the annotation expression
+    * @param annotation the annotation class
+    * @param properties the properties to use for replacement
+    * @return new annotation instance
+    * @throws Exception for any error
+    */
+   public static Object createAnnotation(final String annotationExpr, final Class<?> annotation, Properties properties) throws Exception
+   {
+      return createAnnotation(getRootExpr(annotationExpr, properties), annotation, null);
+   }
+
+   /**
+    * Create annotation.
+    *
+    * @param annotationExpr the annotation expression
+    * @param cl the classloader to use
+    * @param properties the properties to use for replacement
+    * @return new annotation instance
+    * @throws Exception for any error
+    */
+   public static Object createAnnotation(String annotationExpr, ClassLoader cl, Properties properties) throws Exception
+   {
+      return createAnnotation(getRootExpr(annotationExpr, properties), null, cl);
+   }
 }

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-06 12:57:33 UTC (rev 79150)
+++ projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/test/AnnotationCreatorTest.java	2008-10-06 13:09:19 UTC (rev 79151)
@@ -63,11 +63,20 @@
    {
       String expr = "@org.jboss.test.annotation.factory.support.SimpleValue(\"${value1}\")";
       System.setProperty("value1", "Test");
-      Annotation annotation  = (Annotation)AnnotationCreator.createAnnotation(expr, SimpleValue.class);
+      Annotation annotation  = (Annotation)AnnotationCreator.createAnnotation(expr, SimpleValue.class, true);
       assertEquals(SimpleValue.class, annotation.annotationType());
       assertEquals("Test", ((SimpleValue)annotation).value());
    }
    
+   public void testSimpleValueWithPropertyReplacementFromProperties() throws Exception
+   {
+      String expr = "@org.jboss.test.annotation.factory.support.SimpleValue(\"${value1}\")";
+      System.setProperty("value1", "Test");
+      Annotation annotation  = (Annotation)AnnotationCreator.createAnnotation(expr, SimpleValue.class, System.getProperties());
+      assertEquals(SimpleValue.class, annotation.annotationType());
+      assertEquals("Test", ((SimpleValue)annotation).value());
+   }
+
    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)";      
@@ -107,7 +116,7 @@
       System.setProperty("enumVal", "org.jboss.test.annotation.factory.support.MyEnum.TWO");
       
       
-      Annotation annotation  = (Annotation)AnnotationCreator.createAnnotation(expr, Complex.class);
+      Annotation annotation  = (Annotation)AnnotationCreator.createAnnotation(expr, Complex.class, true);
       assertEquals(Complex.class, annotation.annotationType());
       Complex complex = (Complex)annotation;
       assertEquals('a', complex.ch());
@@ -146,5 +155,4 @@
       {
       }
    }
-
 }




More information about the jboss-cvs-commits mailing list