KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > util > ObjectType


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: ObjectType.java,v 1.9 2004/02/23 10:29:35 aruny Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler.util;
21
22 import org.apache.bcel.generic.ALOAD;
23 import org.apache.bcel.generic.ASTORE;
24 import org.apache.bcel.generic.BranchHandle;
25 import org.apache.bcel.generic.ConstantPoolGen;
26 import org.apache.bcel.generic.GOTO;
27 import org.apache.bcel.generic.IFNULL;
28 import org.apache.bcel.generic.INVOKEVIRTUAL;
29 import org.apache.bcel.generic.Instruction;
30 import org.apache.bcel.generic.InstructionList;
31 import org.apache.bcel.generic.PUSH;
32 import org.apache.xalan.xsltc.compiler.Constants;
33
34 /**
35  * @author Todd Miller
36  * @author Santiago Pericas-Geertsen
37  */

38 public final class ObjectType extends Type {
39     
40     private String JavaDoc _javaClassName = "java.lang.Object";
41     private Class JavaDoc _clazz = java.lang.Object JavaDoc.class;
42
43     /**
44      * Used to represent a Java Class type such is required to support
45      * non-static java functions.
46      * @param javaClassName name of the class such as 'com.foo.Processor'
47      */

48     protected ObjectType(String JavaDoc javaClassName) {
49     _javaClassName = javaClassName;
50
51     try {
52           _clazz = ObjectFactory.findProviderClass(
53             javaClassName, ObjectFactory.findClassLoader(), true);
54     }
55     catch (ClassNotFoundException JavaDoc e) {
56       _clazz = null;
57     }
58     }
59     
60     protected ObjectType(Class JavaDoc clazz) {
61         _clazz = clazz;
62         _javaClassName = clazz.getName();
63     }
64
65     public int hashCode() {
66         return toString().hashCode();
67     }
68
69     public boolean equals(Object JavaDoc obj) {
70         return (obj instanceof ObjectType);
71     }
72
73     public String JavaDoc getJavaClassName() {
74     return _javaClassName;
75     }
76     
77     public Class JavaDoc getJavaClass() {
78         return _clazz;
79     }
80
81     public String JavaDoc toString() {
82     return _javaClassName;
83     }
84
85     public boolean identicalTo(Type other) {
86     return this == other;
87     }
88
89     public String JavaDoc toSignature() {
90     final StringBuffer JavaDoc result = new StringBuffer JavaDoc("L");
91     result.append(_javaClassName.replace('.', '/')).append(';');
92     return result.toString();
93     }
94
95     public org.apache.bcel.generic.Type toJCType() {
96     return Util.getJCRefType(toSignature());
97     }
98
99     /**
100      * Translates a void into an object of internal type <code>type</code>.
101      * This translation is needed when calling external functions
102      * that return void.
103      *
104      * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo
105      */

106     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
107                 Type type) {
108     if (type == Type.String) {
109         translateTo(classGen, methodGen, (StringType) type);
110     }
111     else {
112         ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
113                     toString(), type.toString());
114         classGen.getParser().reportError(Constants.FATAL, err);
115     }
116     }
117
118     /**
119      * Expects an integer on the stack and pushes its string value by calling
120      * <code>Integer.toString(int i)</code>.
121      *
122      * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo
123      */

124     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
125                 StringType type) {
126     final ConstantPoolGen cpg = classGen.getConstantPool();
127     final InstructionList il = methodGen.getInstructionList();
128
129     il.append(DUP);
130     final BranchHandle ifNull = il.append(new IFNULL(null));
131     il.append(new INVOKEVIRTUAL(cpg.addMethodref(_javaClassName,
132                             "toString",
133                             "()" + STRING_SIG)));
134     final BranchHandle gotobh = il.append(new GOTO(null));
135     ifNull.setTarget(il.append(POP));
136     il.append(new PUSH(cpg, ""));
137     gotobh.setTarget(il.append(NOP));
138     }
139
140     /**
141      * Translates an object of this type to the external (Java) type denoted
142      * by <code>clazz</code>. This method is used to translate parameters
143      * when external functions are called.
144      */

145     public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
146                 Class JavaDoc clazz) {
147         if (clazz.isAssignableFrom(_clazz))
148         methodGen.getInstructionList().append(NOP);
149     else {
150         ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
151                    toString(), clazz.getClass().toString());
152         classGen.getParser().reportError(Constants.FATAL, err);
153     }
154     }
155     
156     /**
157      * Translates an external Java type into an Object type
158      */

159     public void translateFrom(ClassGenerator classGen,
160                   MethodGenerator methodGen, Class JavaDoc clazz) {
161     methodGen.getInstructionList().append(NOP);
162     }
163
164     public Instruction LOAD(int slot) {
165     return new ALOAD(slot);
166     }
167     
168     public Instruction STORE(int slot) {
169     return new ASTORE(slot);
170     }
171 }
172
Popular Tags