KickJava   Java API By Example, From Geeks To Geeks.

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


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: CastCall.java,v 1.3 2004/02/16 22:24:29 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.CHECKCAST;
26 import com.sun.org.apache.bcel.internal.generic.InstructionList;
27 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
28 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ObjectType;
32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
33
34 /**
35  * @author Santiago Pericas-Geertsen
36  */

37 final class CastCall extends FunctionCall {
38     
39     /**
40      * Name of the class that is the target of the cast. Must be a
41      * fully-qualified Java class Name.
42      */

43     private String JavaDoc _className;
44
45     /**
46      * A reference to the expression being casted.
47      */

48     private Expression _right;
49
50     /**
51      * Constructor.
52      */

53     public CastCall(QName fname, Vector JavaDoc arguments) {
54     super(fname, arguments);
55     }
56
57     /**
58      * Type check the two parameters for this function
59      */

60     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
61     // Check that the function was passed exactly two arguments
62
if (argumentCount() != 2) {
63         throw new TypeCheckError(new ErrorMsg(ErrorMsg.ILLEGAL_ARG_ERR,
64                                                   getName(), this));
65     }
66
67         // The first argument must be a literal String
68
Expression exp = argument(0);
69         if (exp instanceof LiteralExpr) {
70             _className = ((LiteralExpr) exp).getValue();
71             _type = Type.newObjectType(_className);
72         }
73         else {
74         throw new TypeCheckError(new ErrorMsg(ErrorMsg.NEED_LITERAL_ERR,
75                                                   getName(), this));
76         }
77         
78          // Second argument must be of type reference or object
79
_right = argument(1);
80         Type tright = _right.typeCheck(stable);
81         if (tright != Type.Reference &&
82             tright instanceof ObjectType == false)
83         {
84         throw new TypeCheckError(new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
85                                                   tright, _type, this));
86         }
87         
88         return _type;
89     }
90     
91     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
92     final ConstantPoolGen cpg = classGen.getConstantPool();
93     final InstructionList il = methodGen.getInstructionList();
94
95         _right.translate(classGen, methodGen);
96         il.append(new CHECKCAST(cpg.addClass(_className)));
97     }
98 }
99
Popular Tags