Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ClassFile.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2004 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.bytecode;
  16. import java.io.DataInputStream;
  17. import java.io.DataOutputStream;
  18. import java.io.IOException;
  19. import java.util.Map;
  20. import java.util.LinkedList;
  21. import java.util.ArrayList;
  22. import java.util.ListIterator;
  23. import java.util.List;
  24. import javassist.CannotCompileException;
  25. /**
  26. * <code>ClassFile</code> represents a Java <code>.class</code> file,
  27. * which consists of a constant pool, methods, fields, and attributes.
  28. *
  29. * @see javassist.CtClass#getClassFile()
  30. */
  31. public final class ClassFile {
  32. int major, minor; // version number
  33. ConstPool constPool;
  34. int thisClass;
  35. int accessFlags;
  36. int superClass;
  37. int[] interfaces;
  38. ArrayList fields;
  39. ArrayList methods;
  40. LinkedList attributes;
  41. String thisclassname; // not JVM-internal name
  42. /**
  43. * Constructs a class file from a byte stream.
  44. */
  45. public ClassFile(DataInputStream in) throws IOException {
  46. read(in);
  47. }
  48. /**
  49. * Constructs a class file including no members.
  50. *
  51. * @param isInterface true if this is an interface.
  52. * false if this is a class.
  53. * @param classname a fully-qualified class name
  54. * @param superclass a fully-qualified super class name
  55. */
  56. public ClassFile(boolean isInterface,
  57. String classname, String superclass) {
  58. major = 45;
  59. minor = 3; // JDK 1.1 or later
  60. constPool = new ConstPool(classname);
  61. thisClass = constPool.getThisClassInfo();
  62. if (isInterface)
  63. accessFlags = AccessFlag.SUPER | AccessFlag.INTERFACE
  64. | AccessFlag.ABSTRACT;
  65. else
  66. accessFlags = AccessFlag.SUPER;
  67. initSuperclass(superclass);
  68. interfaces = null;
  69. fields = new ArrayList();
  70. methods = new ArrayList();
  71. thisclassname = classname;
  72. attributes = new LinkedList();
  73. attributes.add(new SourceFileAttribute(constPool,
  74. getSourcefileName(thisclassname)));
  75. }
  76. private void initSuperclass(String superclass) {
  77. if (superclass != null)
  78. superClass = constPool.addClassInfo(superclass);
  79. else
  80. superClass = constPool.addClassInfo("java.lang.Object");
  81. }
  82. private static String getSourcefileName(String qname) {
  83. int index = qname.lastIndexOf('.');
  84. if (index >= 0)
  85. qname = qname.substring(index + 1);
  86. return qname + ".java";
  87. }
  88. /**
  89. * Discards all attributes, associated with both the class file and
  90. * the members such as a code attribute and exceptions attribute.
  91. * The unused constant pool entries are also discarded (a new packed
  92. * constant pool is constructed).
  93. */
  94. public void prune() {
  95. ConstPool cp = new ConstPool(thisclassname);
  96. superClass = cp.addClassInfo(getSuperclass());
  97. if (interfaces != null) {
  98. int n = interfaces.length;
  99. for (int i = 0; i < n; ++i)
  100. interfaces[i]
  101. = cp.addClassInfo(constPool.getClassInfo(interfaces[i]));
  102. }
  103. ArrayList list = methods;
  104. int n = list.size();
  105. for (int i = 0; i < n; ++i) {
  106. MethodInfo minfo = (MethodInfo)list.get(i);
  107. minfo.prune(cp);
  108. }
  109. list = fields;
  110. n = list.size();
  111. for (int i = 0; i < n; ++i) {
  112. FieldInfo finfo = (FieldInfo)list.get(i);
  113. finfo.prune(cp);
  114. }
  115. attributes = new LinkedList();
  116. cp.prune();
  117. constPool = cp;
  118. }
  119. /**
  120. * Returns a constant pool table.
  121. */
  122. public ConstPool getConstPool() {
  123. return constPool;
  124. }
  125. /**
  126. * Returns true if this is an interface.
  127. */
  128. public boolean isInterface() {
  129. return (accessFlags & AccessFlag.INTERFACE) != 0;
  130. }
  131. /**
  132. * Returns true if this is a final class or interface.
  133. */
  134. public boolean isFinal() {
  135. return (accessFlags & AccessFlag.FINAL) != 0;
  136. }
  137. /**
  138. * Returns true if this is an abstract class or an interface.
  139. */
  140. public boolean isAbstract() {
  141. return (accessFlags & AccessFlag.ABSTRACT) != 0;
  142. }
  143. /**
  144. * Returns access flags.
  145. *
  146. * @see javassist.bytecode.AccessFlag
  147. */
  148. public int getAccessFlags() {
  149. return accessFlags;
  150. }
  151. /**
  152. * Changes access flags.
  153. *
  154. * @see javassist.bytecode.AccessFlag
  155. */
  156. public void setAccessFlags(int acc) {
  157. accessFlags = acc | AccessFlag.SUPER;
  158. }
  159. /**
  160. * Returns the class name.
  161. */
  162. public String getName() {
  163. return thisclassname;
  164. }
  165. /**
  166. * Sets the class name. This method substitutes the new name
  167. * for all occurrences of the old class name in the class file.
  168. */
  169. public void setName(String name) {
  170. renameClass(thisclassname, name);
  171. }
  172. /**
  173. * Returns the super class name.
  174. */
  175. public String getSuperclass() {
  176. return constPool.getClassInfo(superClass);
  177. }
  178. /**
  179. * Returns the index of the constant pool entry representing
  180. * the super class.
  181. */
  182. public int getSuperclassId() {
  183. return superClass;
  184. }
  185. /**
  186. * Sets the super class.
  187. *
  188. * <p>This method modifies constructors so that they call
  189. * constructors declared in the new super class.
  190. */
  191. public void setSuperclass(String superclass)
  192. throws CannotCompileException
  193. {
  194. if (superclass == null)
  195. superclass = "java.lang.Object";
  196. try {
  197. superClass = constPool.addClassInfo(superclass);
  198. ArrayList list = methods;
  199. int n = list.size();
  200. for (int i = 0; i < n; ++i) {
  201. MethodInfo minfo = (MethodInfo)list.get(i);
  202. minfo.setSuperclass(superclass);
  203. }
  204. }
  205. catch (BadBytecode e) {
  206. throw new CannotCompileException(e);
  207. }
  208. }
  209. /**
  210. * Replaces all occurrences of a class name in the class file.
  211. *
  212. * <p>If class X is substituted for class Y in the class file,
  213. * X and Y must have the same signature. If Y provides a method
  214. * m(), X must provide it even if X inherits m() from the super class.
  215. * If this fact is not guaranteed, the bytecode verifier may cause
  216. * an error.
  217. *
  218. * @param oldname the replaced class name
  219. * @param newname the substituted class name
  220. */
  221. public final void renameClass(String oldname, String newname) {
  222. ArrayList list;
  223. int n;
  224. if (oldname.equals(newname))
  225. return;
  226. if (oldname.equals(thisclassname))
  227. thisclassname = newname;
  228. oldname = Descriptor.toJvmName(oldname);
  229. newname = Descriptor.toJvmName(newname);
  230. constPool.renameClass(oldname, newname);
  231. list = methods;
  232. n = list.size();
  233. for (int i = 0; i < n; ++i) {
  234. MethodInfo minfo = (MethodInfo)list.get(i);
  235. String desc = minfo.getDescriptor();
  236. minfo.setDescriptor(Descriptor.rename(desc, oldname, newname));
  237. }
  238. list = fields;
  239. n = list.size();
  240. for (int i = 0; i < n; ++i) {
  241. FieldInfo finfo = (FieldInfo)list.get(i);
  242. String desc = finfo.getDescriptor();
  243. finfo.setDescriptor(Descriptor.rename(desc, oldname, newname));
  244. }
  245. }
  246. /**
  247. * Replaces all occurrences of several class names in the class file.
  248. *
  249. * @param classnames specifies which class name is replaced
  250. * with which new name. Class names must
  251. * be described with the JVM-internal
  252. * representation like
  253. * <code>java/lang/Object</code>.
  254. *
  255. * @see #renameClass(String,String)
  256. */
  257. public final void renameClass(Map classnames) {
  258. String jvmNewThisName
  259. = (String)classnames.get(Descriptor.toJvmName(thisclassname));
  260. if (jvmNewThisName != null)
  261. thisclassname = Descriptor.toJavaName(jvmNewThisName);
  262. constPool.renameClass(classnames);
  263. ArrayList list = methods;
  264. int n = list.size();
  265. for (int i = 0; i < n; ++i) {
  266. MethodInfo minfo = (MethodInfo)list.get(i);
  267. String desc = minfo.getDescriptor();
  268. minfo.setDescriptor(Descriptor.rename(desc, classnames));
  269. }
  270. list = fields;
  271. n = list.size();
  272. for (int i = 0; i < n; ++i) {
  273. FieldInfo finfo = (FieldInfo)list.get(i);
  274. String desc = finfo.getDescriptor();
  275. finfo.setDescriptor(Descriptor.rename(desc, classnames));
  276. }
  277. }
  278. /**
  279. * Returns the names of the interfaces implemented by the class.
  280. */
  281. public String[] getInterfaces() {
  282. if (interfaces == null)
  283. return new String[0];
  284. else {
  285. int n = interfaces.length;
  286. String[] list = new String[n];
  287. for (int i = 0; i < n; ++i)
  288. list[i] = constPool.getClassInfo(interfaces[i]);
  289. return list;
  290. }
  291. }
  292. /**
  293. * Sets the interfaces.
  294. *
  295. * @param nameList the names of the interfaces.
  296. */
  297. public void setInterfaces(String[] nameList) {
  298. if (nameList != null) {
  299. int n = nameList.length;
  300. interfaces = new int[n];
  301. for (int i = 0; i < n; ++i)
  302. interfaces[i] = constPool.addClassInfo(nameList[i]);
  303. }
  304. }
  305. /**
  306. * Appends an interface to the
  307. * interfaces implemented by the class.
  308. */
  309. public void addInterface(String name) {
  310. int info = constPool.addClassInfo(name);
  311. if (interfaces == null) {
  312. interfaces = new int[1];
  313. interfaces[0] = info;
  314. }
  315. else {
  316. int n = interfaces.length;
  317. int[] newarray = new int[n + 1];
  318. System.arraycopy(interfaces, 0, newarray, 0, n);
  319. newarray[n] = info;
  320. interfaces = newarray;
  321. }
  322. }
  323. /**
  324. * Returns all the fields declared in the class.
  325. *
  326. * @return a list of <code>FieldInfo</code>.
  327. * @see FieldInfo
  328. */
  329. public List getFields() {
  330. return fields;
  331. }
  332. /**
  333. * Appends a field to the class.
  334. */
  335. public void addField(FieldInfo finfo) throws CannotCompileException {
  336. testExistingField(finfo.getName(), finfo.getDescriptor());
  337. fields.add(finfo);
  338. }
  339. private void addField0(FieldInfo finfo) {
  340. fields.add(finfo);
  341. }
  342. private void testExistingField(String name, String descriptor)
  343. throws CannotCompileException
  344. {
  345. ListIterator it = fields.listIterator(0);
  346. while (it.hasNext()) {
  347. FieldInfo minfo = (FieldInfo)it.next();
  348. if (minfo.getName().equals(name))
  349. throw new CannotCompileException("duplicate field: " + name);
  350. }
  351. }
  352. /**
  353. * Returns all the methods declared in the class.
  354. *
  355. * @return a list of <code>MethodInfo</code>.
  356. * @see MethodInfo
  357. */
  358. public List getMethods() {
  359. return methods;
  360. }
  361. /**
  362. * Returns the method with the specified name. If there are multiple
  363. * methods with that name, this method returns one of them.
  364. *
  365. * @return null if no such a method is found.
  366. */
  367. public MethodInfo getMethod(String name) {
  368. ArrayList list = methods;
  369. int n = list.size();
  370. for (int i = 0; i < n; ++i) {
  371. MethodInfo minfo = (MethodInfo)list.get(i);
  372. if (minfo.getName().equals(name))
  373. return minfo;
  374. }
  375. return null;
  376. }
  377. /**
  378. * Returns a static initializer (class initializer), or null if
  379. * it does not exist.
  380. */
  381. public MethodInfo getStaticInitializer() {
  382. return getMethod(MethodInfo.nameClinit);
  383. }
  384. /**
  385. * Appends a method to the class.
  386. */
  387. public void addMethod(MethodInfo minfo) throws CannotCompileException {
  388. testExistingMethod(minfo.getName(), minfo.getDescriptor());
  389. methods.add(minfo);
  390. }
  391. private void addMethod0(MethodInfo minfo) {
  392. methods.add(minfo);
  393. }
  394. private void testExistingMethod(String name, String descriptor)
  395. throws CannotCompileException
  396. {
  397. ListIterator it = methods.listIterator(0);
  398. while (it.hasNext()) {
  399. MethodInfo minfo = (MethodInfo)it.next();
  400. if (minfo.getName().equals(name)
  401. && Descriptor.eqParamTypes(minfo.getDescriptor(), descriptor))
  402. throw new CannotCompileException("duplicate method: " + name);
  403. }
  404. }
  405. /**
  406. * Returns all the attributes.
  407. *
  408. * @return a list of <code>AttributeInfo</code> objects.
  409. * @see AttributeInfo
  410. */
  411. public List getAttributes() {
  412. return attributes;
  413. }
  414. /**
  415. * Returns the attribute with the specified name.
  416. *
  417. * @param name attribute name
  418. */
  419. public AttributeInfo getAttribute(String name) {
  420. LinkedList list = attributes;
  421. int n = list.size();
  422. for (int i = 0; i < n; ++i) {
  423. AttributeInfo ai = (AttributeInfo)list.get(i);
  424. if (ai.getName().equals(name))
  425. return ai;
  426. }
  427. return null;
  428. }
  429. /**
  430. * Appends an attribute. If there is already an attribute with
  431. * the same name, the new one substitutes for it.
  432. */
  433. public void addAttribute(AttributeInfo info) {
  434. AttributeInfo.remove(attributes, info.getName());
  435. attributes.add(info);
  436. }
  437. /**
  438. * Returns the source file containing this class.
  439. *
  440. * @return null if this information is not available.
  441. */
  442. public String getSourceFile() {
  443. SourceFileAttribute sf
  444. = (SourceFileAttribute)getAttribute(SourceFileAttribute.tag);
  445. if (sf == null)
  446. return null;
  447. else
  448. return sf.getFileName();
  449. }
  450. private void read(DataInputStream in) throws IOException {
  451. int i, n;
  452. int magic = in.readInt();
  453. if (magic != 0xCAFEBABE)
  454. throw new IOException("non class file");
  455. minor = in.readUnsignedShort();
  456. major = in.readUnsignedShort();
  457. constPool = new ConstPool(in);
  458. accessFlags = in.readUnsignedShort();
  459. thisClass = in.readUnsignedShort();
  460. constPool.setThisClassInfo(thisClass);
  461. superClass = in.readUnsignedShort();
  462. n = in.readUnsignedShort();
  463. if (n == 0)
  464. interfaces = null;
  465. else {
  466. interfaces = new int[n];
  467. for (i = 0; i < n; ++i)
  468. interfaces[i] = in.readUnsignedShort();
  469. }
  470. ConstPool cp = constPool;
  471. n = in.readUnsignedShort();
  472. fields = new ArrayList();
  473. for (i = 0; i < n; ++i)
  474. addField0(new FieldInfo(cp, in));
  475. n = in.readUnsignedShort();
  476. methods = new ArrayList();
  477. for (i = 0; i < n; ++i)
  478. addMethod0(new MethodInfo(cp, in));
  479. attributes = new LinkedList();
  480. n = in.readUnsignedShort();
  481. for (i = 0; i < n; ++i)
  482. addAttribute(AttributeInfo.read(cp, in));
  483. thisclassname = constPool.getClassInfo(thisClass);
  484. }
  485. /**
  486. * Writes a class file represened by this object
  487. * into an output stream.
  488. */
  489. public void write(DataOutputStream out) throws IOException {
  490. int i, n;
  491. out.writeInt(0xCAFEBABE); // magic
  492. out.writeShort(minor); // minor version
  493. out.writeShort(major); // major version
  494. constPool.write(out); // constant pool
  495. out.writeShort(accessFlags);
  496. out.writeShort(thisClass);
  497. out.writeShort(superClass);
  498. if (interfaces == null)
  499. n = 0;
  500. else
  501. n = interfaces.length;
  502. out.writeShort(n);
  503. for (i = 0; i < n; ++i)
  504. out.writeShort(interfaces[i]);
  505. ArrayList list = fields;
  506. n = list.size();
  507. out.writeShort(n);
  508. for (i = 0; i < n; ++i) {
  509. FieldInfo finfo = (FieldInfo)list.get(i);
  510. finfo.write(out);
  511. }
  512. list = methods;
  513. n = list.size();
  514. out.writeShort(n);
  515. for (i = 0; i < n; ++i) {
  516. MethodInfo minfo = (MethodInfo)list.get(i);
  517. minfo.write(out);
  518. }
  519. out.writeShort(attributes.size());
  520. AttributeInfo.writeAll(attributes, out);
  521. }
  522. }