1. /*
  2. * Copyright 2000,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.jlink;
  18. import java.io.DataInput;
  19. import java.io.DataInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. /**
  23. * Reads just enough of a class file to determine the class' full name.
  24. *
  25. * <p>Extremely minimal constant pool implementation, mainly to support extracting
  26. * strings from a class file.
  27. */
  28. class ConstantPool {
  29. static final
  30. byte UTF8 = 1, UNUSED = 2, INTEGER = 3, FLOAT = 4, LONG = 5, DOUBLE = 6,
  31. CLASS = 7, STRING = 8, FIELDREF = 9, METHODREF = 10,
  32. INTERFACEMETHODREF = 11, NAMEANDTYPE = 12;
  33. byte[] types;
  34. Object[] values;
  35. ConstantPool(DataInput data) throws IOException {
  36. super();
  37. int count = data.readUnsignedShort();
  38. types = new byte [ count ];
  39. values = new Object [ count ];
  40. // read in all constant pool entries.
  41. for (int i = 1; i < count; i++) {
  42. byte type = data.readByte();
  43. types[i] = type;
  44. switch (type) {
  45. case UTF8 :
  46. values[i] = data.readUTF();
  47. break;
  48. case UNUSED :
  49. break;
  50. case INTEGER :
  51. values[i] = new Integer(data.readInt());
  52. break;
  53. case FLOAT :
  54. values[i] = new Float(data.readFloat());
  55. break;
  56. case LONG :
  57. values[i] = new Long(data.readLong());
  58. ++i;
  59. break;
  60. case DOUBLE :
  61. values[i] = new Double(data.readDouble());
  62. ++i;
  63. break;
  64. case CLASS :
  65. case STRING :
  66. values[i] = new Integer(data.readUnsignedShort());
  67. break;
  68. case FIELDREF :
  69. case METHODREF :
  70. case INTERFACEMETHODREF :
  71. case NAMEANDTYPE :
  72. values[i] = new Integer(data.readInt());
  73. break;
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. * Provides a quick and dirty way to determine the true name of a class
  80. * given just an InputStream. Reads in just enough to perform this
  81. * minimal task only.
  82. */
  83. public class ClassNameReader extends Object {
  84. public static String getClassName(InputStream input) throws IOException {
  85. DataInputStream data = new DataInputStream(input);
  86. // verify this is a valid class file.
  87. int cookie = data.readInt();
  88. if (cookie != 0xCAFEBABE) {
  89. return null;
  90. }
  91. /* int version = */ data.readInt();
  92. // read the constant pool.
  93. ConstantPool constants = new ConstantPool(data);
  94. Object[] values = constants.values;
  95. // read access flags and class index.
  96. /* int accessFlags = */ data.readUnsignedShort();
  97. int classIndex = data.readUnsignedShort();
  98. Integer stringIndex = (Integer) values[classIndex];
  99. String className = (String) values[stringIndex.intValue()];
  100. return className;
  101. }
  102. }