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.

ClassFile.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- 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. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode;
  17. import java.io.DataInputStream;
  18. import java.io.DataOutputStream;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.ListIterator;
  23. import java.util.Map;
  24. import javassist.CannotCompileException;
  25. /**
  26. * <code>ClassFile</code> represents a Java <code>.class</code> file, which
  27. * 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. ArrayList attributes;
  41. String thisclassname; // not JVM-internal name
  42. String[] cachedInterfaces;
  43. String cachedSuperclass;
  44. /**
  45. * The major version number of class files
  46. * for JDK 1.1.
  47. */
  48. public static final int JAVA_1 = 45;
  49. /**
  50. * The major version number of class files
  51. * for JDK 1.2.
  52. */
  53. public static final int JAVA_2 = 46;
  54. /**
  55. * The major version number of class files
  56. * for JDK 1.3.
  57. */
  58. public static final int JAVA_3 = 47;
  59. /**
  60. * The major version number of class files
  61. * for JDK 1.4.
  62. */
  63. public static final int JAVA_4 = 48;
  64. /**
  65. * The major version number of class files
  66. * for JDK 1.5.
  67. */
  68. public static final int JAVA_5 = 49;
  69. /**
  70. * The major version number of class files
  71. * for JDK 1.6.
  72. */
  73. public static final int JAVA_6 = 50;
  74. /**
  75. * The major version number of class files
  76. * for JDK 1.7.
  77. */
  78. public static final int JAVA_7 = 51;
  79. /**
  80. * The major version number of class files
  81. * for JDK 1.8.
  82. */
  83. public static final int JAVA_8 = 52;
  84. /**
  85. * The major version number of class files created
  86. * from scratch. The default value is 47 (JDK 1.3).
  87. * It is 49 (JDK 1.5)
  88. * if the JVM supports <code>java.lang.StringBuilder</code>.
  89. * It is 50 (JDK 1.6)
  90. * if the JVM supports <code>java.util.zip.DeflaterInputStream</code>.
  91. * It is 51 (JDK 1.7)
  92. * if the JVM supports <code>java.lang.invoke.CallSite</code>.
  93. */
  94. public static int MAJOR_VERSION = JAVA_3;
  95. static {
  96. try {
  97. Class.forName("java.lang.StringBuilder");
  98. MAJOR_VERSION = JAVA_5;
  99. Class.forName("java.util.zip.DeflaterInputStream");
  100. MAJOR_VERSION = JAVA_6;
  101. Class.forName("java.lang.invoke.CallSite");
  102. MAJOR_VERSION = JAVA_7;
  103. }
  104. catch (Throwable t) {}
  105. }
  106. /**
  107. * Constructs a class file from a byte stream.
  108. */
  109. public ClassFile(DataInputStream in) throws IOException {
  110. read(in);
  111. }
  112. /**
  113. * Constructs a class file including no members.
  114. *
  115. * @param isInterface
  116. * true if this is an interface. false if this is a class.
  117. * @param classname
  118. * a fully-qualified class name
  119. * @param superclass
  120. * a fully-qualified super class name
  121. */
  122. public ClassFile(boolean isInterface, String classname, String superclass) {
  123. major = MAJOR_VERSION;
  124. minor = 0; // JDK 1.3 or later
  125. constPool = new ConstPool(classname);
  126. thisClass = constPool.getThisClassInfo();
  127. if (isInterface)
  128. accessFlags = AccessFlag.INTERFACE | AccessFlag.ABSTRACT;
  129. else
  130. accessFlags = AccessFlag.SUPER;
  131. initSuperclass(superclass);
  132. interfaces = null;
  133. fields = new ArrayList();
  134. methods = new ArrayList();
  135. thisclassname = classname;
  136. attributes = new ArrayList();
  137. attributes.add(new SourceFileAttribute(constPool,
  138. getSourcefileName(thisclassname)));
  139. }
  140. private void initSuperclass(String superclass) {
  141. if (superclass != null) {
  142. this.superClass = constPool.addClassInfo(superclass);
  143. cachedSuperclass = superclass;
  144. }
  145. else {
  146. this.superClass = constPool.addClassInfo("java.lang.Object");
  147. cachedSuperclass = "java.lang.Object";
  148. }
  149. }
  150. private static String getSourcefileName(String qname) {
  151. int index = qname.lastIndexOf('.');
  152. if (index >= 0)
  153. qname = qname.substring(index + 1);
  154. return qname + ".java";
  155. }
  156. /**
  157. * Eliminates dead constant pool items. If a method or a field is removed,
  158. * the constant pool items used by that method/field become dead items. This
  159. * method recreates a constant pool.
  160. */
  161. public void compact() {
  162. ConstPool cp = compact0();
  163. ArrayList list = methods;
  164. int n = list.size();
  165. for (int i = 0; i < n; ++i) {
  166. MethodInfo minfo = (MethodInfo)list.get(i);
  167. minfo.compact(cp);
  168. }
  169. list = fields;
  170. n = list.size();
  171. for (int i = 0; i < n; ++i) {
  172. FieldInfo finfo = (FieldInfo)list.get(i);
  173. finfo.compact(cp);
  174. }
  175. attributes = AttributeInfo.copyAll(attributes, cp);
  176. constPool = cp;
  177. }
  178. private ConstPool compact0() {
  179. ConstPool cp = new ConstPool(thisclassname);
  180. thisClass = cp.getThisClassInfo();
  181. String sc = getSuperclass();
  182. if (sc != null)
  183. superClass = cp.addClassInfo(getSuperclass());
  184. if (interfaces != null) {
  185. int n = interfaces.length;
  186. for (int i = 0; i < n; ++i)
  187. interfaces[i]
  188. = cp.addClassInfo(constPool.getClassInfo(interfaces[i]));
  189. }
  190. return cp;
  191. }
  192. /**
  193. * Discards all attributes, associated with both the class file and the
  194. * members such as a code attribute and exceptions attribute. The unused
  195. * constant pool entries are also discarded (a new packed constant pool is
  196. * constructed).
  197. */
  198. public void prune() {
  199. ConstPool cp = compact0();
  200. ArrayList newAttributes = new ArrayList();
  201. AttributeInfo invisibleAnnotations
  202. = getAttribute(AnnotationsAttribute.invisibleTag);
  203. if (invisibleAnnotations != null) {
  204. invisibleAnnotations = invisibleAnnotations.copy(cp, null);
  205. newAttributes.add(invisibleAnnotations);
  206. }
  207. AttributeInfo visibleAnnotations
  208. = getAttribute(AnnotationsAttribute.visibleTag);
  209. if (visibleAnnotations != null) {
  210. visibleAnnotations = visibleAnnotations.copy(cp, null);
  211. newAttributes.add(visibleAnnotations);
  212. }
  213. AttributeInfo signature
  214. = getAttribute(SignatureAttribute.tag);
  215. if (signature != null) {
  216. signature = signature.copy(cp, null);
  217. newAttributes.add(signature);
  218. }
  219. ArrayList list = methods;
  220. int n = list.size();
  221. for (int i = 0; i < n; ++i) {
  222. MethodInfo minfo = (MethodInfo)list.get(i);
  223. minfo.prune(cp);
  224. }
  225. list = fields;
  226. n = list.size();
  227. for (int i = 0; i < n; ++i) {
  228. FieldInfo finfo = (FieldInfo)list.get(i);
  229. finfo.prune(cp);
  230. }
  231. attributes = newAttributes;
  232. constPool = cp;
  233. }
  234. /**
  235. * Returns a constant pool table.
  236. */
  237. public ConstPool getConstPool() {
  238. return constPool;
  239. }
  240. /**
  241. * Returns true if this is an interface.
  242. */
  243. public boolean isInterface() {
  244. return (accessFlags & AccessFlag.INTERFACE) != 0;
  245. }
  246. /**
  247. * Returns true if this is a final class or interface.
  248. */
  249. public boolean isFinal() {
  250. return (accessFlags & AccessFlag.FINAL) != 0;
  251. }
  252. /**
  253. * Returns true if this is an abstract class or an interface.
  254. */
  255. public boolean isAbstract() {
  256. return (accessFlags & AccessFlag.ABSTRACT) != 0;
  257. }
  258. /**
  259. * Returns access flags.
  260. *
  261. * @see javassist.bytecode.AccessFlag
  262. */
  263. public int getAccessFlags() {
  264. return accessFlags;
  265. }
  266. /**
  267. * Changes access flags.
  268. *
  269. * @see javassist.bytecode.AccessFlag
  270. */
  271. public void setAccessFlags(int acc) {
  272. if ((acc & AccessFlag.INTERFACE) == 0)
  273. acc |= AccessFlag.SUPER;
  274. accessFlags = acc;
  275. }
  276. /**
  277. * Returns access and property flags of this nested class.
  278. * This method returns -1 if the class is not a nested class.
  279. *
  280. * <p>The returned value is obtained from <code>inner_class_access_flags</code>
  281. * of the entry representing this nested class itself
  282. * in <code>InnerClasses_attribute</code>>.
  283. */
  284. public int getInnerAccessFlags() {
  285. InnerClassesAttribute ica
  286. = (InnerClassesAttribute)getAttribute(InnerClassesAttribute.tag);
  287. if (ica == null)
  288. return -1;
  289. String name = getName();
  290. int n = ica.tableLength();
  291. for (int i = 0; i < n; ++i)
  292. if (name.equals(ica.innerClass(i)))
  293. return ica.accessFlags(i);
  294. return -1;
  295. }
  296. /**
  297. * Returns the class name.
  298. */
  299. public String getName() {
  300. return thisclassname;
  301. }
  302. /**
  303. * Sets the class name. This method substitutes the new name for all
  304. * occurrences of the old class name in the class file.
  305. */
  306. public void setName(String name) {
  307. renameClass(thisclassname, name);
  308. }
  309. /**
  310. * Returns the super class name.
  311. */
  312. public String getSuperclass() {
  313. if (cachedSuperclass == null)
  314. cachedSuperclass = constPool.getClassInfo(superClass);
  315. return cachedSuperclass;
  316. }
  317. /**
  318. * Returns the index of the constant pool entry representing the super
  319. * class.
  320. */
  321. public int getSuperclassId() {
  322. return superClass;
  323. }
  324. /**
  325. * Sets the super class.
  326. *
  327. * <p>
  328. * The new super class should inherit from the old super class.
  329. * This method modifies constructors so that they call constructors declared
  330. * in the new super class.
  331. */
  332. public void setSuperclass(String superclass) throws CannotCompileException {
  333. if (superclass == null)
  334. superclass = "java.lang.Object";
  335. try {
  336. this.superClass = constPool.addClassInfo(superclass);
  337. ArrayList list = methods;
  338. int n = list.size();
  339. for (int i = 0; i < n; ++i) {
  340. MethodInfo minfo = (MethodInfo)list.get(i);
  341. minfo.setSuperclass(superclass);
  342. }
  343. }
  344. catch (BadBytecode e) {
  345. throw new CannotCompileException(e);
  346. }
  347. cachedSuperclass = superclass;
  348. }
  349. /**
  350. * Replaces all occurrences of a class name in the class file.
  351. *
  352. * <p>
  353. * If class X is substituted for class Y in the class file, X and Y must
  354. * have the same signature. If Y provides a method m(), X must provide it
  355. * even if X inherits m() from the super class. If this fact is not
  356. * guaranteed, the bytecode verifier may cause an error.
  357. *
  358. * @param oldname
  359. * the replaced class name
  360. * @param newname
  361. * the substituted class name
  362. */
  363. public final void renameClass(String oldname, String newname) {
  364. ArrayList list;
  365. int n;
  366. if (oldname.equals(newname))
  367. return;
  368. if (oldname.equals(thisclassname))
  369. thisclassname = newname;
  370. oldname = Descriptor.toJvmName(oldname);
  371. newname = Descriptor.toJvmName(newname);
  372. constPool.renameClass(oldname, newname);
  373. AttributeInfo.renameClass(attributes, oldname, newname);
  374. list = methods;
  375. n = list.size();
  376. for (int i = 0; i < n; ++i) {
  377. MethodInfo minfo = (MethodInfo)list.get(i);
  378. String desc = minfo.getDescriptor();
  379. minfo.setDescriptor(Descriptor.rename(desc, oldname, newname));
  380. AttributeInfo.renameClass(minfo.getAttributes(), oldname, newname);
  381. }
  382. list = fields;
  383. n = list.size();
  384. for (int i = 0; i < n; ++i) {
  385. FieldInfo finfo = (FieldInfo)list.get(i);
  386. String desc = finfo.getDescriptor();
  387. finfo.setDescriptor(Descriptor.rename(desc, oldname, newname));
  388. AttributeInfo.renameClass(finfo.getAttributes(), oldname, newname);
  389. }
  390. }
  391. /**
  392. * Replaces all occurrences of several class names in the class file.
  393. *
  394. * @param classnames
  395. * specifies which class name is replaced with which new name.
  396. * Class names must be described with the JVM-internal
  397. * representation like <code>java/lang/Object</code>.
  398. * @see #renameClass(String,String)
  399. */
  400. public final void renameClass(Map classnames) {
  401. String jvmNewThisName = (String)classnames.get(Descriptor
  402. .toJvmName(thisclassname));
  403. if (jvmNewThisName != null)
  404. thisclassname = Descriptor.toJavaName(jvmNewThisName);
  405. constPool.renameClass(classnames);
  406. AttributeInfo.renameClass(attributes, classnames);
  407. ArrayList list = methods;
  408. int n = list.size();
  409. for (int i = 0; i < n; ++i) {
  410. MethodInfo minfo = (MethodInfo)list.get(i);
  411. String desc = minfo.getDescriptor();
  412. minfo.setDescriptor(Descriptor.rename(desc, classnames));
  413. AttributeInfo.renameClass(minfo.getAttributes(), classnames);
  414. }
  415. list = fields;
  416. n = list.size();
  417. for (int i = 0; i < n; ++i) {
  418. FieldInfo finfo = (FieldInfo)list.get(i);
  419. String desc = finfo.getDescriptor();
  420. finfo.setDescriptor(Descriptor.rename(desc, classnames));
  421. AttributeInfo.renameClass(finfo.getAttributes(), classnames);
  422. }
  423. }
  424. /**
  425. * Internal-use only.
  426. * <code>CtClass.getRefClasses()</code> calls this method.
  427. */
  428. public final void getRefClasses(Map classnames) {
  429. constPool.renameClass(classnames);
  430. AttributeInfo.getRefClasses(attributes, classnames);
  431. ArrayList list = methods;
  432. int n = list.size();
  433. for (int i = 0; i < n; ++i) {
  434. MethodInfo minfo = (MethodInfo)list.get(i);
  435. String desc = minfo.getDescriptor();
  436. Descriptor.rename(desc, classnames);
  437. AttributeInfo.getRefClasses(minfo.getAttributes(), classnames);
  438. }
  439. list = fields;
  440. n = list.size();
  441. for (int i = 0; i < n; ++i) {
  442. FieldInfo finfo = (FieldInfo)list.get(i);
  443. String desc = finfo.getDescriptor();
  444. Descriptor.rename(desc, classnames);
  445. AttributeInfo.getRefClasses(finfo.getAttributes(), classnames);
  446. }
  447. }
  448. /**
  449. * Returns the names of the interfaces implemented by the class.
  450. * The returned array is read only.
  451. */
  452. public String[] getInterfaces() {
  453. if (cachedInterfaces != null)
  454. return cachedInterfaces;
  455. String[] rtn = null;
  456. if (interfaces == null)
  457. rtn = new String[0];
  458. else {
  459. int n = interfaces.length;
  460. String[] list = new String[n];
  461. for (int i = 0; i < n; ++i)
  462. list[i] = constPool.getClassInfo(interfaces[i]);
  463. rtn = list;
  464. }
  465. cachedInterfaces = rtn;
  466. return rtn;
  467. }
  468. /**
  469. * Sets the interfaces.
  470. *
  471. * @param nameList
  472. * the names of the interfaces.
  473. */
  474. public void setInterfaces(String[] nameList) {
  475. cachedInterfaces = null;
  476. if (nameList != null) {
  477. int n = nameList.length;
  478. interfaces = new int[n];
  479. for (int i = 0; i < n; ++i)
  480. interfaces[i] = constPool.addClassInfo(nameList[i]);
  481. }
  482. }
  483. /**
  484. * Appends an interface to the interfaces implemented by the class.
  485. */
  486. public void addInterface(String name) {
  487. cachedInterfaces = null;
  488. int info = constPool.addClassInfo(name);
  489. if (interfaces == null) {
  490. interfaces = new int[1];
  491. interfaces[0] = info;
  492. }
  493. else {
  494. int n = interfaces.length;
  495. int[] newarray = new int[n + 1];
  496. System.arraycopy(interfaces, 0, newarray, 0, n);
  497. newarray[n] = info;
  498. interfaces = newarray;
  499. }
  500. }
  501. /**
  502. * Returns all the fields declared in the class.
  503. *
  504. * @return a list of <code>FieldInfo</code>.
  505. * @see FieldInfo
  506. */
  507. public List getFields() {
  508. return fields;
  509. }
  510. /**
  511. * Appends a field to the class.
  512. *
  513. * @throws DuplicateMemberException when the field is already included.
  514. */
  515. public void addField(FieldInfo finfo) throws DuplicateMemberException {
  516. testExistingField(finfo.getName(), finfo.getDescriptor());
  517. fields.add(finfo);
  518. }
  519. /**
  520. * Just appends a field to the class.
  521. * It does not check field duplication.
  522. * Use this method only when minimizing performance overheads
  523. * is seriously required.
  524. *
  525. * @since 3.13
  526. */
  527. public final void addField2(FieldInfo finfo) {
  528. fields.add(finfo);
  529. }
  530. private void testExistingField(String name, String descriptor)
  531. throws DuplicateMemberException {
  532. ListIterator it = fields.listIterator(0);
  533. while (it.hasNext()) {
  534. FieldInfo minfo = (FieldInfo)it.next();
  535. if (minfo.getName().equals(name))
  536. throw new DuplicateMemberException("duplicate field: " + name);
  537. }
  538. }
  539. /**
  540. * Returns all the methods declared in the class.
  541. *
  542. * @return a list of <code>MethodInfo</code>.
  543. * @see MethodInfo
  544. */
  545. public List getMethods() {
  546. return methods;
  547. }
  548. /**
  549. * Returns the method with the specified name. If there are multiple methods
  550. * with that name, this method returns one of them.
  551. *
  552. * @return null if no such method is found.
  553. */
  554. public MethodInfo getMethod(String name) {
  555. ArrayList list = methods;
  556. int n = list.size();
  557. for (int i = 0; i < n; ++i) {
  558. MethodInfo minfo = (MethodInfo)list.get(i);
  559. if (minfo.getName().equals(name))
  560. return minfo;
  561. }
  562. return null;
  563. }
  564. /**
  565. * Returns a static initializer (class initializer), or null if it does not
  566. * exist.
  567. */
  568. public MethodInfo getStaticInitializer() {
  569. return getMethod(MethodInfo.nameClinit);
  570. }
  571. /**
  572. * Appends a method to the class.
  573. * If there is a bridge method with the same name and signature,
  574. * then the bridge method is removed before a new method is added.
  575. *
  576. * @throws DuplicateMemberException when the method is already included.
  577. */
  578. public void addMethod(MethodInfo minfo) throws DuplicateMemberException {
  579. testExistingMethod(minfo);
  580. methods.add(minfo);
  581. }
  582. /**
  583. * Just appends a method to the class.
  584. * It does not check method duplication or remove a bridge method.
  585. * Use this method only when minimizing performance overheads
  586. * is seriously required.
  587. *
  588. * @since 3.13
  589. */
  590. public final void addMethod2(MethodInfo minfo) {
  591. methods.add(minfo);
  592. }
  593. private void testExistingMethod(MethodInfo newMinfo)
  594. throws DuplicateMemberException
  595. {
  596. String name = newMinfo.getName();
  597. String descriptor = newMinfo.getDescriptor();
  598. ListIterator it = methods.listIterator(0);
  599. while (it.hasNext())
  600. if (isDuplicated(newMinfo, name, descriptor, (MethodInfo)it.next(), it))
  601. throw new DuplicateMemberException("duplicate method: " + name
  602. + " in " + this.getName());
  603. }
  604. private static boolean isDuplicated(MethodInfo newMethod, String newName,
  605. String newDesc, MethodInfo minfo,
  606. ListIterator it)
  607. {
  608. if (!minfo.getName().equals(newName))
  609. return false;
  610. String desc = minfo.getDescriptor();
  611. if (!Descriptor.eqParamTypes(desc, newDesc))
  612. return false;
  613. if (desc.equals(newDesc)) {
  614. if (notBridgeMethod(minfo))
  615. return true;
  616. else {
  617. // if the bridge method with the same signature
  618. // already exists, replace it.
  619. it.remove();
  620. return false;
  621. }
  622. }
  623. else
  624. return false;
  625. // return notBridgeMethod(minfo) && notBridgeMethod(newMethod);
  626. }
  627. /* For a bridge method, see Sec. 15.12.4.5 of JLS 3rd Ed.
  628. */
  629. private static boolean notBridgeMethod(MethodInfo minfo) {
  630. return (minfo.getAccessFlags() & AccessFlag.BRIDGE) == 0;
  631. }
  632. /**
  633. * Returns all the attributes. The returned <code>List</code> object
  634. * is shared with this object. If you add a new attribute to the list,
  635. * the attribute is also added to the classs file represented by this
  636. * object. If you remove an attribute from the list, it is also removed
  637. * from the class file.
  638. *
  639. * @return a list of <code>AttributeInfo</code> objects.
  640. * @see AttributeInfo
  641. */
  642. public List getAttributes() {
  643. return attributes;
  644. }
  645. /**
  646. * Returns the attribute with the specified name. If there are multiple
  647. * attributes with that name, this method returns either of them. It
  648. * returns null if the specified attributed is not found.
  649. *
  650. * @param name attribute name
  651. * @see #getAttributes()
  652. */
  653. public AttributeInfo getAttribute(String name) {
  654. ArrayList list = attributes;
  655. int n = list.size();
  656. for (int i = 0; i < n; ++i) {
  657. AttributeInfo ai = (AttributeInfo)list.get(i);
  658. if (ai.getName().equals(name))
  659. return ai;
  660. }
  661. return null;
  662. }
  663. /**
  664. * Appends an attribute. If there is already an attribute with the same
  665. * name, the new one substitutes for it.
  666. *
  667. * @see #getAttributes()
  668. */
  669. public void addAttribute(AttributeInfo info) {
  670. AttributeInfo.remove(attributes, info.getName());
  671. attributes.add(info);
  672. }
  673. /**
  674. * Returns the source file containing this class.
  675. *
  676. * @return null if this information is not available.
  677. */
  678. public String getSourceFile() {
  679. SourceFileAttribute sf
  680. = (SourceFileAttribute)getAttribute(SourceFileAttribute.tag);
  681. if (sf == null)
  682. return null;
  683. else
  684. return sf.getFileName();
  685. }
  686. private void read(DataInputStream in) throws IOException {
  687. int i, n;
  688. int magic = in.readInt();
  689. if (magic != 0xCAFEBABE)
  690. throw new IOException("bad magic number: " + Integer.toHexString(magic));
  691. minor = in.readUnsignedShort();
  692. major = in.readUnsignedShort();
  693. constPool = new ConstPool(in);
  694. accessFlags = in.readUnsignedShort();
  695. thisClass = in.readUnsignedShort();
  696. constPool.setThisClassInfo(thisClass);
  697. superClass = in.readUnsignedShort();
  698. n = in.readUnsignedShort();
  699. if (n == 0)
  700. interfaces = null;
  701. else {
  702. interfaces = new int[n];
  703. for (i = 0; i < n; ++i)
  704. interfaces[i] = in.readUnsignedShort();
  705. }
  706. ConstPool cp = constPool;
  707. n = in.readUnsignedShort();
  708. fields = new ArrayList();
  709. for (i = 0; i < n; ++i)
  710. addField2(new FieldInfo(cp, in));
  711. n = in.readUnsignedShort();
  712. methods = new ArrayList();
  713. for (i = 0; i < n; ++i)
  714. addMethod2(new MethodInfo(cp, in));
  715. attributes = new ArrayList();
  716. n = in.readUnsignedShort();
  717. for (i = 0; i < n; ++i)
  718. addAttribute(AttributeInfo.read(cp, in));
  719. thisclassname = constPool.getClassInfo(thisClass);
  720. }
  721. /**
  722. * Writes a class file represented by this object into an output stream.
  723. */
  724. public void write(DataOutputStream out) throws IOException {
  725. int i, n;
  726. out.writeInt(0xCAFEBABE); // magic
  727. out.writeShort(minor); // minor version
  728. out.writeShort(major); // major version
  729. constPool.write(out); // constant pool
  730. out.writeShort(accessFlags);
  731. out.writeShort(thisClass);
  732. out.writeShort(superClass);
  733. if (interfaces == null)
  734. n = 0;
  735. else
  736. n = interfaces.length;
  737. out.writeShort(n);
  738. for (i = 0; i < n; ++i)
  739. out.writeShort(interfaces[i]);
  740. ArrayList list = fields;
  741. n = list.size();
  742. out.writeShort(n);
  743. for (i = 0; i < n; ++i) {
  744. FieldInfo finfo = (FieldInfo)list.get(i);
  745. finfo.write(out);
  746. }
  747. list = methods;
  748. n = list.size();
  749. out.writeShort(n);
  750. for (i = 0; i < n; ++i) {
  751. MethodInfo minfo = (MethodInfo)list.get(i);
  752. minfo.write(out);
  753. }
  754. out.writeShort(attributes.size());
  755. AttributeInfo.writeAll(attributes, out);
  756. }
  757. /**
  758. * Get the Major version.
  759. *
  760. * @return the major version
  761. */
  762. public int getMajorVersion() {
  763. return major;
  764. }
  765. /**
  766. * Set the major version.
  767. *
  768. * @param major
  769. * the major version
  770. */
  771. public void setMajorVersion(int major) {
  772. this.major = major;
  773. }
  774. /**
  775. * Get the minor version.
  776. *
  777. * @return the minor version
  778. */
  779. public int getMinorVersion() {
  780. return minor;
  781. }
  782. /**
  783. * Set the minor version.
  784. *
  785. * @param minor
  786. * the minor version
  787. */
  788. public void setMinorVersion(int minor) {
  789. this.minor = minor;
  790. }
  791. /**
  792. * Sets the major and minor version to Java 5.
  793. *
  794. * If the major version is older than 49, Java 5
  795. * extensions such as annotations are ignored
  796. * by the JVM.
  797. */
  798. public void setVersionToJava5() {
  799. this.major = 49;
  800. this.minor = 0;
  801. }
  802. }