1 46 47 package org.codehaus.groovy.classgen; 48 49 import org.codehaus.groovy.ast.ClassNode; 50 import org.codehaus.groovy.ast.ConstructorNode; 51 import org.codehaus.groovy.ast.MethodNode; 52 import org.codehaus.groovy.ast.Parameter; 53 import org.codehaus.groovy.ast.PropertyNode; 54 import org.codehaus.groovy.ast.Type; 55 import org.codehaus.groovy.ast.expr.BinaryExpression; 56 import org.codehaus.groovy.ast.expr.ConstantExpression; 57 import org.codehaus.groovy.ast.expr.Expression; 58 import org.codehaus.groovy.ast.expr.ListExpression; 59 import org.codehaus.groovy.ast.expr.MapExpression; 60 import org.codehaus.groovy.ast.expr.TupleExpression; 61 import org.codehaus.groovy.ast.expr.VariableExpression; 62 import org.codehaus.groovy.ast.stmt.ExpressionStatement; 63 import org.codehaus.groovy.ast.stmt.ForStatement; 64 import org.codehaus.groovy.ast.stmt.Statement; 65 import org.codehaus.groovy.ast.stmt.BlockStatement; 66 import org.codehaus.groovy.runtime.InvokerHelper; 67 import org.codehaus.groovy.runtime.InvokerInvocationException; 68 import org.codehaus.groovy.syntax.Token; 69 70 75 public class TupleListTest extends TestSupport { 76 77 public void testIterateOverTuple() throws Exception { 78 TupleExpression listExpression = new TupleExpression(); 79 listExpression.addExpression(new ConstantExpression("a")); 80 listExpression.addExpression(new ConstantExpression("b")); 81 listExpression.addExpression(new ConstantExpression("c")); 82 assertIterate("iterateOverTuple", listExpression); 83 } 84 85 public void testIterateOverList() throws Exception { 86 ListExpression listExpression = new ListExpression(); 87 listExpression.addExpression(new ConstantExpression("a")); 88 listExpression.addExpression(new ConstantExpression("b")); 89 listExpression.addExpression(new ConstantExpression("c")); 90 listExpression.addExpression(new ConstantExpression("a")); 91 listExpression.addExpression(new ConstantExpression("b")); 92 listExpression.addExpression(new ConstantExpression("c")); 93 assertIterate("iterateOverList", listExpression); 94 } 95 96 public void testIterateOverMap() throws Exception { 97 MapExpression mapExpression = new MapExpression(); 98 mapExpression.addMapEntryExpression(new ConstantExpression("a"), new ConstantExpression("x")); 99 mapExpression.addMapEntryExpression(new ConstantExpression("b"), new ConstantExpression("y")); 100 mapExpression.addMapEntryExpression(new ConstantExpression("c"), new ConstantExpression("z")); 101 assertIterate("iterateOverMap", mapExpression); 102 } 103 104 protected void assertIterate(String methodName, Expression listExpression) throws Exception { 105 ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, "java.lang.Object"); 106 classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null)); 107 classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, "java.lang.String", "Foo", null, null, null)); 108 109 Statement loopStatement = createPrintlnStatement(new VariableExpression("i")); 110 111 BlockStatement block = new BlockStatement(); 112 block.addStatement(new ExpressionStatement(new BinaryExpression(new VariableExpression("list"), Token.newSymbol("=", 0, 0), listExpression))); 113 block.addStatement(new ForStatement("i", Type.DYNAMIC_TYPE, new VariableExpression("list"), loopStatement)); 114 classNode.addMethod(new MethodNode(methodName, ACC_PUBLIC, "void", Parameter.EMPTY_ARRAY, block)); 115 116 Class fooClass = loadClass(classNode); 117 assertTrue("Loaded a new class", fooClass != null); 118 119 Object bean = fooClass.newInstance(); 120 assertTrue("Managed to create bean", bean != null); 121 122 System.out.println("################ Now about to invoke method"); 123 124 try { 125 InvokerHelper.invokeMethod(bean, methodName, null); 126 } 127 catch (InvokerInvocationException e) { 128 System.out.println("Caught: " + e.getCause()); 129 e.getCause().printStackTrace(); 130 fail("Should not have thrown an exception"); 131 } 132 System.out.println("################ Done"); } 133 134 } 135 | Popular Tags |