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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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 int getModifiers() {
  307. ClassFile cf = getClassFile2();
  308. int acc = cf.getAccessFlags();
  309. acc = AccessFlag.clear(acc, AccessFlag.SUPER);
  310. int inner = cf.getInnerAccessFlags();
  311. if (inner != -1 && (inner & AccessFlag.STATIC) != 0)
  312. acc |= AccessFlag.STATIC;
  313. return AccessFlag.toModifier(acc);
  314. }
  315. public void setModifiers(int mod) {
  316. if (Modifier.isStatic(mod))
  317. throw new RuntimeException("cannot set to static");
  318. checkModify();
  319. int acc = AccessFlag.of(mod) | AccessFlag.SUPER;
  320. getClassFile2().setAccessFlags(acc);
  321. }
  322. public Object[] getAnnotations() throws ClassNotFoundException {
  323. ClassFile cf = getClassFile2();
  324. AnnotationsAttribute ainfo = (AnnotationsAttribute)
  325. cf.getAttribute(AnnotationsAttribute.invisibleTag);
  326. AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
  327. cf.getAttribute(AnnotationsAttribute.visibleTag);
  328. return toAnnotationType(getClassPool(), ainfo, ainfo2);
  329. }
  330. static Object[] toAnnotationType(ClassPool cp, AnnotationsAttribute a1,
  331. AnnotationsAttribute a2) throws ClassNotFoundException {
  332. Annotation[] anno1, anno2;
  333. int size1, size2;
  334. if (a1 == null) {
  335. anno1 = null;
  336. size1 = 0;
  337. }
  338. else {
  339. anno1 = a1.getAnnotations();
  340. size1 = anno1.length;
  341. }
  342. if (a2 == null) {
  343. anno2 = null;
  344. size2 = 0;
  345. }
  346. else {
  347. anno2 = a2.getAnnotations();
  348. size2 = anno2.length;
  349. }
  350. Object[] result = new Object[size1 + size2];
  351. ClassLoader cl = Thread.currentThread().getContextClassLoader();
  352. for (int i = 0; i < size1; i++)
  353. result[i] = anno1[i].toAnnotationType(cl, cp);
  354. for (int j = 0; j < size2; j++)
  355. result[j + size1] = anno2[j].toAnnotationType(cl, cp);
  356. return result;
  357. }
  358. static Object[][] toAnnotationType(ClassPool cp, ParameterAnnotationsAttribute a1,
  359. ParameterAnnotationsAttribute a2) throws ClassNotFoundException {
  360. int numParameters = 0;
  361. if (a1 != null)
  362. numParameters = a1.numParameters();
  363. else if (a2 != null)
  364. numParameters = a2.numParameters();
  365. else
  366. return new Object[0][];
  367. Object[][] result = new Object[numParameters][];
  368. ClassLoader cl = Thread.currentThread().getContextClassLoader();
  369. for (int i = 0; i < numParameters; i++) {
  370. Annotation[] anno1, anno2;
  371. int size1, size2;
  372. if (a1 == null) {
  373. anno1 = null;
  374. size1 = 0;
  375. }
  376. else {
  377. anno1 = a1.getAnnotations()[i];
  378. size1 = anno1.length;
  379. }
  380. if (a2 == null) {
  381. anno2 = null;
  382. size2 = 0;
  383. }
  384. else {
  385. anno2 = a2.getAnnotations()[i];
  386. size2 = anno2.length;
  387. }
  388. result[i] = new Object[size1 + size2];
  389. for (int j = 0; j < size1; ++j)
  390. result[i][j] = anno1[j].toAnnotationType(cl, cp);
  391. for (int j = 0; j < size2; ++j)
  392. result[i][j + size1] = anno2[j].toAnnotationType(cl, cp);
  393. }
  394. return result;
  395. }
  396. public boolean subclassOf(CtClass superclass) {
  397. if (superclass == null)
  398. return false;
  399. String superName = superclass.getName();
  400. CtClass curr = this;
  401. try {
  402. while (curr != null) {
  403. if (curr.getName().equals(superName))
  404. return true;
  405. curr = curr.getSuperclass();
  406. }
  407. }
  408. catch (Exception ignored) {}
  409. return false;
  410. }
  411. public CtClass getSuperclass() throws NotFoundException {
  412. String supername = getClassFile2().getSuperclass();
  413. if (supername == null)
  414. return null;
  415. else
  416. return classPool.get(supername);
  417. }
  418. public void setSuperclass(CtClass clazz) throws CannotCompileException {
  419. checkModify();
  420. if (isInterface())
  421. addInterface(clazz);
  422. else
  423. getClassFile2().setSuperclass(clazz.getName());
  424. }
  425. public CtClass[] getInterfaces() throws NotFoundException {
  426. String[] ifs = getClassFile2().getInterfaces();
  427. int num = ifs.length;
  428. CtClass[] ifc = new CtClass[num];
  429. for (int i = 0; i < num; ++i)
  430. ifc[i] = classPool.get(ifs[i]);
  431. return ifc;
  432. }
  433. public void setInterfaces(CtClass[] list) {
  434. checkModify();
  435. String[] ifs;
  436. if (list == null)
  437. ifs = new String[0];
  438. else {
  439. int num = list.length;
  440. ifs = new String[num];
  441. for (int i = 0; i < num; ++i)
  442. ifs[i] = list[i].getName();
  443. }
  444. getClassFile2().setInterfaces(ifs);
  445. }
  446. public void addInterface(CtClass anInterface) {
  447. checkModify();
  448. if (anInterface != null)
  449. getClassFile2().addInterface(anInterface.getName());
  450. }
  451. public CtClass getDeclaringClass() throws NotFoundException {
  452. ClassFile cf = getClassFile2();
  453. InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(
  454. InnerClassesAttribute.tag);
  455. if (ica == null)
  456. return null;
  457. String name = getName();
  458. int n = ica.tableLength();
  459. for (int i = 0; i < n; ++i)
  460. if (name.equals(ica.innerClass(i))) {
  461. String outName = ica.outerClass(i);
  462. if (outName != null)
  463. return classPool.get(outName);
  464. else {
  465. // maybe anonymous or local class.
  466. EnclosingMethodAttribute ema
  467. = (EnclosingMethodAttribute)cf.getAttribute(
  468. EnclosingMethodAttribute.tag);
  469. if (ema != null)
  470. return classPool.get(ema.className());
  471. }
  472. }
  473. return null;
  474. }
  475. public CtMethod getEnclosingMethod() throws NotFoundException {
  476. ClassFile cf = getClassFile2();
  477. EnclosingMethodAttribute ema
  478. = (EnclosingMethodAttribute)cf.getAttribute(
  479. EnclosingMethodAttribute.tag);
  480. if (ema != null) {
  481. CtClass enc = classPool.get(ema.className());
  482. return enc.getMethod(ema.methodName(), ema.methodDescriptor());
  483. }
  484. return null;
  485. }
  486. public CtClass makeNestedClass(String name, boolean isStatic) {
  487. if (!isStatic)
  488. throw new RuntimeException(
  489. "sorry, only nested static class is supported");
  490. checkModify();
  491. CtClass c = classPool.makeNestedClass(getName() + "$" + name);
  492. ClassFile cf = getClassFile2();
  493. ClassFile cf2 = c.getClassFile2();
  494. InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(
  495. InnerClassesAttribute.tag);
  496. if (ica == null) {
  497. ica = new InnerClassesAttribute(cf.getConstPool());
  498. cf.addAttribute(ica);
  499. }
  500. ica.append(c.getName(), this.getName(), name,
  501. (cf2.getAccessFlags() & ~AccessFlag.SUPER) | AccessFlag.STATIC);
  502. cf2.addAttribute(ica.copy(cf2.getConstPool(), null));
  503. return c;
  504. }
  505. public CtField[] getFields() {
  506. ArrayList alist = new ArrayList();
  507. getFields(alist, this);
  508. return (CtField[])alist.toArray(new CtField[alist.size()]);
  509. }
  510. private static void getFields(ArrayList alist, CtClass cc) {
  511. int i, num;
  512. if (cc == null)
  513. return;
  514. try {
  515. getFields(alist, cc.getSuperclass());
  516. }
  517. catch (NotFoundException e) {}
  518. try {
  519. CtClass[] ifs = cc.getInterfaces();
  520. num = ifs.length;
  521. for (i = 0; i < num; ++i)
  522. getFields(alist, ifs[i]);
  523. }
  524. catch (NotFoundException e) {}
  525. CtMember cf = ((CtClassType)cc).getFieldsCache();
  526. while (cf != null) {
  527. if (!Modifier.isPrivate(cf.getModifiers()))
  528. alist.add(cf);
  529. cf = cf.next;
  530. }
  531. }
  532. public CtField getField(String name) throws NotFoundException {
  533. CtField f = getField2(name);
  534. if (f == null)
  535. throw new NotFoundException("field: " + name + " in " + getName());
  536. else
  537. return f;
  538. }
  539. CtField getField2(String name) {
  540. CtField df = getDeclaredField2(name);
  541. if (df != null)
  542. return df;
  543. try {
  544. CtClass[] ifs = getInterfaces();
  545. int num = ifs.length;
  546. for (int i = 0; i < num; ++i) {
  547. CtField f = ifs[i].getField2(name);
  548. if (f != null)
  549. return f;
  550. }
  551. CtClass s = getSuperclass();
  552. if (s != null)
  553. return s.getField2(name);
  554. }
  555. catch (NotFoundException e) {}
  556. return null;
  557. }
  558. public CtField[] getDeclaredFields() {
  559. CtMember cf = getFieldsCache();
  560. int num = CtField.count(cf);
  561. CtField[] cfs = new CtField[num];
  562. int i = 0;
  563. while (cf != null) {
  564. cfs[i++] = (CtField)cf;
  565. cf = cf.next;
  566. }
  567. return cfs;
  568. }
  569. protected CtMember getFieldsCache() {
  570. if (fieldsCache == null) {
  571. List list = getClassFile2().getFields();
  572. int n = list.size();
  573. CtMember allFields = null;
  574. CtField tail = null;
  575. for (int i = 0; i < n; ++i) {
  576. FieldInfo finfo = (FieldInfo)list.get(i);
  577. CtField newTail = new CtField(finfo, this);
  578. allFields = CtMember.append(allFields, tail, newTail);
  579. tail = newTail;
  580. }
  581. fieldsCache = allFields;
  582. }
  583. return fieldsCache;
  584. }
  585. public CtField getDeclaredField(String name) throws NotFoundException {
  586. CtField f = getDeclaredField2(name);
  587. if (f == null)
  588. throw new NotFoundException("field: " + name + " in " + getName());
  589. else
  590. return f;
  591. }
  592. private CtField getDeclaredField2(String name) {
  593. CtMember cf = getFieldsCache();
  594. while (cf != null) {
  595. if (cf.getName().equals(name))
  596. return (CtField)cf;
  597. cf = cf.next;
  598. }
  599. return null;
  600. }
  601. public CtBehavior[] getDeclaredBehaviors() {
  602. CtMember cc = getConstructorsCache();
  603. CtMember cm = getMethodsCache();
  604. int num = CtMember.count(cm) + CtMember.count(cc);
  605. CtBehavior[] cb = new CtBehavior[num];
  606. int i = 0;
  607. while (cc != null) {
  608. cb[i++] = (CtBehavior)cc;
  609. cc = cc.next;
  610. }
  611. while (cm != null) {
  612. cb[i++] = (CtBehavior)cm;
  613. cm = cm.next;
  614. }
  615. return cb;
  616. }
  617. public CtConstructor[] getConstructors() {
  618. CtConstructor[] cons = getDeclaredConstructors();
  619. if (cons.length == 0)
  620. return cons;
  621. int n = 0;
  622. int i = cons.length;
  623. while (--i >= 0)
  624. if (!Modifier.isPrivate(cons[i].getModifiers()))
  625. ++n;
  626. CtConstructor[] result = new CtConstructor[n];
  627. n = 0;
  628. i = cons.length;
  629. while (--i >= 0) {
  630. CtConstructor c = cons[i];
  631. if (!Modifier.isPrivate(c.getModifiers()))
  632. result[n++] = c;
  633. }
  634. return result;
  635. }
  636. public CtConstructor getConstructor(String desc)
  637. throws NotFoundException
  638. {
  639. CtConstructor cc = (CtConstructor)getConstructorsCache();
  640. while (cc != null) {
  641. if (cc.getMethodInfo2().getDescriptor().equals(desc))
  642. return cc;
  643. cc = (CtConstructor)cc.next;
  644. }
  645. return super.getConstructor(desc);
  646. }
  647. public CtConstructor[] getDeclaredConstructors() {
  648. CtMember cc = getConstructorsCache();
  649. int num = CtMember.count(cc);
  650. CtConstructor[] ccs = new CtConstructor[num];
  651. int i = 0;
  652. while (cc != null) {
  653. ccs[i++] = (CtConstructor)cc;
  654. cc = cc.next;
  655. }
  656. return ccs;
  657. }
  658. protected CtMember getConstructorsCache() {
  659. if (constructorsCache == null) {
  660. List list = getClassFile2().getMethods();
  661. int n = list.size();
  662. CtMember allConstructors = null;
  663. CtConstructor tail = null;
  664. for (int i = 0; i < n; ++i) {
  665. MethodInfo minfo = (MethodInfo)list.get(i);
  666. if (minfo.isConstructor()) {
  667. CtConstructor newTail = new CtConstructor(minfo, this);
  668. allConstructors = CtMember.append(allConstructors, tail, newTail);
  669. tail = newTail;
  670. }
  671. }
  672. constructorsCache = allConstructors;
  673. }
  674. return constructorsCache;
  675. }
  676. public CtConstructor getClassInitializer() {
  677. if (classInitializerCache == null) {
  678. MethodInfo minfo = getClassFile2().getStaticInitializer();
  679. if (minfo != null)
  680. classInitializerCache = new CtConstructor(minfo, this);
  681. }
  682. return classInitializerCache;
  683. }
  684. public CtMethod[] getMethods() {
  685. HashMap h = new HashMap();
  686. getMethods0(h, this);
  687. return (CtMethod[])h.values().toArray(new CtMethod[h.size()]);
  688. }
  689. private static void getMethods0(HashMap h, CtClass cc) {
  690. try {
  691. CtClass[] ifs = cc.getInterfaces();
  692. int size = ifs.length;
  693. for (int i = 0; i < size; ++i)
  694. getMethods0(h, ifs[i]);
  695. }
  696. catch (NotFoundException e) {}
  697. try {
  698. CtClass s = cc.getSuperclass();
  699. if (s != null)
  700. getMethods0(h, s);
  701. }
  702. catch (NotFoundException e) {}
  703. if (cc instanceof CtClassType) {
  704. CtMember cm = ((CtClassType)cc).getMethodsCache();
  705. while (cm != null) {
  706. if (!Modifier.isPrivate(cm.getModifiers()))
  707. h.put(((CtMethod)cm).getStringRep(), cm);
  708. cm = cm.next;
  709. }
  710. }
  711. }
  712. public CtMethod getMethod(String name, String desc)
  713. throws NotFoundException
  714. {
  715. CtMethod m = getMethod0(this, name, desc);
  716. if (m != null)
  717. return m;
  718. else
  719. throw new NotFoundException(name + "(..) is not found in "
  720. + getName());
  721. }
  722. private static CtMethod getMethod0(CtClass cc,
  723. String name, String desc) {
  724. if (cc instanceof CtClassType) {
  725. CtMethod cm = (CtMethod)((CtClassType)cc).getMethodsCache();
  726. while (cm != null) {
  727. if (cm.getName().equals(name)
  728. && cm.getMethodInfo2().getDescriptor().equals(desc))
  729. return cm;
  730. cm = (CtMethod)cm.next;
  731. }
  732. }
  733. try {
  734. CtClass s = cc.getSuperclass();
  735. if (s != null) {
  736. CtMethod m = getMethod0(s, name, desc);
  737. if (m != null)
  738. return m;
  739. }
  740. }
  741. catch (NotFoundException e) {}
  742. try {
  743. CtClass[] ifs = cc.getInterfaces();
  744. int size = ifs.length;
  745. for (int i = 0; i < size; ++i) {
  746. CtMethod m = getMethod0(ifs[i], name, desc);
  747. if (m != null)
  748. return m;
  749. }
  750. }
  751. catch (NotFoundException e) {}
  752. return null;
  753. }
  754. public CtMethod[] getDeclaredMethods() {
  755. CtMember cm = getMethodsCache();
  756. int num = CtMember.count(cm);
  757. CtMethod[] cms = new CtMethod[num];
  758. int i = 0;
  759. while (cm != null) {
  760. cms[i++] = (CtMethod)cm;
  761. cm = cm.next;
  762. }
  763. return cms;
  764. }
  765. public CtMethod getDeclaredMethod(String name) throws NotFoundException {
  766. CtMember m = getMethodsCache();
  767. while (m != null) {
  768. if (m.getName().equals(name))
  769. return (CtMethod)m;
  770. m = m.next;
  771. }
  772. throw new NotFoundException(name + "(..) is not found in "
  773. + getName());
  774. }
  775. public CtMethod getDeclaredMethod(String name, CtClass[] params)
  776. throws NotFoundException
  777. {
  778. String desc = Descriptor.ofParameters(params);
  779. CtMethod m = (CtMethod)getMethodsCache();
  780. while (m != null) {
  781. if (m.getName().equals(name)
  782. && m.getMethodInfo2().getDescriptor().startsWith(desc))
  783. return m;
  784. m = (CtMethod)m.next;
  785. }
  786. throw new NotFoundException(name + "(..) is not found in "
  787. + getName());
  788. }
  789. protected CtMember getMethodsCache() {
  790. if (methodsCache == null) {
  791. List list = getClassFile2().getMethods();
  792. int n = list.size();
  793. CtMember allMethods = null;
  794. CtMethod tail = null;
  795. for (int i = 0; i < n; ++i) {
  796. MethodInfo minfo = (MethodInfo)list.get(i);
  797. if (minfo.isMethod()) {
  798. CtMethod newTail = new CtMethod(minfo, this);
  799. allMethods = CtMember.append(allMethods, tail, newTail);
  800. tail = newTail;
  801. }
  802. }
  803. methodsCache = allMethods;
  804. }
  805. return methodsCache;
  806. }
  807. public void addField(CtField f, String init)
  808. throws CannotCompileException
  809. {
  810. addField(f, CtField.Initializer.byExpr(init));
  811. }
  812. public void addField(CtField f, CtField.Initializer init)
  813. throws CannotCompileException
  814. {
  815. checkModify();
  816. if (f.getDeclaringClass() != this)
  817. throw new CannotCompileException("cannot add");
  818. if (init == null)
  819. init = f.getInit();
  820. if (init != null) {
  821. int mod = f.getModifiers();
  822. if (Modifier.isStatic(mod) && Modifier.isFinal(mod))
  823. try {
  824. ConstPool cp = getClassFile2().getConstPool();
  825. int index = init.getConstantValue(cp, f.getType());
  826. if (index != 0) {
  827. f.getFieldInfo2().addAttribute(new ConstantAttribute(cp, index));
  828. init = null;
  829. }
  830. }
  831. catch (NotFoundException e) {}
  832. }
  833. getFieldsCache();
  834. fieldsCache = CtField.append(fieldsCache, f);
  835. getClassFile2().addField(f.getFieldInfo2());
  836. if (init != null) {
  837. FieldInitLink fil = new FieldInitLink(f, init);
  838. FieldInitLink link = fieldInitializers;
  839. if (link == null)
  840. fieldInitializers = fil;
  841. else {
  842. while (link.next != null)
  843. link = link.next;
  844. link.next = fil;
  845. }
  846. }
  847. }
  848. public void removeField(CtField f) throws NotFoundException {
  849. checkModify();
  850. FieldInfo fi = f.getFieldInfo2();
  851. ClassFile cf = getClassFile2();
  852. if (cf.getFields().remove(fi)) {
  853. fieldsCache = CtMember.remove(fieldsCache, f);
  854. memberRemoved = true;
  855. }
  856. else
  857. throw new NotFoundException(f.toString());
  858. }
  859. public CtConstructor makeClassInitializer()
  860. throws CannotCompileException
  861. {
  862. CtConstructor clinit = getClassInitializer();
  863. if (clinit != null)
  864. return clinit;
  865. checkModify();
  866. ClassFile cf = getClassFile2();
  867. Bytecode code = new Bytecode(cf.getConstPool(), 0, 0);
  868. modifyClassConstructor(cf, code, 0, 0);
  869. return getClassInitializer();
  870. }
  871. public void addConstructor(CtConstructor c)
  872. throws CannotCompileException
  873. {
  874. checkModify();
  875. if (c.getDeclaringClass() != this)
  876. throw new CannotCompileException("cannot add");
  877. getConstructorsCache();
  878. constructorsCache = (CtConstructor)CtMember.append(constructorsCache, c);
  879. getClassFile2().addMethod(c.getMethodInfo2());
  880. }
  881. public void removeConstructor(CtConstructor m) throws NotFoundException {
  882. checkModify();
  883. MethodInfo mi = m.getMethodInfo2();
  884. ClassFile cf = getClassFile2();
  885. if (cf.getMethods().remove(mi)) {
  886. constructorsCache = CtMember.remove(constructorsCache, m);
  887. memberRemoved = true;
  888. }
  889. else
  890. throw new NotFoundException(m.toString());
  891. }
  892. public void addMethod(CtMethod m) throws CannotCompileException {
  893. checkModify();
  894. if (m.getDeclaringClass() != this)
  895. throw new CannotCompileException("cannot add");
  896. getMethodsCache();
  897. methodsCache = CtMember.append(methodsCache, m);
  898. getClassFile2().addMethod(m.getMethodInfo2());
  899. if ((m.getModifiers() & Modifier.ABSTRACT) != 0)
  900. setModifiers(getModifiers() | Modifier.ABSTRACT);
  901. }
  902. public void removeMethod(CtMethod m) throws NotFoundException {
  903. checkModify();
  904. MethodInfo mi = m.getMethodInfo2();
  905. ClassFile cf = getClassFile2();
  906. if (cf.getMethods().remove(mi)) {
  907. methodsCache = CtMember.remove(methodsCache, m);
  908. memberRemoved = true;
  909. }
  910. else
  911. throw new NotFoundException(m.toString());
  912. }
  913. public byte[] getAttribute(String name) {
  914. AttributeInfo ai = getClassFile2().getAttribute(name);
  915. if (ai == null)
  916. return null;
  917. else
  918. return ai.get();
  919. }
  920. public void setAttribute(String name, byte[] data) {
  921. checkModify();
  922. ClassFile cf = getClassFile2();
  923. cf.addAttribute(new AttributeInfo(cf.getConstPool(), name, data));
  924. }
  925. public void instrument(CodeConverter converter)
  926. throws CannotCompileException
  927. {
  928. checkModify();
  929. ClassFile cf = getClassFile2();
  930. ConstPool cp = cf.getConstPool();
  931. List list = cf.getMethods();
  932. int n = list.size();
  933. for (int i = 0; i < n; ++i) {
  934. MethodInfo minfo = (MethodInfo)list.get(i);
  935. converter.doit(this, minfo, cp);
  936. }
  937. }
  938. public void instrument(ExprEditor editor)
  939. throws CannotCompileException
  940. {
  941. checkModify();
  942. ClassFile cf = getClassFile2();
  943. List list = cf.getMethods();
  944. int n = list.size();
  945. for (int i = 0; i < n; ++i) {
  946. MethodInfo minfo = (MethodInfo)list.get(i);
  947. editor.doit(this, minfo);
  948. }
  949. }
  950. /**
  951. * @see javassist.CtClass#prune()
  952. * @see javassist.CtClass#stopPruning(boolean)
  953. */
  954. public void prune() {
  955. if (wasPruned)
  956. return;
  957. wasPruned = wasFrozen = true;
  958. getClassFile2().prune();
  959. }
  960. public void toBytecode(DataOutputStream out)
  961. throws CannotCompileException, IOException
  962. {
  963. try {
  964. if (isModified()) {
  965. checkPruned("toBytecode");
  966. ClassFile cf = getClassFile2();
  967. if (memberRemoved) {
  968. cf.compact();
  969. memberRemoved = false;
  970. }
  971. modifyClassConstructor(cf);
  972. modifyConstructors(cf);
  973. cf.write(out);
  974. out.flush();
  975. fieldInitializers = null;
  976. if (doPruning) {
  977. // to save memory
  978. cf.prune();
  979. wasPruned = true;
  980. }
  981. }
  982. else {
  983. classPool.writeClassfile(getName(), out);
  984. // to save memory
  985. eraseCache();
  986. classfile = null;
  987. }
  988. wasFrozen = true;
  989. }
  990. catch (NotFoundException e) {
  991. throw new CannotCompileException(e);
  992. }
  993. catch (IOException e) {
  994. throw new CannotCompileException(e);
  995. }
  996. }
  997. /* See also checkModified()
  998. */
  999. private void checkPruned(String method) {
  1000. if (wasPruned)
  1001. throw new RuntimeException(method + "(): " + getName()
  1002. + " was pruned.");
  1003. }
  1004. public boolean stopPruning(boolean stop) {
  1005. boolean prev = !doPruning;
  1006. doPruning = !stop;
  1007. return prev;
  1008. }
  1009. private void modifyClassConstructor(ClassFile cf)
  1010. throws CannotCompileException, NotFoundException
  1011. {
  1012. if (fieldInitializers == null)
  1013. return;
  1014. Bytecode code = new Bytecode(cf.getConstPool(), 0, 0);
  1015. Javac jv = new Javac(code, this);
  1016. int stacksize = 0;
  1017. boolean doInit = false;
  1018. for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
  1019. CtField f = fi.field;
  1020. if (Modifier.isStatic(f.getModifiers())) {
  1021. doInit = true;
  1022. int s = fi.init.compileIfStatic(f.getType(), f.getName(),
  1023. code, jv);
  1024. if (stacksize < s)
  1025. stacksize = s;
  1026. }
  1027. }
  1028. if (doInit) // need an initializer for static fileds.
  1029. modifyClassConstructor(cf, code, stacksize, 0);
  1030. }
  1031. private void modifyClassConstructor(ClassFile cf, Bytecode code,
  1032. int stacksize, int localsize)
  1033. throws CannotCompileException
  1034. {
  1035. MethodInfo m = cf.getStaticInitializer();
  1036. if (m == null) {
  1037. code.add(Bytecode.RETURN);
  1038. code.setMaxStack(stacksize);
  1039. code.setMaxLocals(localsize);
  1040. m = new MethodInfo(cf.getConstPool(), "<clinit>", "()V");
  1041. m.setAccessFlags(AccessFlag.STATIC);
  1042. m.setCodeAttribute(code.toCodeAttribute());
  1043. cf.addMethod(m);
  1044. }
  1045. else {
  1046. CodeAttribute codeAttr = m.getCodeAttribute();
  1047. if (codeAttr == null)
  1048. throw new CannotCompileException("empty <clinit>");
  1049. try {
  1050. CodeIterator it = codeAttr.iterator();
  1051. int pos = it.insertEx(code.get());
  1052. it.insert(code.getExceptionTable(), pos);
  1053. int maxstack = codeAttr.getMaxStack();
  1054. if (maxstack < stacksize)
  1055. codeAttr.setMaxStack(stacksize);
  1056. int maxlocals = codeAttr.getMaxLocals();
  1057. if (maxlocals < localsize)
  1058. codeAttr.setMaxLocals(localsize);
  1059. }
  1060. catch (BadBytecode e) {
  1061. throw new CannotCompileException(e);
  1062. }
  1063. }
  1064. }
  1065. private void modifyConstructors(ClassFile cf)
  1066. throws CannotCompileException, NotFoundException
  1067. {
  1068. if (fieldInitializers == null)
  1069. return;
  1070. ConstPool cp = cf.getConstPool();
  1071. List list = cf.getMethods();
  1072. int n = list.size();
  1073. for (int i = 0; i < n; ++i) {
  1074. MethodInfo minfo = (MethodInfo)list.get(i);
  1075. if (minfo.isConstructor()) {
  1076. CodeAttribute codeAttr = minfo.getCodeAttribute();
  1077. if (codeAttr != null)
  1078. try {
  1079. Bytecode init = new Bytecode(cp, 0,
  1080. codeAttr.getMaxLocals());
  1081. CtClass[] params
  1082. = Descriptor.getParameterTypes(
  1083. minfo.getDescriptor(),
  1084. classPool);
  1085. int stacksize = makeFieldInitializer(init, params);
  1086. insertAuxInitializer(codeAttr, init, stacksize);
  1087. }
  1088. catch (BadBytecode e) {
  1089. throw new CannotCompileException(e);
  1090. }
  1091. }
  1092. }
  1093. }
  1094. private static void insertAuxInitializer(CodeAttribute codeAttr,
  1095. Bytecode initializer,
  1096. int stacksize)
  1097. throws BadBytecode
  1098. {
  1099. CodeIterator it = codeAttr.iterator();
  1100. int index = it.skipSuperConstructor();
  1101. if (index < 0) {
  1102. index = it.skipThisConstructor();
  1103. if (index >= 0)
  1104. return; // this() is called.
  1105. // Neither this() or super() is called.
  1106. }
  1107. int pos = it.insertEx(initializer.get());
  1108. it.insert(initializer.getExceptionTable(), pos);
  1109. int maxstack = codeAttr.getMaxStack();
  1110. if (maxstack < stacksize)
  1111. codeAttr.setMaxStack(stacksize);
  1112. }
  1113. private int makeFieldInitializer(Bytecode code, CtClass[] parameters)
  1114. throws CannotCompileException, NotFoundException
  1115. {
  1116. int stacksize = 0;
  1117. Javac jv = new Javac(code, this);
  1118. try {
  1119. jv.recordParams(parameters, false);
  1120. }
  1121. catch (CompileError e) {
  1122. throw new CannotCompileException(e);
  1123. }
  1124. for (FieldInitLink fi = fieldInitializers; fi != null; fi = fi.next) {
  1125. CtField f = fi.field;
  1126. if (!Modifier.isStatic(f.getModifiers())) {
  1127. int s = fi.init.compile(f.getType(), f.getName(), code,
  1128. parameters, jv);
  1129. if (stacksize < s)
  1130. stacksize = s;
  1131. }
  1132. }
  1133. return stacksize;
  1134. }
  1135. // Methods used by CtNewWrappedMethod
  1136. Hashtable getHiddenMethods() {
  1137. if (hiddenMethods == null)
  1138. hiddenMethods = new Hashtable();
  1139. return hiddenMethods;
  1140. }
  1141. int getUniqueNumber() { return uniqueNumberSeed++; }
  1142. public String makeUniqueName(String prefix) {
  1143. HashMap table = new HashMap();
  1144. makeMemberList(table);
  1145. Set keys = table.keySet();
  1146. String[] methods = new String[keys.size()];
  1147. keys.toArray(methods);
  1148. if (notFindInArray(prefix, methods))
  1149. return prefix;
  1150. int i = 100;
  1151. String name;
  1152. do {
  1153. if (i > 999)
  1154. throw new RuntimeException("too many unique name");
  1155. name = prefix + i++;
  1156. } while (!notFindInArray(name, methods));
  1157. return name;
  1158. }
  1159. private static boolean notFindInArray(String prefix, String[] values) {
  1160. int len = values.length;
  1161. for (int i = 0; i < len; i++)
  1162. if (values[i].startsWith(prefix))
  1163. return false;
  1164. return true;
  1165. }
  1166. private void makeMemberList(HashMap table) {
  1167. int mod = getModifiers();
  1168. if (Modifier.isAbstract(mod) || Modifier.isInterface(mod))
  1169. try {
  1170. CtClass[] ifs = getInterfaces();
  1171. int size = ifs.length;
  1172. for (int i = 0; i < size; i++) {
  1173. CtClass ic =ifs[i];
  1174. if (ic != null && ic instanceof CtClassType)
  1175. ((CtClassType)ic).makeMemberList(table);
  1176. }
  1177. }
  1178. catch (NotFoundException e) {}
  1179. try {
  1180. CtClass s = getSuperclass();
  1181. if (s != null && s instanceof CtClassType)
  1182. ((CtClassType)s).makeMemberList(table);
  1183. }
  1184. catch (NotFoundException e) {}
  1185. List list = getClassFile2().getMethods();
  1186. int n = list.size();
  1187. for (int i = 0; i < n; i++) {
  1188. MethodInfo minfo = (MethodInfo)list.get(i);
  1189. table.put(minfo.getName(), this);
  1190. }
  1191. list = getClassFile2().getFields();
  1192. n = list.size();
  1193. for (int i = 0; i < n; i++) {
  1194. FieldInfo finfo = (FieldInfo)list.get(i);
  1195. table.put(finfo.getName(), this);
  1196. }
  1197. }
  1198. }
  1199. class FieldInitLink {
  1200. FieldInitLink next;
  1201. CtField field;
  1202. CtField.Initializer init;
  1203. FieldInitLink(CtField f, CtField.Initializer i) {
  1204. next = null;
  1205. field = f;
  1206. init = i;
  1207. }
  1208. }