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

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