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 27KB

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