[jboss-cvs] JBossAS SVN: r79178 - in projects/jboss-mdr/branches/Branch_2_0/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
Tue Oct 7 04:03:30 EDT 2008
Author: alesj
Date: 2008-10-07 04:03:30 -0400 (Tue, 07 Oct 2008)
New Revision: 79178
Modified:
projects/jboss-mdr/branches/Branch_2_0/src/main/java/org/jboss/annotation/factory/AnnotationCreator.java
projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/annotation/factory/test/AnnotationCreatorTest.java
Log:
Port JBMDR-45 to Branch_2_0.
Modified: projects/jboss-mdr/branches/Branch_2_0/src/main/java/org/jboss/annotation/factory/AnnotationCreator.java
===================================================================
--- projects/jboss-mdr/branches/Branch_2_0/src/main/java/org/jboss/annotation/factory/AnnotationCreator.java 2008-10-07 08:01:52 UTC (rev 79177)
+++ projects/jboss-mdr/branches/Branch_2_0/src/main/java/org/jboss/annotation/factory/AnnotationCreator.java 2008-10-07 08:03:30 UTC (rev 79178)
@@ -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;
@@ -45,6 +46,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.logging.Logger;
import org.jboss.util.StringPropertyReplacer;
/**
@@ -52,10 +54,13 @@
*
* @author <a href="mailto:bill at jboss.org">Bill Burke</a>
* @author <a href="mailto:kabir.khan at jboss.org">Kabir Khan</a>
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
* @version $Revision$
*/
public class AnnotationCreator implements AnnotationParserVisitor
{
+ private static final Logger log = Logger.getLogger(AnnotationCreator.class);
+
private Class<?> annotation;
private Class<?> type;
public Object typeValue;
@@ -99,7 +104,6 @@
this.loader = Thread.currentThread().getContextClassLoader();
}
-
public Object visit(ASTMemberValuePairs node, Object data)
{
node.childrenAccept(this, data);
@@ -323,32 +327,56 @@
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
+ {
+ String info = (annotation != null) ? annotation.getName() : node.getIdentifier();
+ log.warn("No ClassLoader provided, using TCCL: " + info);
+ loader = Thread.currentThread().getContextClassLoader();
+ }
+
if (annotation == null)
{
annotation = loader.loadClass(node.getIdentifier());
@@ -382,20 +410,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/branches/Branch_2_0/src/test/java/org/jboss/test/annotation/factory/test/AnnotationCreatorTest.java
===================================================================
--- projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/annotation/factory/test/AnnotationCreatorTest.java 2008-10-07 08:01:52 UTC (rev 79177)
+++ projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/annotation/factory/test/AnnotationCreatorTest.java 2008-10-07 08:03:30 UTC (rev 79178)
@@ -63,10 +63,19 @@
{
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
{
@@ -87,13 +96,12 @@
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})";
-
+ "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");
@@ -106,9 +114,9 @@
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);
+
+
+ Annotation annotation = (Annotation)AnnotationCreator.createAnnotation(expr, Complex.class, true);
assertEquals(Complex.class, annotation.annotationType());
Complex complex = (Complex)annotation;
assertEquals('a', complex.ch());
@@ -147,5 +155,4 @@
{
}
}
-
}
More information about the jboss-cvs-commits
mailing list