View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  /*
21   * $Id: CastCall.java,v 1.2.4.1 2005/09/01 11:47:58 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler;
25  
26  import java.util.Vector;
27  
28  import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
29  import com.sun.org.apache.bcel.internal.generic.CHECKCAST;
30  import com.sun.org.apache.bcel.internal.generic.InstructionList;
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.ErrorMsg;
34  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
35  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ObjectType;
36  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
37  
38  /**
39   * @author Santiago Pericas-Geertsen
40   */
41  final class CastCall extends FunctionCall {
42  
43      /**
44       * Name of the class that is the target of the cast. Must be a
45       * fully-qualified Java class Name.
46       */
47      private String _className;
48  
49      /**
50       * A reference to the expression being casted.
51       */
52      private Expression _right;
53  
54      /**
55       * Constructor.
56       */
57      public CastCall(QName fname, Vector arguments) {
58          super(fname, arguments);
59      }
60  
61      /**
62       * Type check the two parameters for this function
63       */
64      public Type typeCheck(SymbolTable stable) throws TypeCheckError {
65          // Check that the function was passed exactly two arguments
66          if (argumentCount() != 2) {
67              throw new TypeCheckError(new ErrorMsg(ErrorMsg.ILLEGAL_ARG_ERR,
68                                                    getName(), this));
69          }
70  
71          // The first argument must be a literal String
72          Expression exp = argument(0);
73          if (exp instanceof LiteralExpr) {
74              _className = ((LiteralExpr) exp).getValue();
75              _type = Type.newObjectType(_className);
76          }
77          else {
78              throw new TypeCheckError(new ErrorMsg(ErrorMsg.NEED_LITERAL_ERR,
79                                                    getName(), this));
80          }
81  
82           // Second argument must be of type reference or object
83          _right = argument(1);
84          Type tright = _right.typeCheck(stable);
85          if (tright != Type.Reference &&
86              tright instanceof ObjectType == false)
87          {
88              throw new TypeCheckError(new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
89                                                    tright, _type, this));
90          }
91  
92          return _type;
93      }
94  
95      public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
96          final ConstantPoolGen cpg = classGen.getConstantPool();
97          final InstructionList il = methodGen.getInstructionList();
98  
99          _right.translate(classGen, methodGen);
100         il.append(new CHECKCAST(cpg.addClass(_className)));
101     }
102 }