01: /*
02: * $Id: StringBufferNode.java,v 1.5 2002/09/16 08:05:05 jkl Exp $
03: *
04: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
05: *
06: * Use is subject to license terms, as defined in
07: * Anvil Sofware License, Version 1.1. See LICENSE
08: * file, or http://njet.org/license-1.1.txt
09: */
10: package anvil.script.expression;
11:
12: import anvil.core.Any;
13: import anvil.codec.Code;
14: import anvil.codec.ConstantPool;
15: import anvil.script.compiler.ByteCompiler;
16: import anvil.script.Context;
17: import java.io.IOException;
18:
19: /**
20: * class StringBufferNode
21: *
22: * @author: Jani Lehtimäki
23: */
24: public class StringBufferNode extends MultiParent {
25:
26: public StringBufferNode(Parent elements) {
27: super (elements);
28: }
29:
30: public int typeOf() {
31: return Node.EXPR_STRING_BUFFER;
32: }
33:
34: public Any eval() {
35: StringBuffer buffer = new StringBuffer();
36: int n = childs();
37: for (int i = 0; i < n; i++) {
38: buffer.append(getChild(i).eval());
39: }
40: return Any.create(buffer.toString());
41: }
42:
43: public void compile(ByteCompiler context, int operation) {
44: Code code = context.getCode();
45: ConstantPool pool = code.getPool();
46: int clazz = pool.addClass("java/lang/StringBuffer");
47: int appendmethod = pool.addMethodRef(clazz, "append",
48: "(Ljava/lang/Object;)Ljava/lang/StringBuffer;");
49: code.anew(clazz);
50: code.dup();
51: code.invokespecial(pool.addMethodRef(clazz, "<init>", "()V"));
52: int n = childs();
53: for (int i = 0; i < n; i++) {
54: getChild(i).compile(context, GET);
55: code.invokevirtual(appendmethod);
56: }
57: code.invokevirtual(code.getPool()
58: .addMethodRef(context.TYPE_OBJECT, "toString",
59: "()Ljava/lang/String;"));
60: code.invokestatic(code.getPool().addMethodRef(context.TYPE_ANY,
61: "create", "(Ljava/lang/String;)Lanvil/core/Any;"));
62: if (operation == GET_BOOLEAN) {
63: context.any2boolean();
64: }
65: }
66:
67: }
|