Field.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /* java.lang.reflect.Field - reflection of Java fields
  2. Copyright (C) 1998, 2001, 2005, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.lang.reflect;
  32. import gnu.java.lang.ClassHelper;
  33. import gnu.java.lang.reflect.FieldSignatureParser;
  34. import java.lang.annotation.Annotation;
  35. /**
  36. * The Field class represents a member variable of a class. It also allows
  37. * dynamic access to a member, via reflection. This works for both
  38. * static and instance fields. Operations on Field objects know how to
  39. * do widening conversions, but throw {@link IllegalArgumentException} if
  40. * a narrowing conversion would be necessary. You can query for information
  41. * on this Field regardless of location, but get and set access may be limited
  42. * by Java language access controls. If you can't do it in the compiler, you
  43. * can't normally do it here either.<p>
  44. *
  45. * <B>Note:</B> This class returns and accepts types as Classes, even
  46. * primitive types; there are Class types defined that represent each
  47. * different primitive type. They are <code>java.lang.Boolean.TYPE,
  48. * java.lang.Byte.TYPE,</code>, also available as <code>boolean.class,
  49. * byte.class</code>, etc. These are not to be confused with the
  50. * classes <code>java.lang.Boolean, java.lang.Byte</code>, etc., which are
  51. * real classes.<p>
  52. *
  53. * Also note that this is not a serializable class. It is entirely feasible
  54. * to make it serializable using the Externalizable interface, but this is
  55. * on Sun, not me.
  56. *
  57. * @author John Keiser
  58. * @author Eric Blake <ebb9@email.byu.edu>
  59. * @author Per Bothner <bothner@cygnus.com>
  60. * @see Member
  61. * @see Class
  62. * @see Class#getField(String)
  63. * @see Class#getDeclaredField(String)
  64. * @see Class#getFields()
  65. * @see Class#getDeclaredFields()
  66. * @since 1.1
  67. * @status updated to 1.4
  68. */
  69. public final class Field
  70. extends AccessibleObject implements Member
  71. {
  72. private Class declaringClass;
  73. private String name;
  74. // Offset in bytes from the start of declaringClass's fields array.
  75. private int offset;
  76. // The Class (or primitive TYPE) of this field.
  77. private Class type;
  78. static final int FIELD_MODIFIERS
  79. = Modifier.FINAL | Modifier.PRIVATE | Modifier.PROTECTED
  80. | Modifier.PUBLIC | Modifier.STATIC | Modifier.TRANSIENT
  81. | Modifier.VOLATILE;
  82. // This is instantiated by Class sometimes, but it uses C++ and
  83. // avoids the Java protection check.
  84. Field ()
  85. {
  86. }
  87. /**
  88. * Gets the class that declared this field, or the class where this field
  89. * is a non-inherited member.
  90. * @return the class that declared this member
  91. */
  92. public Class<?> getDeclaringClass()
  93. {
  94. return declaringClass;
  95. }
  96. /**
  97. * Gets the name of this field.
  98. * @return the name of this field
  99. */
  100. public native String getName();
  101. /**
  102. * Return the raw modifiers for this field.
  103. * @return the field's modifiers
  104. */
  105. private native int getModifiersInternal();
  106. /**
  107. * Gets the modifiers this field uses. Use the <code>Modifier</code>
  108. * class to interpret the values. A field can only have a subset of the
  109. * following modifiers: public, private, protected, static, final,
  110. * transient, and volatile.
  111. *
  112. * @return an integer representing the modifiers to this Member
  113. * @see Modifier
  114. */
  115. public int getModifiers()
  116. {
  117. return getModifiersInternal() & FIELD_MODIFIERS;
  118. }
  119. /**
  120. * Return true if this field is synthetic, false otherwise.
  121. * @since 1.5
  122. */
  123. public boolean isSynthetic()
  124. {
  125. return (getModifiersInternal() & Modifier.SYNTHETIC) != 0;
  126. }
  127. /**
  128. * Return true if this field represents an enum constant,
  129. * false otherwise.
  130. * @since 1.5
  131. */
  132. public boolean isEnumConstant()
  133. {
  134. return (getModifiersInternal() & Modifier.ENUM) != 0;
  135. }
  136. /**
  137. * Gets the type of this field.
  138. * @return the type of this field
  139. */
  140. public native Class<?> getType();
  141. /**
  142. * Compare two objects to see if they are semantically equivalent.
  143. * Two Fields are semantically equivalent if they have the same declaring
  144. * class, name, and type. Since you can't creat a Field except through
  145. * the VM, this is just the == relation.
  146. *
  147. * @param o the object to compare to
  148. * @return <code>true</code> if they are equal; <code>false</code> if not
  149. */
  150. public boolean equals (Object fld)
  151. {
  152. if (! (fld instanceof Field))
  153. return false;
  154. Field f = (Field) fld;
  155. return declaringClass == f.declaringClass && offset == f.offset;
  156. }
  157. /**
  158. * Get the hash code for the Field. The Field hash code is the hash code
  159. * of its name XOR'd with the hash code of its class name.
  160. *
  161. * @return the hash code for the object.
  162. */
  163. public int hashCode()
  164. {
  165. return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
  166. }
  167. /**
  168. * Get a String representation of the Field. A Field's String
  169. * representation is "&lt;modifiers&gt; &lt;type&gt;
  170. * &lt;class&gt;.&lt;fieldname&gt;".<br> Example:
  171. * <code>public transient boolean gnu.parse.Parser.parseComplete</code>
  172. *
  173. * @return the String representation of the Field
  174. */
  175. public String toString ()
  176. {
  177. StringBuffer sbuf = new StringBuffer ();
  178. int mods = getModifiers();
  179. if (mods != 0)
  180. {
  181. Modifier.toString(mods, sbuf);
  182. sbuf.append(' ');
  183. }
  184. Method.appendClassName (sbuf, getType ());
  185. sbuf.append(' ');
  186. Method.appendClassName (sbuf, getDeclaringClass());
  187. sbuf.append('.');
  188. sbuf.append(getName());
  189. return sbuf.toString();
  190. }
  191. public String toGenericString()
  192. {
  193. StringBuilder sb = new StringBuilder(64);
  194. Modifier.toString(getModifiers(), sb).append(' ');
  195. sb.append(getGenericType()).append(' ');
  196. sb.append(getDeclaringClass().getName()).append('.');
  197. sb.append(getName());
  198. return sb.toString();
  199. }
  200. /**
  201. * Get the value of this Field. If it is primitive, it will be wrapped
  202. * in the appropriate wrapper type (boolean = java.lang.Boolean).<p>
  203. *
  204. * If the field is static, <code>o</code> will be ignored. Otherwise, if
  205. * <code>o</code> is null, you get a <code>NullPointerException</code>,
  206. * and if it is incompatible with the declaring class of the field, you
  207. * get an <code>IllegalArgumentException</code>.<p>
  208. *
  209. * Next, if this Field enforces access control, your runtime context is
  210. * evaluated, and you may have an <code>IllegalAccessException</code> if
  211. * you could not access this field in similar compiled code. If the field
  212. * is static, and its class is uninitialized, you trigger class
  213. * initialization, which may end in a
  214. * <code>ExceptionInInitializerError</code>.<p>
  215. *
  216. * Finally, the field is accessed, and primitives are wrapped (but not
  217. * necessarily in new objects). This method accesses the field of the
  218. * declaring class, even if the instance passed in belongs to a subclass
  219. * which declares another field to hide this one.
  220. *
  221. * @param o the object to get the value of this Field from
  222. * @return the value of the Field
  223. * @throws IllegalAccessException if you could not normally access this field
  224. * (i.e. it is not public)
  225. * @throws IllegalArgumentException if <code>o</code> is not an instance of
  226. * the class or interface declaring this field
  227. * @throws NullPointerException if <code>o</code> is null and this field
  228. * requires an instance
  229. * @throws ExceptionInInitializerError if accessing a static field triggered
  230. * class initialization, which then failed
  231. * @see #getBoolean(Object)
  232. * @see #getByte(Object)
  233. * @see #getChar(Object)
  234. * @see #getShort(Object)
  235. * @see #getInt(Object)
  236. * @see #getLong(Object)
  237. * @see #getFloat(Object)
  238. * @see #getDouble(Object)
  239. */
  240. public Object get(Object obj)
  241. throws IllegalAccessException
  242. {
  243. return get(null, obj);
  244. }
  245. /**
  246. * Get the value of this boolean Field. If the field is static,
  247. * <code>o</code> will be ignored.
  248. *
  249. * @param o the object to get the value of this Field from
  250. * @return the value of the Field
  251. * @throws IllegalAccessException if you could not normally access this field
  252. * (i.e. it is not public)
  253. * @throws IllegalArgumentException if this is not a boolean field of
  254. * <code>o</code>, or if <code>o</code> is not an instance of the
  255. * declaring class of this field
  256. * @throws NullPointerException if <code>o</code> is null and this field
  257. * requires an instance
  258. * @throws ExceptionInInitializerError if accessing a static field triggered
  259. * class initialization, which then failed
  260. * @see #get(Object)
  261. */
  262. public boolean getBoolean(Object obj)
  263. throws IllegalAccessException
  264. {
  265. return getBoolean(null, obj);
  266. }
  267. /**
  268. * Get the value of this byte Field. If the field is static,
  269. * <code>o</code> will be ignored.
  270. *
  271. * @param o the object to get the value of this Field from
  272. * @return the value of the Field
  273. * @throws IllegalAccessException if you could not normally access this field
  274. * (i.e. it is not public)
  275. * @throws IllegalArgumentException if this is not a byte field of
  276. * <code>o</code>, or if <code>o</code> is not an instance of the
  277. * declaring class of this field
  278. * @throws NullPointerException if <code>o</code> is null and this field
  279. * requires an instance
  280. * @throws ExceptionInInitializerError if accessing a static field triggered
  281. * class initialization, which then failed
  282. * @see #get(Object)
  283. */
  284. public byte getByte(Object obj)
  285. throws IllegalAccessException
  286. {
  287. return getByte(null, obj);
  288. }
  289. /**
  290. * Get the value of this Field as a char. If the field is static,
  291. * <code>o</code> will be ignored.
  292. *
  293. * @throws IllegalAccessException if you could not normally access this field
  294. * (i.e. it is not public)
  295. * @throws IllegalArgumentException if this is not a char field of
  296. * <code>o</code>, or if <code>o</code> is not an instance
  297. * of the declaring class of this field
  298. * @throws NullPointerException if <code>o</code> is null and this field
  299. * requires an instance
  300. * @throws ExceptionInInitializerError if accessing a static field triggered
  301. * class initialization, which then failed
  302. * @see #get(Object)
  303. */
  304. public char getChar(Object obj)
  305. throws IllegalAccessException
  306. {
  307. return getChar(null, obj);
  308. }
  309. /**
  310. * Get the value of this Field as a short. If the field is static,
  311. * <code>o</code> will be ignored.
  312. *
  313. * @param o the object to get the value of this Field from
  314. * @return the value of the Field
  315. * @throws IllegalAccessException if you could not normally access this field
  316. * (i.e. it is not public)
  317. * @throws IllegalArgumentException if this is not a byte or short
  318. * field of <code>o</code>, or if <code>o</code> is not an instance
  319. * of the declaring class of this field
  320. * @throws NullPointerException if <code>o</code> is null and this field
  321. * requires an instance
  322. * @throws ExceptionInInitializerError if accessing a static field triggered
  323. * class initialization, which then failed
  324. * @see #get(Object)
  325. */
  326. public short getShort(Object obj)
  327. throws IllegalAccessException
  328. {
  329. return getShort(null, obj);
  330. }
  331. /**
  332. * Get the value of this Field as an int. If the field is static,
  333. * <code>o</code> will be ignored.
  334. *
  335. * @param o the object to get the value of this Field from
  336. * @return the value of the Field
  337. * @throws IllegalAccessException if you could not normally access this field
  338. * (i.e. it is not public)
  339. * @throws IllegalArgumentException if this is not a byte, short, char, or
  340. * int field of <code>o</code>, or if <code>o</code> is not an
  341. * instance of the declaring class of this field
  342. * @throws NullPointerException if <code>o</code> is null and this field
  343. * requires an instance
  344. * @throws ExceptionInInitializerError if accessing a static field triggered
  345. * class initialization, which then failed
  346. * @see #get(Object)
  347. */
  348. public int getInt(Object obj)
  349. throws IllegalAccessException
  350. {
  351. return getInt(null, obj);
  352. }
  353. /**
  354. * Get the value of this Field as a long. If the field is static,
  355. * <code>o</code> will be ignored.
  356. *
  357. * @param o the object to get the value of this Field from
  358. * @return the value of the Field
  359. * @throws IllegalAccessException if you could not normally access this field
  360. * (i.e. it is not public)
  361. * @throws IllegalArgumentException if this is not a byte, short, char, int,
  362. * or long field of <code>o</code>, or if <code>o</code> is not an
  363. * instance of the declaring class of this field
  364. * @throws NullPointerException if <code>o</code> is null and this field
  365. * requires an instance
  366. * @throws ExceptionInInitializerError if accessing a static field triggered
  367. * class initialization, which then failed
  368. * @see #get(Object)
  369. */
  370. public long getLong(Object obj)
  371. throws IllegalAccessException
  372. {
  373. return getLong(null, obj);
  374. }
  375. /**
  376. * Get the value of this Field as a float. If the field is static,
  377. * <code>o</code> will be ignored.
  378. *
  379. * @param o the object to get the value of this Field from
  380. * @return the value of the Field
  381. * @throws IllegalAccessException if you could not normally access this field
  382. * (i.e. it is not public)
  383. * @throws IllegalArgumentException if this is not a byte, short, char, int,
  384. * long, or float field of <code>o</code>, or if <code>o</code> is
  385. * not an instance of the declaring class of this field
  386. * @throws NullPointerException if <code>o</code> is null and this field
  387. * requires an instance
  388. * @throws ExceptionInInitializerError if accessing a static field triggered
  389. * class initialization, which then failed
  390. * @see #get(Object)
  391. */
  392. public float getFloat(Object obj)
  393. throws IllegalAccessException
  394. {
  395. return getFloat(null, obj);
  396. }
  397. /**
  398. * Get the value of this Field as a double. If the field is static,
  399. * <code>o</code> will be ignored.
  400. *
  401. * @param o the object to get the value of this Field from
  402. * @return the value of the Field
  403. * @throws IllegalAccessException if you could not normally access this field
  404. * (i.e. it is not public)
  405. * @throws IllegalArgumentException if this is not a byte, short, char, int,
  406. * long, float, or double field of <code>o</code>, or if
  407. * <code>o</code> is not an instance of the declaring class of this
  408. * field
  409. * @throws NullPointerException if <code>o</code> is null and this field
  410. * requires an instance
  411. * @throws ExceptionInInitializerError if accessing a static field triggered
  412. * class initialization, which then failed
  413. * @see #get(Object)
  414. */
  415. public double getDouble(Object obj)
  416. throws IllegalAccessException
  417. {
  418. return getDouble(null, obj);
  419. }
  420. private native boolean getBoolean (Class caller, Object obj)
  421. throws IllegalArgumentException, IllegalAccessException;
  422. private native char getChar (Class caller, Object obj)
  423. throws IllegalArgumentException, IllegalAccessException;
  424. private native byte getByte (Class caller, Object obj)
  425. throws IllegalArgumentException, IllegalAccessException;
  426. private native short getShort (Class caller, Object obj)
  427. throws IllegalArgumentException, IllegalAccessException;
  428. private native int getInt (Class caller, Object obj)
  429. throws IllegalArgumentException, IllegalAccessException;
  430. private native long getLong (Class caller, Object obj)
  431. throws IllegalArgumentException, IllegalAccessException;
  432. private native float getFloat (Class caller, Object obj)
  433. throws IllegalArgumentException, IllegalAccessException;
  434. private native double getDouble (Class caller, Object obj)
  435. throws IllegalArgumentException, IllegalAccessException;
  436. private native Object get (Class caller, Object obj)
  437. throws IllegalArgumentException, IllegalAccessException;
  438. /**
  439. * Set the value of this Field. If it is a primitive field, the value
  440. * will be unwrapped from the passed object (boolean = java.lang.Boolean).<p>
  441. *
  442. * If the field is static, <code>o</code> will be ignored. Otherwise, if
  443. * <code>o</code> is null, you get a <code>NullPointerException</code>,
  444. * and if it is incompatible with the declaring class of the field, you
  445. * get an <code>IllegalArgumentException</code>.<p>
  446. *
  447. * Next, if this Field enforces access control, your runtime context is
  448. * evaluated, and you may have an <code>IllegalAccessException</code> if
  449. * you could not access this field in similar compiled code. This also
  450. * occurs whether or not there is access control if the field is final.
  451. * If the field is primitive, and unwrapping your argument fails, you will
  452. * get an <code>IllegalArgumentException</code>; likewise, this error
  453. * happens if <code>value</code> cannot be cast to the correct object type.
  454. * If the field is static, and its class is uninitialized, you trigger class
  455. * initialization, which may end in a
  456. * <code>ExceptionInInitializerError</code>.<p>
  457. *
  458. * Finally, the field is set with the widened value. This method accesses
  459. * the field of the declaring class, even if the instance passed in belongs
  460. * to a subclass which declares another field to hide this one.
  461. *
  462. * @param o the object to set this Field on
  463. * @param value the value to set this Field to
  464. * @throws IllegalAccessException if you could not normally access this field
  465. * (i.e. it is not public)
  466. * @throws IllegalArgumentException if <code>value</code> cannot be
  467. * converted by a widening conversion to the underlying type of
  468. * the Field, or if <code>o</code> is not an instance of the class
  469. * declaring this field
  470. * @throws NullPointerException if <code>o</code> is null and this field
  471. * requires an instance
  472. * @throws ExceptionInInitializerError if accessing a static field triggered
  473. * class initialization, which then failed
  474. * @see #setBoolean(Object, boolean)
  475. * @see #setByte(Object, byte)
  476. * @see #setChar(Object, char)
  477. * @see #setShort(Object, short)
  478. * @see #setInt(Object, int)
  479. * @see #setLong(Object, long)
  480. * @see #setFloat(Object, float)
  481. * @see #setDouble(Object, double)
  482. */
  483. public void set(Object object, Object value)
  484. throws IllegalAccessException
  485. {
  486. set(null, object, value);
  487. }
  488. /**
  489. * Set this boolean Field. If the field is static, <code>o</code> will be
  490. * ignored.
  491. *
  492. * @param o the object to set this Field on
  493. * @param value the value to set this Field to
  494. * @throws IllegalAccessException if you could not normally access this field
  495. * (i.e. it is not public)
  496. * @throws IllegalArgumentException if this is not a boolean field, or if
  497. * <code>o</code> is not an instance of the class declaring this
  498. * field
  499. * @throws NullPointerException if <code>o</code> is null and this field
  500. * requires an instance
  501. * @throws ExceptionInInitializerError if accessing a static field triggered
  502. * class initialization, which then failed
  503. * @see #set(Object, Object)
  504. */
  505. public void setBoolean(Object obj, boolean b)
  506. throws IllegalAccessException
  507. {
  508. setBoolean(null, obj, b, true);
  509. }
  510. /**
  511. * Set this byte Field. If the field is static, <code>o</code> will be
  512. * ignored.
  513. *
  514. * @param o the object to set this Field on
  515. * @param value the value to set this Field to
  516. * @throws IllegalAccessException if you could not normally access this field
  517. * (i.e. it is not public)
  518. * @throws IllegalArgumentException if this is not a byte, short, int, long,
  519. * float, or double field, or if <code>o</code> is not an instance
  520. * of the class declaring this field
  521. * @throws NullPointerException if <code>o</code> is null and this field
  522. * requires an instance
  523. * @throws ExceptionInInitializerError if accessing a static field triggered
  524. * class initialization, which then failed
  525. * @see #set(Object, Object)
  526. */
  527. public void setByte(Object obj, byte b)
  528. throws IllegalAccessException
  529. {
  530. setByte(null, obj, b, true);
  531. }
  532. /**
  533. * Set this char Field. If the field is static, <code>o</code> will be
  534. * ignored.
  535. *
  536. * @param o the object to set this Field on
  537. * @param value the value to set this Field to
  538. * @throws IllegalAccessException if you could not normally access this field
  539. * (i.e. it is not public)
  540. * @throws IllegalArgumentException if this is not a char, int, long,
  541. * float, or double field, or if <code>o</code> is not an instance
  542. * of the class declaring this field
  543. * @throws NullPointerException if <code>o</code> is null and this field
  544. * requires an instance
  545. * @throws ExceptionInInitializerError if accessing a static field triggered
  546. * class initialization, which then failed
  547. * @see #set(Object, Object)
  548. */
  549. public void setChar(Object obj, char c)
  550. throws IllegalAccessException
  551. {
  552. setChar(null, obj, c, true);
  553. }
  554. /**
  555. * Set this short Field. If the field is static, <code>o</code> will be
  556. * ignored.
  557. *
  558. * @param o the object to set this Field on
  559. * @param value the value to set this Field to
  560. * @throws IllegalAccessException if you could not normally access this field
  561. * (i.e. it is not public)
  562. * @throws IllegalArgumentException if this is not a short, int, long,
  563. * float, or double field, or if <code>o</code> is not an instance
  564. * of the class declaring this field
  565. * @throws NullPointerException if <code>o</code> is null and this field
  566. * requires an instance
  567. * @throws ExceptionInInitializerError if accessing a static field triggered
  568. * class initialization, which then failed
  569. * @see #set(Object, Object)
  570. */
  571. public void setShort(Object obj, short s)
  572. throws IllegalAccessException
  573. {
  574. setShort(null, obj, s, true);
  575. }
  576. /**
  577. * Set this int Field. If the field is static, <code>o</code> will be
  578. * ignored.
  579. *
  580. * @param o the object to set this Field on
  581. * @param value the value to set this Field to
  582. * @throws IllegalAccessException if you could not normally access this field
  583. * (i.e. it is not public)
  584. * @throws IllegalArgumentException if this is not an int, long, float, or
  585. * double field, or if <code>o</code> is not an instance of the
  586. * class declaring this field
  587. * @throws NullPointerException if <code>o</code> is null and this field
  588. * requires an instance
  589. * @throws ExceptionInInitializerError if accessing a static field triggered
  590. * class initialization, which then failed
  591. * @see #set(Object, Object)
  592. */
  593. public void setInt(Object obj, int i)
  594. throws IllegalAccessException
  595. {
  596. setInt(null, obj, i, true);
  597. }
  598. /**
  599. * Set this long Field. If the field is static, <code>o</code> will be
  600. * ignored.
  601. *
  602. * @param o the object to set this Field on
  603. * @param value the value to set this Field to
  604. * @throws IllegalAccessException if you could not normally access this field
  605. * (i.e. it is not public)
  606. * @throws IllegalArgumentException if this is not a long, float, or double
  607. * field, or if <code>o</code> is not an instance of the class
  608. * declaring this field
  609. * @throws NullPointerException if <code>o</code> is null and this field
  610. * requires an instance
  611. * @throws ExceptionInInitializerError if accessing a static field triggered
  612. * class initialization, which then failed
  613. * @see #set(Object, Object)
  614. */
  615. public void setLong(Object obj, long l)
  616. throws IllegalArgumentException, IllegalAccessException
  617. {
  618. setLong(null, obj, l, true);
  619. }
  620. /**
  621. * Set this float Field. If the field is static, <code>o</code> will be
  622. * ignored.
  623. *
  624. * @param o the object to set this Field on
  625. * @param value the value to set this Field to
  626. * @throws IllegalAccessException if you could not normally access this field
  627. * (i.e. it is not public)
  628. * @throws IllegalArgumentException if this is not a float or long field, or
  629. * if <code>o</code> is not an instance of the class declaring this
  630. * field
  631. * @throws NullPointerException if <code>o</code> is null and this field
  632. * requires an instance
  633. * @throws ExceptionInInitializerError if accessing a static field triggered
  634. * class initialization, which then failed
  635. * @see #set(Object, Object)
  636. */
  637. public void setFloat(Object obj, float f)
  638. throws IllegalAccessException
  639. {
  640. setFloat(null, obj, f, true);
  641. }
  642. /**
  643. * Set this double Field. If the field is static, <code>o</code> will be
  644. * ignored.
  645. *
  646. * @param o the object to set this Field on
  647. * @param value the value to set this Field to
  648. * @throws IllegalAccessException if you could not normally access this field
  649. * (i.e. it is not public)
  650. * @throws IllegalArgumentException if this is not a double field, or if
  651. * <code>o</code> is not an instance of the class declaring this
  652. * field
  653. * @throws NullPointerException if <code>o</code> is null and this field
  654. * requires an instance
  655. * @throws ExceptionInInitializerError if accessing a static field triggered
  656. * class initialization, which then failed
  657. * @see #set(Object, Object)
  658. */
  659. public void setDouble(Object obj, double d)
  660. throws IllegalAccessException
  661. {
  662. setDouble(null, obj, d, true);
  663. }
  664. /**
  665. * Return the generic type of the field. If the field type is not a generic
  666. * type, the method returns the same as <code>getType()</code>.
  667. *
  668. * @throws GenericSignatureFormatError if the generic signature does
  669. * not conform to the format specified in the Virtual Machine
  670. * specification, version 3.
  671. * @since 1.5
  672. */
  673. public Type getGenericType()
  674. {
  675. String signature = getSignature();
  676. if (signature == null)
  677. return getType();
  678. FieldSignatureParser p = new FieldSignatureParser(getDeclaringClass(),
  679. signature);
  680. return p.getFieldType();
  681. }
  682. public <T extends Annotation> T getAnnotation(Class<T> annoClass)
  683. {
  684. Annotation[] annos = getDeclaredAnnotations();
  685. for (int i = 0; i < annos.length; ++i)
  686. if (annos[i].annotationType() == annoClass)
  687. return (T) annos[i];
  688. return null;
  689. }
  690. public Annotation[] getDeclaredAnnotations()
  691. {
  692. Annotation[] result = getDeclaredAnnotationsInternal();
  693. if (result == null)
  694. result = new Annotation[0];
  695. return result;
  696. }
  697. private native Annotation[] getDeclaredAnnotationsInternal();
  698. /**
  699. * Return the String in the Signature attribute for this field. If there
  700. * is no Signature attribute, return null.
  701. */
  702. private native String getSignature();
  703. native void setByte (Class caller, Object obj, byte b, boolean checkFinal)
  704. throws IllegalArgumentException, IllegalAccessException;
  705. native void setShort (Class caller, Object obj, short s, boolean checkFinal)
  706. throws IllegalArgumentException, IllegalAccessException;
  707. native void setInt (Class caller, Object obj, int i, boolean checkFinal)
  708. throws IllegalArgumentException, IllegalAccessException;
  709. native void setLong (Class caller, Object obj, long l, boolean checkFinal)
  710. throws IllegalArgumentException, IllegalAccessException;
  711. native void setFloat (Class caller, Object obj, float f, boolean checkFinal)
  712. throws IllegalArgumentException, IllegalAccessException;
  713. native void setDouble (Class caller, Object obj, double d,
  714. boolean checkFinal)
  715. throws IllegalArgumentException, IllegalAccessException;
  716. native void setChar (Class caller, Object obj, char c, boolean checkFinal)
  717. throws IllegalArgumentException, IllegalAccessException;
  718. native void setBoolean (Class caller, Object obj, boolean b,
  719. boolean checkFinal)
  720. throws IllegalArgumentException, IllegalAccessException;
  721. native void set (Class caller, Object obj, Object val, Class type,
  722. boolean checkFinal)
  723. throws IllegalArgumentException, IllegalAccessException;
  724. private void set (Class caller, Object object, Object value)
  725. throws IllegalArgumentException, IllegalAccessException
  726. {
  727. Class type = getType();
  728. if (! type.isPrimitive())
  729. set(caller, object, value, type, true);
  730. else if (value instanceof Byte)
  731. setByte(caller, object, ((Byte) value).byteValue(), true);
  732. else if (value instanceof Short)
  733. setShort (caller, object, ((Short) value).shortValue(), true);
  734. else if (value instanceof Integer)
  735. setInt(caller, object, ((Integer) value).intValue(), true);
  736. else if (value instanceof Long)
  737. setLong(caller, object, ((Long) value).longValue(), true);
  738. else if (value instanceof Float)
  739. setFloat(caller, object, ((Float) value).floatValue(), true);
  740. else if (value instanceof Double)
  741. setDouble(caller, object, ((Double) value).doubleValue(), true);
  742. else if (value instanceof Character)
  743. setChar(caller, object, ((Character) value).charValue(), true);
  744. else if (value instanceof Boolean)
  745. setBoolean(caller, object, ((Boolean) value).booleanValue(), true);
  746. else
  747. throw new IllegalArgumentException();
  748. }
  749. }