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