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

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