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.

CtClassType.java 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2005 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;
  16. import java.io.BufferedInputStream;
  17. import java.io.DataInputStream;
  18. import java.io.DataOutputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.net.URL;
  22. import java.util.ArrayList;
  23. import java.util.Enumeration;
  24. import java.util.HashMap;
  25. import java.util.Hashtable;
  26. import java.util.List;
  27. import javassist.bytecode.AccessFlag;
  28. import javassist.bytecode.AttributeInfo;
  29. import javassist.bytecode.AnnotationsAttribute;
  30. import javassist.bytecode.BadBytecode;
  31. import javassist.bytecode.Bytecode;
  32. import javassist.bytecode.ClassFile;
  33. import javassist.bytecode.CodeAttribute;
  34. import javassist.bytecode.ConstantAttribute;
  35. import javassist.bytecode.CodeIterator;
  36. import javassist.bytecode.ConstPool;
  37. import javassist.bytecode.Descriptor;
  38. import javassist.bytecode.EnclosingMethodAttribute;
  39. import javassist.bytecode.FieldInfo;
  40. import javassist.bytecode.InnerClassesAttribute;
  41. import javassist.bytecode.MethodInfo;
  42. import javassist.bytecode.annotation.Annotation;
  43. import javassist.compiler.AccessorMaker;
  44. import javassist.compiler.CompileError;
  45. import javassist.compiler.Javac;
  46. import javassist.expr.ExprEditor;
  47. /**
  48. * Class types.
  49. */
  50. class CtClassType extends CtClass {
  51. ClassPool classPool;
  52. boolean wasChanged;
  53. private boolean wasFrozen;
  54. boolean wasPruned;
  55. boolean memberRemoved;
  56. ClassFile classfile;
  57. private CtMember fieldsCache;
  58. private CtMember methodsCache;
  59. private CtMember constructorsCache;
  60. private CtConstructor classInitializerCache;
  61. private AccessorMaker accessors;
  62. private FieldInitLink fieldInitializers;
  63. private Hashtable hiddenMethods; // must be synchronous
  64. private int uniqueNumberSeed;
  65. private boolean doPruning = ClassPool.doPruning;
  66. int getCounter;
  67. private static int readCounter = 0;
  68. private static final int READ_THRESHOLD = 100; // see getClassFile2()
  69. CtClassType(String name, ClassPool cp) {
  70. super(name);
  71. classPool = cp;
  72. wasChanged = wasFrozen = wasPruned = memberRemoved = false;
  73. classfile = null;
  74. accessors = null;
  75. fieldInitializers = null;
  76. hiddenMethods = null;
  77. uniqueNumberSeed = 0;
  78. eraseCache();
  79. getCounter = 0;
  80. }
  81. CtClassType(InputStream ins, ClassPool cp) throws IOException {
  82. this((String)null, cp);
  83. classfile = new ClassFile(new DataInputStream(ins));
  84. qualifiedName = classfile.getName();
  85. }
  86. protected void extendToString(StringBuffer buffer) {
  87. if (wasChanged)
  88. buffer.append("changed ");
  89. if (wasFrozen)
  90. buffer.append("frozen ");
  91. if (wasPruned)
  92. buffer.append("pruned ");
  93. buffer.append(Modifier.toString(getModifiers()));
  94. buffer.append(" class ");
  95. buffer.append(getName());
  96. try {
  97. CtClass ext = getSuperclass();
  98. if (ext != null) {
  99. String name = ext.getName();
  100. if (!name.equals("java.lang.Object"))
  101. buffer.append(" extends " + ext.getName());
  102. }
  103. }
  104. catch (NotFoundException e) {
  105. buffer.append(" extends ??");
  106. }
  107. try {
  108. CtClass[] intf = getInterfaces();
  109. if (intf.length > 0)
  110. buffer.append(" implements ");
  111. for (int i = 0; i < intf.length; ++i) {
  112. buffer.append(intf[i].getName());
  113. buffer.append(", ");
  114. }
  115. }
  116. catch (NotFoundException e) {
  117. buffer.append(" extends ??");
  118. }
  119. CtMember field = getFieldsCache();
  120. buffer.append(" fields=");
  121. while (field != null) {
  122. buffer.append(field);
  123. buffer.append(", ");
  124. field = field.next;
  125. }
  126. CtMember c = getConstructorsCache();
  127. buffer.append(" constructors=");
  128. while (c != null) {
  129. buffer.append(c);
  130. buffer.append(", ");
  131. c = c.next;
  132. }
  133. CtMember m = getMethodsCache();
  134. buffer.append(" methods=");
  135. while (m != null) {
  136. buffer.append(m);
  137. buffer.append(", ");
  138. m = m.next;
  139. }
  140. }
  141. protected void eraseCache() {
  142. fieldsCache = null;
  143. constructorsCache = null;
  144. classInitializerCache = null;
  145. methodsCache = null;
  146. }
  147. public AccessorMaker getAccessorMaker() {
  148. if (accessors == null)
  149. accessors = new AccessorMaker(this);
  150. return accessors;
  151. }
  152. public ClassFile getClassFile2() {
  153. if (classfile != null)
  154. return classfile;
  155. if (readCounter++ > READ_THRESHOLD
  156. && ClassPool.releaseUnmodifiedClassFile) {
  157. releaseClassFiles();
  158. readCounter = 0;
  159. }
  160. InputStream fin = null;
  161. try {
  162. fin = classPool.openClassfile(getName());
  163. if (fin == null)
  164. throw new NotFoundException(getName());
  165. fin = new BufferedInputStream(fin);
  166. classfile = new ClassFile(new DataInputStream(fin));
  167. if (!classfile.getName().equals(qualifiedName))
  168. throw new RuntimeException(classfile.getName() + " in "
  169. + qualifiedName.replace('.', '/') + ".java");
  170. return classfile;
  171. }
  172. catch (NotFoundException e) {
  173. throw new RuntimeException(e.toString());
  174. }
  175. catch (IOException e) {
  176. throw new RuntimeException(e.toString());
  177. }
  178. finally {
  179. if (fin != null)
  180. try {
  181. fin.close();
  182. }
  183. catch (IOException e) {}
  184. }
  185. }
  186. /* Inherited from CtClass. Called by get() in ClassPool.
  187. *
  188. * @see javassist.CtClass#incGetCounter()
  189. */
  190. void incGetCounter() { ++getCounter; }
  191. /**
  192. * Releases the class files and cached CtBehaviors
  193. * of the CtClasses that have not been recently used
  194. * if they are unmodified.
  195. */
  196. private void releaseClassFiles() {
  197. Enumeration e = classPool.classes.elements();
  198. while (e.hasMoreElements()) {
  199. Object obj = e.nextElement();
  200. if (obj instanceof CtClassType) {
  201. CtClassType cct = (CtClassType)obj;
  202. if (cct.getCounter < 2 && !cct.isModified()) {
  203. cct.eraseCache();
  204. cct.classfile = null;
  205. }
  206. cct.getCounter = 0;
  207. }
  208. }
  209. }
  210. public ClassPool getClassPool() { return classPool; }
  211. void setClassPool(ClassPool cp) { classPool = cp; }
  212. public URL getURL() throws NotFoundException {
  213. URL url = classPool.find(getName());
  214. if (url == null)
  215. throw new NotFoundException(getName());
  216. else
  217. return url;
  218. }
  219. public boolean isModified() { return wasChanged; }
  220. public boolean isFrozen() { return wasFrozen; }
  221. void freeze() { wasFrozen = true; }
  222. void checkModify() throws RuntimeException {
  223. if (isFrozen()) {
  224. String msg = getName() + " class is frozen";
  225. if (wasPruned)
  226. msg += " and pruned";
  227. throw new RuntimeException(msg);
  228. }
  229. wasChanged = true;
  230. }
  231. public void defrost() {
  232. checkPruned("defrost");
  233. wasFrozen = false;
  234. }
  235. public boolean subtypeOf(CtClass clazz) throws NotFoundException {
  236. int i;
  237. String cname = clazz.getName();
  238. if (this == clazz || getName().equals(cname))
  239. return true;
  240. ClassFile file = getClassFile2();
  241. String supername = file.getSuperclass();
  242. if (supername != null && supername.equals(cname))
  243. return true;
  244. String[] ifs = file.getInterfaces();
  245. int num = ifs.length;
  246. for (i = 0; i < num; ++i)
  247. if (ifs[i].equals(cname))
  248. return true;
  249. if (supername != null && classPool.get(supername).subtypeOf(clazz))
  250. return true;
  251. for (i = 0; i < num; ++i)
  252. if (classPool.get(ifs[i]).subtypeOf(clazz))
  253. return true;
  254. return false;
  255. }
  256. public void setName(String name) throws RuntimeException {
  257. String oldname = getName();
  258. if (name.equals(oldname))
  259. return;
  260. // check this in advance although classNameChanged() below does.
  261. classPool.checkNotFrozen(name);
  262. ClassFile cf = getClassFile2();
  263. super.setName(name);
  264. cf.setName(name);
  265. eraseCache();
  266. classPool.classNameChanged(oldname, this);
  267. }
  268. public void replaceClassName(ClassMap classnames)
  269. throws RuntimeException
  270. {
  271. String oldClassName = getName();
  272. String newClassName
  273. = (String)classnames.get(Descriptor.toJvmName(oldClassName));
  274. if (newClassName != null) {
  275. newClassName = Descriptor.toJavaName(newClassName);
  276. // check this in advance although classNameChanged() below does.
  277. classPool.checkNotFrozen(newClassName);
  278. }
  279. super.replaceClassName(classnames);
  280. ClassFile cf = getClassFile2();
  281. cf.renameClass(classnames);
  282. eraseCache();
  283. if (newClassName != null) {
  284. super.setName(newClassName);
  285. classPool.classNameChanged(oldClassName, this);
  286. }
  287. }
  288. public void replaceClassName(String oldname, String newname)
  289. throws RuntimeException
  290. {
  291. String thisname = getName();
  292. if (thisname.equals(oldname))
  293. setName(newname);
  294. else {
  295. super.replaceClassName(oldname, newname);
  296. getClassFile2().renameClass(oldname, newname);
  297. eraseCache();
  298. }
  299. }
  300. public boolean isInterface() {
  301. return Modifier.isInterface(getModifiers());
  302. }
  303. public int getModifiers() {
  304. int acc = getClassFile2().getAccessFlags();
  305. acc = AccessFlag.clear(acc, AccessFlag.SUPER);
  306. return AccessFlag.toModifier(acc);
  307. }
  308. public void setModifiers(int mod) {
  309. checkModify();
  310. int acc = AccessFlag.of(mod) | AccessFlag.SUPER;
  311. getClassFile2().setAccessFlags(acc);
  312. }
  313. public Object[] getAnnotations() throws ClassNotFoundException {
  314. ClassFile cf = getClassFile2();
  315. AnnotationsAttribute ainfo = (AnnotationsAttribute)
  316. cf.getAttribute(AnnotationsAttribute.invisibleTag);
  317. AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
  318. cf.getAttribute(AnnotationsAttribute.visibleTag);
  319. return toAnnotationType(getClassPool(), ainfo, ainfo2);
  320. }
  321. static Object[] toAnnotationType(ClassPool cp, AnnotationsAttribute a1,
  322. AnnotationsAttribute a2) throws ClassNotFoundException {
  323. Annotation[] anno1, anno2;
  324. int size1, size2;
  325. if (a1 == null) {
  326. anno1 = null;
  327. size1 = 0;
  328. }
  329. else {
  330. anno1 = a1.getAnnotations();
  331. size1 = anno1.length;
  332. }
  333. if (a2 == null) {
  334. anno2 = null;
  335. size2 = 0;
  336. }
  337. else {
  338. anno2 = a2.getAnnotations();
  339. size2 = anno2.length;
  340. }
  341. Object[] result = new Object[size1 + size2];
  342. ClassLoader cl = Thread.currentThread().getContextClassLoader();
  343. for (int i = 0; i < size1; i++)
  344. result[i] = anno1[i].toAnnotationType(cl, cp);
  345. for (int j = 0; j < size2; j++)
  346. result[j + size1] = anno2[j].toAnnotationType(cl, cp);
  347. return result;
  348. }
  349. public boolean subclassOf(CtClass superclass) {
  350. if (superclass == null)
  351. return false;
  352. String superName = superclass.getName();
  353. CtClass curr = this;
  354. try {
  355. while (curr != null) {
  356. if (curr.getName().equals(superName))
  357. return true;
  358. curr = curr.getSuperclass();
  359. }
  360. }
  361. catch (Exception ignored) {}
  362. return false;
  363. }
  364. public CtClass getSuperclass() throws NotFoundException {
  365. String supername = getClassFile2().getSuperclass();
  366. if (supername == null)
  367. return null;
  368. else
  369. return classPool.get(supername);
  370. }
  371. public void setSuperclass(CtClass clazz) throws CannotCompileException {
  372. checkModify();
  373. if (isInterface())
  374. addInterface(clazz);
  375. else
  376. getClassFile2().setSuperclass(clazz.getName());
  377. }
  378. public CtClass[] getInterfaces() throws NotFoundException {
  379. String[] ifs = getClassFile2().getInterfaces();
  380. int num = ifs.length;
  381. CtClass[] ifc = new CtClass[num];
  382. for (int i = 0; i < num; ++i)
  383. ifc[i] = classPool.get(ifs[i]);
  384. return ifc;
  385. }
  386. public void setInterfaces(CtClass[] list) {
  387. checkModify();
  388. String[] ifs;
  389. if (list == null)
  390. ifs = new String[0];
  391. else {
  392. int num = list.length;
  393. ifs = new String[num];
  394. for (int i = 0; i < num; ++i)
  395. ifs[i] = list[i].getName();
  396. }
  397. getClassFile2().setInterfaces(ifs);
  398. }
  399. public void addInterface(CtClass anInterface) {
  400. checkModify();
  401. if (anInterface != null)
  402. getClassFile2().addInterface(anInterface.getName());
  403. }
  404. public CtClass getDeclaringClass() throws NotFoundException {
  405. ClassFile cf = getClassFile2();
  406. InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(
  407. InnerClassesAttribute.tag);
  408. if (ica == null)
  409. return null;
  410. String name = getName();
  411. int n = ica.tableLength();
  412. for (int i = 0; i < n; ++i)
  413. if (name.equals(ica.innerClass(i))) {
  414. String outName = ica.outerClass(i);
  415. if (outName != null)
  416. return classPool.get(outName);
  417. }
  418. return null;
  419. }
  420. public CtClass getEnclosingClass() throws NotFoundException {
  421. CtClass enc = getDeclaringClass();
  422. if (enc == null) {
  423. ClassFile cf = getClassFile2();
  424. EnclosingMethodAttribute ema
  425. = (EnclosingMethodAttribute)cf.getAttribute(
  426. EnclosingMethodAttribute.tag);
  427. if (ema != null)
  428. enc = classPool.get(ema.className());
  429. }
  430. return enc;
  431. }
  432. public CtClass makeNestedClass(String name, boolean isStatic) {
  433. if (!isStatic)
  434. throw new RuntimeException(
  435. "sorry, only nested static class is supported");
  436. checkModify();
  437. CtClass c = classPool.makeNestedClass(getName() + "$" + name);
  438. ClassFile cf = getClassFile2();
  439. InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(
  440. InnerClassesAttribute.tag);
  441. if (ica == null) {
  442. ica = new InnerClassesAttribute(cf.getConstPool());
  443. cf.addAttribute(ica);
  444. }
  445. ica.append(c.getName(), this.getName(), name, AccessFlag.STATIC);
  446. ClassFile cf2 = c.getClassFile2();
  447. cf2.addAttribute(ica.copy(cf2.getConstPool(), null));
  448. return c;
  449. }
  450. public CtField[] getFields() {
  451. ArrayList alist = new ArrayList();
  452. getFields(alist, this);
  453. return (CtField[])alist.toArray(new CtField[alist.size()]);
  454. }
  455. private static void getFields(ArrayList alist, CtClass cc) {
  456. int i, num;
  457. if (cc == null)
  458. return;
  459. try {
  460. getFields(alist, cc.getSuperclass());
  461. }
  462. catch (NotFoundException e) {}
  463. try {
  464. CtClass[] ifs = cc.getInterfaces();
  465. num = ifs.length;
  466. for (i = 0; i < num; ++i)
  467. getFields(alist, ifs[i]);
  468. }
  469. catch (NotFoundException e) {}
  470. CtMember cf = ((CtClassType)cc).getFieldsCache();
  471. while (cf != null) {
  472. if (Modifier.isPublic(cf.getModifiers()))
  473. alist.add(cf);
  474. cf = cf.next;
  475. }
  476. }
  477. public CtField getField(String name) throws NotFoundException {
  478. CtField f = getField2(name);
  479. if (f == null)
  480. throw new NotFoundException("field: " + name + " in " + getName());
  481. else
  482. return f;
  483. }
  484. CtField getField2(String name) {
  485. CtField df = getDeclaredField2(name);
  486. if (df != null)
  487. return df;
  488. try {
  489. CtClass[] ifs = getInterfaces();
  490. int num = ifs.length;
  491. for (int i = 0; i < num; ++i) {
  492. CtField f = ifs[i].getField2(name);
  493. if (f != null)
  494. return f;
  495. }
  496. CtClass s = getSuperclass();
  497. if (s != null)
  498. return s.getField2(name);
  499. }
  500. catch (NotFoundException e) {}
  501. return null;
  502. }
  503. public CtField[] getDeclaredFields() {
  504. CtMember cf = getFieldsCache();
  505. int num = CtField.count(cf);
  506. CtField[] cfs = new CtField[num];
  507. int i = 0;
  508. while (cf != null) {
  509. cfs[i++] = (CtField)cf;
  510. cf = cf.next;
  511. }
  512. return cfs;
  513. }
  514. protected CtMember getFieldsCache() {
  515. if (fieldsCache == null) {
  516. List list = getClassFile2().getFields();
  517. int n = list.size();
  518. for (int i = 0; i < n; ++i) {
  519. FieldInfo finfo = (FieldInfo)list.get(i);
  520. fieldsCache = CtMember.append(fieldsCache,
  521. new CtField(finfo, this));
  522. }
  523. }
  524. return fieldsCache;
  525. }
  526. public CtField getDeclaredField(String name) throws NotFoundException {
  527. CtField f = getDeclaredField2(name);
  528. if (f == null)
  529. throw new NotFoundException("field: " + name + " in " + getName());
  530. else
  531. return f;
  532. }
  533. private CtField getDeclaredField2(String name) {
  534. CtMember cf = getFieldsCache();
  535. while (cf != null) {
  536. if (cf.getName().equals(name))
  537. return (CtField)cf;
  538. cf = cf.next;
  539. }
  540. return null;
  541. }
  542. public CtBehavior[] getDeclaredBehaviors() {
  543. CtMember cc = getConstructorsCache();
  544. CtMember cm = getMethodsCache();
  545. int num = CtMember.count(cm) + CtMember.count(cc);
  546. CtBehavior[] cb = new CtBehavior[num];
  547. int i = 0;
  548. while (cc != null) {
  549. cb[i++] = (CtBehavior)cc;
  550. cc = cc.next;
  551. }
  552. while (cm != null) {
  553. cb[i++] = (CtBehavior)cm;
  554. cm = cm.next;
  555. }
  556. return cb;
  557. }
  558. public CtConstructor[] getConstructors() {
  559. CtConstructor[] cons = getDeclaredConstructors();
  560. if (cons.length == 0)
  561. return cons;
  562. int n = 0;
  563. int i = cons.length;
  564. while (--i >= 0)
  565. if (Modifier.isPublic(cons[i].getModifiers()))
  566. ++n;
  567. CtConstructor[] result = new CtConstructor[n];
  568. n = 0;
  569. i = cons.length;
  570. while (--i >= 0) {
  571. CtConstructor c = cons[i];
  572. if (Modifier.isPublic(c.getModifiers()))
  573. result[n++] = c;
  574. }
  575. return result;
  576. }
  577. public CtConstructor getConstructor(String desc)
  578. throws NotFoundException
  579. {
  580. CtConstructor cc = (CtConstructor)getConstructorsCache();
  581. while (cc != null) {
  582. if (cc.getMethodInfo2().getDescriptor().equals(desc))
  583. return cc;
  584. cc = (CtConstructor)cc.next;
  585. }
  586. return super.getConstructor(desc);
  587. }
  588. public CtConstructor[] getDeclaredConstructors() {
  589. CtMember cc = getConstructorsCache();
  590. int num = CtMember.count(cc);
  591. CtConstructor[] ccs = new CtConstructor[num];
  592. int i = 0;
  593. while (cc != null) {
  594. ccs[i++] = (CtConstructor)cc;
  595. cc = cc.next;
  596. }
  597. return ccs;
  598. }
  599. protected CtMember getConstructorsCache() {
  600. if (constructorsCache == null) {
  601. List list = getClassFile2().getMethods();
  602. int n = list.size();
  603. for (int i = 0; i < n; ++i) {
  604. MethodInfo minfo = (MethodInfo)list.get(i);
  605. if (minfo.isConstructor())
  606. constructorsCache
  607. = CtMember.append(constructorsCache,
  608. new CtConstructor(minfo, this));
  609. }
  610. }
  611. return constructorsCache;
  612. }
  613. public CtConstructor getClassInitializer() {
  614. if (classInitializerCache == null) {
  615. MethodInfo minfo = getClassFile2().getStaticInitializer();
  616. if (minfo != null)
  617. classInitializerCache = new CtConstructor(minfo, this);
  618. }
  619. return classInitializerCache;
  620. }
  621. public CtMethod[] getMethods() {
  622. HashMap h = new HashMap();
  623. getMethods0(h, this);
  624. return (CtMethod[])h.values().toArray(new CtMethod[0]);
  625. }
  626. private static void getMethods0(HashMap h, CtClass cc) {
  627. try {
  628. CtClass[] ifs = cc.getInterfaces();
  629. int size = ifs.length;
  630. for (int i = 0; i < size; ++i)
  631. getMethods0(h, ifs[i]);
  632. }
  633. catch (NotFoundException e) {}
  634. try {
  635. CtClass s = cc.getSuperclass();
  636. if (s != null)
  637. getMethods0(h, s);
  638. }
  639. catch (NotFoundException e) {}
  640. if (cc instanceof CtClassType) {
  641. CtMember cm = ((CtClassType)cc).getMethodsCache();
  642. while (cm != null) {
  643. if (Modifier.isPublic(cm.getModifiers()))
  644. h.put(((CtMethod)cm).getStringRep(), cm);
  645. cm = cm.next;
  646. }
  647. }
  648. }
  649. public CtMethod getMethod(String name, String desc)
  650. throws NotFoundException
  651. {
  652. CtMethod m = getMethod0(this, name, desc);
  653. if (m != null)
  654. return m;
  655. else
  656. throw new NotFoundException(name + "(..) is not found in "
  657. + getName());
  658. }
  659. private static CtMethod getMethod0(CtClass cc,
  660. String name, String desc) {
  661. if (cc instanceof CtClassType) {
  662. CtMethod cm = (CtMethod)((CtClassType)cc).getMethodsCache();
  663. while (cm != null) {
  664. if (cm.getName().equals(name)
  665. && cm.getMethodInfo2().getDescriptor().equals(desc))
  666. return cm;
  667. cm = (CtMethod)cm.next;
  668. }
  669. }
  670. try {
  671. CtClass s = cc.getSuperclass();
  672. if (s != null) {
  673. CtMethod m = getMethod0(s, name, desc);
  674. if (m != null)
  675. return m;
  676. }
  677. }
  678. catch (NotFoundException e) {}
  679. try {
  680. CtClass[] ifs = cc.getInterfaces();
  681. int size = ifs.length;
  682. for (int i = 0; i < size; ++i) {
  683. CtMethod m = getMethod0(ifs[i], name, desc);
  684. if (m != null)
  685. return m;
  686. }
  687. }
  688. catch (NotFoundException e) {}
  689. return null;
  690. }
  691. public CtMethod[] getDeclaredMethods() {
  692. CtMember cm = getMethodsCache();
  693. int num = CtMember.count(cm);
  694. CtMethod[] cms = new CtMethod[num];
  695. int i = 0;
  696. while (cm != null) {
  697. cms[i++] = (CtMethod)cm;
  698. cm = cm.next;
  699. }
  700. return cms;
  701. }
  702. public CtMethod getDeclaredMethod(String name) throws NotFoundException {
  703. CtMember m = getMethodsCache();
  704. while (m != null) {
  705. if (m.getName().equals(name))
  706. return (CtMethod)m;
  707. m = m.next;
  708. }
  709. throw new NotFoundException(name + "(..) is not found in "
  710. + getName());
  711. }
  712. public CtMethod getDeclaredMethod(String name, CtClass[] params)
  713. throws NotFoundException
  714. {
  715. String desc = Descriptor.ofParameters(params);
  716. CtMethod m = (CtMethod)getMethodsCache();
  717. while (m != null) {
  718. if (m.getName().equals(name)
  719. && m.getMethodInfo2().getDescriptor().startsWith(desc))
  720. return m;
  721. m = (CtMethod)m.next;
  722. }
  723. throw new NotFoundException(name + "(..) is not found in "
  724. + getName());
  725. }
  726. protected CtMember getMethodsCache() {
  727. if (methodsCache == null) {
  728. List list = getClassFile2().getMethods();
  729. int n = list.size();
  730. for (int i = 0; i < n; ++i) {
  731. MethodInfo minfo = (MethodInfo)list.get(i);
  732. if (minfo.isMethod())
  733. methodsCache = CtMember.append(methodsCache,
  734. new CtMethod(minfo, this));
  735. }
  736. }
  737. return methodsCache;
  738. }
  739. public void addField(CtField f, String init)
  740. throws CannotCompileException
  741. {
  742. addField(f, CtField.Initializer.byExpr(init));
  743. }
  744. public void addField(CtField f, CtField.Initializer init)
  745. throws CannotCompileException
  746. {
  747. checkModify();
  748. if (f.getDeclaringClass() != this)
  749. throw new CannotCompileException("cannot add");
  750. if (init == null)
  751. init = f.getInit();
  752. if (init != null) {
  753. int mod = f.getModifiers();
  754. if (Modifier.isStatic(mod) && Modifier.isFinal(mod))
  755. try {
  756. ConstPool cp = getClassFile2().getConstPool();
  757. int index = init.getConstantValue(cp, f.getType());
  758. if (index != 0) {
  759. f.getFieldInfo2().addAttribute(new ConstantAttribute(cp, index));
  760. init = null;
  761. }
  762. }
  763. catch (NotFoundException e) {}
  764. }
  765. getFieldsCache();
  766. fieldsCache = CtField.append(fieldsCache, f);
  767. getClassFile2().addField(f.getFieldInfo2());
  768. if (init != null) {
  769. FieldInitLink fil = new FieldInitLink(f, init);
  770. FieldInitLink link = fieldInitializers;
  771. if (link == null)
  772. fieldInitializers = fil;
  773. else {
  774. while (link.next != null)
  775. link = link.next;
  776. link.next = fil;
  777. }
  778. }
  779. }
  780. public void removeField(CtField f) throws NotFoundException {
  781. checkModify();
  782. FieldInfo fi = f.getFieldInfo2();
  783. ClassFile cf = getClassFile2();
  784. if (cf.getFields().remove(fi)) {
  785. fieldsCache = CtMember.remove(fieldsCache, f);
  786. memberRemoved = true;
  787. }
  788. else
  789. throw new NotFoundException(f.toString());
  790. }
  791. public CtConstructor makeClassInitializer()
  792. throws CannotCompileException
  793. {
  794. CtConstructor clinit = getClassInitializer();
  795. if (clinit != null)
  796. return clinit;
  797. checkModify();
  798. ClassFile cf = getClassFile2();
  799. Bytecode code = new Bytecode(cf.getConstPool(), 0, 0);
  800. modifyClassConstructor(cf, code, 0, 0);
  801. return getClassInitializer();
  802. }
  803. public void addConstructor(CtConstructor c)
  804. throws CannotCompileException
  805. {
  806. checkModify();
  807. if (c.getDeclaringClass() != this)
  808. throw new CannotCompileException("cannot add");
  809. getConstructorsCache();
  810. constructorsCache = (CtConstructor)CtMember.append(constructorsCache, c);
  811. getClassFile2().addMethod(c.getMethodInfo2());
  812. }
  813. public void removeConstructor(CtConstructor m) throws NotFoundException {
  814. checkModify();
  815. MethodInfo mi = m.getMethodInfo2();
  816. ClassFile cf = getClassFile2();
  817. if (cf.getMethods().remove(mi)) {
  818. constructorsCache = CtMember.remove(constructorsCache, m);
  819. memberRemoved = true;
  820. }
  821. else
  822. throw new NotFoundException(m.toString());
  823. }
  824. public void addMethod(CtMethod m) throws CannotCompileException {
  825. checkModify();
  826. if (m.getDeclaringClass() != this)
  827. throw new CannotCompileException("cannot add");
  828. getMethodsCache();
  829. methodsCache = CtMember.append(methodsCache, m);
  830. getClassFile2().addMethod(m.getMethodInfo2());
  831. if ((m.getModifiers() & Modifier.ABSTRACT) != 0)
  832. setModifiers(getModifiers() | Modifier.ABSTRACT);
  833. }
  834. public void removeMethod(CtMethod m) throws NotFoundException {
  835. checkModify();
  836. MethodInfo mi = m.getMethodInfo2();
  837. ClassFile cf = getClassFile2();
  838. if (cf.getMethods().remove(mi)) {
  839. methodsCache = CtMember.remove(methodsCache, m);
  840. memberRemoved = true;
  841. }
  842. else
  843. throw new NotFoundException(m.toString());
  844. }
  845. public byte[] getAttribute(String name) {
  846. AttributeInfo ai = getClassFile2().getAttribute(name);
  847. if (ai == null)
  848. return null;
  849. else
  850. return ai.get();
  851. }
  852. public void setAttribute(String name, byte[] data) {
  853. checkModify();
  854. ClassFile cf = getClassFile2();
  855. cf.addAttribute(new AttributeInfo(cf.getConstPool(), name, data));
  856. }
  857. public void instrument(CodeConverter converter)
  858. throws CannotCompileException
  859. {
  860. checkModify();
  861. ClassFile cf = getClassFile2();
  862. ConstPool cp = cf.getConstPool();
  863. List list = cf.getMethods();
  864. int n = list.size();
  865. for (int i = 0; i < n; ++i) {
  866. MethodInfo minfo = (MethodInfo)list.get(i);
  867. converter.doit(this, minfo, cp);
  868. }
  869. }
  870. public void instrument(ExprEditor editor)
  871. throws CannotCompileException
  872. {
  873. checkModify();
  874. ClassFile cf = getClassFile2();
  875. List list = cf.getMethods();
  876. int n = list.size();
  877. for (int i = 0; i < n; ++i) {
  878. MethodInfo minfo = (MethodInfo)list.get(i);
  879. editor.doit(this, minfo);
  880. }
  881. }
  882. /**
  883. * @see javassist.CtClass#prune()
  884. * @see javassist.CtClass#stopPruning(boolean)
  885. */
  886. public void prune() {
  887. if (wasPruned)
  888. return;
  889. wasPruned = wasFrozen = true;
  890. getClassFile2().prune();
  891. }
  892. public void toBytecode(DataOutputStream out)
  893. throws CannotCompileException, IOException
  894. {
  895. try {
  896. if (isModified()) {
  897. checkPruned("toBytecode");
  898. ClassFile cf = getClassFile2();
  899. if (memberRemoved) {
  900. cf.compact();
  901. memberRemoved = false;
  902. }
  903. modifyClassConstructor(cf);
  904. modifyConstructors(cf);
  905. cf.write(out);
  906. out.flush();
  907. fieldInitializers = null;
  908. if (doPruning) {
  909. // to save memory
  910. cf.prune();
  911. wasPruned = true;
  912. }
  913. }
  914. else {
  915. classPool.writeClassfile(getName(), out);
  916. // to save memory
  917. eraseCache();
  918. classfile = null;
  919. }
  920. wasFrozen = true;
  921. }
  922. catch (NotFoundException e) {
  923. throw new CannotCompileException(e);
  924. }
  925. catch (IOException e) {
  926. throw new CannotCompileException(e);
  927. }
  928. }
  929. /* See also checkModified()
  930. */
  931. private void checkPruned(String method) {
  932. if (wasPruned)
  933. throw new RuntimeException(method + "(): " + getName()
  934. + " was pruned.");
  935. }
  936. public void stopPruning(boolean stop) {
  937. doPruning = !stop;
  938. }
  939. private void modifyClassConstructor(ClassFile cf)
  940. throws CannotCompileException, NotFoundException
  941. {
  942. if (fieldInitializers == null)
  943. return;
  944. Bytecode code = new Bytecode(cf.getConstPool(), 0, 0);
  945. Javac jv = new Javac(code, this);
  946. int stacksize = 0;
  947. boolean doInit = false;
  948. for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
  949. CtField f = fi.field;
  950. if (Modifier.isStatic(f.getModifiers())) {
  951. doInit = true;
  952. int s = fi.init.compileIfStatic(f.getType(), f.getName(),
  953. code, jv);
  954. if (stacksize < s)
  955. stacksize = s;
  956. }
  957. }
  958. if (doInit) // need an initializer for static fileds.
  959. modifyClassConstructor(cf, code, stacksize, 0);
  960. }
  961. private void modifyClassConstructor(ClassFile cf, Bytecode code,
  962. int stacksize, int localsize)
  963. throws CannotCompileException
  964. {
  965. MethodInfo m = cf.getStaticInitializer();
  966. if (m == null) {
  967. code.add(Bytecode.RETURN);
  968. code.setMaxStack(stacksize);
  969. code.setMaxLocals(localsize);
  970. m = new MethodInfo(cf.getConstPool(), "<clinit>", "()V");
  971. m.setAccessFlags(AccessFlag.STATIC);
  972. m.setCodeAttribute(code.toCodeAttribute());
  973. cf.addMethod(m);
  974. }
  975. else {
  976. CodeAttribute codeAttr = m.getCodeAttribute();
  977. if (codeAttr == null)
  978. throw new CannotCompileException("empty <clinit>");
  979. try {
  980. CodeIterator it = codeAttr.iterator();
  981. int pos = it.insertEx(code.get());
  982. it.insert(code.getExceptionTable(), pos);
  983. int maxstack = codeAttr.getMaxStack();
  984. if (maxstack < stacksize)
  985. codeAttr.setMaxStack(stacksize);
  986. int maxlocals = codeAttr.getMaxLocals();
  987. if (maxlocals < localsize)
  988. codeAttr.setMaxLocals(localsize);
  989. }
  990. catch (BadBytecode e) {
  991. throw new CannotCompileException(e);
  992. }
  993. }
  994. }
  995. private void modifyConstructors(ClassFile cf)
  996. throws CannotCompileException, NotFoundException
  997. {
  998. if (fieldInitializers == null)
  999. return;
  1000. ConstPool cp = cf.getConstPool();
  1001. List list = cf.getMethods();
  1002. int n = list.size();
  1003. for (int i = 0; i < n; ++i) {
  1004. MethodInfo minfo = (MethodInfo)list.get(i);
  1005. if (minfo.isConstructor()) {
  1006. CodeAttribute codeAttr = minfo.getCodeAttribute();
  1007. if (codeAttr != null)
  1008. try {
  1009. Bytecode init = new Bytecode(cp, 0,
  1010. codeAttr.getMaxLocals());
  1011. CtClass[] params
  1012. = Descriptor.getParameterTypes(
  1013. minfo.getDescriptor(),
  1014. classPool);
  1015. int stacksize = makeFieldInitializer(init, params);
  1016. insertAuxInitializer(codeAttr, init, stacksize);
  1017. }
  1018. catch (BadBytecode e) {
  1019. throw new CannotCompileException(e);
  1020. }
  1021. }
  1022. }
  1023. }
  1024. private static void insertAuxInitializer(CodeAttribute codeAttr,
  1025. Bytecode initializer,
  1026. int stacksize)
  1027. throws BadBytecode
  1028. {
  1029. CodeIterator it = codeAttr.iterator();
  1030. int index = it.skipSuperConstructor();
  1031. if (index < 0) {
  1032. index = it.skipThisConstructor();
  1033. if (index >= 0)
  1034. return; // this() is called.
  1035. // Neither this() or super() is called.
  1036. }
  1037. int pos = it.insertEx(initializer.get());
  1038. it.insert(initializer.getExceptionTable(), pos);
  1039. int maxstack = codeAttr.getMaxStack();
  1040. if (maxstack < stacksize)
  1041. codeAttr.setMaxStack(stacksize);
  1042. }
  1043. private int makeFieldInitializer(Bytecode code, CtClass[] parameters)
  1044. throws CannotCompileException, NotFoundException
  1045. {
  1046. int stacksize = 0;
  1047. Javac jv = new Javac(code, this);
  1048. try {
  1049. jv.recordParams(parameters, false);
  1050. }
  1051. catch (CompileError e) {
  1052. throw new CannotCompileException(e);
  1053. }
  1054. for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
  1055. CtField f = fi.field;
  1056. if (!Modifier.isStatic(f.getModifiers())) {
  1057. int s = fi.init.compile(f.getType(), f.getName(), code,
  1058. parameters, jv);
  1059. if (stacksize < s)
  1060. stacksize = s;
  1061. }
  1062. }
  1063. return stacksize;
  1064. }
  1065. // Methods used by CtNewWrappedMethod
  1066. Hashtable getHiddenMethods() {
  1067. if (hiddenMethods == null)
  1068. hiddenMethods = new Hashtable();
  1069. return hiddenMethods;
  1070. }
  1071. int getUniqueNumber() { return uniqueNumberSeed++; }
  1072. }
  1073. class FieldInitLink {
  1074. FieldInitLink next;
  1075. CtField field;
  1076. CtField.Initializer init;
  1077. FieldInitLink(CtField f, CtField.Initializer i) {
  1078. next = null;
  1079. field = f;
  1080. init = i;
  1081. }
  1082. }