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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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. * <ul><pre>
  134. * "public String name;"
  135. * "public int k = 3;"</pre></ul>
  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 class.
  223. *
  224. * @param clz the annotation class.
  225. * @return <code>true</code> if the annotation is found, otherwise <code>false</code>.
  226. * @since 3.11
  227. */
  228. public boolean hasAnnotation(Class clz) {
  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(clz, 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. public void setType(CtClass clazz) {
  347. declaringClass.checkModify();
  348. fieldInfo.setDescriptor(Descriptor.of(clazz));
  349. }
  350. /**
  351. * Returns the value of this field if it is a constant field.
  352. * This method works only if the field type is a primitive type
  353. * or <code>String</code> type. Otherwise, it returns <code>null</code>.
  354. * A constant field is <code>static</code> and <code>final</code>.
  355. *
  356. * @return a <code>Integer</code>, <code>Long</code>, <code>Float</code>,
  357. * <code>Double</code>, <code>Boolean</code>,
  358. * or <code>String</code> object
  359. * representing the constant value.
  360. * <code>null</code> if it is not a constant field
  361. * or if the field type is not a primitive type
  362. * or <code>String</code>.
  363. */
  364. public Object getConstantValue() {
  365. // When this method is modified,
  366. // see also getConstantFieldValue() in TypeChecker.
  367. int index = fieldInfo.getConstantValue();
  368. if (index == 0)
  369. return null;
  370. ConstPool cp = fieldInfo.getConstPool();
  371. switch (cp.getTag(index)) {
  372. case ConstPool.CONST_Long :
  373. return new Long(cp.getLongInfo(index));
  374. case ConstPool.CONST_Float :
  375. return new Float(cp.getFloatInfo(index));
  376. case ConstPool.CONST_Double :
  377. return new Double(cp.getDoubleInfo(index));
  378. case ConstPool.CONST_Integer :
  379. int value = cp.getIntegerInfo(index);
  380. // "Z" means boolean type.
  381. if ("Z".equals(fieldInfo.getDescriptor()))
  382. return new Boolean(value != 0);
  383. else
  384. return new Integer(value);
  385. case ConstPool.CONST_String :
  386. return cp.getStringInfo(index);
  387. default :
  388. throw new RuntimeException("bad tag: " + cp.getTag(index)
  389. + " at " + index);
  390. }
  391. }
  392. /**
  393. * Obtains an attribute with the given name.
  394. * If that attribute is not found in the class file, this
  395. * method returns null.
  396. *
  397. * <p>Note that an attribute is a data block specified by
  398. * the class file format.
  399. * See {@link javassist.bytecode.AttributeInfo}.
  400. *
  401. * @param name attribute name
  402. */
  403. public byte[] getAttribute(String name) {
  404. AttributeInfo ai = fieldInfo.getAttribute(name);
  405. if (ai == null)
  406. return null;
  407. else
  408. return ai.get();
  409. }
  410. /**
  411. * Adds an attribute. The attribute is saved in the class file.
  412. *
  413. * <p>Note that an attribute is a data block specified by
  414. * the class file format.
  415. * See {@link javassist.bytecode.AttributeInfo}.
  416. *
  417. * @param name attribute name
  418. * @param data attribute value
  419. */
  420. public void setAttribute(String name, byte[] data) {
  421. declaringClass.checkModify();
  422. fieldInfo.addAttribute(new AttributeInfo(fieldInfo.getConstPool(),
  423. name, data));
  424. }
  425. // inner classes
  426. /**
  427. * Instances of this class specify how to initialize a field.
  428. * <code>Initializer</code> is passed to
  429. * <code>CtClass.addField()</code> with a <code>CtField</code>.
  430. *
  431. * <p>This class cannot be instantiated with the <code>new</code> operator.
  432. * Factory methods such as <code>byParameter()</code> and
  433. * <code>byNew</code>
  434. * must be used for the instantiation. They create a new instance with
  435. * the given parameters and return it.
  436. *
  437. * @see CtClass#addField(CtField,CtField.Initializer)
  438. */
  439. public static abstract class Initializer {
  440. /**
  441. * Makes an initializer that assigns a constant integer value.
  442. * The field must be integer, short, char, or byte type.
  443. */
  444. public static Initializer constant(int i) {
  445. return new IntInitializer(i);
  446. }
  447. /**
  448. * Makes an initializer that assigns a constant boolean value.
  449. * The field must be boolean type.
  450. */
  451. public static Initializer constant(boolean b) {
  452. return new IntInitializer(b ? 1 : 0);
  453. }
  454. /**
  455. * Makes an initializer that assigns a constant long value.
  456. * The field must be long type.
  457. */
  458. public static Initializer constant(long l) {
  459. return new LongInitializer(l);
  460. }
  461. /**
  462. * Makes an initializer that assigns a constant float value.
  463. * The field must be float type.
  464. */
  465. public static Initializer constant(float l) {
  466. return new FloatInitializer(l);
  467. }
  468. /**
  469. * Makes an initializer that assigns a constant double value.
  470. * The field must be double type.
  471. */
  472. public static Initializer constant(double d) {
  473. return new DoubleInitializer(d);
  474. }
  475. /**
  476. * Makes an initializer that assigns a constant string value.
  477. * The field must be <code>java.lang.String</code> type.
  478. */
  479. public static Initializer constant(String s) {
  480. return new StringInitializer(s);
  481. }
  482. /**
  483. * Makes an initializer using a constructor parameter.
  484. *
  485. * <p>The initial value is the
  486. * N-th parameter given to the constructor of the object including
  487. * the field. If the constructor takes less than N parameters,
  488. * the field is not initialized.
  489. * If the field is static, it is never initialized.
  490. *
  491. * @param nth the n-th (&gt;= 0) parameter is used as
  492. * the initial value.
  493. * If nth is 0, then the first parameter is
  494. * used.
  495. */
  496. public static Initializer byParameter(int nth) {
  497. ParamInitializer i = new ParamInitializer();
  498. i.nthParam = nth;
  499. return i;
  500. }
  501. /**
  502. * Makes an initializer creating a new object.
  503. *
  504. * <p>This initializer creates a new object and uses it as the initial
  505. * value of the field. The constructor of the created object receives
  506. * the parameter:
  507. *
  508. * <ul><code>Object obj</code> - the object including the field.<br>
  509. * </ul>
  510. *
  511. * <p>If the initialized field is static, then the constructor does
  512. * not receive any parameters.
  513. *
  514. * @param objectType the class instantiated for the initial value.
  515. */
  516. public static Initializer byNew(CtClass objectType) {
  517. NewInitializer i = new NewInitializer();
  518. i.objectType = objectType;
  519. i.stringParams = null;
  520. i.withConstructorParams = false;
  521. return i;
  522. }
  523. /**
  524. * Makes an initializer creating a new object.
  525. *
  526. * <p>This initializer creates a new object and uses it as the initial
  527. * value of the field. The constructor of the created object receives
  528. * the parameters:
  529. *
  530. * <ul><code>Object obj</code> - the object including the field.<br>
  531. * <code>String[] strs</code> - the character strings specified
  532. * by <code>stringParams</code><br>
  533. * </ul>
  534. *
  535. * <p>If the initialized field is static, then the constructor
  536. * receives only <code>strs</code>.
  537. *
  538. * @param objectType the class instantiated for the initial value.
  539. * @param stringParams the array of strings passed to the
  540. * constructor.
  541. */
  542. public static Initializer byNew(CtClass objectType,
  543. String[] stringParams) {
  544. NewInitializer i = new NewInitializer();
  545. i.objectType = objectType;
  546. i.stringParams = stringParams;
  547. i.withConstructorParams = false;
  548. return i;
  549. }
  550. /**
  551. * Makes an initializer creating a new object.
  552. *
  553. * <p>This initializer creates a new object and uses it as the initial
  554. * value of the field. The constructor of the created object receives
  555. * the parameters:
  556. *
  557. * <ul><code>Object obj</code> - the object including the field.<br>
  558. * <code>Object[] args</code> - the parameters passed to the
  559. * constructor of the object including the
  560. * filed.
  561. * </ul>
  562. *
  563. * <p>If the initialized field is static, then the constructor does
  564. * not receive any parameters.
  565. *
  566. * @param objectType the class instantiated for the initial value.
  567. *
  568. * @see javassist.CtField.Initializer#byNewArray(CtClass,int)
  569. * @see javassist.CtField.Initializer#byNewArray(CtClass,int[])
  570. */
  571. public static Initializer byNewWithParams(CtClass objectType) {
  572. NewInitializer i = new NewInitializer();
  573. i.objectType = objectType;
  574. i.stringParams = null;
  575. i.withConstructorParams = true;
  576. return i;
  577. }
  578. /**
  579. * Makes an initializer creating a new object.
  580. *
  581. * <p>This initializer creates a new object and uses it as the initial
  582. * value of the field. The constructor of the created object receives
  583. * the parameters:
  584. *
  585. * <ul><code>Object obj</code> - the object including the field.<br>
  586. * <code>String[] strs</code> - the character strings specified
  587. * by <code>stringParams</code><br>
  588. * <code>Object[] args</code> - the parameters passed to the
  589. * constructor of the object including the
  590. * filed.
  591. * </ul>
  592. *
  593. * <p>If the initialized field is static, then the constructor receives
  594. * only <code>strs</code>.
  595. *
  596. * @param objectType the class instantiated for the initial value.
  597. * @param stringParams the array of strings passed to the
  598. * constructor.
  599. */
  600. public static Initializer byNewWithParams(CtClass objectType,
  601. String[] stringParams) {
  602. NewInitializer i = new NewInitializer();
  603. i.objectType = objectType;
  604. i.stringParams = stringParams;
  605. i.withConstructorParams = true;
  606. return i;
  607. }
  608. /**
  609. * Makes an initializer calling a static method.
  610. *
  611. * <p>This initializer calls a static method and uses the returned
  612. * value as the initial value of the field.
  613. * The called method receives the parameters:
  614. *
  615. * <ul><code>Object obj</code> - the object including the field.<br>
  616. * </ul>
  617. *
  618. * <p>If the initialized field is static, then the method does
  619. * not receive any parameters.
  620. *
  621. * <p>The type of the returned value must be the same as the field
  622. * type.
  623. *
  624. * @param methodClass the class that the static method is
  625. * declared in.
  626. * @param methodName the name of the satic method.
  627. */
  628. public static Initializer byCall(CtClass methodClass,
  629. String methodName) {
  630. MethodInitializer i = new MethodInitializer();
  631. i.objectType = methodClass;
  632. i.methodName = methodName;
  633. i.stringParams = null;
  634. i.withConstructorParams = false;
  635. return i;
  636. }
  637. /**
  638. * Makes an initializer calling a static method.
  639. *
  640. * <p>This initializer calls a static method and uses the returned
  641. * value as the initial value of the field. The called method
  642. * receives the parameters:
  643. *
  644. * <ul><code>Object obj</code> - the object including the field.<br>
  645. * <code>String[] strs</code> - the character strings specified
  646. * by <code>stringParams</code><br>
  647. * </ul>
  648. *
  649. * <p>If the initialized field is static, then the method
  650. * receive only <code>strs</code>.
  651. *
  652. * <p>The type of the returned value must be the same as the field
  653. * type.
  654. *
  655. * @param methodClass the class that the static method is
  656. * declared in.
  657. * @param methodName the name of the satic method.
  658. * @param stringParams the array of strings passed to the
  659. * static method.
  660. */
  661. public static Initializer byCall(CtClass methodClass,
  662. String methodName,
  663. String[] stringParams) {
  664. MethodInitializer i = new MethodInitializer();
  665. i.objectType = methodClass;
  666. i.methodName = methodName;
  667. i.stringParams = stringParams;
  668. i.withConstructorParams = false;
  669. return i;
  670. }
  671. /**
  672. * Makes an initializer calling a static method.
  673. *
  674. * <p>This initializer calls a static method and uses the returned
  675. * value as the initial value of the field. The called method
  676. * receives the parameters:
  677. *
  678. * <ul><code>Object obj</code> - the object including the field.<br>
  679. * <code>Object[] args</code> - the parameters passed to the
  680. * constructor of the object including the
  681. * filed.
  682. * </ul>
  683. *
  684. * <p>If the initialized field is static, then the method does
  685. * not receive any parameters.
  686. *
  687. * <p>The type of the returned value must be the same as the field
  688. * type.
  689. *
  690. * @param methodClass the class that the static method is
  691. * declared in.
  692. * @param methodName the name of the satic method.
  693. */
  694. public static Initializer byCallWithParams(CtClass methodClass,
  695. String methodName) {
  696. MethodInitializer i = new MethodInitializer();
  697. i.objectType = methodClass;
  698. i.methodName = methodName;
  699. i.stringParams = null;
  700. i.withConstructorParams = true;
  701. return i;
  702. }
  703. /**
  704. * Makes an initializer calling a static method.
  705. *
  706. * <p>This initializer calls a static method and uses the returned
  707. * value as the initial value of the field. The called method
  708. * receives the parameters:
  709. *
  710. * <ul><code>Object obj</code> - the object including the field.<br>
  711. * <code>String[] strs</code> - the character strings specified
  712. * by <code>stringParams</code><br>
  713. * <code>Object[] args</code> - the parameters passed to the
  714. * constructor of the object including the
  715. * filed.
  716. * </ul>
  717. *
  718. * <p>If the initialized field is static, then the method
  719. * receive only <code>strs</code>.
  720. *
  721. * <p>The type of the returned value must be the same as the field
  722. * type.
  723. *
  724. * @param methodClass the class that the static method is
  725. * declared in.
  726. * @param methodName the name of the satic method.
  727. * @param stringParams the array of strings passed to the
  728. * static method.
  729. */
  730. public static Initializer byCallWithParams(CtClass methodClass,
  731. String methodName, String[] stringParams) {
  732. MethodInitializer i = new MethodInitializer();
  733. i.objectType = methodClass;
  734. i.methodName = methodName;
  735. i.stringParams = stringParams;
  736. i.withConstructorParams = true;
  737. return i;
  738. }
  739. /**
  740. * Makes an initializer creating a new array.
  741. *
  742. * @param type the type of the array.
  743. * @param size the size of the array.
  744. * @throws NotFoundException if the type of the array components
  745. * is not found.
  746. */
  747. public static Initializer byNewArray(CtClass type, int size)
  748. throws NotFoundException
  749. {
  750. return new ArrayInitializer(type.getComponentType(), size);
  751. }
  752. /**
  753. * Makes an initializer creating a new multi-dimensional array.
  754. *
  755. * @param type the type of the array.
  756. * @param sizes an <code>int</code> array of the size in every
  757. * dimension.
  758. * The first element is the size in the first
  759. * dimension. The second is in the second, etc.
  760. */
  761. public static Initializer byNewArray(CtClass type, int[] sizes) {
  762. return new MultiArrayInitializer(type, sizes);
  763. }
  764. /**
  765. * Makes an initializer.
  766. *
  767. * @param source initializer expression.
  768. */
  769. public static Initializer byExpr(String source) {
  770. return new CodeInitializer(source);
  771. }
  772. static Initializer byExpr(ASTree source) {
  773. return new PtreeInitializer(source);
  774. }
  775. // Check whether this initializer is valid for the field type.
  776. // If it is invaild, this method throws an exception.
  777. void check(String desc) throws CannotCompileException {}
  778. // produce codes for initialization
  779. abstract int compile(CtClass type, String name, Bytecode code,
  780. CtClass[] parameters, Javac drv)
  781. throws CannotCompileException;
  782. // produce codes for initialization
  783. abstract int compileIfStatic(CtClass type, String name,
  784. Bytecode code, Javac drv) throws CannotCompileException;
  785. // returns the index of CONSTANT_Integer_info etc
  786. // if the value is constant. Otherwise, 0.
  787. int getConstantValue(ConstPool cp, CtClass type) { return 0; }
  788. }
  789. static abstract class CodeInitializer0 extends Initializer {
  790. abstract void compileExpr(Javac drv) throws CompileError;
  791. int compile(CtClass type, String name, Bytecode code,
  792. CtClass[] parameters, Javac drv)
  793. throws CannotCompileException
  794. {
  795. try {
  796. code.addAload(0);
  797. compileExpr(drv);
  798. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  799. return code.getMaxStack();
  800. }
  801. catch (CompileError e) {
  802. throw new CannotCompileException(e);
  803. }
  804. }
  805. int compileIfStatic(CtClass type, String name, Bytecode code,
  806. Javac drv) throws CannotCompileException
  807. {
  808. try {
  809. compileExpr(drv);
  810. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  811. return code.getMaxStack();
  812. }
  813. catch (CompileError e) {
  814. throw new CannotCompileException(e);
  815. }
  816. }
  817. int getConstantValue2(ConstPool cp, CtClass type, ASTree tree) {
  818. if (type.isPrimitive()) {
  819. if (tree instanceof IntConst) {
  820. long value = ((IntConst)tree).get();
  821. if (type == CtClass.doubleType)
  822. return cp.addDoubleInfo((double)value);
  823. else if (type == CtClass.floatType)
  824. return cp.addFloatInfo((float)value);
  825. else if (type == CtClass.longType)
  826. return cp.addLongInfo(value);
  827. else if (type != CtClass.voidType)
  828. return cp.addIntegerInfo((int)value);
  829. }
  830. else if (tree instanceof DoubleConst) {
  831. double value = ((DoubleConst)tree).get();
  832. if (type == CtClass.floatType)
  833. return cp.addFloatInfo((float)value);
  834. else if (type == CtClass.doubleType)
  835. return cp.addDoubleInfo(value);
  836. }
  837. }
  838. else if (tree instanceof StringL
  839. && type.getName().equals(javaLangString))
  840. return cp.addStringInfo(((StringL)tree).get());
  841. return 0;
  842. }
  843. }
  844. static class CodeInitializer extends CodeInitializer0 {
  845. private String expression;
  846. CodeInitializer(String expr) { expression = expr; }
  847. void compileExpr(Javac drv) throws CompileError {
  848. drv.compileExpr(expression);
  849. }
  850. int getConstantValue(ConstPool cp, CtClass type) {
  851. try {
  852. ASTree t = Javac.parseExpr(expression, new SymbolTable());
  853. return getConstantValue2(cp, type, t);
  854. }
  855. catch (CompileError e) {
  856. return 0;
  857. }
  858. }
  859. }
  860. static class PtreeInitializer extends CodeInitializer0 {
  861. private ASTree expression;
  862. PtreeInitializer(ASTree expr) { expression = expr; }
  863. void compileExpr(Javac drv) throws CompileError {
  864. drv.compileExpr(expression);
  865. }
  866. int getConstantValue(ConstPool cp, CtClass type) {
  867. return getConstantValue2(cp, type, expression);
  868. }
  869. }
  870. /**
  871. * A field initialized with a parameter passed to the constructor
  872. * of the class containing that field.
  873. */
  874. static class ParamInitializer extends Initializer {
  875. int nthParam;
  876. ParamInitializer() {}
  877. int compile(CtClass type, String name, Bytecode code,
  878. CtClass[] parameters, Javac drv)
  879. throws CannotCompileException
  880. {
  881. if (parameters != null && nthParam < parameters.length) {
  882. code.addAload(0);
  883. int nth = nthParamToLocal(nthParam, parameters, false);
  884. int s = code.addLoad(nth, type) + 1;
  885. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  886. return s; // stack size
  887. }
  888. else
  889. return 0; // do not initialize
  890. }
  891. /**
  892. * Computes the index of the local variable that the n-th parameter
  893. * is assigned to.
  894. *
  895. * @param nth n-th parameter
  896. * @param params list of parameter types
  897. * @param isStatic true if the method is static.
  898. */
  899. static int nthParamToLocal(int nth, CtClass[] params,
  900. boolean isStatic) {
  901. CtClass longType = CtClass.longType;
  902. CtClass doubleType = CtClass.doubleType;
  903. int k;
  904. if (isStatic)
  905. k = 0;
  906. else
  907. k = 1; // 0 is THIS.
  908. for (int i = 0; i < nth; ++i) {
  909. CtClass type = params[i];
  910. if (type == longType || type == doubleType)
  911. k += 2;
  912. else
  913. ++k;
  914. }
  915. return k;
  916. }
  917. int compileIfStatic(CtClass type, String name, Bytecode code,
  918. Javac drv) throws CannotCompileException
  919. {
  920. return 0;
  921. }
  922. }
  923. /**
  924. * A field initialized with an object created by the new operator.
  925. */
  926. static class NewInitializer extends Initializer {
  927. CtClass objectType;
  928. String[] stringParams;
  929. boolean withConstructorParams;
  930. NewInitializer() {}
  931. /**
  932. * Produces codes in which a new object is created and assigned to
  933. * the field as the initial value.
  934. */
  935. int compile(CtClass type, String name, Bytecode code,
  936. CtClass[] parameters, Javac drv)
  937. throws CannotCompileException
  938. {
  939. int stacksize;
  940. code.addAload(0);
  941. code.addNew(objectType);
  942. code.add(Bytecode.DUP);
  943. code.addAload(0);
  944. if (stringParams == null)
  945. stacksize = 4;
  946. else
  947. stacksize = compileStringParameter(code) + 4;
  948. if (withConstructorParams)
  949. stacksize += CtNewWrappedMethod.compileParameterList(code,
  950. parameters, 1);
  951. code.addInvokespecial(objectType, "<init>", getDescriptor());
  952. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  953. return stacksize;
  954. }
  955. private String getDescriptor() {
  956. final String desc3
  957. = "(Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)V";
  958. if (stringParams == null)
  959. if (withConstructorParams)
  960. return "(Ljava/lang/Object;[Ljava/lang/Object;)V";
  961. else
  962. return "(Ljava/lang/Object;)V";
  963. else
  964. if (withConstructorParams)
  965. return desc3;
  966. else
  967. return "(Ljava/lang/Object;[Ljava/lang/String;)V";
  968. }
  969. /**
  970. * Produces codes for a static field.
  971. */
  972. int compileIfStatic(CtClass type, String name, Bytecode code,
  973. Javac drv) throws CannotCompileException
  974. {
  975. String desc;
  976. code.addNew(objectType);
  977. code.add(Bytecode.DUP);
  978. int stacksize = 2;
  979. if (stringParams == null)
  980. desc = "()V";
  981. else {
  982. desc = "([Ljava/lang/String;)V";
  983. stacksize += compileStringParameter(code);
  984. }
  985. code.addInvokespecial(objectType, "<init>", desc);
  986. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  987. return stacksize;
  988. }
  989. protected final int compileStringParameter(Bytecode code)
  990. throws CannotCompileException
  991. {
  992. int nparam = stringParams.length;
  993. code.addIconst(nparam);
  994. code.addAnewarray(javaLangString);
  995. for (int j = 0; j < nparam; ++j) {
  996. code.add(Bytecode.DUP); // dup
  997. code.addIconst(j); // iconst_<j>
  998. code.addLdc(stringParams[j]); // ldc ...
  999. code.add(Bytecode.AASTORE); // aastore
  1000. }
  1001. return 4;
  1002. }
  1003. }
  1004. /**
  1005. * A field initialized with the result of a static method call.
  1006. */
  1007. static class MethodInitializer extends NewInitializer {
  1008. String methodName;
  1009. // the method class is specified by objectType.
  1010. MethodInitializer() {}
  1011. /**
  1012. * Produces codes in which a new object is created and assigned to
  1013. * the field as the initial value.
  1014. */
  1015. int compile(CtClass type, String name, Bytecode code,
  1016. CtClass[] parameters, Javac drv)
  1017. throws CannotCompileException
  1018. {
  1019. int stacksize;
  1020. code.addAload(0);
  1021. code.addAload(0);
  1022. if (stringParams == null)
  1023. stacksize = 2;
  1024. else
  1025. stacksize = compileStringParameter(code) + 2;
  1026. if (withConstructorParams)
  1027. stacksize += CtNewWrappedMethod.compileParameterList(code,
  1028. parameters, 1);
  1029. String typeDesc = Descriptor.of(type);
  1030. String mDesc = getDescriptor() + typeDesc;
  1031. code.addInvokestatic(objectType, methodName, mDesc);
  1032. code.addPutfield(Bytecode.THIS, name, typeDesc);
  1033. return stacksize;
  1034. }
  1035. private String getDescriptor() {
  1036. final String desc3
  1037. = "(Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)";
  1038. if (stringParams == null)
  1039. if (withConstructorParams)
  1040. return "(Ljava/lang/Object;[Ljava/lang/Object;)";
  1041. else
  1042. return "(Ljava/lang/Object;)";
  1043. else
  1044. if (withConstructorParams)
  1045. return desc3;
  1046. else
  1047. return "(Ljava/lang/Object;[Ljava/lang/String;)";
  1048. }
  1049. /**
  1050. * Produces codes for a static field.
  1051. */
  1052. int compileIfStatic(CtClass type, String name, Bytecode code,
  1053. Javac drv) throws CannotCompileException
  1054. {
  1055. String desc;
  1056. int stacksize = 1;
  1057. if (stringParams == null)
  1058. desc = "()";
  1059. else {
  1060. desc = "([Ljava/lang/String;)";
  1061. stacksize += compileStringParameter(code);
  1062. }
  1063. String typeDesc = Descriptor.of(type);
  1064. code.addInvokestatic(objectType, methodName, desc + typeDesc);
  1065. code.addPutstatic(Bytecode.THIS, name, typeDesc);
  1066. return stacksize;
  1067. }
  1068. }
  1069. static class IntInitializer extends Initializer {
  1070. int value;
  1071. IntInitializer(int v) { value = v; }
  1072. void check(String desc) throws CannotCompileException {
  1073. char c = desc.charAt(0);
  1074. if (c != 'I' && c != 'S' && c != 'B' && c != 'C' && c != 'Z')
  1075. throw new CannotCompileException("type mismatch");
  1076. }
  1077. int compile(CtClass type, String name, Bytecode code,
  1078. CtClass[] parameters, Javac drv)
  1079. throws CannotCompileException
  1080. {
  1081. code.addAload(0);
  1082. code.addIconst(value);
  1083. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1084. return 2; // stack size
  1085. }
  1086. int compileIfStatic(CtClass type, String name, Bytecode code,
  1087. Javac drv) throws CannotCompileException
  1088. {
  1089. code.addIconst(value);
  1090. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1091. return 1; // stack size
  1092. }
  1093. int getConstantValue(ConstPool cp, CtClass type) {
  1094. return cp.addIntegerInfo(value);
  1095. }
  1096. }
  1097. static class LongInitializer extends Initializer {
  1098. long value;
  1099. LongInitializer(long v) { value = v; }
  1100. void check(String desc) throws CannotCompileException {
  1101. if (!desc.equals("J"))
  1102. throw new CannotCompileException("type mismatch");
  1103. }
  1104. int compile(CtClass type, String name, Bytecode code,
  1105. CtClass[] parameters, Javac drv)
  1106. throws CannotCompileException
  1107. {
  1108. code.addAload(0);
  1109. code.addLdc2w(value);
  1110. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1111. return 3; // stack size
  1112. }
  1113. int compileIfStatic(CtClass type, String name, Bytecode code,
  1114. Javac drv) throws CannotCompileException
  1115. {
  1116. code.addLdc2w(value);
  1117. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1118. return 2; // stack size
  1119. }
  1120. int getConstantValue(ConstPool cp, CtClass type) {
  1121. if (type == CtClass.longType)
  1122. return cp.addLongInfo(value);
  1123. else
  1124. return 0;
  1125. }
  1126. }
  1127. static class FloatInitializer extends Initializer {
  1128. float value;
  1129. FloatInitializer(float v) { value = v; }
  1130. void check(String desc) throws CannotCompileException {
  1131. if (!desc.equals("F"))
  1132. throw new CannotCompileException("type mismatch");
  1133. }
  1134. int compile(CtClass type, String name, Bytecode code,
  1135. CtClass[] parameters, Javac drv)
  1136. throws CannotCompileException
  1137. {
  1138. code.addAload(0);
  1139. code.addFconst(value);
  1140. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1141. return 3; // stack size
  1142. }
  1143. int compileIfStatic(CtClass type, String name, Bytecode code,
  1144. Javac drv) throws CannotCompileException
  1145. {
  1146. code.addFconst(value);
  1147. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1148. return 2; // stack size
  1149. }
  1150. int getConstantValue(ConstPool cp, CtClass type) {
  1151. if (type == CtClass.floatType)
  1152. return cp.addFloatInfo(value);
  1153. else
  1154. return 0;
  1155. }
  1156. }
  1157. static class DoubleInitializer extends Initializer {
  1158. double value;
  1159. DoubleInitializer(double v) { value = v; }
  1160. void check(String desc) throws CannotCompileException {
  1161. if (!desc.equals("D"))
  1162. throw new CannotCompileException("type mismatch");
  1163. }
  1164. int compile(CtClass type, String name, Bytecode code,
  1165. CtClass[] parameters, Javac drv)
  1166. throws CannotCompileException
  1167. {
  1168. code.addAload(0);
  1169. code.addLdc2w(value);
  1170. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1171. return 3; // stack size
  1172. }
  1173. int compileIfStatic(CtClass type, String name, Bytecode code,
  1174. Javac drv) throws CannotCompileException
  1175. {
  1176. code.addLdc2w(value);
  1177. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1178. return 2; // stack size
  1179. }
  1180. int getConstantValue(ConstPool cp, CtClass type) {
  1181. if (type == CtClass.doubleType)
  1182. return cp.addDoubleInfo(value);
  1183. else
  1184. return 0;
  1185. }
  1186. }
  1187. static class StringInitializer extends Initializer {
  1188. String value;
  1189. StringInitializer(String v) { value = v; }
  1190. int compile(CtClass type, String name, Bytecode code,
  1191. CtClass[] parameters, Javac drv)
  1192. throws CannotCompileException
  1193. {
  1194. code.addAload(0);
  1195. code.addLdc(value);
  1196. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1197. return 2; // stack size
  1198. }
  1199. int compileIfStatic(CtClass type, String name, Bytecode code,
  1200. Javac drv) throws CannotCompileException
  1201. {
  1202. code.addLdc(value);
  1203. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1204. return 1; // stack size
  1205. }
  1206. int getConstantValue(ConstPool cp, CtClass type) {
  1207. if (type.getName().equals(javaLangString))
  1208. return cp.addStringInfo(value);
  1209. else
  1210. return 0;
  1211. }
  1212. }
  1213. static class ArrayInitializer extends Initializer {
  1214. CtClass type;
  1215. int size;
  1216. ArrayInitializer(CtClass t, int s) { type = t; size = s; }
  1217. private void addNewarray(Bytecode code) {
  1218. if (type.isPrimitive())
  1219. code.addNewarray(((CtPrimitiveType)type).getArrayType(),
  1220. size);
  1221. else
  1222. code.addAnewarray(type, size);
  1223. }
  1224. int compile(CtClass type, String name, Bytecode code,
  1225. CtClass[] parameters, Javac drv)
  1226. throws CannotCompileException
  1227. {
  1228. code.addAload(0);
  1229. addNewarray(code);
  1230. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1231. return 2; // stack size
  1232. }
  1233. int compileIfStatic(CtClass type, String name, Bytecode code,
  1234. Javac drv) throws CannotCompileException
  1235. {
  1236. addNewarray(code);
  1237. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1238. return 1; // stack size
  1239. }
  1240. }
  1241. static class MultiArrayInitializer extends Initializer {
  1242. CtClass type;
  1243. int[] dim;
  1244. MultiArrayInitializer(CtClass t, int[] d) { type = t; dim = d; }
  1245. void check(String desc) throws CannotCompileException {
  1246. if (desc.charAt(0) != '[')
  1247. throw new CannotCompileException("type mismatch");
  1248. }
  1249. int compile(CtClass type, String name, Bytecode code,
  1250. CtClass[] parameters, Javac drv)
  1251. throws CannotCompileException
  1252. {
  1253. code.addAload(0);
  1254. int s = code.addMultiNewarray(type, dim);
  1255. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1256. return s + 1; // stack size
  1257. }
  1258. int compileIfStatic(CtClass type, String name, Bytecode code,
  1259. Javac drv) throws CannotCompileException
  1260. {
  1261. int s = code.addMultiNewarray(type, dim);
  1262. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1263. return s; // stack size
  1264. }
  1265. }
  1266. }