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

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