KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > VariableRef


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: VariableRef.java,v 1.17 2004/02/24 02:58:42 zongaro Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler;
21
22 import org.apache.bcel.generic.CHECKCAST;
23 import org.apache.bcel.generic.ConstantPoolGen;
24 import org.apache.bcel.generic.GETFIELD;
25 import org.apache.bcel.generic.INVOKEINTERFACE;
26 import org.apache.bcel.generic.InstructionList;
27 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
28 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
29 import org.apache.xalan.xsltc.compiler.util.NodeSetType;
30
31 /**
32  * @author Jacek Ambroziak
33  * @author Santiago Pericas-Geertsen
34  * @author Morten Jorgensen
35  * @author Erwin Bolwidt <ejb@klomp.org>
36  */

37 final class VariableRef extends VariableRefBase {
38
39     public VariableRef(Variable variable) {
40     super(variable);
41     }
42
43     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
44     final ConstantPoolGen cpg = classGen.getConstantPool();
45     final InstructionList il = methodGen.getInstructionList();
46
47     // Fall-through for variables that are implemented as methods
48
if (_type.implementedAsMethod()) return;
49
50     final String JavaDoc name = _variable.getEscapedName();
51     final String JavaDoc signature = _type.toSignature();
52
53     if (_variable.isLocal()) {
54         if (classGen.isExternal()) {
55         Closure variableClosure = _closure;
56         while (variableClosure != null) {
57             if (variableClosure.inInnerClass()) break;
58             variableClosure = variableClosure.getParentClosure();
59         }
60         
61         if (variableClosure != null) {
62             il.append(ALOAD_0);
63             il.append(new GETFIELD(
64             cpg.addFieldref(variableClosure.getInnerClassName(),
65                 name, signature)));
66         }
67         else {
68             il.append(_variable.loadInstruction());
69             _variable.removeReference(this);
70         }
71         }
72         else {
73         il.append(_variable.loadInstruction());
74         _variable.removeReference(this);
75         }
76     }
77     else {
78         final String JavaDoc className = classGen.getClassName();
79         il.append(classGen.loadTranslet());
80         if (classGen.isExternal()) {
81         il.append(new CHECKCAST(cpg.addClass(className)));
82         }
83         il.append(new GETFIELD(cpg.addFieldref(className,name,signature)));
84     }
85
86     if (_variable.getType() instanceof NodeSetType) {
87         // The method cloneIterator() also does resetting
88
final int clone = cpg.addInterfaceMethodref(NODE_ITERATOR,
89                                "cloneIterator",
90                                "()" +
91                             NODE_ITERATOR_SIG);
92         il.append(new INVOKEINTERFACE(clone, 1));
93     }
94     }
95 }
96
Popular Tags