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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2007 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. protected 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 thisClass.getClassPool().getCtClass(name);
  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 except try-catch.
  79. */
  80. public void replace(String statement) throws CannotCompileException {
  81. thisClass.getClassFile(); // to call checkModify().
  82. ConstPool constPool = getConstPool();
  83. int pos = currentPos;
  84. int index = iterator.u16bitAt(pos + 1);
  85. Javac jc = new Javac(thisClass);
  86. ClassPool cp = thisClass.getClassPool();
  87. CodeAttribute ca = iterator.get();
  88. try {
  89. CtClass[] params
  90. = new CtClass[] { cp.get(javaLangObject) };
  91. CtClass retType = getType();
  92. int paramVar = ca.getMaxLocals();
  93. jc.recordParams(javaLangObject, params, true, paramVar,
  94. withinStatic());
  95. int retVar = jc.recordReturnType(retType, true);
  96. jc.recordProceed(new ProceedForCast(index, retType));
  97. /* Is $_ included in the source code?
  98. */
  99. checkResultValue(retType, statement);
  100. Bytecode bytecode = jc.getBytecode();
  101. storeStack(params, true, paramVar, bytecode);
  102. jc.recordLocalVariables(ca, pos);
  103. bytecode.addConstZero(retType);
  104. bytecode.addStore(retVar, retType); // initialize $_
  105. jc.compileStmnt(statement);
  106. bytecode.addLoad(retVar, retType);
  107. replace0(pos, bytecode, 3);
  108. }
  109. catch (CompileError e) { throw new CannotCompileException(e); }
  110. catch (NotFoundException e) { throw new CannotCompileException(e); }
  111. catch (BadBytecode e) {
  112. throw new CannotCompileException("broken method");
  113. }
  114. }
  115. /* <type> $proceed(Object obj)
  116. */
  117. static class ProceedForCast implements ProceedHandler {
  118. int index;
  119. CtClass retType;
  120. ProceedForCast(int i, CtClass t) {
  121. index = i;
  122. retType = t;
  123. }
  124. public void doit(JvstCodeGen gen, Bytecode bytecode, ASTList args)
  125. throws CompileError
  126. {
  127. if (gen.getMethodArgsLength(args) != 1)
  128. throw new CompileError(Javac.proceedName
  129. + "() cannot take more than one parameter "
  130. + "for cast");
  131. gen.atMethodArgs(args, new int[1], new int[1], new String[1]);
  132. bytecode.addOpcode(Opcode.CHECKCAST);
  133. bytecode.addIndex(index);
  134. gen.setType(retType);
  135. }
  136. public void setReturnType(JvstTypeChecker c, ASTList args)
  137. throws CompileError
  138. {
  139. c.atMethodArgs(args, new int[1], new int[1], new String[1]);
  140. c.setType(retType);
  141. }
  142. }
  143. }