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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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 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. * <p><code>Object obj</code> - the object including the field.
  509. *
  510. * <p>If the initialized field is static, then the constructor does
  511. * not receive any parameters.
  512. *
  513. * @param objectType the class instantiated for the initial value.
  514. */
  515. public static Initializer byNew(CtClass objectType) {
  516. NewInitializer i = new NewInitializer();
  517. i.objectType = objectType;
  518. i.stringParams = null;
  519. i.withConstructorParams = false;
  520. return i;
  521. }
  522. /**
  523. * Makes an initializer creating a new object.
  524. *
  525. * <p>This initializer creates a new object and uses it as the initial
  526. * value of the field. The constructor of the created object receives
  527. * the parameters:
  528. *
  529. * <p><code>Object obj</code> - the object including the field.<br>
  530. * <code>String[] strs</code> - the character strings specified
  531. * by <code>stringParams</code><br>
  532. *
  533. * <p>If the initialized field is static, then the constructor
  534. * receives only <code>strs</code>.
  535. *
  536. * @param objectType the class instantiated for the initial value.
  537. * @param stringParams the array of strings passed to the
  538. * constructor.
  539. */
  540. public static Initializer byNew(CtClass objectType,
  541. String[] stringParams) {
  542. NewInitializer i = new NewInitializer();
  543. i.objectType = objectType;
  544. i.stringParams = stringParams;
  545. i.withConstructorParams = false;
  546. return i;
  547. }
  548. /**
  549. * Makes an initializer creating a new object.
  550. *
  551. * <p>This initializer creates a new object and uses it as the initial
  552. * value of the field. The constructor of the created object receives
  553. * the parameters:
  554. *
  555. * <p><code>Object obj</code> - the object including the field.<br>
  556. * <code>Object[] args</code> - the parameters passed to the
  557. * constructor of the object including the
  558. * filed.
  559. *
  560. * <p>If the initialized field is static, then the constructor does
  561. * not receive any parameters.
  562. *
  563. * @param objectType the class instantiated for the initial value.
  564. *
  565. * @see javassist.CtField.Initializer#byNewArray(CtClass,int)
  566. * @see javassist.CtField.Initializer#byNewArray(CtClass,int[])
  567. */
  568. public static Initializer byNewWithParams(CtClass objectType) {
  569. NewInitializer i = new NewInitializer();
  570. i.objectType = objectType;
  571. i.stringParams = null;
  572. i.withConstructorParams = true;
  573. return i;
  574. }
  575. /**
  576. * Makes an initializer creating a new object.
  577. *
  578. * <p>This initializer creates a new object and uses it as the initial
  579. * value of the field. The constructor of the created object receives
  580. * the parameters:
  581. *
  582. * <p><code>Object obj</code> - the object including the field.<br>
  583. * <code>String[] strs</code> - the character strings specified
  584. * by <code>stringParams</code><br>
  585. * <code>Object[] args</code> - the parameters passed to the
  586. * constructor of the object including the
  587. * filed.
  588. *
  589. * <p>If the initialized field is static, then the constructor receives
  590. * only <code>strs</code>.
  591. *
  592. * @param objectType the class instantiated for the initial value.
  593. * @param stringParams the array of strings passed to the
  594. * constructor.
  595. */
  596. public static Initializer byNewWithParams(CtClass objectType,
  597. String[] stringParams) {
  598. NewInitializer i = new NewInitializer();
  599. i.objectType = objectType;
  600. i.stringParams = stringParams;
  601. i.withConstructorParams = true;
  602. return i;
  603. }
  604. /**
  605. * Makes an initializer calling a static method.
  606. *
  607. * <p>This initializer calls a static method and uses the returned
  608. * value as the initial value of the field.
  609. * The called method receives the parameters:
  610. *
  611. * <p><code>Object obj</code> - the object including the field.
  612. *
  613. * <p>If the initialized field is static, then the method does
  614. * not receive any parameters.
  615. *
  616. * <p>The type of the returned value must be the same as the field
  617. * type.
  618. *
  619. * @param methodClass the class that the static method is
  620. * declared in.
  621. * @param methodName the name of the satic method.
  622. */
  623. public static Initializer byCall(CtClass methodClass,
  624. String methodName) {
  625. MethodInitializer i = new MethodInitializer();
  626. i.objectType = methodClass;
  627. i.methodName = methodName;
  628. i.stringParams = null;
  629. i.withConstructorParams = false;
  630. return i;
  631. }
  632. /**
  633. * Makes an initializer calling a static method.
  634. *
  635. * <p>This initializer calls a static method and uses the returned
  636. * value as the initial value of the field. The called method
  637. * receives the parameters:
  638. *
  639. * <p><code>Object obj</code> - the object including the field.<br>
  640. * <code>String[] strs</code> - the character strings specified
  641. * by <code>stringParams</code><br>
  642. *
  643. * <p>If the initialized field is static, then the method
  644. * receive only <code>strs</code>.
  645. *
  646. * <p>The type of the returned value must be the same as the field
  647. * type.
  648. *
  649. * @param methodClass the class that the static method is
  650. * declared in.
  651. * @param methodName the name of the satic method.
  652. * @param stringParams the array of strings passed to the
  653. * static method.
  654. */
  655. public static Initializer byCall(CtClass methodClass,
  656. String methodName,
  657. String[] stringParams) {
  658. MethodInitializer i = new MethodInitializer();
  659. i.objectType = methodClass;
  660. i.methodName = methodName;
  661. i.stringParams = stringParams;
  662. i.withConstructorParams = false;
  663. return i;
  664. }
  665. /**
  666. * Makes an initializer calling a static method.
  667. *
  668. * <p>This initializer calls a static method and uses the returned
  669. * value as the initial value of the field. The called method
  670. * receives the parameters:
  671. *
  672. * <p><code>Object obj</code> - the object including the field.<br>
  673. * <code>Object[] args</code> - the parameters passed to the
  674. * constructor of the object including the
  675. * filed.
  676. *
  677. * <p>If the initialized field is static, then the method does
  678. * not receive any parameters.
  679. *
  680. * <p>The type of the returned value must be the same as the field
  681. * type.
  682. *
  683. * @param methodClass the class that the static method is
  684. * declared in.
  685. * @param methodName the name of the satic method.
  686. */
  687. public static Initializer byCallWithParams(CtClass methodClass,
  688. String methodName) {
  689. MethodInitializer i = new MethodInitializer();
  690. i.objectType = methodClass;
  691. i.methodName = methodName;
  692. i.stringParams = null;
  693. i.withConstructorParams = true;
  694. return i;
  695. }
  696. /**
  697. * Makes an initializer calling a static method.
  698. *
  699. * <p>This initializer calls a static method and uses the returned
  700. * value as the initial value of the field. The called method
  701. * receives the parameters:
  702. *
  703. * <p><code>Object obj</code> - the object including the field.<br>
  704. * <code>String[] strs</code> - the character strings specified
  705. * by <code>stringParams</code><br>
  706. * <code>Object[] args</code> - the parameters passed to the
  707. * constructor of the object including the
  708. * filed.
  709. *
  710. * <p>If the initialized field is static, then the method
  711. * receive only <code>strs</code>.
  712. *
  713. * <p>The type of the returned value must be the same as the field
  714. * type.
  715. *
  716. * @param methodClass the class that the static method is
  717. * declared in.
  718. * @param methodName the name of the satic method.
  719. * @param stringParams the array of strings passed to the
  720. * static method.
  721. */
  722. public static Initializer byCallWithParams(CtClass methodClass,
  723. String methodName, String[] stringParams) {
  724. MethodInitializer i = new MethodInitializer();
  725. i.objectType = methodClass;
  726. i.methodName = methodName;
  727. i.stringParams = stringParams;
  728. i.withConstructorParams = true;
  729. return i;
  730. }
  731. /**
  732. * Makes an initializer creating a new array.
  733. *
  734. * @param type the type of the array.
  735. * @param size the size of the array.
  736. * @throws NotFoundException if the type of the array components
  737. * is not found.
  738. */
  739. public static Initializer byNewArray(CtClass type, int size)
  740. throws NotFoundException
  741. {
  742. return new ArrayInitializer(type.getComponentType(), size);
  743. }
  744. /**
  745. * Makes an initializer creating a new multi-dimensional array.
  746. *
  747. * @param type the type of the array.
  748. * @param sizes an <code>int</code> array of the size in every
  749. * dimension.
  750. * The first element is the size in the first
  751. * dimension. The second is in the second, etc.
  752. */
  753. public static Initializer byNewArray(CtClass type, int[] sizes) {
  754. return new MultiArrayInitializer(type, sizes);
  755. }
  756. /**
  757. * Makes an initializer.
  758. *
  759. * @param source initializer expression.
  760. */
  761. public static Initializer byExpr(String source) {
  762. return new CodeInitializer(source);
  763. }
  764. static Initializer byExpr(ASTree source) {
  765. return new PtreeInitializer(source);
  766. }
  767. // Check whether this initializer is valid for the field type.
  768. // If it is invaild, this method throws an exception.
  769. void check(String desc) throws CannotCompileException {}
  770. // produce codes for initialization
  771. abstract int compile(CtClass type, String name, Bytecode code,
  772. CtClass[] parameters, Javac drv)
  773. throws CannotCompileException;
  774. // produce codes for initialization
  775. abstract int compileIfStatic(CtClass type, String name,
  776. Bytecode code, Javac drv) throws CannotCompileException;
  777. // returns the index of CONSTANT_Integer_info etc
  778. // if the value is constant. Otherwise, 0.
  779. int getConstantValue(ConstPool cp, CtClass type) { return 0; }
  780. }
  781. static abstract class CodeInitializer0 extends Initializer {
  782. abstract void compileExpr(Javac drv) throws CompileError;
  783. int compile(CtClass type, String name, Bytecode code,
  784. CtClass[] parameters, Javac drv)
  785. throws CannotCompileException
  786. {
  787. try {
  788. code.addAload(0);
  789. compileExpr(drv);
  790. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  791. return code.getMaxStack();
  792. }
  793. catch (CompileError e) {
  794. throw new CannotCompileException(e);
  795. }
  796. }
  797. int compileIfStatic(CtClass type, String name, Bytecode code,
  798. Javac drv) throws CannotCompileException
  799. {
  800. try {
  801. compileExpr(drv);
  802. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  803. return code.getMaxStack();
  804. }
  805. catch (CompileError e) {
  806. throw new CannotCompileException(e);
  807. }
  808. }
  809. int getConstantValue2(ConstPool cp, CtClass type, ASTree tree) {
  810. if (type.isPrimitive()) {
  811. if (tree instanceof IntConst) {
  812. long value = ((IntConst)tree).get();
  813. if (type == CtClass.doubleType)
  814. return cp.addDoubleInfo((double)value);
  815. else if (type == CtClass.floatType)
  816. return cp.addFloatInfo((float)value);
  817. else if (type == CtClass.longType)
  818. return cp.addLongInfo(value);
  819. else if (type != CtClass.voidType)
  820. return cp.addIntegerInfo((int)value);
  821. }
  822. else if (tree instanceof DoubleConst) {
  823. double value = ((DoubleConst)tree).get();
  824. if (type == CtClass.floatType)
  825. return cp.addFloatInfo((float)value);
  826. else if (type == CtClass.doubleType)
  827. return cp.addDoubleInfo(value);
  828. }
  829. }
  830. else if (tree instanceof StringL
  831. && type.getName().equals(javaLangString))
  832. return cp.addStringInfo(((StringL)tree).get());
  833. return 0;
  834. }
  835. }
  836. static class CodeInitializer extends CodeInitializer0 {
  837. private String expression;
  838. CodeInitializer(String expr) { expression = expr; }
  839. void compileExpr(Javac drv) throws CompileError {
  840. drv.compileExpr(expression);
  841. }
  842. int getConstantValue(ConstPool cp, CtClass type) {
  843. try {
  844. ASTree t = Javac.parseExpr(expression, new SymbolTable());
  845. return getConstantValue2(cp, type, t);
  846. }
  847. catch (CompileError e) {
  848. return 0;
  849. }
  850. }
  851. }
  852. static class PtreeInitializer extends CodeInitializer0 {
  853. private ASTree expression;
  854. PtreeInitializer(ASTree expr) { expression = expr; }
  855. void compileExpr(Javac drv) throws CompileError {
  856. drv.compileExpr(expression);
  857. }
  858. int getConstantValue(ConstPool cp, CtClass type) {
  859. return getConstantValue2(cp, type, expression);
  860. }
  861. }
  862. /**
  863. * A field initialized with a parameter passed to the constructor
  864. * of the class containing that field.
  865. */
  866. static class ParamInitializer extends Initializer {
  867. int nthParam;
  868. ParamInitializer() {}
  869. int compile(CtClass type, String name, Bytecode code,
  870. CtClass[] parameters, Javac drv)
  871. throws CannotCompileException
  872. {
  873. if (parameters != null && nthParam < parameters.length) {
  874. code.addAload(0);
  875. int nth = nthParamToLocal(nthParam, parameters, false);
  876. int s = code.addLoad(nth, type) + 1;
  877. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  878. return s; // stack size
  879. }
  880. else
  881. return 0; // do not initialize
  882. }
  883. /**
  884. * Computes the index of the local variable that the n-th parameter
  885. * is assigned to.
  886. *
  887. * @param nth n-th parameter
  888. * @param params list of parameter types
  889. * @param isStatic true if the method is static.
  890. */
  891. static int nthParamToLocal(int nth, CtClass[] params,
  892. boolean isStatic) {
  893. CtClass longType = CtClass.longType;
  894. CtClass doubleType = CtClass.doubleType;
  895. int k;
  896. if (isStatic)
  897. k = 0;
  898. else
  899. k = 1; // 0 is THIS.
  900. for (int i = 0; i < nth; ++i) {
  901. CtClass type = params[i];
  902. if (type == longType || type == doubleType)
  903. k += 2;
  904. else
  905. ++k;
  906. }
  907. return k;
  908. }
  909. int compileIfStatic(CtClass type, String name, Bytecode code,
  910. Javac drv) throws CannotCompileException
  911. {
  912. return 0;
  913. }
  914. }
  915. /**
  916. * A field initialized with an object created by the new operator.
  917. */
  918. static class NewInitializer extends Initializer {
  919. CtClass objectType;
  920. String[] stringParams;
  921. boolean withConstructorParams;
  922. NewInitializer() {}
  923. /**
  924. * Produces codes in which a new object is created and assigned to
  925. * the field as the initial value.
  926. */
  927. int compile(CtClass type, String name, Bytecode code,
  928. CtClass[] parameters, Javac drv)
  929. throws CannotCompileException
  930. {
  931. int stacksize;
  932. code.addAload(0);
  933. code.addNew(objectType);
  934. code.add(Bytecode.DUP);
  935. code.addAload(0);
  936. if (stringParams == null)
  937. stacksize = 4;
  938. else
  939. stacksize = compileStringParameter(code) + 4;
  940. if (withConstructorParams)
  941. stacksize += CtNewWrappedMethod.compileParameterList(code,
  942. parameters, 1);
  943. code.addInvokespecial(objectType, "<init>", getDescriptor());
  944. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  945. return stacksize;
  946. }
  947. private String getDescriptor() {
  948. final String desc3
  949. = "(Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)V";
  950. if (stringParams == null)
  951. if (withConstructorParams)
  952. return "(Ljava/lang/Object;[Ljava/lang/Object;)V";
  953. else
  954. return "(Ljava/lang/Object;)V";
  955. else
  956. if (withConstructorParams)
  957. return desc3;
  958. else
  959. return "(Ljava/lang/Object;[Ljava/lang/String;)V";
  960. }
  961. /**
  962. * Produces codes for a static field.
  963. */
  964. int compileIfStatic(CtClass type, String name, Bytecode code,
  965. Javac drv) throws CannotCompileException
  966. {
  967. String desc;
  968. code.addNew(objectType);
  969. code.add(Bytecode.DUP);
  970. int stacksize = 2;
  971. if (stringParams == null)
  972. desc = "()V";
  973. else {
  974. desc = "([Ljava/lang/String;)V";
  975. stacksize += compileStringParameter(code);
  976. }
  977. code.addInvokespecial(objectType, "<init>", desc);
  978. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  979. return stacksize;
  980. }
  981. protected final int compileStringParameter(Bytecode code)
  982. throws CannotCompileException
  983. {
  984. int nparam = stringParams.length;
  985. code.addIconst(nparam);
  986. code.addAnewarray(javaLangString);
  987. for (int j = 0; j < nparam; ++j) {
  988. code.add(Bytecode.DUP); // dup
  989. code.addIconst(j); // iconst_<j>
  990. code.addLdc(stringParams[j]); // ldc ...
  991. code.add(Bytecode.AASTORE); // aastore
  992. }
  993. return 4;
  994. }
  995. }
  996. /**
  997. * A field initialized with the result of a static method call.
  998. */
  999. static class MethodInitializer extends NewInitializer {
  1000. String methodName;
  1001. // the method class is specified by objectType.
  1002. MethodInitializer() {}
  1003. /**
  1004. * Produces codes in which a new object is created and assigned to
  1005. * the field as the initial value.
  1006. */
  1007. int compile(CtClass type, String name, Bytecode code,
  1008. CtClass[] parameters, Javac drv)
  1009. throws CannotCompileException
  1010. {
  1011. int stacksize;
  1012. code.addAload(0);
  1013. code.addAload(0);
  1014. if (stringParams == null)
  1015. stacksize = 2;
  1016. else
  1017. stacksize = compileStringParameter(code) + 2;
  1018. if (withConstructorParams)
  1019. stacksize += CtNewWrappedMethod.compileParameterList(code,
  1020. parameters, 1);
  1021. String typeDesc = Descriptor.of(type);
  1022. String mDesc = getDescriptor() + typeDesc;
  1023. code.addInvokestatic(objectType, methodName, mDesc);
  1024. code.addPutfield(Bytecode.THIS, name, typeDesc);
  1025. return stacksize;
  1026. }
  1027. private String getDescriptor() {
  1028. final String desc3
  1029. = "(Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)";
  1030. if (stringParams == null)
  1031. if (withConstructorParams)
  1032. return "(Ljava/lang/Object;[Ljava/lang/Object;)";
  1033. else
  1034. return "(Ljava/lang/Object;)";
  1035. else
  1036. if (withConstructorParams)
  1037. return desc3;
  1038. else
  1039. return "(Ljava/lang/Object;[Ljava/lang/String;)";
  1040. }
  1041. /**
  1042. * Produces codes for a static field.
  1043. */
  1044. int compileIfStatic(CtClass type, String name, Bytecode code,
  1045. Javac drv) throws CannotCompileException
  1046. {
  1047. String desc;
  1048. int stacksize = 1;
  1049. if (stringParams == null)
  1050. desc = "()";
  1051. else {
  1052. desc = "([Ljava/lang/String;)";
  1053. stacksize += compileStringParameter(code);
  1054. }
  1055. String typeDesc = Descriptor.of(type);
  1056. code.addInvokestatic(objectType, methodName, desc + typeDesc);
  1057. code.addPutstatic(Bytecode.THIS, name, typeDesc);
  1058. return stacksize;
  1059. }
  1060. }
  1061. static class IntInitializer extends Initializer {
  1062. int value;
  1063. IntInitializer(int v) { value = v; }
  1064. void check(String desc) throws CannotCompileException {
  1065. char c = desc.charAt(0);
  1066. if (c != 'I' && c != 'S' && c != 'B' && c != 'C' && c != 'Z')
  1067. throw new CannotCompileException("type mismatch");
  1068. }
  1069. int compile(CtClass type, String name, Bytecode code,
  1070. CtClass[] parameters, Javac drv)
  1071. throws CannotCompileException
  1072. {
  1073. code.addAload(0);
  1074. code.addIconst(value);
  1075. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1076. return 2; // stack size
  1077. }
  1078. int compileIfStatic(CtClass type, String name, Bytecode code,
  1079. Javac drv) throws CannotCompileException
  1080. {
  1081. code.addIconst(value);
  1082. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1083. return 1; // stack size
  1084. }
  1085. int getConstantValue(ConstPool cp, CtClass type) {
  1086. return cp.addIntegerInfo(value);
  1087. }
  1088. }
  1089. static class LongInitializer extends Initializer {
  1090. long value;
  1091. LongInitializer(long v) { value = v; }
  1092. void check(String desc) throws CannotCompileException {
  1093. if (!desc.equals("J"))
  1094. throw new CannotCompileException("type mismatch");
  1095. }
  1096. int compile(CtClass type, String name, Bytecode code,
  1097. CtClass[] parameters, Javac drv)
  1098. throws CannotCompileException
  1099. {
  1100. code.addAload(0);
  1101. code.addLdc2w(value);
  1102. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1103. return 3; // stack size
  1104. }
  1105. int compileIfStatic(CtClass type, String name, Bytecode code,
  1106. Javac drv) throws CannotCompileException
  1107. {
  1108. code.addLdc2w(value);
  1109. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1110. return 2; // stack size
  1111. }
  1112. int getConstantValue(ConstPool cp, CtClass type) {
  1113. if (type == CtClass.longType)
  1114. return cp.addLongInfo(value);
  1115. else
  1116. return 0;
  1117. }
  1118. }
  1119. static class FloatInitializer extends Initializer {
  1120. float value;
  1121. FloatInitializer(float v) { value = v; }
  1122. void check(String desc) throws CannotCompileException {
  1123. if (!desc.equals("F"))
  1124. throw new CannotCompileException("type mismatch");
  1125. }
  1126. int compile(CtClass type, String name, Bytecode code,
  1127. CtClass[] parameters, Javac drv)
  1128. throws CannotCompileException
  1129. {
  1130. code.addAload(0);
  1131. code.addFconst(value);
  1132. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1133. return 3; // stack size
  1134. }
  1135. int compileIfStatic(CtClass type, String name, Bytecode code,
  1136. Javac drv) throws CannotCompileException
  1137. {
  1138. code.addFconst(value);
  1139. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1140. return 2; // stack size
  1141. }
  1142. int getConstantValue(ConstPool cp, CtClass type) {
  1143. if (type == CtClass.floatType)
  1144. return cp.addFloatInfo(value);
  1145. else
  1146. return 0;
  1147. }
  1148. }
  1149. static class DoubleInitializer extends Initializer {
  1150. double value;
  1151. DoubleInitializer(double v) { value = v; }
  1152. void check(String desc) throws CannotCompileException {
  1153. if (!desc.equals("D"))
  1154. throw new CannotCompileException("type mismatch");
  1155. }
  1156. int compile(CtClass type, String name, Bytecode code,
  1157. CtClass[] parameters, Javac drv)
  1158. throws CannotCompileException
  1159. {
  1160. code.addAload(0);
  1161. code.addLdc2w(value);
  1162. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1163. return 3; // stack size
  1164. }
  1165. int compileIfStatic(CtClass type, String name, Bytecode code,
  1166. Javac drv) throws CannotCompileException
  1167. {
  1168. code.addLdc2w(value);
  1169. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1170. return 2; // stack size
  1171. }
  1172. int getConstantValue(ConstPool cp, CtClass type) {
  1173. if (type == CtClass.doubleType)
  1174. return cp.addDoubleInfo(value);
  1175. else
  1176. return 0;
  1177. }
  1178. }
  1179. static class StringInitializer extends Initializer {
  1180. String value;
  1181. StringInitializer(String v) { value = v; }
  1182. int compile(CtClass type, String name, Bytecode code,
  1183. CtClass[] parameters, Javac drv)
  1184. throws CannotCompileException
  1185. {
  1186. code.addAload(0);
  1187. code.addLdc(value);
  1188. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1189. return 2; // stack size
  1190. }
  1191. int compileIfStatic(CtClass type, String name, Bytecode code,
  1192. Javac drv) throws CannotCompileException
  1193. {
  1194. code.addLdc(value);
  1195. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1196. return 1; // stack size
  1197. }
  1198. int getConstantValue(ConstPool cp, CtClass type) {
  1199. if (type.getName().equals(javaLangString))
  1200. return cp.addStringInfo(value);
  1201. else
  1202. return 0;
  1203. }
  1204. }
  1205. static class ArrayInitializer extends Initializer {
  1206. CtClass type;
  1207. int size;
  1208. ArrayInitializer(CtClass t, int s) { type = t; size = s; }
  1209. private void addNewarray(Bytecode code) {
  1210. if (type.isPrimitive())
  1211. code.addNewarray(((CtPrimitiveType)type).getArrayType(),
  1212. size);
  1213. else
  1214. code.addAnewarray(type, size);
  1215. }
  1216. int compile(CtClass type, String name, Bytecode code,
  1217. CtClass[] parameters, Javac drv)
  1218. throws CannotCompileException
  1219. {
  1220. code.addAload(0);
  1221. addNewarray(code);
  1222. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1223. return 2; // stack size
  1224. }
  1225. int compileIfStatic(CtClass type, String name, Bytecode code,
  1226. Javac drv) throws CannotCompileException
  1227. {
  1228. addNewarray(code);
  1229. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1230. return 1; // stack size
  1231. }
  1232. }
  1233. static class MultiArrayInitializer extends Initializer {
  1234. CtClass type;
  1235. int[] dim;
  1236. MultiArrayInitializer(CtClass t, int[] d) { type = t; dim = d; }
  1237. void check(String desc) throws CannotCompileException {
  1238. if (desc.charAt(0) != '[')
  1239. throw new CannotCompileException("type mismatch");
  1240. }
  1241. int compile(CtClass type, String name, Bytecode code,
  1242. CtClass[] parameters, Javac drv)
  1243. throws CannotCompileException
  1244. {
  1245. code.addAload(0);
  1246. int s = code.addMultiNewarray(type, dim);
  1247. code.addPutfield(Bytecode.THIS, name, Descriptor.of(type));
  1248. return s + 1; // stack size
  1249. }
  1250. int compileIfStatic(CtClass type, String name, Bytecode code,
  1251. Javac drv) throws CannotCompileException
  1252. {
  1253. int s = code.addMultiNewarray(type, dim);
  1254. code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type));
  1255. return s; // stack size
  1256. }
  1257. }
  1258. }