1 46 47 package org.codehaus.groovy.classgen; 48 49 import org.codehaus.groovy.ast.ClassNode; 50 import org.codehaus.groovy.ast.MethodNode; 51 import org.codehaus.groovy.ast.Parameter; 52 import org.codehaus.groovy.ast.expr.BinaryExpression; 53 import org.codehaus.groovy.ast.expr.BooleanExpression; 54 import org.codehaus.groovy.ast.expr.ConstantExpression; 55 import org.codehaus.groovy.ast.expr.GStringExpression; 56 import org.codehaus.groovy.ast.expr.MethodCallExpression; 57 import org.codehaus.groovy.ast.expr.VariableExpression; 58 import org.codehaus.groovy.ast.stmt.AssertStatement; 59 import org.codehaus.groovy.ast.stmt.BlockStatement; 60 import org.codehaus.groovy.ast.stmt.ExpressionStatement; 61 import org.codehaus.groovy.runtime.InvokerHelper; 62 import org.codehaus.groovy.runtime.InvokerInvocationException; 63 import org.codehaus.groovy.syntax.Token; 64 65 70 public class GStringTest extends TestSupport { 71 72 public void testConstructor() throws Exception { 73 ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, "java.lang.Object"); 74 75 77 GStringExpression compositeStringExpr = new GStringExpression( "hello ${user}!" ); 79 compositeStringExpr.addString(new ConstantExpression("Hello ")); 80 compositeStringExpr.addValue(new VariableExpression("user")); 81 compositeStringExpr.addString(new ConstantExpression("!")); 82 BlockStatement block = new BlockStatement(); 83 block.addStatement( 84 new ExpressionStatement( 85 new BinaryExpression( 86 new VariableExpression("user"), 87 Token.newSymbol("=", -1, -1), 88 new ConstantExpression("World")))); 89 block.addStatement( 90 new ExpressionStatement( 91 new BinaryExpression(new VariableExpression("str"), Token.newSymbol( "=", -1, -1), compositeStringExpr))); 92 block.addStatement( 93 new ExpressionStatement( 94 new MethodCallExpression(VariableExpression.THIS_EXPRESSION, "println", new VariableExpression("str")))); 95 96 block.addStatement( 97 new ExpressionStatement( 98 new BinaryExpression( 99 new VariableExpression("text"), 100 Token.newSymbol( "=", -1, -1), 101 new MethodCallExpression(new VariableExpression("str"), "toString", ConstantExpression.NULL)))); 102 103 block.addStatement( 104 new AssertStatement( 105 new BooleanExpression( 106 new BinaryExpression( 107 new VariableExpression("text"), 108 Token.newSymbol( "==", -1, -1), 109 new ConstantExpression("Hello World!"))))); 110 classNode.addMethod(new MethodNode("stringDemo", ACC_PUBLIC, "void", Parameter.EMPTY_ARRAY, block)); 111 112 Class fooClass = loadClass(classNode); 113 assertTrue("Loaded a new class", fooClass != null); 114 115 Object bean = fooClass.newInstance(); 116 assertTrue("Managed to create bean", bean != null); 117 118 System.out.println("################ Now about to invoke method"); 119 120 122 try { 123 InvokerHelper.invokeMethod(bean, "stringDemo", null); 124 } 125 catch (InvokerInvocationException e) { 126 System.out.println("Caught: " + e.getCause()); 127 e.getCause().printStackTrace(); 128 fail("Should not have thrown an exception"); 129 } 130 System.out.println("################ Done"); 131 } 132 } 133 | Popular Tags |