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

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