KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > compiler > ConcatCall


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: ConcatCall.java,v 1.7 2004/02/16 22:24:28 minchau Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.xsltc.compiler;
21
22 import java.util.Vector JavaDoc;
23
24 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
25 import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL;
26 import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
27 import com.sun.org.apache.bcel.internal.generic.Instruction;
28 import com.sun.org.apache.bcel.internal.generic.InstructionList;
29 import com.sun.org.apache.bcel.internal.generic.NEW;
30 import com.sun.org.apache.bcel.internal.generic.PUSH;
31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
35
36 /**
37  * @author Jacek Ambroziak
38  * @author Santiago Pericas-Geertsen
39  */

40 final class ConcatCall extends FunctionCall {
41     public ConcatCall(QName fname, Vector JavaDoc arguments) {
42     super(fname, arguments);
43     }
44
45     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
46     for (int i = 0; i < argumentCount(); i++) {
47         final Expression exp = argument(i);
48         if (!exp.typeCheck(stable).identicalTo(Type.String)) {
49         setArgument(i, new CastExpr(exp, Type.String));
50         }
51     }
52     return _type = Type.String;
53     }
54     
55     /** translate leaves a String on the stack */
56     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
57     final ConstantPoolGen cpg = classGen.getConstantPool();
58     final InstructionList il = methodGen.getInstructionList();
59     final int nArgs = argumentCount();
60     
61     switch (nArgs) {
62     case 0:
63         il.append(new PUSH(cpg, EMPTYSTRING));
64         break;
65         
66     case 1:
67         argument().translate(classGen, methodGen);
68         break;
69
70     default:
71         final int initBuffer = cpg.addMethodref(STRING_BUFFER_CLASS,
72                             "<init>", "()V");
73         final Instruction append =
74         new INVOKEVIRTUAL(cpg.addMethodref(STRING_BUFFER_CLASS,
75                            "append",
76                            "("+STRING_SIG+")"
77                            +STRING_BUFFER_SIG));
78         
79         final int toString = cpg.addMethodref(STRING_BUFFER_CLASS,
80                           "toString",
81                           "()"+STRING_SIG);
82         
83         il.append(new NEW(cpg.addClass(STRING_BUFFER_CLASS)));
84         il.append(DUP);
85         il.append(new INVOKESPECIAL(initBuffer));
86         for (int i = 0; i < nArgs; i++) {
87         argument(i).translate(classGen, methodGen);
88         il.append(append);
89         }
90         il.append(new INVOKEVIRTUAL(toString));
91     }
92     }
93 }
94
Popular Tags