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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2007 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.lang.ref.WeakReference;
  17. import java.io.BufferedInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.DataInputStream;
  21. import java.io.DataOutputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.net.URL;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.Hashtable;
  28. import java.util.List;
  29. import java.util.Set;
  30. import javassist.bytecode.AccessFlag;
  31. import javassist.bytecode.AttributeInfo;
  32. import javassist.bytecode.AnnotationsAttribute;
  33. import javassist.bytecode.BadBytecode;
  34. import javassist.bytecode.Bytecode;
  35. import javassist.bytecode.ClassFile;
  36. import javassist.bytecode.CodeAttribute;
  37. import javassist.bytecode.ConstantAttribute;
  38. import javassist.bytecode.CodeIterator;
  39. import javassist.bytecode.ConstPool;
  40. import javassist.bytecode.Descriptor;
  41. import javassist.bytecode.EnclosingMethodAttribute;
  42. import javassist.bytecode.FieldInfo;
  43. import javassist.bytecode.InnerClassesAttribute;
  44. import javassist.bytecode.MethodInfo;
  45. import javassist.bytecode.ParameterAnnotationsAttribute;
  46. import javassist.bytecode.annotation.Annotation;
  47. import javassist.compiler.AccessorMaker;
  48. import javassist.compiler.CompileError;
  49. import javassist.compiler.Javac;
  50. import javassist.expr.ExprEditor;
  51. /**
  52. * Class types.
  53. */
  54. class CtClassType extends CtClass {
  55. ClassPool classPool;
  56. boolean wasChanged;
  57. private boolean wasFrozen;
  58. boolean wasPruned;
  59. boolean gcConstPool; // if true, the constant pool entries will be garbage collected.
  60. ClassFile classfile;
  61. byte[] rawClassfile; // backup storage
  62. private WeakReference memberCache;
  63. private AccessorMaker accessors;
  64. private FieldInitLink fieldInitializers;
  65. private Hashtable hiddenMethods; // must be synchronous
  66. private int uniqueNumberSeed;
  67. private boolean doPruning = ClassPool.doPruning;
  68. private int getCount;
  69. private static final int GET_THRESHOLD = 2; // see compress()
  70. CtClassType(String name, ClassPool cp) {
  71. super(name);
  72. classPool = cp;
  73. wasChanged = wasFrozen = wasPruned = gcConstPool = false;
  74. classfile = null;
  75. rawClassfile = null;
  76. memberCache = null;
  77. accessors = null;
  78. fieldInitializers = null;
  79. hiddenMethods = null;
  80. uniqueNumberSeed = 0;
  81. getCount = 0;
  82. }
  83. CtClassType(InputStream ins, ClassPool cp) throws IOException {
  84. this((String)null, cp);
  85. classfile = new ClassFile(new DataInputStream(ins));
  86. qualifiedName = classfile.getName();
  87. }
  88. protected void extendToString(StringBuffer buffer) {
  89. if (wasChanged)
  90. buffer.append("changed ");
  91. if (wasFrozen)
  92. buffer.append("frozen ");
  93. if (wasPruned)
  94. buffer.append("pruned ");
  95. buffer.append(Modifier.toString(getModifiers()));
  96. buffer.append(" class ");
  97. buffer.append(getName());
  98. try {
  99. CtClass ext = getSuperclass();
  100. if (ext != null) {
  101. String name = ext.getName();
  102. if (!name.equals("java.lang.Object"))
  103. buffer.append(" extends " + ext.getName());
  104. }
  105. }
  106. catch (NotFoundException e) {
  107. buffer.append(" extends ??");
  108. }
  109. try {
  110. CtClass[] intf = getInterfaces();
  111. if (intf.length > 0)
  112. buffer.append(" implements ");
  113. for (int i = 0; i < intf.length; ++i) {
  114. buffer.append(intf[i].getName());
  115. buffer.append(", ");
  116. }
  117. }
  118. catch (NotFoundException e) {
  119. buffer.append(" extends ??");
  120. }
  121. CtMember.Cache memCache = getMembers();
  122. exToString(buffer, " fields=",
  123. memCache.fieldHead(), memCache.lastField());
  124. exToString(buffer, " constructors=",
  125. memCache.consHead(), memCache.lastCons());
  126. exToString(buffer, " methods=",
  127. memCache.methodHead(), memCache.lastMethod());
  128. }
  129. private void exToString(StringBuffer buffer, String msg,
  130. CtMember head, CtMember tail) {
  131. buffer.append(msg);
  132. while (head != tail) {
  133. head = head.next();
  134. buffer.append(head);
  135. buffer.append(", ");
  136. }
  137. }
  138. public AccessorMaker getAccessorMaker() {
  139. if (accessors == null)
  140. accessors = new AccessorMaker(this);
  141. return accessors;
  142. }
  143. public ClassFile getClassFile2() {
  144. ClassFile cfile = classfile;
  145. if (cfile != null)
  146. return cfile;
  147. classPool.compress();
  148. if (rawClassfile != null) {
  149. try {
  150. classfile = new ClassFile(new DataInputStream(
  151. new ByteArrayInputStream(rawClassfile)));
  152. rawClassfile = null;
  153. getCount = GET_THRESHOLD;
  154. return classfile;
  155. }
  156. catch (IOException e) {
  157. throw new RuntimeException(e.toString(), e);
  158. }
  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 cf = new ClassFile(new DataInputStream(fin));
  167. if (!cf.getName().equals(qualifiedName))
  168. throw new RuntimeException("cannot find " + qualifiedName + ": "
  169. + cf.getName() + " found in "
  170. + qualifiedName.replace('.', '/') + ".class");
  171. classfile = cf;
  172. return cf;
  173. }
  174. catch (NotFoundException e) {
  175. throw new RuntimeException(e.toString(), e);
  176. }
  177. catch (IOException e) {
  178. throw new RuntimeException(e.toString(), e);
  179. }
  180. finally {
  181. if (fin != null)
  182. try {
  183. fin.close();
  184. }
  185. catch (IOException e) {}
  186. }
  187. }
  188. /* Inherited from CtClass. Called by get() in ClassPool.
  189. *
  190. * @see javassist.CtClass#incGetCounter()
  191. * @see #toBytecode(DataOutputStream)
  192. */
  193. final void incGetCounter() { ++getCount; }
  194. /**
  195. * Invoked from ClassPool#compress().
  196. * It releases the class files that have not been recently used
  197. * if they are unmodified.
  198. */
  199. void compress() {
  200. if (getCount < GET_THRESHOLD)
  201. if (!isModified() && ClassPool.releaseUnmodifiedClassFile)
  202. removeClassFile();
  203. else if (isFrozen() && !wasPruned)
  204. saveClassFile();
  205. getCount = 0;
  206. }
  207. /**
  208. * Converts a ClassFile object into a byte array
  209. * for saving memory space.
  210. */
  211. private synchronized void saveClassFile() {
  212. /* getMembers() and releaseClassFile() are also synchronized.
  213. */
  214. if (classfile == null || hasMemberCache() != null)
  215. return;
  216. ByteArrayOutputStream barray = new ByteArrayOutputStream();
  217. DataOutputStream out = new DataOutputStream(barray);
  218. try {
  219. classfile.write(out);
  220. barray.close();
  221. rawClassfile = barray.toByteArray();
  222. classfile = null;
  223. }
  224. catch (IOException e) {}
  225. }
  226. private synchronized void removeClassFile() {
  227. if (classfile != null && !isModified() && hasMemberCache() == null)
  228. classfile = null;
  229. }
  230. public ClassPool getClassPool() { return classPool; }
  231. void setClassPool(ClassPool cp) { classPool = cp; }
  232. public URL getURL() throws NotFoundException {
  233. URL url = classPool.find(getName());
  234. if (url == null)
  235. throw new NotFoundException(getName());
  236. else
  237. return url;
  238. }
  239. public boolean isModified() { return wasChanged; }
  240. public boolean isFrozen() { return wasFrozen; }
  241. public void freeze() { wasFrozen = true; }
  242. void checkModify() throws RuntimeException {
  243. if (isFrozen()) {
  244. String msg = getName() + " class is frozen";
  245. if (wasPruned)
  246. msg += " and pruned";
  247. throw new RuntimeException(msg);
  248. }
  249. wasChanged = true;
  250. }
  251. public void defrost() {
  252. checkPruned("defrost");
  253. wasFrozen = false;
  254. }
  255. public boolean subtypeOf(CtClass clazz) throws NotFoundException {
  256. int i;
  257. String cname = clazz.getName();
  258. if (this == clazz || getName().equals(cname))
  259. return true;
  260. ClassFile file = getClassFile2();
  261. String supername = file.getSuperclass();
  262. if (supername != null && supername.equals(cname))
  263. return true;
  264. String[] ifs = file.getInterfaces();
  265. int num = ifs.length;
  266. for (i = 0; i < num; ++i)
  267. if (ifs[i].equals(cname))
  268. return true;
  269. if (supername != null && classPool.get(supername).subtypeOf(clazz))
  270. return true;
  271. for (i = 0; i < num; ++i)
  272. if (classPool.get(ifs[i]).subtypeOf(clazz))
  273. return true;
  274. return false;
  275. }
  276. public void setName(String name) throws RuntimeException {
  277. String oldname = getName();
  278. if (name.equals(oldname))
  279. return;
  280. // check this in advance although classNameChanged() below does.
  281. classPool.checkNotFrozen(name);
  282. ClassFile cf = getClassFile2();
  283. super.setName(name);
  284. cf.setName(name);
  285. nameReplaced();
  286. classPool.classNameChanged(oldname, this);
  287. }
  288. public void replaceClassName(ClassMap classnames)
  289. throws RuntimeException
  290. {
  291. String oldClassName = getName();
  292. String newClassName
  293. = (String)classnames.get(Descriptor.toJvmName(oldClassName));
  294. if (newClassName != null) {
  295. newClassName = Descriptor.toJavaName(newClassName);
  296. // check this in advance although classNameChanged() below does.
  297. classPool.checkNotFrozen(newClassName);
  298. }
  299. super.replaceClassName(classnames);
  300. ClassFile cf = getClassFile2();
  301. cf.renameClass(classnames);
  302. nameReplaced();
  303. if (newClassName != null) {
  304. super.setName(newClassName);
  305. classPool.classNameChanged(oldClassName, this);
  306. }
  307. }
  308. public void replaceClassName(String oldname, String newname)
  309. throws RuntimeException
  310. {
  311. String thisname = getName();
  312. if (thisname.equals(oldname))
  313. setName(newname);
  314. else {
  315. super.replaceClassName(oldname, newname);
  316. getClassFile2().renameClass(oldname, newname);
  317. nameReplaced();
  318. }
  319. }
  320. public boolean isInterface() {
  321. return Modifier.isInterface(getModifiers());
  322. }
  323. public boolean isAnnotation() {
  324. return Modifier.isAnnotation(getModifiers());
  325. }
  326. public boolean isEnum() {
  327. return Modifier.isEnum(getModifiers());
  328. }
  329. public int getModifiers() {
  330. ClassFile cf = getClassFile2();
  331. int acc = cf.getAccessFlags();
  332. acc = AccessFlag.clear(acc, AccessFlag.SUPER);
  333. int inner = cf.getInnerAccessFlags();
  334. if (inner != -1 && (inner & AccessFlag.STATIC) != 0)
  335. acc |= AccessFlag.STATIC;
  336. return AccessFlag.toModifier(acc);
  337. }
  338. public CtClass[] getNestedClasses() throws NotFoundException {
  339. ClassFile cf = getClassFile2();
  340. InnerClassesAttribute ica
  341. = (InnerClassesAttribute)cf.getAttribute(InnerClassesAttribute.tag);
  342. if (ica == null)
  343. return new CtClass[0];
  344. String thisName = cf.getName();
  345. int n = ica.tableLength();
  346. ArrayList list = new ArrayList(n);
  347. for (int i = 0; i < n; i++) {
  348. String outer = ica.outerClass(i);
  349. /*
  350. * If a nested class is local or anonymous,
  351. * the outer_class_info_index is 0.
  352. */
  353. if (outer == null || outer.equals(thisName)) {
  354. String inner = ica.innerClass(i);
  355. if (inner != null)
  356. list.add(classPool.get(inner));
  357. }
  358. }
  359. return (CtClass[])list.toArray(new CtClass[list.size()]);
  360. }
  361. public void setModifiers(int mod) {
  362. ClassFile cf = getClassFile2();
  363. if (Modifier.isStatic(mod)) {
  364. int flags = cf.getInnerAccessFlags();
  365. if (flags != -1 && (flags & AccessFlag.STATIC) != 0)
  366. mod = mod & ~Modifier.STATIC;
  367. else
  368. throw new RuntimeException("cannot change " + getName() + " into a static class");
  369. }
  370. checkModify();
  371. cf.setAccessFlags(AccessFlag.of(mod));
  372. }
  373. public Object[] getAnnotations() throws ClassNotFoundException {
  374. return getAnnotations(false);
  375. }
  376. public Object[] getAvailableAnnotations(){
  377. try {
  378. return getAnnotations(true);
  379. }
  380. catch (ClassNotFoundException e) {
  381. throw new RuntimeException("Unexpected exception ", e);
  382. }
  383. }
  384. private Object[] getAnnotations(boolean ignoreNotFound)
  385. throws ClassNotFoundException
  386. {
  387. ClassFile cf = getClassFile2();
  388. AnnotationsAttribute ainfo = (AnnotationsAttribute)
  389. cf.getAttribute(AnnotationsAttribute.invisibleTag);
  390. AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
  391. cf.getAttribute(AnnotationsAttribute.visibleTag);
  392. return toAnnotationType(ignoreNotFound, getClassPool(), ainfo, ainfo2);
  393. }
  394. static Object[] toAnnotationType(boolean ignoreNotFound, ClassPool cp,
  395. AnnotationsAttribute a1, AnnotationsAttribute a2)
  396. throws ClassNotFoundException
  397. {
  398. Annotation[] anno1, anno2;
  399. int size1, size2;
  400. if (a1 == null) {
  401. anno1 = null;
  402. size1 = 0;
  403. }
  404. else {
  405. anno1 = a1.getAnnotations();
  406. size1 = anno1.length;
  407. }
  408. if (a2 == null) {
  409. anno2 = null;
  410. size2 = 0;
  411. }
  412. else {
  413. anno2 = a2.getAnnotations();
  414. size2 = anno2.length;
  415. }
  416. if (!ignoreNotFound){
  417. Object[] result = new Object[size1 + size2];
  418. for (int i = 0; i < size1; i++)
  419. result[i] = toAnnoType(anno1[i], cp);
  420. for (int j = 0; j < size2; j++)
  421. result[j + size1] = toAnnoType(anno2[j], cp);
  422. return result;
  423. }
  424. else{
  425. ArrayList annotations = new ArrayList();
  426. for (int i = 0 ; i < size1 ; i++){
  427. try{
  428. annotations.add(toAnnoType(anno1[i], cp));
  429. }
  430. catch(ClassNotFoundException e){}
  431. }
  432. for (int j = 0; j < size2; j++) {
  433. try{
  434. annotations.add(toAnnoType(anno2[j], cp));
  435. }
  436. catch(ClassNotFoundException e){}
  437. }
  438. return annotations.toArray();
  439. }
  440. }
  441. static Object[][] toAnnotationType(boolean ignoreNotFound, ClassPool cp,
  442. ParameterAnnotationsAttribute a1,
  443. ParameterAnnotationsAttribute a2,
  444. MethodInfo minfo)
  445. throws ClassNotFoundException
  446. {
  447. int numParameters = 0;
  448. if (a1 != null)
  449. numParameters = a1.numParameters();
  450. else if (a2 != null)
  451. numParameters = a2.numParameters();
  452. else
  453. numParameters = Descriptor.numOfParameters(minfo.getDescriptor());
  454. Object[][] result = new Object[numParameters][];
  455. for (int i = 0; i < numParameters; i++) {
  456. Annotation[] anno1, anno2;
  457. int size1, size2;
  458. if (a1 == null) {
  459. anno1 = null;
  460. size1 = 0;
  461. }
  462. else {
  463. anno1 = a1.getAnnotations()[i];
  464. size1 = anno1.length;
  465. }
  466. if (a2 == null) {
  467. anno2 = null;
  468. size2 = 0;
  469. }
  470. else {
  471. anno2 = a2.getAnnotations()[i];
  472. size2 = anno2.length;
  473. }
  474. if (!ignoreNotFound){
  475. result[i] = new Object[size1 + size2];
  476. for (int j = 0; j < size1; ++j)
  477. result[i][j] = toAnnoType(anno1[j], cp);
  478. for (int j = 0; j < size2; ++j)
  479. result[i][j + size1] = toAnnoType(anno2[j], cp);
  480. }
  481. else{
  482. ArrayList annotations = new ArrayList();
  483. for (int j = 0 ; j < size1 ; j++){
  484. try{
  485. annotations.add(toAnnoType(anno1[j], cp));
  486. }
  487. catch(ClassNotFoundException e){}
  488. }
  489. for (int j = 0; j < size2; j++){
  490. try{
  491. annotations.add(toAnnoType(anno2[j], cp));
  492. }
  493. catch(ClassNotFoundException e){}
  494. }
  495. result[i] = annotations.toArray();
  496. }
  497. }
  498. return result;
  499. }
  500. private static Object toAnnoType(Annotation anno, ClassPool cp)
  501. throws ClassNotFoundException
  502. {
  503. try {
  504. ClassLoader cl = cp.getClassLoader();
  505. return anno.toAnnotationType(cl, cp);
  506. }
  507. catch (ClassNotFoundException e) {
  508. ClassLoader cl2 = cp.getClass().getClassLoader();
  509. return anno.toAnnotationType(cl2, cp);
  510. }
  511. }
  512. public boolean subclassOf(CtClass superclass) {
  513. if (superclass == null)
  514. return false;
  515. String superName = superclass.getName();
  516. CtClass curr = this;
  517. try {
  518. while (curr != null) {
  519. if (curr.getName().equals(superName))
  520. return true;
  521. curr = curr.getSuperclass();
  522. }
  523. }
  524. catch (Exception ignored) {}
  525. return false;
  526. }
  527. public CtClass getSuperclass() throws NotFoundException {
  528. String supername = getClassFile2().getSuperclass();
  529. if (supername == null)
  530. return null;
  531. else
  532. return classPool.get(supername);
  533. }
  534. public void setSuperclass(CtClass clazz) throws CannotCompileException {
  535. checkModify();
  536. if (isInterface())
  537. addInterface(clazz);
  538. else
  539. getClassFile2().setSuperclass(clazz.getName());
  540. }
  541. public CtClass[] getInterfaces() throws NotFoundException {
  542. String[] ifs = getClassFile2().getInterfaces();
  543. int num = ifs.length;
  544. CtClass[] ifc = new CtClass[num];
  545. for (int i = 0; i < num; ++i)
  546. ifc[i] = classPool.get(ifs[i]);
  547. return ifc;
  548. }
  549. public void setInterfaces(CtClass[] list) {
  550. checkModify();
  551. String[] ifs;
  552. if (list == null)
  553. ifs = new String[0];
  554. else {
  555. int num = list.length;
  556. ifs = new String[num];
  557. for (int i = 0; i < num; ++i)
  558. ifs[i] = list[i].getName();
  559. }
  560. getClassFile2().setInterfaces(ifs);
  561. }
  562. public void addInterface(CtClass anInterface) {
  563. checkModify();
  564. if (anInterface != null)
  565. getClassFile2().addInterface(anInterface.getName());
  566. }
  567. public CtClass getDeclaringClass() throws NotFoundException {
  568. ClassFile cf = getClassFile2();
  569. InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(
  570. InnerClassesAttribute.tag);
  571. if (ica == null)
  572. return null;
  573. String name = getName();
  574. int n = ica.tableLength();
  575. for (int i = 0; i < n; ++i)
  576. if (name.equals(ica.innerClass(i))) {
  577. String outName = ica.outerClass(i);
  578. if (outName != null)
  579. return classPool.get(outName);
  580. else {
  581. // maybe anonymous or local class.
  582. EnclosingMethodAttribute ema
  583. = (EnclosingMethodAttribute)cf.getAttribute(
  584. EnclosingMethodAttribute.tag);
  585. if (ema != null)
  586. return classPool.get(ema.className());
  587. }
  588. }
  589. return null;
  590. }
  591. public CtMethod getEnclosingMethod() throws NotFoundException {
  592. ClassFile cf = getClassFile2();
  593. EnclosingMethodAttribute ema
  594. = (EnclosingMethodAttribute)cf.getAttribute(
  595. EnclosingMethodAttribute.tag);
  596. if (ema != null) {
  597. CtClass enc = classPool.get(ema.className());
  598. return enc.getMethod(ema.methodName(), ema.methodDescriptor());
  599. }
  600. return null;
  601. }
  602. public CtClass makeNestedClass(String name, boolean isStatic) {
  603. if (!isStatic)
  604. throw new RuntimeException(
  605. "sorry, only nested static class is supported");
  606. checkModify();
  607. CtClass c = classPool.makeNestedClass(getName() + "$" + name);
  608. ClassFile cf = getClassFile2();
  609. ClassFile cf2 = c.getClassFile2();
  610. InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(
  611. InnerClassesAttribute.tag);
  612. if (ica == null) {
  613. ica = new InnerClassesAttribute(cf.getConstPool());
  614. cf.addAttribute(ica);
  615. }
  616. ica.append(c.getName(), this.getName(), name,
  617. (cf2.getAccessFlags() & ~AccessFlag.SUPER) | AccessFlag.STATIC);
  618. cf2.addAttribute(ica.copy(cf2.getConstPool(), null));
  619. return c;
  620. }
  621. /* flush cached names.
  622. */
  623. private void nameReplaced() {
  624. CtMember.Cache cache = hasMemberCache();
  625. if (cache != null) {
  626. CtMember mth = cache.methodHead();
  627. CtMember tail = cache.lastMethod();
  628. while (mth != tail) {
  629. mth = mth.next();
  630. mth.nameReplaced();
  631. }
  632. }
  633. }
  634. /**
  635. * Returns null if members are not cached.
  636. */
  637. protected CtMember.Cache hasMemberCache() {
  638. if (memberCache != null)
  639. return (CtMember.Cache)memberCache.get();
  640. else
  641. return null;
  642. }
  643. protected synchronized CtMember.Cache getMembers() {
  644. CtMember.Cache cache = null;
  645. if (memberCache == null
  646. || (cache = (CtMember.Cache)memberCache.get()) == null) {
  647. cache = new CtMember.Cache(this);
  648. makeFieldCache(cache);
  649. makeBehaviorCache(cache);
  650. memberCache = new WeakReference(cache);
  651. }
  652. return cache;
  653. }
  654. private void makeFieldCache(CtMember.Cache cache) {
  655. List list = getClassFile2().getFields();
  656. int n = list.size();
  657. for (int i = 0; i < n; ++i) {
  658. FieldInfo finfo = (FieldInfo)list.get(i);
  659. CtField newField = new CtField(finfo, this);
  660. cache.addField(newField);
  661. }
  662. }
  663. private void makeBehaviorCache(CtMember.Cache cache) {
  664. List list = getClassFile2().getMethods();
  665. int n = list.size();
  666. for (int i = 0; i < n; ++i) {
  667. MethodInfo minfo = (MethodInfo)list.get(i);
  668. if (minfo.isMethod()) {
  669. CtMethod newMethod = new CtMethod(minfo, this);
  670. cache.addMethod(newMethod);
  671. }
  672. else {
  673. CtConstructor newCons = new CtConstructor(minfo, this);
  674. cache.addConstructor(newCons);
  675. }
  676. }
  677. }
  678. public CtField[] getFields() {
  679. ArrayList alist = new ArrayList();
  680. getFields(alist, this);
  681. return (CtField[])alist.toArray(new CtField[alist.size()]);
  682. }
  683. private static void getFields(ArrayList alist, CtClass cc) {
  684. int i, num;
  685. if (cc == null)
  686. return;
  687. try {
  688. getFields(alist, cc.getSuperclass());
  689. }
  690. catch (NotFoundException e) {}
  691. try {
  692. CtClass[] ifs = cc.getInterfaces();
  693. num = ifs.length;
  694. for (i = 0; i < num; ++i)
  695. getFields(alist, ifs[i]);
  696. }
  697. catch (NotFoundException e) {}
  698. CtMember.Cache memCache = ((CtClassType)cc).getMembers();
  699. CtMember field = memCache.fieldHead();
  700. CtMember tail = memCache.lastField();
  701. while (field != tail) {
  702. field = field.next();
  703. if (!Modifier.isPrivate(field.getModifiers()))
  704. alist.add(field);
  705. }
  706. }
  707. public CtField getField(String name) throws NotFoundException {
  708. CtField f = getField2(name);
  709. if (f == null)
  710. throw new NotFoundException("field: " + name + " in " + getName());
  711. else
  712. return f;
  713. }
  714. CtField getField2(String name) {
  715. CtField df = getDeclaredField2(name);
  716. if (df != null)
  717. return df;
  718. try {
  719. CtClass[] ifs = getInterfaces();
  720. int num = ifs.length;
  721. for (int i = 0; i < num; ++i) {
  722. CtField f = ifs[i].getField2(name);
  723. if (f != null)
  724. return f;
  725. }
  726. CtClass s = getSuperclass();
  727. if (s != null)
  728. return s.getField2(name);
  729. }
  730. catch (NotFoundException e) {}
  731. return null;
  732. }
  733. public CtField[] getDeclaredFields() {
  734. CtMember.Cache memCache = getMembers();
  735. CtMember field = memCache.fieldHead();
  736. CtMember tail = memCache.lastField();
  737. int num = CtMember.Cache.count(field, tail);
  738. CtField[] cfs = new CtField[num];
  739. int i = 0;
  740. while (field != tail) {
  741. field = field.next();
  742. cfs[i++] = (CtField)field;
  743. }
  744. return cfs;
  745. }
  746. public CtField getDeclaredField(String name) throws NotFoundException {
  747. CtField f = getDeclaredField2(name);
  748. if (f == null)
  749. throw new NotFoundException("field: " + name + " in " + getName());
  750. else
  751. return f;
  752. }
  753. private CtField getDeclaredField2(String name) {
  754. CtMember.Cache memCache = getMembers();
  755. CtMember field = memCache.fieldHead();
  756. CtMember tail = memCache.lastField();
  757. while (field != tail) {
  758. field = field.next();
  759. if (field.getName().equals(name))
  760. return (CtField)field;
  761. }
  762. return null;
  763. }
  764. public CtBehavior[] getDeclaredBehaviors() {
  765. CtMember.Cache memCache = getMembers();
  766. CtMember cons = memCache.consHead();
  767. CtMember consTail = memCache.lastCons();
  768. int cnum = CtMember.Cache.count(cons, consTail);
  769. CtMember mth = memCache.methodHead();
  770. CtMember mthTail = memCache.lastMethod();
  771. int mnum = CtMember.Cache.count(mth, mthTail);
  772. CtBehavior[] cb = new CtBehavior[cnum + mnum];
  773. int i = 0;
  774. while (cons != consTail) {
  775. cons = cons.next();
  776. cb[i++] = (CtBehavior)cons;
  777. }
  778. while (mth != mthTail) {
  779. mth = mth.next();
  780. cb[i++] = (CtBehavior)mth;
  781. }
  782. return cb;
  783. }
  784. public CtConstructor[] getConstructors() {
  785. CtMember.Cache memCache = getMembers();
  786. CtMember cons = memCache.consHead();
  787. CtMember consTail = memCache.lastCons();
  788. int n = 0;
  789. CtMember mem = cons;
  790. while (mem != consTail) {
  791. mem = mem.next();
  792. if (isPubCons((CtConstructor)mem))
  793. n++;
  794. }
  795. CtConstructor[] result = new CtConstructor[n];
  796. int i = 0;
  797. mem = cons;
  798. while (mem != consTail) {
  799. mem = mem.next();
  800. CtConstructor cc = (CtConstructor)mem;
  801. if (isPubCons(cc))
  802. result[i++] = cc;
  803. }
  804. return result;
  805. }
  806. private static boolean isPubCons(CtConstructor cons) {
  807. return !Modifier.isPrivate(cons.getModifiers())
  808. && cons.isConstructor();
  809. }
  810. public CtConstructor getConstructor(String desc)
  811. throws NotFoundException
  812. {
  813. CtMember.Cache memCache = getMembers();
  814. CtMember cons = memCache.consHead();
  815. CtMember consTail = memCache.lastCons();
  816. while (cons != consTail) {
  817. cons = cons.next();
  818. CtConstructor cc = (CtConstructor)cons;
  819. if (cc.getMethodInfo2().getDescriptor().equals(desc)
  820. && cc.isConstructor())
  821. return cc;
  822. }
  823. return super.getConstructor(desc);
  824. }
  825. public CtConstructor[] getDeclaredConstructors() {
  826. CtMember.Cache memCache = getMembers();
  827. CtMember cons = memCache.consHead();
  828. CtMember consTail = memCache.lastCons();
  829. int n = 0;
  830. CtMember mem = cons;
  831. while (mem != consTail) {
  832. mem = mem.next();
  833. CtConstructor cc = (CtConstructor)mem;
  834. if (cc.isConstructor())
  835. n++;
  836. }
  837. CtConstructor[] result = new CtConstructor[n];
  838. int i = 0;
  839. mem = cons;
  840. while (mem != consTail) {
  841. mem = mem.next();
  842. CtConstructor cc = (CtConstructor)mem;
  843. if (cc.isConstructor())
  844. result[i++] = cc;
  845. }
  846. return result;
  847. }
  848. public CtConstructor getClassInitializer() {
  849. CtMember.Cache memCache = getMembers();
  850. CtMember cons = memCache.consHead();
  851. CtMember consTail = memCache.lastCons();
  852. while (cons != consTail) {
  853. cons = cons.next();
  854. CtConstructor cc = (CtConstructor)cons;
  855. if (cc.isClassInitializer())
  856. return cc;
  857. }
  858. return null;
  859. }
  860. public CtMethod[] getMethods() {
  861. HashMap h = new HashMap();
  862. getMethods0(h, this);
  863. return (CtMethod[])h.values().toArray(new CtMethod[h.size()]);
  864. }
  865. private static void getMethods0(HashMap h, CtClass cc) {
  866. try {
  867. CtClass[] ifs = cc.getInterfaces();
  868. int size = ifs.length;
  869. for (int i = 0; i < size; ++i)
  870. getMethods0(h, ifs[i]);
  871. }
  872. catch (NotFoundException e) {}
  873. try {
  874. CtClass s = cc.getSuperclass();
  875. if (s != null)
  876. getMethods0(h, s);
  877. }
  878. catch (NotFoundException e) {}
  879. if (cc instanceof CtClassType) {
  880. CtMember.Cache memCache = ((CtClassType)cc).getMembers();
  881. CtMember mth = memCache.methodHead();
  882. CtMember mthTail = memCache.lastMethod();
  883. while (mth != mthTail) {
  884. mth = mth.next();
  885. if (!Modifier.isPrivate(mth.getModifiers()))
  886. h.put(((CtMethod)mth).getStringRep(), mth);
  887. }
  888. }
  889. }
  890. public CtMethod getMethod(String name, String desc)
  891. throws NotFoundException
  892. {
  893. CtMethod m = getMethod0(this, name, desc);
  894. if (m != null)
  895. return m;
  896. else
  897. throw new NotFoundException(name + "(..) is not found in "
  898. + getName());
  899. }
  900. private static CtMethod getMethod0(CtClass cc,
  901. String name, String desc) {
  902. if (cc instanceof CtClassType) {
  903. CtMember.Cache memCache = ((CtClassType)cc).getMembers();
  904. CtMember mth = memCache.methodHead();
  905. CtMember mthTail = memCache.lastMethod();
  906. while (mth != mthTail) {
  907. mth = mth.next();
  908. if (mth.getName().equals(name)
  909. && ((CtMethod)mth).getMethodInfo2().getDescriptor().equals(desc))
  910. return (CtMethod)mth;
  911. }
  912. }
  913. try {
  914. CtClass s = cc.getSuperclass();
  915. if (s != null) {
  916. CtMethod m = getMethod0(s, name, desc);
  917. if (m != null)
  918. return m;
  919. }
  920. }
  921. catch (NotFoundException e) {}
  922. try {
  923. CtClass[] ifs = cc.getInterfaces();
  924. int size = ifs.length;
  925. for (int i = 0; i < size; ++i) {
  926. CtMethod m = getMethod0(ifs[i], name, desc);
  927. if (m != null)
  928. return m;
  929. }
  930. }
  931. catch (NotFoundException e) {}
  932. return null;
  933. }
  934. public CtMethod[] getDeclaredMethods() {
  935. CtMember.Cache memCache = getMembers();
  936. CtMember mth = memCache.methodHead();
  937. CtMember mthTail = memCache.lastMethod();
  938. int num = CtMember.Cache.count(mth, mthTail);
  939. CtMethod[] cms = new CtMethod[num];
  940. int i = 0;
  941. while (mth != mthTail) {
  942. mth = mth.next();
  943. cms[i++] = (CtMethod)mth;
  944. }
  945. return cms;
  946. }
  947. public CtMethod getDeclaredMethod(String name) throws NotFoundException {
  948. CtMember.Cache memCache = getMembers();
  949. CtMember mth = memCache.methodHead();
  950. CtMember mthTail = memCache.lastMethod();
  951. while (mth != mthTail) {
  952. mth = mth.next();
  953. if (mth.getName().equals(name))
  954. return (CtMethod)mth;
  955. }
  956. throw new NotFoundException(name + "(..) is not found in "
  957. + getName());
  958. }
  959. public CtMethod getDeclaredMethod(String name, CtClass[] params)
  960. throws NotFoundException
  961. {
  962. String desc = Descriptor.ofParameters(params);
  963. CtMember.Cache memCache = getMembers();
  964. CtMember mth = memCache.methodHead();
  965. CtMember mthTail = memCache.lastMethod();
  966. while (mth != mthTail) {
  967. mth = mth.next();
  968. if (mth.getName().equals(name)
  969. && ((CtMethod)mth).getMethodInfo2().getDescriptor().startsWith(desc))
  970. return (CtMethod)mth;
  971. }
  972. throw new NotFoundException(name + "(..) is not found in "
  973. + getName());
  974. }
  975. public void addField(CtField f, String init)
  976. throws CannotCompileException
  977. {
  978. addField(f, CtField.Initializer.byExpr(init));
  979. }
  980. public void addField(CtField f, CtField.Initializer init)
  981. throws CannotCompileException
  982. {
  983. checkModify();
  984. if (f.getDeclaringClass() != this)
  985. throw new CannotCompileException("cannot add");
  986. if (init == null)
  987. init = f.getInit();
  988. if (init != null) {
  989. init.check(f.getSignature());
  990. int mod = f.getModifiers();
  991. if (Modifier.isStatic(mod) && Modifier.isFinal(mod))
  992. try {
  993. ConstPool cp = getClassFile2().getConstPool();
  994. int index = init.getConstantValue(cp, f.getType());
  995. if (index != 0) {
  996. f.getFieldInfo2().addAttribute(new ConstantAttribute(cp, index));
  997. init = null;
  998. }
  999. }
  1000. catch (NotFoundException e) {}
  1001. }
  1002. getMembers().addField(f);
  1003. getClassFile2().addField(f.getFieldInfo2());
  1004. if (init != null) {
  1005. FieldInitLink fil = new FieldInitLink(f, init);
  1006. FieldInitLink link = fieldInitializers;
  1007. if (link == null)
  1008. fieldInitializers = fil;
  1009. else {
  1010. while (link.next != null)
  1011. link = link.next;
  1012. link.next = fil;
  1013. }
  1014. }
  1015. }
  1016. public void removeField(CtField f) throws NotFoundException {
  1017. checkModify();
  1018. FieldInfo fi = f.getFieldInfo2();
  1019. ClassFile cf = getClassFile2();
  1020. if (cf.getFields().remove(fi)) {
  1021. getMembers().remove(f);
  1022. gcConstPool = true;
  1023. }
  1024. else
  1025. throw new NotFoundException(f.toString());
  1026. }
  1027. public CtConstructor makeClassInitializer()
  1028. throws CannotCompileException
  1029. {
  1030. CtConstructor clinit = getClassInitializer();
  1031. if (clinit != null)
  1032. return clinit;
  1033. checkModify();
  1034. ClassFile cf = getClassFile2();
  1035. Bytecode code = new Bytecode(cf.getConstPool(), 0, 0);
  1036. modifyClassConstructor(cf, code, 0, 0);
  1037. return getClassInitializer();
  1038. }
  1039. public void addConstructor(CtConstructor c)
  1040. throws CannotCompileException
  1041. {
  1042. checkModify();
  1043. if (c.getDeclaringClass() != this)
  1044. throw new CannotCompileException("cannot add");
  1045. getMembers().addConstructor(c);
  1046. getClassFile2().addMethod(c.getMethodInfo2());
  1047. }
  1048. public void removeConstructor(CtConstructor m) throws NotFoundException {
  1049. checkModify();
  1050. MethodInfo mi = m.getMethodInfo2();
  1051. ClassFile cf = getClassFile2();
  1052. if (cf.getMethods().remove(mi)) {
  1053. getMembers().remove(m);
  1054. gcConstPool = true;
  1055. }
  1056. else
  1057. throw new NotFoundException(m.toString());
  1058. }
  1059. public void addMethod(CtMethod m) throws CannotCompileException {
  1060. checkModify();
  1061. if (m.getDeclaringClass() != this)
  1062. throw new CannotCompileException("cannot add");
  1063. getMembers().addMethod(m);
  1064. getClassFile2().addMethod(m.getMethodInfo2());
  1065. if ((m.getModifiers() & Modifier.ABSTRACT) != 0)
  1066. setModifiers(getModifiers() | Modifier.ABSTRACT);
  1067. }
  1068. public void removeMethod(CtMethod m) throws NotFoundException {
  1069. checkModify();
  1070. MethodInfo mi = m.getMethodInfo2();
  1071. ClassFile cf = getClassFile2();
  1072. if (cf.getMethods().remove(mi)) {
  1073. getMembers().remove(m);
  1074. gcConstPool = true;
  1075. }
  1076. else
  1077. throw new NotFoundException(m.toString());
  1078. }
  1079. public byte[] getAttribute(String name) {
  1080. AttributeInfo ai = getClassFile2().getAttribute(name);
  1081. if (ai == null)
  1082. return null;
  1083. else
  1084. return ai.get();
  1085. }
  1086. public void setAttribute(String name, byte[] data) {
  1087. checkModify();
  1088. ClassFile cf = getClassFile2();
  1089. cf.addAttribute(new AttributeInfo(cf.getConstPool(), name, data));
  1090. }
  1091. public void instrument(CodeConverter converter)
  1092. throws CannotCompileException
  1093. {
  1094. checkModify();
  1095. ClassFile cf = getClassFile2();
  1096. ConstPool cp = cf.getConstPool();
  1097. List list = cf.getMethods();
  1098. int n = list.size();
  1099. for (int i = 0; i < n; ++i) {
  1100. MethodInfo minfo = (MethodInfo)list.get(i);
  1101. converter.doit(this, minfo, cp);
  1102. }
  1103. }
  1104. public void instrument(ExprEditor editor)
  1105. throws CannotCompileException
  1106. {
  1107. checkModify();
  1108. ClassFile cf = getClassFile2();
  1109. List list = cf.getMethods();
  1110. int n = list.size();
  1111. for (int i = 0; i < n; ++i) {
  1112. MethodInfo minfo = (MethodInfo)list.get(i);
  1113. editor.doit(this, minfo);
  1114. }
  1115. }
  1116. /**
  1117. * @see javassist.CtClass#prune()
  1118. * @see javassist.CtClass#stopPruning(boolean)
  1119. */
  1120. public void prune() {
  1121. if (wasPruned)
  1122. return;
  1123. wasPruned = wasFrozen = true;
  1124. getClassFile2().prune();
  1125. }
  1126. public void rebuildClassFile() { gcConstPool = true; }
  1127. public void toBytecode(DataOutputStream out)
  1128. throws CannotCompileException, IOException
  1129. {
  1130. try {
  1131. if (isModified()) {
  1132. checkPruned("toBytecode");
  1133. ClassFile cf = getClassFile2();
  1134. if (gcConstPool) {
  1135. cf.compact();
  1136. gcConstPool = false;
  1137. }
  1138. modifyClassConstructor(cf);
  1139. modifyConstructors(cf);
  1140. cf.write(out);
  1141. out.flush();
  1142. fieldInitializers = null;
  1143. if (doPruning) {
  1144. // to save memory
  1145. cf.prune();
  1146. wasPruned = true;
  1147. }
  1148. }
  1149. else {
  1150. classPool.writeClassfile(getName(), out);
  1151. // to save memory
  1152. // classfile = null;
  1153. }
  1154. getCount = 0;
  1155. wasFrozen = true;
  1156. }
  1157. catch (NotFoundException e) {
  1158. throw new CannotCompileException(e);
  1159. }
  1160. catch (IOException e) {
  1161. throw new CannotCompileException(e);
  1162. }
  1163. }
  1164. /* See also checkModified()
  1165. */
  1166. private void checkPruned(String method) {
  1167. if (wasPruned)
  1168. throw new RuntimeException(method + "(): " + getName()
  1169. + " was pruned.");
  1170. }
  1171. public boolean stopPruning(boolean stop) {
  1172. boolean prev = !doPruning;
  1173. doPruning = !stop;
  1174. return prev;
  1175. }
  1176. private void modifyClassConstructor(ClassFile cf)
  1177. throws CannotCompileException, NotFoundException
  1178. {
  1179. if (fieldInitializers == null)
  1180. return;
  1181. Bytecode code = new Bytecode(cf.getConstPool(), 0, 0);
  1182. Javac jv = new Javac(code, this);
  1183. int stacksize = 0;
  1184. boolean doInit = false;
  1185. for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
  1186. CtField f = fi.field;
  1187. if (Modifier.isStatic(f.getModifiers())) {
  1188. doInit = true;
  1189. int s = fi.init.compileIfStatic(f.getType(), f.getName(),
  1190. code, jv);
  1191. if (stacksize < s)
  1192. stacksize = s;
  1193. }
  1194. }
  1195. if (doInit) // need an initializer for static fileds.
  1196. modifyClassConstructor(cf, code, stacksize, 0);
  1197. }
  1198. private void modifyClassConstructor(ClassFile cf, Bytecode code,
  1199. int stacksize, int localsize)
  1200. throws CannotCompileException
  1201. {
  1202. MethodInfo m = cf.getStaticInitializer();
  1203. if (m == null) {
  1204. code.add(Bytecode.RETURN);
  1205. code.setMaxStack(stacksize);
  1206. code.setMaxLocals(localsize);
  1207. m = new MethodInfo(cf.getConstPool(), "<clinit>", "()V");
  1208. m.setAccessFlags(AccessFlag.STATIC);
  1209. m.setCodeAttribute(code.toCodeAttribute());
  1210. cf.addMethod(m);
  1211. CtMember.Cache cache = hasMemberCache();
  1212. if (cache != null)
  1213. cache.addConstructor(new CtConstructor(m, this));
  1214. }
  1215. else {
  1216. CodeAttribute codeAttr = m.getCodeAttribute();
  1217. if (codeAttr == null)
  1218. throw new CannotCompileException("empty <clinit>");
  1219. try {
  1220. CodeIterator it = codeAttr.iterator();
  1221. int pos = it.insertEx(code.get());
  1222. it.insert(code.getExceptionTable(), pos);
  1223. int maxstack = codeAttr.getMaxStack();
  1224. if (maxstack < stacksize)
  1225. codeAttr.setMaxStack(stacksize);
  1226. int maxlocals = codeAttr.getMaxLocals();
  1227. if (maxlocals < localsize)
  1228. codeAttr.setMaxLocals(localsize);
  1229. }
  1230. catch (BadBytecode e) {
  1231. throw new CannotCompileException(e);
  1232. }
  1233. }
  1234. try {
  1235. m.rebuildStackMapIf6(classPool, cf);
  1236. }
  1237. catch (BadBytecode e) {
  1238. throw new CannotCompileException(e);
  1239. }
  1240. }
  1241. private void modifyConstructors(ClassFile cf)
  1242. throws CannotCompileException, NotFoundException
  1243. {
  1244. if (fieldInitializers == null)
  1245. return;
  1246. ConstPool cp = cf.getConstPool();
  1247. List list = cf.getMethods();
  1248. int n = list.size();
  1249. for (int i = 0; i < n; ++i) {
  1250. MethodInfo minfo = (MethodInfo)list.get(i);
  1251. if (minfo.isConstructor()) {
  1252. CodeAttribute codeAttr = minfo.getCodeAttribute();
  1253. if (codeAttr != null)
  1254. try {
  1255. Bytecode init = new Bytecode(cp, 0,
  1256. codeAttr.getMaxLocals());
  1257. CtClass[] params
  1258. = Descriptor.getParameterTypes(
  1259. minfo.getDescriptor(),
  1260. classPool);
  1261. int stacksize = makeFieldInitializer(init, params);
  1262. insertAuxInitializer(codeAttr, init, stacksize);
  1263. minfo.rebuildStackMapIf6(classPool, cf);
  1264. }
  1265. catch (BadBytecode e) {
  1266. throw new CannotCompileException(e);
  1267. }
  1268. }
  1269. }
  1270. }
  1271. private static void insertAuxInitializer(CodeAttribute codeAttr,
  1272. Bytecode initializer,
  1273. int stacksize)
  1274. throws BadBytecode
  1275. {
  1276. CodeIterator it = codeAttr.iterator();
  1277. int index = it.skipSuperConstructor();
  1278. if (index < 0) {
  1279. index = it.skipThisConstructor();
  1280. if (index >= 0)
  1281. return; // this() is called.
  1282. // Neither this() or super() is called.
  1283. }
  1284. int pos = it.insertEx(initializer.get());
  1285. it.insert(initializer.getExceptionTable(), pos);
  1286. int maxstack = codeAttr.getMaxStack();
  1287. if (maxstack < stacksize)
  1288. codeAttr.setMaxStack(stacksize);
  1289. }
  1290. private int makeFieldInitializer(Bytecode code, CtClass[] parameters)
  1291. throws CannotCompileException, NotFoundException
  1292. {
  1293. int stacksize = 0;
  1294. Javac jv = new Javac(code, this);
  1295. try {
  1296. jv.recordParams(parameters, false);
  1297. }
  1298. catch (CompileError e) {
  1299. throw new CannotCompileException(e);
  1300. }
  1301. for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
  1302. CtField f = fi.field;
  1303. if (!Modifier.isStatic(f.getModifiers())) {
  1304. int s = fi.init.compile(f.getType(), f.getName(), code,
  1305. parameters, jv);
  1306. if (stacksize < s)
  1307. stacksize = s;
  1308. }
  1309. }
  1310. return stacksize;
  1311. }
  1312. // Methods used by CtNewWrappedMethod
  1313. Hashtable getHiddenMethods() {
  1314. if (hiddenMethods == null)
  1315. hiddenMethods = new Hashtable();
  1316. return hiddenMethods;
  1317. }
  1318. int getUniqueNumber() { return uniqueNumberSeed++; }
  1319. public String makeUniqueName(String prefix) {
  1320. HashMap table = new HashMap();
  1321. makeMemberList(table);
  1322. Set keys = table.keySet();
  1323. String[] methods = new String[keys.size()];
  1324. keys.toArray(methods);
  1325. if (notFindInArray(prefix, methods))
  1326. return prefix;
  1327. int i = 100;
  1328. String name;
  1329. do {
  1330. if (i > 999)
  1331. throw new RuntimeException("too many unique name");
  1332. name = prefix + i++;
  1333. } while (!notFindInArray(name, methods));
  1334. return name;
  1335. }
  1336. private static boolean notFindInArray(String prefix, String[] values) {
  1337. int len = values.length;
  1338. for (int i = 0; i < len; i++)
  1339. if (values[i].startsWith(prefix))
  1340. return false;
  1341. return true;
  1342. }
  1343. private void makeMemberList(HashMap table) {
  1344. int mod = getModifiers();
  1345. if (Modifier.isAbstract(mod) || Modifier.isInterface(mod))
  1346. try {
  1347. CtClass[] ifs = getInterfaces();
  1348. int size = ifs.length;
  1349. for (int i = 0; i < size; i++) {
  1350. CtClass ic =ifs[i];
  1351. if (ic != null && ic instanceof CtClassType)
  1352. ((CtClassType)ic).makeMemberList(table);
  1353. }
  1354. }
  1355. catch (NotFoundException e) {}
  1356. try {
  1357. CtClass s = getSuperclass();
  1358. if (s != null && s instanceof CtClassType)
  1359. ((CtClassType)s).makeMemberList(table);
  1360. }
  1361. catch (NotFoundException e) {}
  1362. List list = getClassFile2().getMethods();
  1363. int n = list.size();
  1364. for (int i = 0; i < n; i++) {
  1365. MethodInfo minfo = (MethodInfo)list.get(i);
  1366. table.put(minfo.getName(), this);
  1367. }
  1368. list = getClassFile2().getFields();
  1369. n = list.size();
  1370. for (int i = 0; i < n; i++) {
  1371. FieldInfo finfo = (FieldInfo)list.get(i);
  1372. table.put(finfo.getName(), this);
  1373. }
  1374. }
  1375. }
  1376. class FieldInitLink {
  1377. FieldInitLink next;
  1378. CtField field;
  1379. CtField.Initializer init;
  1380. FieldInitLink(CtField f, CtField.Initializer i) {
  1381. next = null;
  1382. field = f;
  1383. init = i;
  1384. }
  1385. }