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: VariableRef.java,v 1.2.4.1 2005/09/05 09:33:50 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler;
25  
26  import com.sun.org.apache.bcel.internal.generic.CHECKCAST;
27  import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
28  import com.sun.org.apache.bcel.internal.generic.GETFIELD;
29  import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
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.NodeSetType;
34  
35  /**
36   * @author Jacek Ambroziak
37   * @author Santiago Pericas-Geertsen
38   * @author Morten Jorgensen
39   * @author Erwin Bolwidt <ejb@klomp.org>
40   */
41  final class VariableRef extends VariableRefBase {
42  
43      public VariableRef(Variable variable) {
44          super(variable);
45      }
46  
47      public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
48          final ConstantPoolGen cpg = classGen.getConstantPool();
49          final InstructionList il = methodGen.getInstructionList();
50  
51          // Fall-through for variables that are implemented as methods
52          if (_type.implementedAsMethod()) return;
53  
54          final String name = _variable.getEscapedName();
55          final String signature = _type.toSignature();
56  
57          if (_variable.isLocal()) {
58              if (classGen.isExternal()) {
59                  Closure variableClosure = _closure;
60                  while (variableClosure != null) {
61                      if (variableClosure.inInnerClass()) break;
62                      variableClosure = variableClosure.getParentClosure();
63                  }
64  
65                  if (variableClosure != null) {
66                      il.append(ALOAD_0);
67                      il.append(new GETFIELD(
68                          cpg.addFieldref(variableClosure.getInnerClassName(),
69                              name, signature)));
70                  }
71                  else {
72                      il.append(_variable.loadInstruction());
73                  }
74              }
75              else {
76                  il.append(_variable.loadInstruction());
77              }
78          }
79          else {
80              final String className = classGen.getClassName();
81              il.append(classGen.loadTranslet());
82              if (classGen.isExternal()) {
83                  il.append(new CHECKCAST(cpg.addClass(className)));
84              }
85              il.append(new GETFIELD(cpg.addFieldref(className,name,signature)));
86          }
87  
88          if (_variable.getType() instanceof NodeSetType) {
89              // The method cloneIterator() also does resetting
90              final int clone = cpg.addInterfaceMethodref(NODE_ITERATOR,
91                                                         "cloneIterator",
92                                                         "()" +
93                                                          NODE_ITERATOR_SIG);
94              il.append(new INVOKEINTERFACE(clone, 1));
95          }
96      }
97  }