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

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