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. package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
  20. import com.sun.org.apache.bcel.internal.generic.ALOAD;
  21. import com.sun.org.apache.bcel.internal.generic.ASTORE;
  22. import com.sun.org.apache.bcel.internal.generic.BranchHandle;
  23. import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  24. import com.sun.org.apache.bcel.internal.generic.GOTO;
  25. import com.sun.org.apache.bcel.internal.generic.IFNULL;
  26. import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
  27. import com.sun.org.apache.bcel.internal.generic.Instruction;
  28. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  29. import com.sun.org.apache.bcel.internal.generic.PUSH;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
  31. /**
  32. * @author Todd Miller
  33. * @author Santiago Pericas-Geertsen
  34. */
  35. public final class ObjectType extends Type {
  36. private String _javaClassName = "java.lang.Object";
  37. private Class _clazz = java.lang.Object.class;
  38. /**
  39. * Used to represent a Java Class type such is required to support
  40. * non-static java functions.
  41. * @param javaClassName name of the class such as 'com.foo.Processor'
  42. */
  43. protected ObjectType(String javaClassName) {
  44. _javaClassName = javaClassName;
  45. try {
  46. _clazz = ObjectFactory.findProviderClass(
  47. javaClassName, ObjectFactory.findClassLoader(), true);
  48. }
  49. catch (ClassNotFoundException e) {
  50. _clazz = null;
  51. }
  52. }
  53. protected ObjectType(Class clazz) {
  54. _clazz = clazz;
  55. _javaClassName = clazz.getName();
  56. }
  57. public int hashCode() {
  58. return toString().hashCode();
  59. }
  60. public boolean equals(Object obj) {
  61. return (obj instanceof ObjectType);
  62. }
  63. public String getJavaClassName() {
  64. return _javaClassName;
  65. }
  66. public Class getJavaClass() {
  67. return _clazz;
  68. }
  69. public String toString() {
  70. return _javaClassName;
  71. }
  72. public boolean identicalTo(Type other) {
  73. return this == other;
  74. }
  75. public String toSignature() {
  76. final StringBuffer result = new StringBuffer("L");
  77. result.append(_javaClassName.replace('.', '/')).append(';');
  78. return result.toString();
  79. }
  80. public com.sun.org.apache.bcel.internal.generic.Type toJCType() {
  81. return Util.getJCRefType(toSignature());
  82. }
  83. /**
  84. * Translates a void into an object of internal type <code>type</code>.
  85. * This translation is needed when calling external functions
  86. * that return void.
  87. *
  88. * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
  89. */
  90. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  91. Type type) {
  92. if (type == Type.String) {
  93. translateTo(classGen, methodGen, (StringType) type);
  94. }
  95. else {
  96. ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
  97. toString(), type.toString());
  98. classGen.getParser().reportError(Constants.FATAL, err);
  99. }
  100. }
  101. /**
  102. * Expects an integer on the stack and pushes its string value by calling
  103. * <code>Integer.toString(int i)</code>.
  104. *
  105. * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
  106. */
  107. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  108. StringType type) {
  109. final ConstantPoolGen cpg = classGen.getConstantPool();
  110. final InstructionList il = methodGen.getInstructionList();
  111. il.append(DUP);
  112. final BranchHandle ifNull = il.append(new IFNULL(null));
  113. il.append(new INVOKEVIRTUAL(cpg.addMethodref(_javaClassName,
  114. "toString",
  115. "()" + STRING_SIG)));
  116. final BranchHandle gotobh = il.append(new GOTO(null));
  117. ifNull.setTarget(il.append(POP));
  118. il.append(new PUSH(cpg, ""));
  119. gotobh.setTarget(il.append(NOP));
  120. }
  121. /**
  122. * Translates an object of this type to the external (Java) type denoted
  123. * by <code>clazz</code>. This method is used to translate parameters
  124. * when external functions are called.
  125. */
  126. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  127. Class clazz) {
  128. if (clazz.isAssignableFrom(_clazz))
  129. methodGen.getInstructionList().append(NOP);
  130. else {
  131. ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
  132. toString(), clazz.getClass().toString());
  133. classGen.getParser().reportError(Constants.FATAL, err);
  134. }
  135. }
  136. /**
  137. * Translates an external Java type into an Object type
  138. */
  139. public void translateFrom(ClassGenerator classGen,
  140. MethodGenerator methodGen, Class clazz) {
  141. methodGen.getInstructionList().append(NOP);
  142. }
  143. public Instruction LOAD(int slot) {
  144. return new ALOAD(slot);
  145. }
  146. public Instruction STORE(int slot) {
  147. return new ASTORE(slot);
  148. }
  149. }