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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 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 javassist.bytecode.*;
  17. import javassist.compiler.Javac;
  18. import javassist.compiler.CompileError;
  19. import javassist.expr.ExprEditor;
  20. import java.io.ByteArrayInputStream;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.InputStream;
  23. import java.io.DataInputStream;
  24. import java.io.DataOutputStream;
  25. import java.io.IOException;
  26. import java.util.ArrayList;
  27. import java.util.HashMap;
  28. import java.util.Hashtable;
  29. import java.util.List;
  30. /**
  31. * Class types.
  32. */
  33. class CtClassType extends CtClass {
  34. protected ClassPool classPool;
  35. protected boolean wasChanged;
  36. protected boolean wasFrozen;
  37. protected ClassFile classfile;
  38. private CtField fieldsCache;
  39. private CtConstructor constructorsCache;
  40. private CtConstructor classInitializerCache;
  41. private CtMethod methodsCache;
  42. private FieldInitLink fieldInitializers;
  43. private Hashtable hiddenMethods; // must be synchronous
  44. private int uniqueNumberSeed;
  45. CtClassType(String name, ClassPool cp) {
  46. super(name);
  47. classPool = cp;
  48. wasChanged = wasFrozen = false;
  49. classfile = null;
  50. fieldInitializers = null;
  51. hiddenMethods = null;
  52. uniqueNumberSeed = 0;
  53. eraseCache();
  54. }
  55. CtClassType(InputStream ins, ClassPool cp) throws IOException {
  56. this((String)null, cp);
  57. classfile = new ClassFile(new DataInputStream(ins));
  58. qualifiedName = classfile.getName();
  59. }
  60. protected void eraseCache() {
  61. fieldsCache = null;
  62. constructorsCache = null;
  63. classInitializerCache = null;
  64. methodsCache = null;
  65. }
  66. public ClassFile getClassFile2() {
  67. if (classfile != null)
  68. return classfile;
  69. try {
  70. byte[] b = classPool.readSource(getName());
  71. DataInputStream dis
  72. = new DataInputStream(new ByteArrayInputStream(b));
  73. return (classfile = new ClassFile(dis));
  74. }
  75. catch (NotFoundException e) {
  76. throw new RuntimeException(e.toString());
  77. }
  78. catch (IOException e) {
  79. throw new RuntimeException(e.toString());
  80. }
  81. catch (CannotCompileException e) {
  82. throw new RuntimeException(e.toString());
  83. }
  84. }
  85. public ClassPool getClassPool() { return classPool; }
  86. public boolean isModified() { return wasChanged; }
  87. public boolean isFrozen() { return wasFrozen; }
  88. void freeze() { wasFrozen = true; }
  89. void checkModify() throws RuntimeException {
  90. super.checkModify();
  91. wasChanged = true;
  92. }
  93. public void defrost() { wasFrozen = false; }
  94. public boolean subtypeOf(CtClass clazz) throws NotFoundException {
  95. int i;
  96. String cname = clazz.getName();
  97. if (this == clazz || getName().equals(cname))
  98. return true;
  99. ClassFile file = getClassFile2();
  100. String supername = file.getSuperclass();
  101. if (supername != null && supername.equals(cname))
  102. return true;
  103. String[] ifs = file.getInterfaces();
  104. int num = ifs.length;
  105. for (i = 0; i < num; ++i)
  106. if (ifs[i].equals(cname))
  107. return true;
  108. if (supername != null && classPool.get(supername).subtypeOf(clazz))
  109. return true;
  110. for (i = 0; i < num; ++i)
  111. if (classPool.get(ifs[i]).subtypeOf(clazz))
  112. return true;
  113. return false;
  114. }
  115. public void setName(String name) throws RuntimeException {
  116. String oldname = getName();
  117. if (name.equals(oldname))
  118. return;
  119. classPool.checkNotFrozen(name,
  120. "the class with the new name is frozen");
  121. ClassFile cf = getClassFile2();
  122. super.setName(name);
  123. cf.setName(name);
  124. eraseCache();
  125. classPool.classNameChanged(oldname, this);
  126. }
  127. public void replaceClassName(ClassMap classnames)
  128. throws RuntimeException
  129. {
  130. String oldClassName = getName();
  131. String newClassName
  132. = (String)classnames.get(Descriptor.toJvmName(oldClassName));
  133. if (newClassName != null) {
  134. newClassName = Descriptor.toJavaName(newClassName);
  135. classPool.checkNotFrozen(newClassName,
  136. "the class " + newClassName + " is frozen");
  137. }
  138. super.replaceClassName(classnames);
  139. ClassFile cf = getClassFile2();
  140. cf.renameClass(classnames);
  141. eraseCache();
  142. if (newClassName != null) {
  143. super.setName(newClassName);
  144. classPool.classNameChanged(oldClassName, this);
  145. }
  146. }
  147. public void replaceClassName(String oldname, String newname)
  148. throws RuntimeException
  149. {
  150. String thisname = getName();
  151. if (thisname.equals(oldname))
  152. setName(newname);
  153. else {
  154. super.replaceClassName(oldname, newname);
  155. getClassFile2().renameClass(oldname, newname);
  156. eraseCache();
  157. }
  158. }
  159. public boolean isInterface() {
  160. return Modifier.isInterface(getModifiers());
  161. }
  162. public int getModifiers() {
  163. int acc = getClassFile2().getAccessFlags();
  164. acc = AccessFlag.clear(acc, AccessFlag.SUPER);
  165. return AccessFlag.toModifier(acc);
  166. }
  167. public void setModifiers(int mod) {
  168. checkModify();
  169. int acc = AccessFlag.of(mod) | AccessFlag.SUPER;
  170. getClassFile2().setAccessFlags(acc);
  171. }
  172. public boolean subclassOf(CtClass superclass) {
  173. if (superclass == null)
  174. return false;
  175. String superName = superclass.getName();
  176. CtClass curr = this;
  177. try {
  178. while (curr != null) {
  179. if (curr.getName().equals(superName))
  180. return true;
  181. curr = curr.getSuperclass();
  182. }
  183. }
  184. catch (Exception ignored) {}
  185. return false;
  186. }
  187. public CtClass getSuperclass() throws NotFoundException {
  188. String supername = getClassFile2().getSuperclass();
  189. if (supername == null)
  190. return null;
  191. else
  192. return classPool.get(supername);
  193. }
  194. public void setSuperclass(CtClass clazz) throws CannotCompileException {
  195. checkModify();
  196. getClassFile2().setSuperclass(clazz.getName());
  197. }
  198. public CtClass[] getInterfaces() throws NotFoundException {
  199. String[] ifs = getClassFile2().getInterfaces();
  200. int num = ifs.length;
  201. CtClass[] ifc = new CtClass[num];
  202. for (int i = 0; i < num; ++i)
  203. ifc[i] = classPool.get(ifs[i]);
  204. return ifc;
  205. }
  206. public void setInterfaces(CtClass[] list) {
  207. checkModify();
  208. String[] ifs;
  209. if (list == null)
  210. ifs = new String[0];
  211. else {
  212. int num = list.length;
  213. ifs = new String[num];
  214. for (int i = 0; i < num; ++i)
  215. ifs[i] = list[i].getName();
  216. }
  217. getClassFile2().setInterfaces(ifs);
  218. }
  219. public void addInterface(CtClass anInterface) {
  220. checkModify();
  221. if (anInterface != null)
  222. getClassFile2().addInterface(anInterface.getName());
  223. }
  224. public CtField[] getFields() {
  225. ArrayList alist = new ArrayList();
  226. getFields(alist, this);
  227. return (CtField[])alist.toArray(new CtField[alist.size()]);
  228. }
  229. private static void getFields(ArrayList alist, CtClass cc) {
  230. int i, num;
  231. if (cc == null)
  232. return;
  233. try {
  234. getFields(alist, cc.getSuperclass());
  235. }
  236. catch (NotFoundException e) {}
  237. try {
  238. CtClass[] ifs = cc.getInterfaces();
  239. num = ifs.length;
  240. for (i = 0; i < num; ++i)
  241. getFields(alist, ifs[i]);
  242. }
  243. catch (NotFoundException e) {}
  244. CtField cf = ((CtClassType)cc).getFieldsCache();
  245. while (cf != null) {
  246. if (Modifier.isPublic(cf.getModifiers()))
  247. alist.add(cf);
  248. cf = cf.next;
  249. }
  250. }
  251. public CtField getField(String name) throws NotFoundException {
  252. try {
  253. return getDeclaredField(name);
  254. }
  255. catch (NotFoundException e) {}
  256. try {
  257. CtClass[] ifs = getInterfaces();
  258. int num = ifs.length;
  259. for (int i = 0; i < num; ++i)
  260. try {
  261. return ifs[i].getField(name);
  262. }
  263. catch (NotFoundException e) {}
  264. }
  265. catch (NotFoundException e) {}
  266. try {
  267. CtClass s = getSuperclass();
  268. if (s != null)
  269. return s.getField(name);
  270. }
  271. catch (NotFoundException e) {}
  272. throw new NotFoundException(name);
  273. }
  274. public CtField[] getDeclaredFields() {
  275. CtField cf = getFieldsCache();
  276. int num = CtField.count(cf);
  277. CtField[] cfs = new CtField[num];
  278. int i = 0;
  279. while (cf != null) {
  280. cfs[i++] = cf;
  281. cf = cf.next;
  282. }
  283. return cfs;
  284. }
  285. protected CtField getFieldsCache() {
  286. if (fieldsCache == null) {
  287. List list = getClassFile2().getFields();
  288. int n = list.size();
  289. for (int i = 0; i < n; ++i) {
  290. FieldInfo finfo = (FieldInfo)list.get(i);
  291. fieldsCache = CtField.append(fieldsCache,
  292. new CtField(finfo, this));
  293. }
  294. }
  295. return fieldsCache;
  296. }
  297. public CtField getDeclaredField(String name) throws NotFoundException {
  298. CtField cf = getFieldsCache();
  299. while (cf != null) {
  300. if (cf.getName().equals(name))
  301. return cf;
  302. cf = cf.next;
  303. }
  304. throw new NotFoundException(name);
  305. }
  306. public CtBehavior[] getDeclaredBehaviors() {
  307. CtConstructor cc = getConstructorsCache();
  308. CtMethod cm = getMethodsCache();
  309. int num = CtMethod.count(cm) + CtConstructor.count(cc);
  310. CtBehavior[] cb = new CtBehavior[num];
  311. int i = 0;
  312. while (cc != null) {
  313. cb[i++] = cc;
  314. cc = cc.next;
  315. }
  316. while (cm != null) {
  317. cb[i++] = cm;
  318. cm = cm.next;
  319. }
  320. return cb;
  321. }
  322. public CtConstructor[] getConstructors() {
  323. CtConstructor[] cons = getDeclaredConstructors();
  324. if (cons.length == 0)
  325. return cons;
  326. int n = 0;
  327. int i = cons.length;
  328. while (--i >= 0)
  329. if (Modifier.isPublic(cons[i].getModifiers()))
  330. ++n;
  331. CtConstructor[] result = new CtConstructor[n];
  332. n = 0;
  333. i = cons.length;
  334. while (--i >= 0) {
  335. CtConstructor c = cons[i];
  336. if (Modifier.isPublic(c.getModifiers()))
  337. result[n++] = c;
  338. }
  339. return result;
  340. }
  341. public CtConstructor getConstructor(String desc)
  342. throws NotFoundException
  343. {
  344. CtConstructor cc = getConstructorsCache();
  345. while (cc != null) {
  346. if (cc.getMethodInfo2().getDescriptor().equals(desc))
  347. return cc;
  348. cc = cc.next;
  349. }
  350. return super.getConstructor(desc);
  351. }
  352. public CtConstructor[] getDeclaredConstructors() {
  353. CtConstructor cc = getConstructorsCache();
  354. int num = CtConstructor.count(cc);
  355. CtConstructor[] ccs = new CtConstructor[num];
  356. int i = 0;
  357. while (cc != null) {
  358. ccs[i++] = cc;
  359. cc = cc.next;
  360. }
  361. return ccs;
  362. }
  363. protected CtConstructor getConstructorsCache() {
  364. if (constructorsCache == null) {
  365. List list = getClassFile2().getMethods();
  366. int n = list.size();
  367. for (int i = 0; i < n; ++i) {
  368. MethodInfo minfo = (MethodInfo)list.get(i);
  369. if (minfo.isConstructor())
  370. constructorsCache
  371. = CtConstructor.append(constructorsCache,
  372. new CtConstructor(minfo, this));
  373. }
  374. }
  375. return constructorsCache;
  376. }
  377. public CtConstructor getClassInitializer() {
  378. if (classInitializerCache == null) {
  379. List list = getClassFile2().getMethods();
  380. int n = list.size();
  381. for (int i = 0; i < n; ++i) {
  382. MethodInfo minfo = (MethodInfo)list.get(i);
  383. if (minfo.isStaticInitializer()) {
  384. classInitializerCache = new CtConstructor(minfo, this);
  385. break;
  386. }
  387. }
  388. }
  389. return classInitializerCache;
  390. }
  391. public CtMethod[] getMethods() {
  392. HashMap h = new HashMap();
  393. getMethods0(h, this);
  394. return (CtMethod[])h.values().toArray(new CtMethod[0]);
  395. }
  396. private static void getMethods0(HashMap h, CtClass cc) {
  397. try {
  398. CtClass[] ifs = cc.getInterfaces();
  399. int size = ifs.length;
  400. for (int i = 0; i < size; ++i)
  401. getMethods0(h, ifs[i]);
  402. }
  403. catch (NotFoundException e) {}
  404. try {
  405. CtClass s = cc.getSuperclass();
  406. if (s != null)
  407. getMethods0(h, s);
  408. }
  409. catch (NotFoundException e) {}
  410. if (cc instanceof CtClassType) {
  411. CtMethod cm = ((CtClassType)cc).getMethodsCache();
  412. while (cm != null) {
  413. if (Modifier.isPublic(cm.getModifiers()))
  414. h.put(cm, cm);
  415. cm = cm.next;
  416. }
  417. }
  418. }
  419. public CtMethod getMethod(String name, String desc)
  420. throws NotFoundException
  421. {
  422. CtMethod m = getMethod0(this, name, desc);
  423. if (m != null)
  424. return m;
  425. else
  426. throw new NotFoundException(name + "(..) is not found in "
  427. + getName());
  428. }
  429. private static CtMethod getMethod0(CtClass cc,
  430. String name, String desc) {
  431. if (cc instanceof CtClassType) {
  432. CtMethod cm = ((CtClassType)cc).getMethodsCache();
  433. while (cm != null) {
  434. if (cm.getName().equals(name)
  435. && cm.getMethodInfo2().getDescriptor().equals(desc))
  436. return cm;
  437. cm = cm.next;
  438. }
  439. }
  440. try {
  441. CtClass s = cc.getSuperclass();
  442. if (s != null) {
  443. CtMethod m = getMethod0(s, name, desc);
  444. if (m != null)
  445. return m;
  446. }
  447. }
  448. catch (NotFoundException e) {}
  449. try {
  450. CtClass[] ifs = cc.getInterfaces();
  451. int size = ifs.length;
  452. for (int i = 0; i < size; ++i) {
  453. CtMethod m = getMethod0(ifs[i], name, desc);
  454. if (m != null)
  455. return m;
  456. }
  457. }
  458. catch (NotFoundException e) {}
  459. return null;
  460. }
  461. public CtMethod[] getDeclaredMethods() {
  462. CtMethod cm = getMethodsCache();
  463. int num = CtMethod.count(cm);
  464. CtMethod[] cms = new CtMethod[num];
  465. int i = 0;
  466. while (cm != null) {
  467. cms[i++] = cm;
  468. cm = cm.next;
  469. }
  470. return cms;
  471. }
  472. public CtMethod getDeclaredMethod(String name) throws NotFoundException {
  473. CtMethod m = getMethodsCache();
  474. while (m != null) {
  475. if (m.getName().equals(name))
  476. return m;
  477. m = m.next;
  478. }
  479. throw new NotFoundException(name + "(..) is not found in "
  480. + getName());
  481. }
  482. public CtMethod getDeclaredMethod(String name, CtClass[] params)
  483. throws NotFoundException
  484. {
  485. String desc = Descriptor.ofParameters(params);
  486. CtMethod m = getMethodsCache();
  487. while (m != null) {
  488. if (m.getName().equals(name)
  489. && m.getMethodInfo2().getDescriptor().startsWith(desc))
  490. return m;
  491. m = m.next;
  492. }
  493. throw new NotFoundException(name + "(..) is not found in "
  494. + getName());
  495. }
  496. protected CtMethod getMethodsCache() {
  497. if (methodsCache == null) {
  498. List list = getClassFile2().getMethods();
  499. int n = list.size();
  500. for (int i = 0; i < n; ++i) {
  501. MethodInfo minfo = (MethodInfo)list.get(i);
  502. if (minfo.isMethod())
  503. methodsCache = CtMethod.append(methodsCache,
  504. new CtMethod(minfo, this));
  505. }
  506. }
  507. return methodsCache;
  508. }
  509. public void addField(CtField f, String init)
  510. throws CannotCompileException
  511. {
  512. addField(f, CtField.Initializer.byExpr(init));
  513. }
  514. public void addField(CtField f, CtField.Initializer init)
  515. throws CannotCompileException
  516. {
  517. checkModify();
  518. if (f.getDeclaringClass() != this)
  519. throw new CannotCompileException("cannot add");
  520. if (init == null)
  521. init = f.getInit();
  522. getFieldsCache();
  523. fieldsCache = CtField.append(fieldsCache, f);
  524. getClassFile2().addField(f.getFieldInfo2());
  525. if (init != null) {
  526. FieldInitLink fil = new FieldInitLink(f, init);
  527. FieldInitLink link = fieldInitializers;
  528. if (link == null)
  529. fieldInitializers = fil;
  530. else {
  531. while (link.next != null)
  532. link = link.next;
  533. link.next = fil;
  534. }
  535. }
  536. }
  537. public void addConstructor(CtConstructor c)
  538. throws CannotCompileException
  539. {
  540. checkModify();
  541. if (c.getDeclaringClass() != this)
  542. throw new CannotCompileException("cannot add");
  543. getConstructorsCache();
  544. constructorsCache = CtConstructor.append(constructorsCache, c);
  545. getClassFile2().addMethod(c.getMethodInfo2());
  546. }
  547. public void addMethod(CtMethod m) throws CannotCompileException {
  548. checkModify();
  549. if (m.getDeclaringClass() != this)
  550. throw new CannotCompileException("cannot add");
  551. getMethodsCache();
  552. methodsCache = CtMethod.append(methodsCache, m);
  553. getClassFile2().addMethod(m.getMethodInfo2());
  554. if ((m.getModifiers() & Modifier.ABSTRACT) != 0)
  555. setModifiers(getModifiers() | Modifier.ABSTRACT);
  556. }
  557. public byte[] getAttribute(String name) {
  558. AttributeInfo ai = getClassFile2().getAttribute(name);
  559. if (ai == null)
  560. return null;
  561. else
  562. return ai.get();
  563. }
  564. public void setAttribute(String name, byte[] data) {
  565. checkModify();
  566. ClassFile cf = getClassFile2();
  567. cf.addAttribute(new AttributeInfo(cf.getConstPool(), name, data));
  568. }
  569. public void instrument(CodeConverter converter)
  570. throws CannotCompileException
  571. {
  572. checkModify();
  573. ClassFile cf = getClassFile2();
  574. ConstPool cp = cf.getConstPool();
  575. List list = cf.getMethods();
  576. int n = list.size();
  577. for (int i = 0; i < n; ++i) {
  578. MethodInfo minfo = (MethodInfo)list.get(i);
  579. converter.doit(this, minfo, cp);
  580. }
  581. }
  582. public void instrument(ExprEditor editor)
  583. throws CannotCompileException
  584. {
  585. checkModify();
  586. ClassFile cf = getClassFile2();
  587. List list = cf.getMethods();
  588. int n = list.size();
  589. for (int i = 0; i < n; ++i) {
  590. MethodInfo minfo = (MethodInfo)list.get(i);
  591. editor.doit(this, minfo);
  592. }
  593. }
  594. void toBytecode(DataOutputStream out)
  595. throws CannotCompileException, IOException
  596. {
  597. ClassFile cf = getClassFile2();
  598. try {
  599. modifyClassConstructor(cf);
  600. modifyConstructors(cf);
  601. }
  602. catch (NotFoundException e) {
  603. throw new CannotCompileException(e);
  604. }
  605. wasFrozen = true;
  606. try {
  607. cf.write(out);
  608. out.flush();
  609. }
  610. catch (IOException e) {
  611. throw new CannotCompileException(e);
  612. }
  613. }
  614. protected void modifyClassConstructor(ClassFile cf)
  615. throws CannotCompileException, NotFoundException
  616. {
  617. Bytecode code = new Bytecode(cf.getConstPool(), 0, 0);
  618. Javac jv = new Javac(code, this);
  619. int stacksize = 0;
  620. boolean none = true;
  621. for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
  622. CtField f = fi.field;
  623. if (Modifier.isStatic(f.getModifiers())) {
  624. none = false;
  625. int s = fi.init.compileIfStatic(f.getType(), f.getName(),
  626. code, jv);
  627. if (stacksize < s)
  628. stacksize = s;
  629. }
  630. }
  631. if (none)
  632. return; // no initializer for static fileds.
  633. MethodInfo m = cf.getStaticInitializer();
  634. if (m == null) {
  635. code.add(Bytecode.RETURN);
  636. code.setMaxStack(stacksize);
  637. m = new MethodInfo(cf.getConstPool(),
  638. "<clinit>", "()V");
  639. m.setAccessFlags(AccessFlag.STATIC);
  640. m.setCodeAttribute(code.toCodeAttribute());
  641. cf.addMethod(m);
  642. }
  643. else {
  644. CodeAttribute codeAttr = m.getCodeAttribute();
  645. if (codeAttr == null)
  646. throw new CannotCompileException("empty <clinit>");
  647. try {
  648. CodeIterator it = codeAttr.iterator();
  649. int pos = it.insertEx(code.get());
  650. it.insert(code.getExceptionTable(), pos);
  651. int maxstack = codeAttr.getMaxStack();
  652. if (maxstack < stacksize)
  653. codeAttr.setMaxStack(stacksize);
  654. }
  655. catch (BadBytecode e) {
  656. throw new CannotCompileException(e);
  657. }
  658. }
  659. }
  660. protected void modifyConstructors(ClassFile cf)
  661. throws CannotCompileException, NotFoundException
  662. {
  663. if (fieldInitializers == null)
  664. return;
  665. ConstPool cp = cf.getConstPool();
  666. List list = cf.getMethods();
  667. int n = list.size();
  668. for (int i = 0; i < n; ++i) {
  669. MethodInfo minfo = (MethodInfo)list.get(i);
  670. if (minfo.isConstructor()) {
  671. CodeAttribute codeAttr = minfo.getCodeAttribute();
  672. if (codeAttr != null)
  673. try {
  674. Bytecode init = new Bytecode(cp, 0,
  675. codeAttr.getMaxLocals());
  676. CtClass[] params
  677. = Descriptor.getParameterTypes(
  678. minfo.getDescriptor(),
  679. classPool);
  680. int stacksize = makeFieldInitializer(init, params);
  681. insertAuxInitializer(codeAttr, init, stacksize);
  682. }
  683. catch (BadBytecode e) {
  684. throw new CannotCompileException(e);
  685. }
  686. }
  687. }
  688. }
  689. private static void insertAuxInitializer(CodeAttribute codeAttr,
  690. Bytecode initializer,
  691. int stacksize)
  692. throws BadBytecode
  693. {
  694. CodeIterator it = codeAttr.iterator();
  695. int index = it.skipSuperConstructor();
  696. if (index < 0) {
  697. index = it.skipThisConstructor();
  698. if (index >= 0)
  699. return; // this() is called.
  700. // Neither this() or super() is called.
  701. }
  702. int pos = it.insertEx(initializer.get());
  703. it.insert(initializer.getExceptionTable(), pos);
  704. int maxstack = codeAttr.getMaxStack();
  705. if (maxstack < stacksize)
  706. codeAttr.setMaxStack(stacksize);
  707. }
  708. private int makeFieldInitializer(Bytecode code, CtClass[] parameters)
  709. throws CannotCompileException, NotFoundException
  710. {
  711. int stacksize = 0;
  712. Javac jv = new Javac(code, this);
  713. try {
  714. jv.recordParams(parameters, false);
  715. }
  716. catch (CompileError e) {
  717. throw new CannotCompileException(e);
  718. }
  719. for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
  720. CtField f = fi.field;
  721. if (!Modifier.isStatic(f.getModifiers())) {
  722. int s = fi.init.compile(f.getType(), f.getName(), code,
  723. parameters, jv);
  724. if (stacksize < s)
  725. stacksize = s;
  726. }
  727. }
  728. return stacksize;
  729. }
  730. // Methods used by CtNewWrappedMethod
  731. Hashtable getHiddenMethods() {
  732. if (hiddenMethods == null)
  733. hiddenMethods = new Hashtable();
  734. return hiddenMethods;
  735. }
  736. int getUniqueNumber() { return uniqueNumberSeed++; }
  737. }
  738. class FieldInitLink {
  739. FieldInitLink next;
  740. CtField field;
  741. CtField.Initializer init;
  742. FieldInitLink(CtField f, CtField.Initializer i) {
  743. next = null;
  744. field = f;
  745. init = i;
  746. }
  747. }