[jboss-cvs] JBossAS SVN: r78752 - 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 Sep 22 09:30:40 EDT 2008


Author: kabir.khan at jboss.com
Date: 2008-09-22 09:30:40 -0400 (Mon, 22 Sep 2008)
New Revision: 78752

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-43] Replace properties in annotations

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-09-22 11:42:19 UTC (rev 78751)
+++ projects/jboss-mdr/trunk/src/main/java/org/jboss/annotation/factory/AnnotationCreator.java	2008-09-22 13:30:40 UTC (rev 78752)
@@ -45,6 +45,7 @@
 import org.jboss.annotation.factory.ast.Node;
 import org.jboss.annotation.factory.ast.SimpleNode;
 import org.jboss.annotation.factory.javassist.DefaultValueAnnotationValidator;
+import org.jboss.util.StringPropertyReplacer;
 
 /**
  * Comment
@@ -322,6 +323,7 @@
    
    private static ASTAnnotation getRootExpr(final String annotationExpr) throws Exception
    {
+      final StringBuffer expr = new StringBuffer();
       try
       {
 
@@ -329,7 +331,9 @@
          {
            public ASTAnnotation run() throws Exception
            {
-              AnnotationParser parser = new AnnotationParser(new StringReader(annotationExpr));
+              String expression = StringPropertyReplacer.replaceProperties(annotationExpr);
+              expr.append(expression);
+              AnnotationParser parser = new AnnotationParser(new StringReader(expression));
               ASTStart start = parser.Start();
               return (ASTAnnotation) start.jjtGetChild(0);
            }
@@ -337,11 +341,10 @@
       }
       catch (PrivilegedActionException e)
       {
-         throw new RuntimeException("Error getting root expression", e.getException());
+         throw new RuntimeException("Error getting root expression " + expr.toString(), e.getException());
       }
    }
    
-   
    public static Object createAnnotation(ASTAnnotation node, Class<?> annotation, ClassLoader cl) throws Exception
    {
       HashMap<String, Object> map = new HashMap<String, Object>();

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-09-22 11:42:19 UTC (rev 78751)
+++ projects/jboss-mdr/trunk/src/test/java/org/jboss/test/annotation/factory/test/AnnotationCreatorTest.java	2008-09-22 13:30:40 UTC (rev 78752)
@@ -59,6 +59,15 @@
       assertEquals("Test", ((SimpleValue)annotation).value());
    }
    
+   public void testSimpleValueWithPropertyReplacement() throws Exception
+   {
+      String expr = "@org.jboss.test.annotation.factory.support.SimpleValue(\"${value1}\")";
+      System.setProperty("value1", "Test");
+      Annotation annotation  = (Annotation)AnnotationCreator.createAnnotation(expr, SimpleValue.class);
+      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)";      
@@ -78,6 +87,42 @@
       assertEquals(new String[]{"Test", "123"}, complex.array());
       assertEquals(MyEnum.TWO, complex.enumVal());
    }
+   
+   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})";
+      
+      System.setProperty("ch", "a");
+      System.setProperty("string", "Test123");
+      System.setProperty("flt", "9.9");
+      System.setProperty("dbl", "123456789.99");
+      System.setProperty("shrt", "1");
+      System.setProperty("lng", "987654321");
+      System.setProperty("integer", "123");
+      System.setProperty("bool", "true");
+      System.setProperty("annotation", "@org.jboss.test.annotation.factory.support.SimpleValue(\"Yes\")");
+      System.setProperty("array", "{\"Test\", \"123\"}");
+      System.setProperty("clazz", "java.lang.Long.class");
+      System.setProperty("enumVal", "org.jboss.test.annotation.factory.support.MyEnum.TWO");
+      
+      
+      Annotation annotation  = (Annotation)AnnotationCreator.createAnnotation(expr, Complex.class);
+      assertEquals(Complex.class, annotation.annotationType());
+      Complex complex = (Complex)annotation;
+      assertEquals('a', complex.ch());
+      assertEquals("Test123", complex.string());
+      assertEquals(9,9, complex.flt());
+      assertEquals(123456789.99, complex.dbl());
+      assertEquals(1, complex.shrt());
+      assertEquals(987654321, complex.lng());
+      assertEquals(123, complex.integer());
+      assertEquals(true, complex.bool());
+      assertEquals(Long.class, complex.clazz());
+      assertEquals("Yes", complex.annotation().value());
+      assertEquals(new String[]{"Test", "123"}, complex.array());
+      assertEquals(MyEnum.TWO, complex.enumVal());
+   }
 
    public void testSerializable() throws Exception
    {




More information about the jboss-cvs-commits mailing list