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.

MethodParameters.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* *******************************************************************
  2. * Copyright (c) 2013 VMware
  3. *
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v 2.0
  7. * which accompanies this distribution and is available at
  8. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  9. *
  10. * Contributors:
  11. * Andy Clement initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.apache.bcel.classfile;
  14. import java.io.ByteArrayInputStream;
  15. import java.io.DataInputStream;
  16. import java.io.DataOutputStream;
  17. import java.io.IOException;
  18. import org.aspectj.apache.bcel.Constants;
  19. // see https://cr.openjdk.java.net/~abuckley/8misc.pdf
  20. public class MethodParameters extends Attribute {
  21. public final static int[] NO_PARAMETER_NAME_INDEXES = new int[0];
  22. public final static int[] NO_PARAMETER_ACCESS_FLAGS = new int[0];
  23. public final static int ACCESS_FLAGS_FINAL = 0x0010;
  24. public final static int ACCESS_FLAGS_SYNTHETIC = 0x1000;
  25. public final static int ACCESS_FLAGS_MANDATED = 0x8000;
  26. // if 'isInPackedState' then this data needs unpacking
  27. private boolean isInPackedState = false;
  28. private byte[] data;
  29. private int[] names;
  30. private int[] accessFlags;
  31. public MethodParameters(int index, int length, DataInputStream dis, ConstantPool cpool) throws IOException {
  32. super(Constants.ATTR_METHOD_PARAMETERS,index,length,cpool);
  33. data = new byte[length];
  34. dis.readFully(data,0,length);
  35. isInPackedState = true;
  36. }
  37. private void ensureInflated() {
  38. if (names!=null) return;
  39. try {
  40. DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
  41. int parametersCount = dis.readUnsignedByte();
  42. if (parametersCount == 0) {
  43. names = NO_PARAMETER_NAME_INDEXES;
  44. accessFlags = NO_PARAMETER_ACCESS_FLAGS;
  45. } else {
  46. names = new int[parametersCount];
  47. accessFlags = new int[parametersCount];
  48. for (int i=0;i<parametersCount;i++) {
  49. names[i] = dis.readUnsignedShort();
  50. accessFlags[i] = dis.readUnsignedShort();
  51. }
  52. }
  53. isInPackedState = false;
  54. } catch (IOException ioe) {
  55. throw new RuntimeException("Unabled to inflate type annotation data, badly formed?");
  56. }
  57. }
  58. public void dump(DataOutputStream dos) throws IOException {
  59. super.dump(dos);
  60. if (isInPackedState) {
  61. dos.write(data);
  62. } else {
  63. dos.writeByte(names.length);
  64. for (int i=0;i<names.length;i++) {
  65. dos.writeShort(names[i]);
  66. dos.writeShort(accessFlags[i]);
  67. }
  68. }
  69. }
  70. public int getParametersCount() {
  71. ensureInflated();
  72. return names.length;
  73. }
  74. public String getParameterName(int parameter) {
  75. ensureInflated();
  76. ConstantUtf8 c = (ConstantUtf8) cpool.getConstant(names[parameter], Constants.CONSTANT_Utf8);
  77. return c.getValue();
  78. }
  79. public int getAccessFlags(int parameter) {
  80. ensureInflated();
  81. return accessFlags[parameter];
  82. }
  83. public boolean isFinal(int parameter) {
  84. return (getAccessFlags(parameter) & ACCESS_FLAGS_FINAL)!=0;
  85. }
  86. public boolean isSynthetic(int parameter) {
  87. return (getAccessFlags(parameter) & ACCESS_FLAGS_SYNTHETIC)!=0;
  88. }
  89. public boolean isMandated(int parameter) {
  90. return (getAccessFlags(parameter) & ACCESS_FLAGS_MANDATED)!=0;
  91. }
  92. @Override
  93. public void accept(ClassVisitor v) {
  94. v.visitMethodParameters(this);
  95. }
  96. }