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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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. MethodInfo minfo = getClassFile2().getStaticInitializer();
  380. if (minfo != null)
  381. classInitializerCache = new CtConstructor(minfo, this);
  382. }
  383. return classInitializerCache;
  384. }
  385. public CtMethod[] getMethods() {
  386. HashMap h = new HashMap();
  387. getMethods0(h, this);
  388. return (CtMethod[])h.values().toArray(new CtMethod[0]);
  389. }
  390. private static void getMethods0(HashMap h, CtClass cc) {
  391. try {
  392. CtClass[] ifs = cc.getInterfaces();
  393. int size = ifs.length;
  394. for (int i = 0; i < size; ++i)
  395. getMethods0(h, ifs[i]);
  396. }
  397. catch (NotFoundException e) {}
  398. try {
  399. CtClass s = cc.getSuperclass();
  400. if (s != null)
  401. getMethods0(h, s);
  402. }
  403. catch (NotFoundException e) {}
  404. if (cc instanceof CtClassType) {
  405. CtMethod cm = ((CtClassType)cc).getMethodsCache();
  406. while (cm != null) {
  407. if (Modifier.isPublic(cm.getModifiers()))
  408. h.put(cm, cm);
  409. cm = cm.next;
  410. }
  411. }
  412. }
  413. public CtMethod getMethod(String name, String desc)
  414. throws NotFoundException
  415. {
  416. CtMethod m = getMethod0(this, name, desc);
  417. if (m != null)
  418. return m;
  419. else
  420. throw new NotFoundException(name + "(..) is not found in "
  421. + getName());
  422. }
  423. private static CtMethod getMethod0(CtClass cc,
  424. String name, String desc) {
  425. if (cc instanceof CtClassType) {
  426. CtMethod cm = ((CtClassType)cc).getMethodsCache();
  427. while (cm != null) {
  428. if (cm.getName().equals(name)
  429. && cm.getMethodInfo2().getDescriptor().equals(desc))
  430. return cm;
  431. cm = cm.next;
  432. }
  433. }
  434. try {
  435. CtClass s = cc.getSuperclass();
  436. if (s != null) {
  437. CtMethod m = getMethod0(s, name, desc);
  438. if (m != null)
  439. return m;
  440. }
  441. }
  442. catch (NotFoundException e) {}
  443. try {
  444. CtClass[] ifs = cc.getInterfaces();
  445. int size = ifs.length;
  446. for (int i = 0; i < size; ++i) {
  447. CtMethod m = getMethod0(ifs[i], name, desc);
  448. if (m != null)
  449. return m;
  450. }
  451. }
  452. catch (NotFoundException e) {}
  453. return null;
  454. }
  455. public CtMethod[] getDeclaredMethods() {
  456. CtMethod cm = getMethodsCache();
  457. int num = CtMethod.count(cm);
  458. CtMethod[] cms = new CtMethod[num];
  459. int i = 0;
  460. while (cm != null) {
  461. cms[i++] = cm;
  462. cm = cm.next;
  463. }
  464. return cms;
  465. }
  466. public CtMethod getDeclaredMethod(String name) throws NotFoundException {
  467. CtMethod m = getMethodsCache();
  468. while (m != null) {
  469. if (m.getName().equals(name))
  470. return m;
  471. m = m.next;
  472. }
  473. throw new NotFoundException(name + "(..) is not found in "
  474. + getName());
  475. }
  476. public CtMethod getDeclaredMethod(String name, CtClass[] params)
  477. throws NotFoundException
  478. {
  479. String desc = Descriptor.ofParameters(params);
  480. CtMethod m = getMethodsCache();
  481. while (m != null) {
  482. if (m.getName().equals(name)
  483. && m.getMethodInfo2().getDescriptor().startsWith(desc))
  484. return m;
  485. m = m.next;
  486. }
  487. throw new NotFoundException(name + "(..) is not found in "
  488. + getName());
  489. }
  490. protected CtMethod getMethodsCache() {
  491. if (methodsCache == null) {
  492. List list = getClassFile2().getMethods();
  493. int n = list.size();
  494. for (int i = 0; i < n; ++i) {
  495. MethodInfo minfo = (MethodInfo)list.get(i);
  496. if (minfo.isMethod())
  497. methodsCache = CtMethod.append(methodsCache,
  498. new CtMethod(minfo, this));
  499. }
  500. }
  501. return methodsCache;
  502. }
  503. public void addField(CtField f, String init)
  504. throws CannotCompileException
  505. {
  506. addField(f, CtField.Initializer.byExpr(init));
  507. }
  508. public void addField(CtField f, CtField.Initializer init)
  509. throws CannotCompileException
  510. {
  511. checkModify();
  512. if (f.getDeclaringClass() != this)
  513. throw new CannotCompileException("cannot add");
  514. if (init == null)
  515. init = f.getInit();
  516. getFieldsCache();
  517. fieldsCache = CtField.append(fieldsCache, f);
  518. getClassFile2().addField(f.getFieldInfo2());
  519. if (init != null) {
  520. FieldInitLink fil = new FieldInitLink(f, init);
  521. FieldInitLink link = fieldInitializers;
  522. if (link == null)
  523. fieldInitializers = fil;
  524. else {
  525. while (link.next != null)
  526. link = link.next;
  527. link.next = fil;
  528. }
  529. }
  530. }
  531. public CtConstructor makeClassInitializer()
  532. throws CannotCompileException
  533. {
  534. CtConstructor clinit = getClassInitializer();
  535. if (clinit != null)
  536. return clinit;
  537. checkModify();
  538. ClassFile cf = getClassFile2();
  539. Bytecode code = new Bytecode(cf.getConstPool(), 0, 0);
  540. modifyClassConstructor(cf, code, 0, 0);
  541. return getClassInitializer();
  542. }
  543. public void addConstructor(CtConstructor c)
  544. throws CannotCompileException
  545. {
  546. checkModify();
  547. if (c.getDeclaringClass() != this)
  548. throw new CannotCompileException("cannot add");
  549. getConstructorsCache();
  550. constructorsCache = CtConstructor.append(constructorsCache, c);
  551. getClassFile2().addMethod(c.getMethodInfo2());
  552. }
  553. public void addMethod(CtMethod m) throws CannotCompileException {
  554. checkModify();
  555. if (m.getDeclaringClass() != this)
  556. throw new CannotCompileException("cannot add");
  557. getMethodsCache();
  558. methodsCache = CtMethod.append(methodsCache, m);
  559. getClassFile2().addMethod(m.getMethodInfo2());
  560. if ((m.getModifiers() & Modifier.ABSTRACT) != 0)
  561. setModifiers(getModifiers() | Modifier.ABSTRACT);
  562. }
  563. public byte[] getAttribute(String name) {
  564. AttributeInfo ai = getClassFile2().getAttribute(name);
  565. if (ai == null)
  566. return null;
  567. else
  568. return ai.get();
  569. }
  570. public void setAttribute(String name, byte[] data) {
  571. checkModify();
  572. ClassFile cf = getClassFile2();
  573. cf.addAttribute(new AttributeInfo(cf.getConstPool(), name, data));
  574. }
  575. public void instrument(CodeConverter converter)
  576. throws CannotCompileException
  577. {
  578. checkModify();
  579. ClassFile cf = getClassFile2();
  580. ConstPool cp = cf.getConstPool();
  581. List list = cf.getMethods();
  582. int n = list.size();
  583. for (int i = 0; i < n; ++i) {
  584. MethodInfo minfo = (MethodInfo)list.get(i);
  585. converter.doit(this, minfo, cp);
  586. }
  587. }
  588. public void instrument(ExprEditor editor)
  589. throws CannotCompileException
  590. {
  591. checkModify();
  592. ClassFile cf = getClassFile2();
  593. List list = cf.getMethods();
  594. int n = list.size();
  595. for (int i = 0; i < n; ++i) {
  596. MethodInfo minfo = (MethodInfo)list.get(i);
  597. editor.doit(this, minfo);
  598. }
  599. }
  600. void toBytecode(DataOutputStream out)
  601. throws CannotCompileException, IOException
  602. {
  603. ClassFile cf = getClassFile2();
  604. try {
  605. modifyClassConstructor(cf);
  606. modifyConstructors(cf);
  607. }
  608. catch (NotFoundException e) {
  609. throw new CannotCompileException(e);
  610. }
  611. wasFrozen = true;
  612. try {
  613. cf.write(out);
  614. out.flush();
  615. }
  616. catch (IOException e) {
  617. throw new CannotCompileException(e);
  618. }
  619. }
  620. protected void modifyClassConstructor(ClassFile cf)
  621. throws CannotCompileException, NotFoundException
  622. {
  623. Bytecode code = new Bytecode(cf.getConstPool(), 0, 0);
  624. Javac jv = new Javac(code, this);
  625. int stacksize = 0;
  626. boolean doInit = false;
  627. for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
  628. CtField f = fi.field;
  629. if (Modifier.isStatic(f.getModifiers())) {
  630. doInit = true;
  631. int s = fi.init.compileIfStatic(f.getType(), f.getName(),
  632. code, jv);
  633. if (stacksize < s)
  634. stacksize = s;
  635. }
  636. }
  637. if (doInit) // need an initializer for static fileds.
  638. modifyClassConstructor(cf, code, stacksize, 0);
  639. }
  640. private void modifyClassConstructor(ClassFile cf, Bytecode code,
  641. int stacksize, int localsize)
  642. throws CannotCompileException
  643. {
  644. MethodInfo m = cf.getStaticInitializer();
  645. if (m == null) {
  646. code.add(Bytecode.RETURN);
  647. code.setMaxStack(stacksize);
  648. code.setMaxLocals(localsize);
  649. m = new MethodInfo(cf.getConstPool(), "<clinit>", "()V");
  650. m.setAccessFlags(AccessFlag.STATIC);
  651. m.setCodeAttribute(code.toCodeAttribute());
  652. cf.addMethod(m);
  653. }
  654. else {
  655. CodeAttribute codeAttr = m.getCodeAttribute();
  656. if (codeAttr == null)
  657. throw new CannotCompileException("empty <clinit>");
  658. try {
  659. CodeIterator it = codeAttr.iterator();
  660. int pos = it.insertEx(code.get());
  661. it.insert(code.getExceptionTable(), pos);
  662. int maxstack = codeAttr.getMaxStack();
  663. if (maxstack < stacksize)
  664. codeAttr.setMaxStack(stacksize);
  665. int maxlocals = codeAttr.getMaxLocals();
  666. if (maxlocals < localsize)
  667. codeAttr.setMaxLocals(localsize);
  668. }
  669. catch (BadBytecode e) {
  670. throw new CannotCompileException(e);
  671. }
  672. }
  673. }
  674. protected void modifyConstructors(ClassFile cf)
  675. throws CannotCompileException, NotFoundException
  676. {
  677. if (fieldInitializers == null)
  678. return;
  679. ConstPool cp = cf.getConstPool();
  680. List list = cf.getMethods();
  681. int n = list.size();
  682. for (int i = 0; i < n; ++i) {
  683. MethodInfo minfo = (MethodInfo)list.get(i);
  684. if (minfo.isConstructor()) {
  685. CodeAttribute codeAttr = minfo.getCodeAttribute();
  686. if (codeAttr != null)
  687. try {
  688. Bytecode init = new Bytecode(cp, 0,
  689. codeAttr.getMaxLocals());
  690. CtClass[] params
  691. = Descriptor.getParameterTypes(
  692. minfo.getDescriptor(),
  693. classPool);
  694. int stacksize = makeFieldInitializer(init, params);
  695. insertAuxInitializer(codeAttr, init, stacksize);
  696. }
  697. catch (BadBytecode e) {
  698. throw new CannotCompileException(e);
  699. }
  700. }
  701. }
  702. }
  703. private static void insertAuxInitializer(CodeAttribute codeAttr,
  704. Bytecode initializer,
  705. int stacksize)
  706. throws BadBytecode
  707. {
  708. CodeIterator it = codeAttr.iterator();
  709. int index = it.skipSuperConstructor();
  710. if (index < 0) {
  711. index = it.skipThisConstructor();
  712. if (index >= 0)
  713. return; // this() is called.
  714. // Neither this() or super() is called.
  715. }
  716. int pos = it.insertEx(initializer.get());
  717. it.insert(initializer.getExceptionTable(), pos);
  718. int maxstack = codeAttr.getMaxStack();
  719. if (maxstack < stacksize)
  720. codeAttr.setMaxStack(stacksize);
  721. }
  722. private int makeFieldInitializer(Bytecode code, CtClass[] parameters)
  723. throws CannotCompileException, NotFoundException
  724. {
  725. int stacksize = 0;
  726. Javac jv = new Javac(code, this);
  727. try {
  728. jv.recordParams(parameters, false);
  729. }
  730. catch (CompileError e) {
  731. throw new CannotCompileException(e);
  732. }
  733. for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
  734. CtField f = fi.field;
  735. if (!Modifier.isStatic(f.getModifiers())) {
  736. int s = fi.init.compile(f.getType(), f.getName(), code,
  737. parameters, jv);
  738. if (stacksize < s)
  739. stacksize = s;
  740. }
  741. }
  742. return stacksize;
  743. }
  744. // Methods used by CtNewWrappedMethod
  745. Hashtable getHiddenMethods() {
  746. if (hiddenMethods == null)
  747. hiddenMethods = new Hashtable();
  748. return hiddenMethods;
  749. }
  750. int getUniqueNumber() { return uniqueNumberSeed++; }
  751. }
  752. class FieldInitLink {
  753. FieldInitLink next;
  754. CtField field;
  755. CtField.Initializer init;
  756. FieldInitLink(CtField f, CtField.Initializer i) {
  757. next = null;
  758. field = f;
  759. init = i;
  760. }
  761. }