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

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