You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Cast.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.expr;
  16. import javassist.*;
  17. import javassist.bytecode.*;
  18. import javassist.compiler.*;
  19. import javassist.compiler.ast.ASTList;
  20. /**
  21. * Explicit type cast.
  22. */
  23. public class Cast extends Expr {
  24. /**
  25. * Undocumented constructor. Do not use; internal-use only.
  26. */
  27. Cast(int pos, CodeIterator i, CtClass declaring, MethodInfo m) {
  28. super(pos, i, declaring, m);
  29. }
  30. /**
  31. * Returns the method or constructor containing the type cast
  32. * expression represented by this object.
  33. */
  34. public CtBehavior where() { return super.where(); }
  35. /**
  36. * Returns the line number of the source line containing the
  37. * type-cast expression.
  38. *
  39. * @return -1 if this information is not available.
  40. */
  41. public int getLineNumber() {
  42. return super.getLineNumber();
  43. }
  44. /**
  45. * Returns the source file containing the type-cast expression.
  46. *
  47. * @return null if this information is not available.
  48. */
  49. public String getFileName() {
  50. return super.getFileName();
  51. }
  52. /**
  53. * Returns the <code>CtClass</code> object representing
  54. * the type specified by the cast.
  55. */
  56. public CtClass getType() throws NotFoundException {
  57. ConstPool cp = getConstPool();
  58. int pos = currentPos;
  59. int index = iterator.u16bitAt(pos + 1);
  60. String name = cp.getClassInfo(index);
  61. return Descriptor.toCtClass(name, thisClass.getClassPool());
  62. }
  63. /**
  64. * Returns the list of exceptions that the expression may throw.
  65. * This list includes both the exceptions that the try-catch statements
  66. * including the expression can catch and the exceptions that
  67. * the throws declaration allows the method to throw.
  68. */
  69. public CtClass[] mayThrow() {
  70. return super.mayThrow();
  71. }
  72. /**
  73. * Replaces the explicit cast operator with the bytecode derived from
  74. * the given source text.
  75. *
  76. * <p>$0 is available but the value is <code>null</code>.
  77. *
  78. * @param statement a Java statement.
  79. */
  80. public void replace(String statement) throws CannotCompileException {
  81. ConstPool constPool = getConstPool();
  82. int pos = currentPos;
  83. int index = iterator.u16bitAt(pos + 1);
  84. Javac jc = new Javac(thisClass);
  85. ClassPool cp = thisClass.getClassPool();
  86. CodeAttribute ca = iterator.get();
  87. try {
  88. CtClass[] params
  89. = new CtClass[] { cp.get(javaLangObject) };
  90. CtClass retType = getType();
  91. int paramVar = ca.getMaxLocals();
  92. jc.recordParams(javaLangObject, params, true, paramVar,
  93. withinStatic());
  94. int retVar = jc.recordReturnType(retType, true);
  95. jc.recordProceed(new ProceedForCast(index, retType));
  96. /* Is $_ included in the source code?
  97. */
  98. checkResultValue(retType, statement);
  99. Bytecode bytecode = jc.getBytecode();
  100. storeStack(params, true, paramVar, bytecode);
  101. jc.compileStmnt(statement);
  102. bytecode.addLoad(retVar, retType);
  103. replace0(pos, bytecode, 3);
  104. }
  105. catch (CompileError e) { throw new CannotCompileException(e); }
  106. catch (NotFoundException e) { throw new CannotCompileException(e); }
  107. catch (BadBytecode e) {
  108. throw new CannotCompileException("broken method");
  109. }
  110. }
  111. /* <type> $proceed(Object obj)
  112. */
  113. static class ProceedForCast implements ProceedHandler {
  114. int index;
  115. CtClass retType;
  116. ProceedForCast(int i, CtClass t) {
  117. index = i;
  118. retType = t;
  119. }
  120. public void doit(JvstCodeGen gen, Bytecode bytecode, ASTList args)
  121. throws CompileError
  122. {
  123. if (gen.atMethodArgsLength(args) != 1)
  124. throw new CompileError(Javac.proceedName
  125. + "() cannot take more than one parameter "
  126. + "for cast");
  127. gen.atMethodArgs(args, new int[1], new int[1], new String[1]);
  128. bytecode.addOpcode(Opcode.CHECKCAST);
  129. bytecode.addIndex(index);
  130. gen.setType(retType);
  131. }
  132. }
  133. }