001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 /* 019 * $Id: ConcatCall.java 468650 2006-10-28 07:03:30Z minchau $ 020 */ 021 022 package org.apache.xalan.xsltc.compiler; 023 024 import java.util.Vector; 025 026 import org.apache.bcel.generic.ConstantPoolGen; 027 import org.apache.bcel.generic.INVOKESPECIAL; 028 import org.apache.bcel.generic.INVOKEVIRTUAL; 029 import org.apache.bcel.generic.Instruction; 030 import org.apache.bcel.generic.InstructionList; 031 import org.apache.bcel.generic.NEW; 032 import org.apache.bcel.generic.PUSH; 033 import org.apache.xalan.xsltc.compiler.util.ClassGenerator; 034 import org.apache.xalan.xsltc.compiler.util.MethodGenerator; 035 import org.apache.xalan.xsltc.compiler.util.Type; 036 import org.apache.xalan.xsltc.compiler.util.TypeCheckError; 037 038 /** 039 * @author Jacek Ambroziak 040 * @author Santiago Pericas-Geertsen 041 */ 042 final class ConcatCall extends FunctionCall { 043 public ConcatCall(QName fname, Vector arguments) { 044 super(fname, arguments); 045 } 046 047 public Type typeCheck(SymbolTable stable) throws TypeCheckError { 048 for (int i = 0; i < argumentCount(); i++) { 049 final Expression exp = argument(i); 050 if (!exp.typeCheck(stable).identicalTo(Type.String)) { 051 setArgument(i, new CastExpr(exp, Type.String)); 052 } 053 } 054 return _type = Type.String; 055 } 056 057 /** translate leaves a String on the stack */ 058 public void translate(ClassGenerator classGen, MethodGenerator methodGen) { 059 final ConstantPoolGen cpg = classGen.getConstantPool(); 060 final InstructionList il = methodGen.getInstructionList(); 061 final int nArgs = argumentCount(); 062 063 switch (nArgs) { 064 case 0: 065 il.append(new PUSH(cpg, EMPTYSTRING)); 066 break; 067 068 case 1: 069 argument().translate(classGen, methodGen); 070 break; 071 072 default: 073 final int initBuffer = cpg.addMethodref(STRING_BUFFER_CLASS, 074 "<init>", "()V"); 075 final Instruction append = 076 new INVOKEVIRTUAL(cpg.addMethodref(STRING_BUFFER_CLASS, 077 "append", 078 "("+STRING_SIG+")" 079 +STRING_BUFFER_SIG)); 080 081 final int toString = cpg.addMethodref(STRING_BUFFER_CLASS, 082 "toString", 083 "()"+STRING_SIG); 084 085 il.append(new NEW(cpg.addClass(STRING_BUFFER_CLASS))); 086 il.append(DUP); 087 il.append(new INVOKESPECIAL(initBuffer)); 088 for (int i = 0; i < nArgs; i++) { 089 argument(i).translate(classGen, methodGen); 090 il.append(append); 091 } 092 il.append(new INVOKEVIRTUAL(toString)); 093 } 094 } 095 }