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.

AttributeUtils.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package org.aspectj.apache.bcel.classfile;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import org.aspectj.apache.bcel.Constants;
  6. public class AttributeUtils {
  7. public static Attribute[] readAttributes(DataInputStream dataInputstream, ConstantPool cpool) {
  8. try {
  9. int length = dataInputstream.readUnsignedShort();
  10. if (length == 0) {
  11. return Attribute.NoAttributes;
  12. }
  13. Attribute[] attrs = new Attribute[length];
  14. for (int i = 0; i < length; i++) {
  15. attrs[i] = Attribute.readAttribute(dataInputstream, cpool);
  16. }
  17. return attrs;
  18. } catch (IOException e) {
  19. throw new ClassFormatException("IOException whilst reading set of attributes: " + e.toString());
  20. }
  21. }
  22. /** Write (serialize) a set of attributes into a specified output stream */
  23. public static void writeAttributes(Attribute[] attributes, DataOutputStream file) throws IOException {
  24. if (attributes == null) {
  25. file.writeShort(0);
  26. } else {
  27. file.writeShort(attributes.length);
  28. for (Attribute attribute : attributes) {
  29. attribute.dump(file);
  30. }
  31. }
  32. }
  33. public static Signature getSignatureAttribute(Attribute[] attributes) {
  34. for (Attribute attribute : attributes) {
  35. if (attribute.tag == Constants.ATTR_SIGNATURE) {
  36. return (Signature) attribute;
  37. }
  38. }
  39. return null;
  40. }
  41. public static Code getCodeAttribute(Attribute[] attributes) {
  42. for (Attribute attribute : attributes) {
  43. if (attribute.tag == Constants.ATTR_CODE) {
  44. return (Code) attribute;
  45. }
  46. }
  47. return null;
  48. }
  49. public static ExceptionTable getExceptionTableAttribute(Attribute[] attributes) {
  50. for (Attribute attribute : attributes) {
  51. if (attribute.tag == Constants.ATTR_EXCEPTIONS) {
  52. return (ExceptionTable) attribute;
  53. }
  54. }
  55. return null;
  56. }
  57. public static ConstantValue getConstantValueAttribute(Attribute[] attributes) {
  58. for (Attribute attribute : attributes) {
  59. if (attribute.getTag() == Constants.ATTR_CONSTANT_VALUE) {
  60. return (ConstantValue) attribute;
  61. }
  62. }
  63. return null;
  64. }
  65. public static void accept(Attribute[] attributes, ClassVisitor visitor) {
  66. for (Attribute attribute : attributes) {
  67. attribute.accept(visitor);
  68. }
  69. }
  70. public static boolean hasSyntheticAttribute(Attribute[] attributes) {
  71. for (Attribute attribute : attributes) {
  72. if (attribute.tag == Constants.ATTR_SYNTHETIC) {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. public static SourceFile getSourceFileAttribute(Attribute[] attributes) {
  79. for (Attribute attribute : attributes) {
  80. if (attribute.tag == Constants.ATTR_SOURCE_FILE) {
  81. return (SourceFile) attribute;
  82. }
  83. }
  84. return null;
  85. }
  86. }