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.

CtField.java 43KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist;
  16. import javassist.bytecode.*;
  17. import javassist.compiler.Javac;
  18. import javassist.compiler.SymbolTable;
  19. import javassist.compiler.CompileError;
  20. import javassist.compiler.ast.ASTree;
  21. import javassist.compiler.ast.IntConst;
  22. import javassist.compiler.ast.DoubleConst;
  23. import javassist.compiler.ast.StringL;
  24. /**
  25. * An instance of CtField represents a field.
  26. *
  27. * @see CtClass#getDeclaredFields()
  28. */
  29. public class CtField extends CtMember {
  30. static final String javaLangString = "java.lang.String";
  31. protected FieldInfo fieldInfo;
  32. /**
  33. * Creates a <code>CtField</code> object.
  34. * The created field must be added to a class
  35. * with <code>CtClass.addField()</code>.
  36. * An initial value of the field is specified
  37. * by a <code>CtField.Initializer</code> object.
  38. *
  39. * <p>If getter and setter methods are needed,
  40. * call <code>CtNewMethod.getter()</code> and
  41. * <code>CtNewMethod.setter()</code>.
  42. *
  43. * @param type field type
  44. * @param name field name
  45. * @param declaring the class to which the field will be added.
  46. *
  47. * @see CtClass#addField(CtField)
  48. * @see CtNewMethod#getter(String,CtField)
  49. * @see CtNewMethod#setter(String,CtField)
  50. * @see CtField.Initializer
  51. */
  52. public CtField(CtClass type, String name, CtClass declaring)
  53. throws CannotCompileException
  54. {
  55. this(Descriptor.of(type), name, declaring);
  56. }
  57. /**
  58. * Creates a copy of the given field.
  59. * The created field must be added to a class
  60. * with <code>CtClass.addField()</code>.
  61. * An initial value of the field is specified
  62. * by a <code>CtField.Initializer</code> object.
  63. *
  64. * <p>If getter and setter methods are needed,
  65. * call <code>CtNewMethod.getter()</code> and
  66. * <code>CtNewMethod.setter()</code>.
  67. *
  68. * @param src the original field
  69. * @param declaring the class to which the field will be added.
  70. * @see CtNewMethod#getter(String,CtField)
  71. * @see CtNewMethod#setter(String,CtField)
  72. * @see CtField.Initializer
  73. */
  74. public CtField(CtField src, CtClass declaring)
  75. throws CannotCompileException
  76. {
  77. this(src.fieldInfo.getDescriptor(), src.fieldInfo.getName(),
  78. declaring);
  79. }
  80. private CtField(String typeDesc, String name, CtClass clazz)
  81. throws CannotCompileException
  82. {
  83. super(clazz);
  84. next = null;
  85. ClassFile cf = clazz.getClassFile2();
  86. if (cf == null)
  87. throw new CannotCompileException("bad declaring class: "
  88. + clazz.getName());
  89. fieldInfo = new FieldInfo(cf.getConstPool(), name, typeDesc);
  90. }
  91. CtField(FieldInfo fi, CtClass clazz) {
  92. super(clazz);
  93. fieldInfo = fi;
  94. next = null;
  95. }
  96. /**
  97. * Returns a String representation of the object.
  98. */
  99. public String toString() {
  100. return getDeclaringClass().getName() + "." + getName()
  101. + ":" + fieldInfo.getDescriptor();
  102. }
  103. protected void extendToString(StringBuffer buffer) {
  104. buffer.append(' ');
  105. buffer.append(getName());
  106. buffer.append(' ');
  107. buffer.append(fieldInfo.getDescriptor());
  108. }
  109. /* Javac.CtFieldWithInit overrides.
  110. */
  111. protected ASTree getInitAST() { return null; }
  112. /* Called by CtClassType.addField().
  113. */
  114. Initializer getInit() {
  115. ASTree tree = getInitAST();
  116. if (tree == null)
  117. return null;
  118. else
  119. return Initializer.byExpr(tree);
  120. }
  121. /**
  122. * Compiles the given source code and creates a field.
  123. * Examples of the source code are:
  124. *
  125. * <ul><pre>
  126. * "public String name;"
  127. * "public int k = 3;"</pre></ul>
  128. *
  129. * <p>Note that the source code ends with <code>';'</code>
  130. * (semicolon).
  131. *
  132. * @param src the source text.
  133. * @param declaring the class to which the created field is added.
  134. */
  135. public static CtField make(String src, CtClass declaring)
  136. throws CannotCompileException
  137. {
  138. Javac compiler = new Javac(declaring);
  139. try {
  140. CtMember obj = compiler.compile(src);
  141. if (obj instanceof CtField)
  142. return (CtField)obj; // an instance of Javac.CtFieldWithInit
  143. }
  144. catch (CompileError e) {
  145. throw new CannotCompileException(e);
  146. }
  147. throw new CannotCompileException("not a field");
  148. }
  149. /**
  150. * Returns the FieldInfo representing the field in the class file.
  151. */
  152. public FieldInfo getFieldInfo() {
  153. declaringClass.checkModify();
  154. return fieldInfo;
  155. }
  156. /**
  157. * Returns the FieldInfo representing the field in the class
  158. * file (read only).
  159. * Normal applications do not need calling this method. Use
  160. * <code>getFieldInfo()</code>.
  161. *
  162. * <p>The <code>FieldInfo</code> object obtained by this method
  163. * is read only. Changes to this object might not be reflected
  164. * on a class file generated by <code>toBytecode()</code>,
  165. * <code>toClass()</code>, etc in <code>CtClass</code>.
  166. *
  167. * <p>This method is available even if the <code>CtClass</code>
  168. * containing this field is frozen. However, if the class is
  169. * frozen, the <code>FieldInfo</code> might be also pruned.
  170. *
  171. * @see #getFieldInfo()
  172. * @see CtClass#isFrozen()
  173. * @see CtClass#prune()
  174. */
  175. public FieldInfo getFieldInfo2() { return fieldInfo; }
  176. /**
  177. * Returns the class declaring the field.
  178. */
  179. public CtClass getDeclaringClass() {
  180. // this is redundant but for javadoc.
  181. return super.getDeclaringClass();
  182. }
  183. /**
  184. * Returns the name of the field.
  185. */
  186. public String getName() {
  187. return fieldInfo.getName();
  188. }
  189. /**
  190. * Changes the name of the field.
  191. */
  192. public void setName(String newName) {
  193. declaringClass.checkModify();
  194. fieldInfo.setName(newName);
  195. }
  196. /**
  197. * Returns the encoded modifiers of the field.
  198. *
  199. * @see Modifier
  200. */
  201. public int getModifiers() {
  202. return AccessFlag.toModifier(fieldInfo.getAccessFlags());
  203. }
  204. /**
  205. * Sets the encoded modifiers of the field.
  206. *
  207. * @see Modifier
  208. */
  209. public void setModifiers(int mod) {
  210. declaringClass.checkModify();
  211. fieldInfo.setAccessFlags(AccessFlag.of(mod));
  212. }
  213. /**
  214. * Returns the annotations associated with this field.
  215. *
  216. * @return an array of annotation-type objects.
  217. * @see CtMember#getAnnotations()
  218. */
  219. public Object[] getAnnotations() throws ClassNotFoundException {
  220. FieldInfo fi = getFieldInfo2();
  221. AnnotationsAttribute ainfo = (AnnotationsAttribute)
  222. fi.getAttribute(AnnotationsAttribute.invisibleTag);
  223. AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
  224. fi.getAttribute(AnnotationsAttribute.visibleTag);
  225. return CtClassType.toAnnotationType(getDeclaringClass().getClassPool(),
  226. ainfo, ainfo2);
  227. }
  228. /**
  229. * Returns the type of the field.
  230. */
  231. public CtClass getType() throws NotFoundException {
  232. return Descriptor.toCtClass(fieldInfo.getDescriptor(),
  233. declaringClass.getClassPool());
  234. }
  235. /**
  236. * Sets the type of the field.
  237. */
  238. public void setType(CtClass clazz) {
  239. declaringClass.checkModify();
  240. fieldInfo.setDescriptor(Descriptor.of(clazz));
  241. }
  242. /**
  243. * Returns the value of this field if it is a constant field.
  244. * This method works only if the field type is a primitive type
  245. * or <code>String</code> type. Otherwise, it returns <code>null</code>.
  246. * A constant field is <code>static</code> and <code>final</code>.
  247. *
  248. * @return a <code>Integer</code>, <code>Long</code>, <code>Float</code>,
  249. * <code>Double</code>, <code>Boolean</code>,
  250. * or <code>String</code> object
  251. * representing the constant value.
  252. * <code>null</code> if it is not a constant field
  253. * or if the field type is not a primitive type
  254. * or <code>String</code>.
  255. */
  256. public Object getConstantValue() {
  257. // When this method is modified,
  258. // see also getConstantFieldValue() in TypeChecker.
  259. int index = fieldInfo.getConstantValue();
  260. if (index == 0)
  261. return null;
  262. ConstPool cp = fieldInfo.getConstPool();
  263. switch (cp.getTag(index)) {
  264. case ConstPool.CONST_Long :
  265. return new Long(cp.getLongInfo(index));
  266. case ConstPool.CONST_Float :
  267. return new Float(cp.getFloatInfo(index));
  268. case ConstPool.CONST_Double :
  269. return new Double(cp.getDoubleInfo(index));
  270. case ConstPool.CONST_Integer :
  271. int value = cp.getIntegerInfo(index);
  272. // "Z" means boolean type.
  273. if ("Z".equals(fieldInfo.getDescriptor()))
  274. return new Boolean(value != 0);
  275. else
  276. return new Integer(value);
  277. case ConstPool.CONST_String :
  278. return cp.getStringInfo(index);
  279. default :
  280. throw new RuntimeException("bad tag: " + cp.getTag(index)
  281. + " at " + index);
  282. }
  283. }
  284. /**
  285. * Obtains an attribute with the given name.
  286. * If that attribute is not found in the class file, this
  287. * method returns null.
  288. *
  289. * @param name attribute name
  290. */
  291. public byte[] getAttribute(String name) {
  292. AttributeInfo ai = fieldInfo.getAttribute(name);
  293. if (ai == null)
  294. return null;
  295. else
  296. return ai.get();
  297. }
  298. /**
  299. * Adds an attribute. The attribute is saved in the class file.
  300. *
  301. * @param name attribute name
  302. * @param data attribute value
  303. */
  304. public void setAttribute(String name, byte[] data) {
  305. declaringClass.checkModify();
  306. fieldInfo.addAttribute(new AttributeInfo(fieldInfo.getConstPool(),
  307. name, data));
  308. }
  309. // inner classes
  310. /**
  311. * Instances of this class specify how to initialize a field.
  312. * <code>Initializer</code> is passed to
  313. * <code>CtClass.addField()</code> with a <code>CtField</code>.
  314. *
  315. * <p>This class cannot be instantiated with the <code>new</code> operator.
  316. * Factory methods such as <code>byParameter()</code> and
  317. * <code>byNew</code>
  318. * must be used for the instantiation. They create a new instance with
  319. * the given parameters and return it.
  320. *
  321. * @see CtClass#addField(CtField,CtField.Initializer)
  322. */
  323. public static abstract class Initializer {
  324. /**
  325. * Makes an initializer that assigns a constant integer value.
  326. * The field must be integer type.
  327. */
  328. public static Initializer constant(int i) {
  329. return new IntInitializer(i);
  330. }
  331. /**
  332. * Makes an initializer that assigns a constant long value.
  333. * The field must be long type.
  334. */
  335. public static Initializer constant(long l) {
  336. return new LongInitializer(l);
  337. }
  338. /**
  339. * Makes an initializer that assigns a constant double value.
  340. * The field must be double type.
  341. */
  342. public static Initializer constant(double d) {
  343. return new DoubleInitializer(d);
  344. }
  345. /**
  346. * Makes an initializer that assigns a constant string value.
  347. * The field must be <code>java.lang.String</code> type.
  348. */
  349. public static Initializer constant(String s) {
  350. return new StringInitializer(s);
  351. }
  352. /**
  353. * Makes an initializer using a constructor parameter.
  354. *
  355. * <p>The initial value is the
  356. * N-th parameter given to the constructor of the object including
  357. * the field. If the constructor takes less than N parameters,
  358. * the field is not initialized.
  359. * If the field is static, it is never initialized.
  360. *
  361. * @param nth the n-th (&gt;= 0) parameter is used as
  362. * the initial value.
  363. * If nth is 0, then the first parameter is
  364. * used.
  365. */
  366. public static Initializer byParameter(int nth) {
  367. ParamInitializer i = new ParamInitializer();
  368. i.nthParam = nth;
  369. return i;
  370. }
  371. /**
  372. * Makes an initializer creating a new object.
  373. *
  374. * <p>This initializer creates a new object and uses it as the initial
  375. * value of the field. The constructor of the created object receives
  376. * the parameter:
  377. *
  378. * <ul><code>Object obj</code> - the object including the field.<br>
  379. * </ul>
  380. *
  381. * <p>If the initialized field is static, then the constructor does
  382. * not receive any parameters.
  383. *
  384. * @param objectType the class instantiated for the initial value.
  385. */
  386. public static Initializer byNew(CtClass objectType) {
  387. NewInitializer i = new NewInitializer();
  388. i.objectType = objectType;
  389. i.stringParams = null;
  390. i.withConstructorParams = false;
  391. return i;
  392. }
  393. /**
  394. * Makes an initializer creating a new object.
  395. *
  396. * <p>This initializer creates a new object and uses it as the initial
  397. * value of the field. The constructor of the created object receives
  398. * the parameters:
  399. *
  400. * <ul><code>Object obj</code> - the object including the field.<br>
  401. * <code>String[] strs</code> - the character strings specified
  402. * by <code>stringParams</code><br>
  403. * </ul>
  404. *
  405. * <p>If the initialized field is static, then the constructor
  406. * receives only <code>strs</code>.
  407. *
  408. * @param objectType the class instantiated for the initial value.
  409. * @param stringParams the array of strings passed to the
  410. * constructor.
  411. */
  412. public static Initializer byNew(CtClass objectType,
  413. String[] stringParams) {
  414. NewInitializer i = new NewInitializer();
  415. i.objectType = objectType;
  416. i.stringParams = stringParams;
  417. i.withConstructorParams = false;
  418. return i;
  419. }
  420. /**
  421. * Makes an initializer creating a new object.
  422. *
  423. * <p>This initializer creates a new object and uses it as the initial
  424. * value of the field. The constructor of the created object receives
  425. * the parameters:
  426. *
  427. * <ul><code>Object obj</code> - the object including the field.<br>
  428. * <code>Object[] args</code> - the parameters passed to the
  429. * constructor of the object including the
  430. * filed.
  431. * </ul>
  432. *
  433. * <p>If the initialized field is static, then the constructor does
  434. * not receive any parameters.
  435. *
  436. * @param objectType the class instantiated for the initial value.
  437. *
  438. * @see javassist.CtField.Initializer#byNewArray(CtClass,int)
  439. * @see javassist.CtField.Initializer#byNewArray(CtClass,int[])
  440. */
  441. public static Initializer byNewWithParams(CtClass objectType) {
  442. NewInitializer i = new NewInitializer();
  443. i.objectType = objectType;
  444. i.stringParams = null;
  445. i.withConstructorParams = true;
  446. return i;
  447. }
  448. /**
  449. * Makes an initializer creating a new object.
  450. *
  451. * <p>This initializer creates a new object and uses it as the initial
  452. * value of the field. The constructor of the created object receives
  453. * the parameters:
  454. *
  455. * <ul><code>Object obj</code> - the object including the field.<br>
  456. * <code>String[] strs</code> - the character strings specified
  457. * by <code>stringParams</code><br>
  458. * <code>Object[] args</code> - the parameters passed to the
  459. * constructor of the object including the
  460. * filed.
  461. * </ul>
  462. *
  463. * <p>If the initialized field is static, then the constructor receives
  464. * only <code>strs</code>.
  465. *
  466. * @param objectType the class instantiated for the initial value.
  467. * @param stringParams the array of strings passed to the
  468. * constructor.
  469. */
  470. public static Initializer byNewWithParams(CtClass objectType,
  471. String[] stringParams) {
  472. NewInitializer i = new NewInitializer();
  473. i.objectType = objectType;
  474. i.stringParams = stringParams;
  475. i.withConstructorParams = true;
  476. return i;
  477. }
  478. /**
  479. * Makes an initializer calling a static method.
  480. *
  481. * <p>This initializer calls a static method and uses the returned
  482. * value as the initial value of the field.
  483. * The called method receives the parameters:
  484. *
  485. * <ul><code>Object obj</code> - the object including the field.<br>
  486. * </ul>
  487. *
  488. * <p>If the initialized field is static, then the method does
  489. * not receive any parameters.
  490. *
  491. * <p>The type of the returned value must be the same as the field
  492. * type.
  493. *
  494. * @param methodClass the class that the static method is
  495. * declared in.
  496. * @param methodName the name of the satic method.
  497. */
  498. public static Initializer byCall(CtClass methodClass,
  499. String methodName) {
  500. MethodInitializer i = new MethodInitializer();
  501. i.objectType = methodClass;
  502. i.methodName = methodName;
  503. i.stringParams = null;
  504. i.withConstructorParams = false;
  505. return i;
  506. }
  507. /**
  508. * Makes an initializer calling a static method.
  509. *
  510. * <p>This initializer calls a static method and uses the returned
  511. * value as the initial value of the field. The called method
  512. * receives the parameters:
  513. *
  514. * <ul><code>Object obj</code> - the object including the field.<br>
  515. * <code>String[] strs</code> - the character strings specified
  516. * by <code>stringParams</code><br>
  517. * </ul>
  518. *
  519. * <p>If the initialized field is static, then the method
  520. * receive only <code>strs</code>.
  521. *
  522. * <p>The type of the returned value must be the same as the field
  523. * type.
  524. *
  525. * @param methodClass the class that the static method is
  526. * declared in.
  527. * @param methodName the name of the satic method.
  528. * @param stringParams the array of strings passed to the
  529. * static method.
  530. */
  531. public static Initializer byCall(CtClass methodClass,
  532. String methodName,
  533. String[] stringParams) {
  534. MethodInitializer i = new MethodInitializer();
  535. i.objectType = methodClass;
  536. i.methodName = methodName;
  537. i.stringParams = stringParams;
  538. i.withConstructorParams = false;
  539. return i;
  540. }
  541. /**
  542. * Makes an initializer calling a static method.
  543. *
  544. * <p>This initializer calls a static method and uses the returned
  545. * value as the initial value of the field. The called method
  546. * receives the parameters:
  547. *
  548. * <ul><code>Object obj</code> - the object including the field.<br>
  549. * <code>Object[] args</code> - the parameters passed to the
  550. * constructor of the object including the
  551. * filed.
  552. * </ul>
  553. *
  554. * <p>If the initialized field is static, then the method does
  555. * not receive any parameters.
  556. *
  557. * <p>The type of the returned value must be the same as the field
  558. * type.
  559. *
  560. * @param methodClass the class that the static method is
  561. * declared in.
  562. * @param methodName the name of the satic method.
  563. */
  564. public static Initializer byCallWithParams(CtClass methodClass,
  565. String methodName) {
  566. MethodInitializer i = new MethodInitializer();
  567. i.objectType = methodClass;
  568. i.methodName = methodName;
  569. i.stringParams = null;
  570. i.withConstructorParams = true;
  571. return i;
  572. }
  573. /**
  574. * Makes an initializer calling a static method.
  575. *
  576. * <p>This initializer calls a static method and uses the returned
  577. * value as the initial value of the field. The called method
  578. * receives the parameters:
  579. *
  580. * <ul><code>Object obj</code> - the object including the field.<br>
  581. * <code>String[] strs</code> - the character strings specified
  582. * by <code>stringParams</code><br>
  583. * <code>Object[] args</code> - the parameters passed to the
  584. * constructor of the object including the
  585. * filed.
  586. * </ul>
  587. *
  588. * <p>If the initialized field is static, then the method
  589. * receive only <code>strs</code>.
  590. *
  591. * <p>The type of the returned value must be the same as the field
  592. * type.
  593. *
  594. * @param methodClass the class that the static method is
  595. * declared in.
  596. * @param methodName the name of the satic method.
  597. * @param stringParams the array of strings passed to the
  598. * static method.
  599. */
  600. public static Initializer byCallWithParams(CtClass methodClass,
  601. String methodName, String[] stringParams) {
  602. MethodInitializer i = new MethodInitializer();
  603. i.objectType = methodClass;
  604. i.methodName = methodName;
  605. i.stringParams = stringParams;
  606. i.withConstructorParams = true;
  607. return i;
  608. }
  609. /**
  610. * Makes an initializer creating a new array.
  611. *
  612. * @param type the type of the array.
  613. * @param size the size of the array.
  614. * @throws NotFoundException if the type of the array components
  615. * is not found.
  616. */
  617. public static Initializer byNewArray(CtClass type, int size)
  618. throws NotFoundException
  619. {
  620. return new ArrayInitializer(type.getComponentType(), size);
  621. }
  622. /**
  623. * Makes an initializer creating a new multi-dimensional array.
  624. *
  625. * @param type the type of the array.
  626. * @param sizes an <code>int</code> array of the size in every
  627. * dimension.
  628. * The first element is the size in the first
  629. * dimension. The second is in the second, etc.
  630. */
  631. public static Initializer byNewArray(CtClass type, int[] sizes) {
  632. return new MultiArrayInitializer(type, sizes);
  633. }
  634. /**
  635. * Makes an initializer.
  636. *
  637. * @param source initializer expression.
  638. */
  639. public static Initializer byExpr(String source) {
  640. return new CodeInitializer(source);
  641. }
  642. static Initializer byExpr(ASTree source) {
  643. return new PtreeInitializer(source);
  644. }
  645. // Check whether this initializer is valid for the field type.
  646. // If it is invaild, this method throws an exception.
  647. void check(CtClass type) throws CannotCompileException {}
  648. // produce codes for initialization
  649. abstract int compile(CtClass type, String name, Bytecode code,
  650. CtClass[] parameters, Javac drv)
  651. throws CannotCompileException;
  652. // produce codes for initialization
  653. abstract int compileIfStatic(CtClass type, String name,
  654. Bytecode code, Javac drv) throws CannotCompileException;
  655. // returns the index of CONSTANT_Integer_info etc
  656. // if the value is constant. Otherwise, 0.
  657. int getConstantValue(ConstPool cp, CtClass type) { return 0; }
  658. }
  659. static abstract class CodeInitializer0 extends Initializer {
  660. abstract void compileExpr(Javac drv) throws CompileError;
  661. int compile(CtClass type, String name, Bytecode code,
  662. CtClass[] parameters, Javac drv)
  663. throws CannotCompileException
  664. {
  665. try {
  666. code.addAload(0);
  667. compileExpr(drv);
  668. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  669. return code.getMaxStack();
  670. }
  671. catch (CompileError e) {
  672. throw new CannotCompileException(e);
  673. }
  674. }
  675. int compileIfStatic(CtClass type, String name, Bytecode code,
  676. Javac drv) throws CannotCompileException
  677. {
  678. try {
  679. compileExpr(drv);
  680. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  681. return code.getMaxStack();
  682. }
  683. catch (CompileError e) {
  684. throw new CannotCompileException(e);
  685. }
  686. }
  687. int getConstantValue2(ConstPool cp, CtClass type, ASTree tree) {
  688. if (type.isPrimitive()) {
  689. if (tree instanceof IntConst) {
  690. long value = ((IntConst)tree).get();
  691. if (type == CtClass.doubleType)
  692. return cp.addDoubleInfo((double)value);
  693. else if (type == CtClass.floatType)
  694. return cp.addFloatInfo((float)value);
  695. else if (type == CtClass.longType)
  696. return cp.addLongInfo(value);
  697. else if (type != CtClass.voidType)
  698. return cp.addIntegerInfo((int)value);
  699. }
  700. else if (tree instanceof DoubleConst) {
  701. double value = ((DoubleConst)tree).get();
  702. if (type == CtClass.floatType)
  703. return cp.addFloatInfo((float)value);
  704. else if (type == CtClass.doubleType)
  705. return cp.addDoubleInfo(value);
  706. }
  707. }
  708. else if (tree instanceof StringL
  709. && type.getName().equals(javaLangString))
  710. return cp.addStringInfo(((StringL)tree).get());
  711. return 0;
  712. }
  713. }
  714. static class CodeInitializer extends CodeInitializer0 {
  715. private String expression;
  716. CodeInitializer(String expr) { expression = expr; }
  717. void compileExpr(Javac drv) throws CompileError {
  718. drv.compileExpr(expression);
  719. }
  720. int getConstantValue(ConstPool cp, CtClass type) {
  721. try {
  722. ASTree t = Javac.parseExpr(expression, new SymbolTable());
  723. return getConstantValue2(cp, type, t);
  724. }
  725. catch (CompileError e) {
  726. return 0;
  727. }
  728. }
  729. }
  730. static class PtreeInitializer extends CodeInitializer0 {
  731. private ASTree expression;
  732. PtreeInitializer(ASTree expr) { expression = expr; }
  733. void compileExpr(Javac drv) throws CompileError {
  734. drv.compileExpr(expression);
  735. }
  736. int getConstantValue(ConstPool cp, CtClass type) {
  737. return getConstantValue2(cp, type, expression);
  738. }
  739. }
  740. /**
  741. * A field initialized with a parameter passed to the constructor
  742. * of the class containing that field.
  743. */
  744. static class ParamInitializer extends Initializer {
  745. int nthParam;
  746. ParamInitializer() {}
  747. int compile(CtClass type, String name, Bytecode code,
  748. CtClass[] parameters, Javac drv)
  749. throws CannotCompileException
  750. {
  751. if (parameters != null && nthParam < parameters.length) {
  752. code.addAload(0);
  753. int nth = nthParamToLocal(nthParam, parameters, false);
  754. int s = code.addLoad(nth, type) + 1;
  755. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  756. return s; // stack size
  757. }
  758. else
  759. return 0; // do not initialize
  760. }
  761. /**
  762. * Computes the index of the local variable that the n-th parameter
  763. * is assigned to.
  764. *
  765. * @param nth n-th parameter
  766. * @param params list of parameter types
  767. * @param isStatic true if the method is static.
  768. */
  769. static int nthParamToLocal(int nth, CtClass[] params,
  770. boolean isStatic) {
  771. CtClass longType = CtClass.longType;
  772. CtClass doubleType = CtClass.doubleType;
  773. int k;
  774. if (isStatic)
  775. k = 0;
  776. else
  777. k = 1; // 0 is THIS.
  778. for (int i = 0; i < nth; ++i) {
  779. CtClass type = params[i];
  780. if (type == longType || type == doubleType)
  781. k += 2;
  782. else
  783. ++k;
  784. }
  785. return k;
  786. }
  787. int compileIfStatic(CtClass type, String name, Bytecode code,
  788. Javac drv) throws CannotCompileException
  789. {
  790. return 0;
  791. }
  792. }
  793. /**
  794. * A field initialized with an object created by the new operator.
  795. */
  796. static class NewInitializer extends Initializer {
  797. CtClass objectType;
  798. String[] stringParams;
  799. boolean withConstructorParams;
  800. NewInitializer() {}
  801. /**
  802. * Produces codes in which a new object is created and assigned to
  803. * the field as the initial value.
  804. */
  805. int compile(CtClass type, String name, Bytecode code,
  806. CtClass[] parameters, Javac drv)
  807. throws CannotCompileException
  808. {
  809. int stacksize;
  810. code.addAload(0);
  811. code.addNew(objectType);
  812. code.add(Bytecode.DUP);
  813. code.addAload(0);
  814. if (stringParams == null)
  815. stacksize = 4;
  816. else
  817. stacksize = compileStringParameter(code) + 4;
  818. if (withConstructorParams)
  819. stacksize += CtNewWrappedMethod.compileParameterList(code,
  820. parameters, 1);
  821. code.addInvokespecial(objectType, "<init>", getDescriptor());
  822. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  823. return stacksize;
  824. }
  825. private String getDescriptor() {
  826. final String desc3
  827. = "(Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)V";
  828. if (stringParams == null)
  829. if (withConstructorParams)
  830. return "(Ljava/lang/Object;[Ljava/lang/Object;)V";
  831. else
  832. return "(Ljava/lang/Object;)V";
  833. else
  834. if (withConstructorParams)
  835. return desc3;
  836. else
  837. return "(Ljava/lang/Object;[Ljava/lang/String;)V";
  838. }
  839. /**
  840. * Produces codes for a static field.
  841. */
  842. int compileIfStatic(CtClass type, String name, Bytecode code,
  843. Javac drv) throws CannotCompileException
  844. {
  845. String desc;
  846. code.addNew(objectType);
  847. code.add(Bytecode.DUP);
  848. int stacksize = 2;
  849. if (stringParams == null)
  850. desc = "()V";
  851. else {
  852. desc = "([Ljava/lang/String;)V";
  853. stacksize += compileStringParameter(code);
  854. }
  855. code.addInvokespecial(objectType, "<init>", desc);
  856. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  857. return stacksize;
  858. }
  859. protected final int compileStringParameter(Bytecode code)
  860. throws CannotCompileException
  861. {
  862. int nparam = stringParams.length;
  863. code.addIconst(nparam);
  864. code.addAnewarray(javaLangString);
  865. for (int j = 0; j < nparam; ++j) {
  866. code.add(Bytecode.DUP); // dup
  867. code.addIconst(j); // iconst_<j>
  868. code.addLdc(stringParams[j]); // ldc ...
  869. code.add(Bytecode.AASTORE); // aastore
  870. }
  871. return 4;
  872. }
  873. }
  874. /**
  875. * A field initialized with the result of a static method call.
  876. */
  877. static class MethodInitializer extends NewInitializer {
  878. String methodName;
  879. // the method class is specified by objectType.
  880. MethodInitializer() {}
  881. /**
  882. * Produces codes in which a new object is created and assigned to
  883. * the field as the initial value.
  884. */
  885. int compile(CtClass type, String name, Bytecode code,
  886. CtClass[] parameters, Javac drv)
  887. throws CannotCompileException
  888. {
  889. int stacksize;
  890. code.addAload(0);
  891. code.addAload(0);
  892. if (stringParams == null)
  893. stacksize = 2;
  894. else
  895. stacksize = compileStringParameter(code) + 2;
  896. if (withConstructorParams)
  897. stacksize += CtNewWrappedMethod.compileParameterList(code,
  898. parameters, 1);
  899. String typeDesc = Descriptor.of(type);
  900. String mDesc = getDescriptor() + typeDesc;
  901. code.addInvokestatic(objectType, methodName, mDesc);
  902. code.addPutfield(Bytecode.THIS, name, typeDesc);
  903. return stacksize;
  904. }
  905. private String getDescriptor() {
  906. final String desc3
  907. = "(Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)";
  908. if (stringParams == null)
  909. if (withConstructorParams)
  910. return "(Ljava/lang/Object;[Ljava/lang/Object;)";
  911. else
  912. return "(Ljava/lang/Object;)";
  913. else
  914. if (withConstructorParams)
  915. return desc3;
  916. else
  917. return "(Ljava/lang/Object;[Ljava/lang/String;)";
  918. }
  919. /**
  920. * Produces codes for a static field.
  921. */
  922. int compileIfStatic(CtClass type, String name, Bytecode code,
  923. Javac drv) throws CannotCompileException
  924. {
  925. String desc;
  926. int stacksize = 1;
  927. if (stringParams == null)
  928. desc = "()";
  929. else {
  930. desc = "([Ljava/lang/String;)";
  931. stacksize += compileStringParameter(code);
  932. }
  933. String typeDesc = Descriptor.of(type);
  934. code.addInvokestatic(objectType, methodName, desc + typeDesc);
  935. code.addPutstatic(Bytecode.THIS, name, typeDesc);
  936. return stacksize;
  937. }
  938. }
  939. static class IntInitializer extends Initializer {
  940. int value;
  941. IntInitializer(int v) { value = v; }
  942. void check(CtClass type) throws CannotCompileException {
  943. if (type != CtClass.intType)
  944. throw new CannotCompileException("type mismatch");
  945. }
  946. int compile(CtClass type, String name, Bytecode code,
  947. CtClass[] parameters, Javac drv)
  948. throws CannotCompileException
  949. {
  950. code.addAload(0);
  951. code.addIconst(value);
  952. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  953. return 2; // stack size
  954. }
  955. int compileIfStatic(CtClass type, String name, Bytecode code,
  956. Javac drv) throws CannotCompileException
  957. {
  958. code.addIconst(value);
  959. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  960. return 1; // stack size
  961. }
  962. int getConstantValue(ConstPool cp, CtClass type) {
  963. if (type == CtClass.intType)
  964. return cp.addIntegerInfo(value);
  965. else
  966. return 0;
  967. }
  968. }
  969. static class LongInitializer extends Initializer {
  970. long value;
  971. LongInitializer(long v) { value = v; }
  972. void check(CtClass type) throws CannotCompileException {
  973. if (type != CtClass.longType)
  974. throw new CannotCompileException("type mismatch");
  975. }
  976. int compile(CtClass type, String name, Bytecode code,
  977. CtClass[] parameters, Javac drv)
  978. throws CannotCompileException
  979. {
  980. code.addAload(0);
  981. code.addLdc2w(value);
  982. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  983. return 3; // stack size
  984. }
  985. int compileIfStatic(CtClass type, String name, Bytecode code,
  986. Javac drv) throws CannotCompileException
  987. {
  988. code.addLdc2w(value);
  989. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  990. return 2; // stack size
  991. }
  992. int getConstantValue(ConstPool cp, CtClass type) {
  993. if (type == CtClass.longType)
  994. return cp.addLongInfo(value);
  995. else
  996. return 0;
  997. }
  998. }
  999. static class DoubleInitializer extends Initializer {
  1000. double value;
  1001. DoubleInitializer(double v) { value = v; }
  1002. void check(CtClass type) throws CannotCompileException {
  1003. if (type != CtClass.doubleType)
  1004. throw new CannotCompileException("type mismatch");
  1005. }
  1006. int compile(CtClass type, String name, Bytecode code,
  1007. CtClass[] parameters, Javac drv)
  1008. throws CannotCompileException
  1009. {
  1010. code.addAload(0);
  1011. code.addLdc2w(value);
  1012. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1013. return 3; // stack size
  1014. }
  1015. int compileIfStatic(CtClass type, String name, Bytecode code,
  1016. Javac drv) throws CannotCompileException
  1017. {
  1018. code.addLdc2w(value);
  1019. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1020. return 2; // stack size
  1021. }
  1022. int getConstantValue(ConstPool cp, CtClass type) {
  1023. if (type == CtClass.doubleType)
  1024. return cp.addDoubleInfo(value);
  1025. else
  1026. return 0;
  1027. }
  1028. }
  1029. static class StringInitializer extends Initializer {
  1030. String value;
  1031. StringInitializer(String v) { value = v; }
  1032. void check(CtClass type) throws CannotCompileException {
  1033. if (!type.getName().equals(javaLangString))
  1034. throw new CannotCompileException("type mismatch");
  1035. }
  1036. int compile(CtClass type, String name, Bytecode code,
  1037. CtClass[] parameters, Javac drv)
  1038. throws CannotCompileException
  1039. {
  1040. code.addAload(0);
  1041. code.addLdc(value);
  1042. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1043. return 2; // stack size
  1044. }
  1045. int compileIfStatic(CtClass type, String name, Bytecode code,
  1046. Javac drv) throws CannotCompileException
  1047. {
  1048. code.addLdc(value);
  1049. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1050. return 1; // stack size
  1051. }
  1052. int getConstantValue(ConstPool cp, CtClass type) {
  1053. if (type.getName().equals(javaLangString))
  1054. return cp.addStringInfo(value);
  1055. else
  1056. return 0;
  1057. }
  1058. }
  1059. static class ArrayInitializer extends Initializer {
  1060. CtClass type;
  1061. int size;
  1062. ArrayInitializer(CtClass t, int s) { type = t; size = s; }
  1063. private void addNewarray(Bytecode code) {
  1064. if (type.isPrimitive())
  1065. code.addNewarray(((CtPrimitiveType)type).getArrayType(),
  1066. size);
  1067. else
  1068. code.addAnewarray(type, size);
  1069. }
  1070. int compile(CtClass type, String name, Bytecode code,
  1071. CtClass[] parameters, Javac drv)
  1072. throws CannotCompileException
  1073. {
  1074. code.addAload(0);
  1075. addNewarray(code);
  1076. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1077. return 2; // stack size
  1078. }
  1079. int compileIfStatic(CtClass type, String name, Bytecode code,
  1080. Javac drv) throws CannotCompileException
  1081. {
  1082. addNewarray(code);
  1083. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1084. return 1; // stack size
  1085. }
  1086. }
  1087. static class MultiArrayInitializer extends Initializer {
  1088. CtClass type;
  1089. int[] dim;
  1090. MultiArrayInitializer(CtClass t, int[] d) { type = t; dim = d; }
  1091. void check(CtClass type) throws CannotCompileException {
  1092. if (!type.isArray())
  1093. throw new CannotCompileException("type mismatch");
  1094. }
  1095. int compile(CtClass type, String name, Bytecode code,
  1096. CtClass[] parameters, Javac drv)
  1097. throws CannotCompileException
  1098. {
  1099. code.addAload(0);
  1100. int s = code.addMultiNewarray(type, dim);
  1101. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1102. return s + 1; // stack size
  1103. }
  1104. int compileIfStatic(CtClass type, String name, Bytecode code,
  1105. Javac drv) throws CannotCompileException
  1106. {
  1107. int s = code.addMultiNewarray(type, dim);
  1108. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1109. return s; // stack size
  1110. }
  1111. }
  1112. }