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.

EnclosingMethod.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation
  3. *
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Andy Clement initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.apache.bcel.classfile;
  14. import java.io.DataInputStream;
  15. import java.io.DataOutputStream;
  16. import java.io.IOException;
  17. import org.aspectj.apache.bcel.Constants;
  18. /**
  19. * This attribute exists for local or
  20. * anonymous classes and ... there can be only one.
  21. */
  22. public class EnclosingMethod extends Attribute {
  23. // Pointer to the CONSTANT_Class_info structure representing the
  24. // innermost class that encloses the declaration of the current class.
  25. private int classIndex;
  26. // If the current class is not immediately enclosed by a method or
  27. // constructor, then the value of the method_index item must be zero.
  28. // Otherwise, the value of the method_index item must point to a
  29. // CONSTANT_NameAndType_info structure representing the name and the
  30. // type of a method in the class referenced by the class we point
  31. // to in the class_index. *It is the compiler responsibility* to
  32. // ensure that the method identified by this index is the closest
  33. // lexically enclosing method that includes the local/anonymous class.
  34. private int methodIndex;
  35. // Ctors - and code to read an attribute in.
  36. public EnclosingMethod(int nameIndex, int len, DataInputStream dis, ConstantPool cpool) throws IOException {
  37. this(nameIndex, len, dis.readUnsignedShort(), dis.readUnsignedShort(), cpool);
  38. }
  39. private EnclosingMethod(int nameIndex, int len, int classIdx,int methodIdx, ConstantPool cpool) {
  40. super(Constants.ATTR_ENCLOSING_METHOD, nameIndex, len, cpool);
  41. classIndex = classIdx;
  42. methodIndex = methodIdx;
  43. }
  44. public void accept(ClassVisitor v) {
  45. v.visitEnclosingMethod(this);
  46. }
  47. public Attribute copy(ConstantPool constant_pool) {
  48. throw new RuntimeException("Not implemented yet!");
  49. // is this next line sufficient?
  50. // return (EnclosingMethod)clone();
  51. }
  52. // Accessors
  53. public final int getEnclosingClassIndex() { return classIndex; }
  54. public final int getEnclosingMethodIndex(){ return methodIndex;}
  55. public final void setEnclosingClassIndex(int idx) {classIndex = idx;}
  56. public final void setEnclosingMethodIndex(int idx){methodIndex= idx;}
  57. public final ConstantClass getEnclosingClass() {
  58. ConstantClass c =
  59. (ConstantClass)cpool.getConstant(classIndex,Constants.CONSTANT_Class);
  60. return c;
  61. }
  62. public final ConstantNameAndType getEnclosingMethod() {
  63. if (methodIndex == 0) return null;
  64. ConstantNameAndType nat =
  65. (ConstantNameAndType)cpool.getConstant(methodIndex,Constants.CONSTANT_NameAndType);
  66. return nat;
  67. }
  68. public final void dump(DataOutputStream file) throws IOException {
  69. super.dump(file);
  70. file.writeShort(classIndex);
  71. file.writeShort(methodIndex);
  72. }
  73. }