1. /*
  2. * Copyright 2001-2002,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. package org.apache.tools.ant.taskdefs.optional.sitraka.bytecode;
  18. import java.io.DataInputStream;
  19. import java.io.IOException;
  20. import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPool;
  21. import org.apache.tools.ant.taskdefs.optional.sitraka.bytecode.attributes.AttributeInfo;
  22. /**
  23. * Method info structure.
  24. * @todo give a more appropriate name to methods.
  25. *
  26. */
  27. public final class MethodInfo {
  28. private int access_flags;
  29. private int loc = -1;
  30. private String name;
  31. private String descriptor;
  32. public MethodInfo() {
  33. }
  34. public void read(ConstantPool constantPool, DataInputStream dis) throws IOException {
  35. access_flags = dis.readShort();
  36. int name_index = dis.readShort();
  37. name = Utils.getUTF8Value(constantPool, name_index);
  38. int descriptor_index = dis.readShort();
  39. descriptor = Utils.getUTF8Value(constantPool, descriptor_index);
  40. int attributes_count = dis.readUnsignedShort();
  41. for (int i = 0; i < attributes_count; i++) {
  42. int attr_id = dis.readShort();
  43. String attr_name = Utils.getUTF8Value(constantPool, attr_id);
  44. int len = dis.readInt();
  45. if (AttributeInfo.CODE.equals(attr_name)) {
  46. readCode(constantPool, dis);
  47. } else {
  48. dis.skipBytes(len);
  49. }
  50. }
  51. }
  52. protected void readCode(ConstantPool constantPool, DataInputStream dis) throws IOException {
  53. // skip max_stack (short), max_local (short)
  54. dis.skipBytes(2 * 2);
  55. // skip bytecode...
  56. int bytecode_len = dis.readInt();
  57. dis.skip(bytecode_len);
  58. // skip exceptions... 1 exception = 4 short.
  59. int exception_count = dis.readShort();
  60. dis.skipBytes(exception_count * 4 * 2);
  61. // read attributes...
  62. int attributes_count = dis.readUnsignedShort();
  63. for (int i = 0; i < attributes_count; i++) {
  64. int attr_id = dis.readShort();
  65. String attr_name = Utils.getUTF8Value(constantPool, attr_id);
  66. int len = dis.readInt();
  67. if (AttributeInfo.LINE_NUMBER_TABLE.equals(attr_name)) {
  68. // we're only interested in lines of code...
  69. loc = dis.readShort();
  70. // skip the table which is 2*loc*short
  71. dis.skip(loc * 2 * 2);
  72. } else {
  73. dis.skipBytes(len);
  74. }
  75. }
  76. }
  77. public int getAccessFlags() {
  78. return access_flags;
  79. }
  80. public String getName() {
  81. return name;
  82. }
  83. public String getDescriptor() {
  84. return descriptor;
  85. }
  86. public String getFullSignature() {
  87. return getReturnType() + " " + getShortSignature();
  88. }
  89. public String getShortSignature() {
  90. StringBuffer buf = new StringBuffer(getName());
  91. buf.append("(");
  92. String[] params = getParametersType();
  93. for (int i = 0; i < params.length; i++) {
  94. buf.append(params[i]);
  95. if (i != params.length - 1) {
  96. buf.append(", ");
  97. }
  98. }
  99. buf.append(")");
  100. return buf.toString();
  101. }
  102. public String getReturnType() {
  103. return Utils.getMethodReturnType(getDescriptor());
  104. }
  105. public String[] getParametersType() {
  106. return Utils.getMethodParams(getDescriptor());
  107. }
  108. public int getNumberOfLines() {
  109. return loc;
  110. }
  111. public String getAccess() {
  112. return Utils.getMethodAccess(access_flags);
  113. }
  114. public String toString() {
  115. StringBuffer sb = new StringBuffer();
  116. sb.append("Method: ").append(getAccess()).append(" ");
  117. sb.append(getFullSignature());
  118. return sb.toString();
  119. }
  120. }