Field.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /* java.lang.reflect.Field - reflection of Java fields
  2. Copyright (C) 1998, 2001, 2005, 2008 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.CPStringBuilder;
  34. import gnu.java.lang.reflect.FieldSignatureParser;
  35. import java.lang.annotation.Annotation;
  36. /**
  37. * The Field class represents a member variable of a class. It also allows
  38. * dynamic access to a member, via reflection. This works for both
  39. * static and instance fields. Operations on Field objects know how to
  40. * do widening conversions, but throw {@link IllegalArgumentException} if
  41. * a narrowing conversion would be necessary. You can query for information
  42. * on this Field regardless of location, but get and set access may be limited
  43. * by Java language access controls. If you can't do it in the compiler, you
  44. * can't normally do it here either.<p>
  45. *
  46. * <B>Note:</B> This class returns and accepts types as Classes, even
  47. * primitive types; there are Class types defined that represent each
  48. * different primitive type. They are <code>java.lang.Boolean.TYPE,
  49. * java.lang.Byte.TYPE,</code>, also available as <code>boolean.class,
  50. * byte.class</code>, etc. These are not to be confused with the
  51. * classes <code>java.lang.Boolean, java.lang.Byte</code>, etc., which are
  52. * real classes.<p>
  53. *
  54. * Also note that this is not a serializable class. It is entirely feasible
  55. * to make it serializable using the Externalizable interface, but this is
  56. * on Sun, not me.
  57. *
  58. * @author John Keiser
  59. * @author Eric Blake <ebb9@email.byu.edu>
  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. static final int FIELD_MODIFIERS
  73. = Modifier.FINAL | Modifier.PRIVATE | Modifier.PROTECTED
  74. | Modifier.PUBLIC | Modifier.STATIC | Modifier.TRANSIENT
  75. | Modifier.VOLATILE;
  76. private FieldSignatureParser p;
  77. VMField f;
  78. /**
  79. * This class is uninstantiable outside the package.
  80. */
  81. Field(VMField f)
  82. {
  83. this.f = f;
  84. f.f = this;
  85. }
  86. /**
  87. * Gets the class that declared this field, or the class where this field
  88. * is a non-inherited member.
  89. * @return the class that declared this member
  90. */
  91. public Class<?> getDeclaringClass()
  92. {
  93. return (Class<?>) f.getDeclaringClass();
  94. }
  95. /**
  96. * Gets the name of this field.
  97. * @return the name of this field
  98. */
  99. public String getName()
  100. {
  101. return f.getName();
  102. }
  103. /**
  104. * Gets the modifiers this field uses. Use the <code>Modifier</code>
  105. * class to interpret the values. A field can only have a subset of the
  106. * following modifiers: public, private, protected, static, final,
  107. * transient, and volatile.
  108. *
  109. * @return an integer representing the modifiers to this Member
  110. * @see Modifier
  111. */
  112. public int getModifiers()
  113. {
  114. return f.getModifiersInternal() & FIELD_MODIFIERS;
  115. }
  116. /**
  117. * Return true if this field is synthetic, false otherwise.
  118. * @since 1.5
  119. */
  120. public boolean isSynthetic()
  121. {
  122. return (f.getModifiersInternal() & Modifier.SYNTHETIC) != 0;
  123. }
  124. /**
  125. * Return true if this field represents an enum constant,
  126. * false otherwise.
  127. * @since 1.5
  128. */
  129. public boolean isEnumConstant()
  130. {
  131. return (f.getModifiersInternal() & Modifier.ENUM) != 0;
  132. }
  133. /**
  134. * Gets the type of this field.
  135. * @return the type of this field
  136. */
  137. public Class<?> getType()
  138. {
  139. return f.getType();
  140. }
  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 o)
  151. {
  152. return f.equals(o);
  153. }
  154. /**
  155. * Get the hash code for the Field. The Field hash code is the hash code
  156. * of its name XOR'd with the hash code of its class name.
  157. *
  158. * @return the hash code for the object.
  159. */
  160. public int hashCode()
  161. {
  162. return f.getDeclaringClass().getName().hashCode() ^ f.getName().hashCode();
  163. }
  164. /**
  165. * Get a String representation of the Field. A Field's String
  166. * representation is "&lt;modifiers&gt; &lt;type&gt;
  167. * &lt;class&gt;.&lt;fieldname&gt;".<br> Example:
  168. * <code>public transient boolean gnu.parse.Parser.parseComplete</code>
  169. *
  170. * @return the String representation of the Field
  171. */
  172. public String toString()
  173. {
  174. // 64 is a reasonable buffer initial size for field
  175. CPStringBuilder sb = new CPStringBuilder(64);
  176. Modifier.toString(getModifiers(), sb).append(' ');
  177. sb.append(ClassHelper.getUserName(getType())).append(' ');
  178. sb.append(getDeclaringClass().getName()).append('.');
  179. sb.append(getName());
  180. return sb.toString();
  181. }
  182. public String toGenericString()
  183. {
  184. CPStringBuilder sb = new CPStringBuilder(64);
  185. Modifier.toString(getModifiers(), sb).append(' ');
  186. sb.append(getGenericType()).append(' ');
  187. sb.append(getDeclaringClass().getName()).append('.');
  188. sb.append(getName());
  189. return sb.toString();
  190. }
  191. /**
  192. * Get the value of this Field. If it is primitive, it will be wrapped
  193. * in the appropriate wrapper type (boolean = java.lang.Boolean).<p>
  194. *
  195. * If the field is static, <code>o</code> will be ignored. Otherwise, if
  196. * <code>o</code> is null, you get a <code>NullPointerException</code>,
  197. * and if it is incompatible with the declaring class of the field, you
  198. * get an <code>IllegalArgumentException</code>.<p>
  199. *
  200. * Next, if this Field enforces access control, your runtime context is
  201. * evaluated, and you may have an <code>IllegalAccessException</code> if
  202. * you could not access this field in similar compiled code. If the field
  203. * is static, and its class is uninitialized, you trigger class
  204. * initialization, which may end in a
  205. * <code>ExceptionInInitializerError</code>.<p>
  206. *
  207. * Finally, the field is accessed, and primitives are wrapped (but not
  208. * necessarily in new objects). This method accesses the field of the
  209. * declaring class, even if the instance passed in belongs to a subclass
  210. * which declares another field to hide this one.
  211. *
  212. * @param o the object to get the value of this Field from
  213. * @return the value of the Field
  214. * @throws IllegalAccessException if you could not normally access this field
  215. * (i.e. it is not public)
  216. * @throws IllegalArgumentException if <code>o</code> is not an instance of
  217. * the class or interface declaring this field
  218. * @throws NullPointerException if <code>o</code> is null and this field
  219. * requires an instance
  220. * @throws ExceptionInInitializerError if accessing a static field triggered
  221. * class initialization, which then failed
  222. * @see #getBoolean(Object)
  223. * @see #getByte(Object)
  224. * @see #getChar(Object)
  225. * @see #getShort(Object)
  226. * @see #getInt(Object)
  227. * @see #getLong(Object)
  228. * @see #getFloat(Object)
  229. * @see #getDouble(Object)
  230. */
  231. public Object get(Object o)
  232. throws IllegalAccessException
  233. {
  234. return f.get(o);
  235. }
  236. /**
  237. * Get the value of this boolean Field. If the field is static,
  238. * <code>o</code> will be ignored.
  239. *
  240. * @param o the object to get the value of this Field from
  241. * @return the value of the Field
  242. * @throws IllegalAccessException if you could not normally access this field
  243. * (i.e. it is not public)
  244. * @throws IllegalArgumentException if this is not a boolean field of
  245. * <code>o</code>, or if <code>o</code> is not an instance of the
  246. * declaring class of this field
  247. * @throws NullPointerException if <code>o</code> is null and this field
  248. * requires an instance
  249. * @throws ExceptionInInitializerError if accessing a static field triggered
  250. * class initialization, which then failed
  251. * @see #get(Object)
  252. */
  253. public boolean getBoolean(Object o)
  254. throws IllegalAccessException
  255. {
  256. return f.getBoolean(o);
  257. }
  258. /**
  259. * Get the value of this byte Field. If the field is static,
  260. * <code>o</code> will be ignored.
  261. *
  262. * @param o the object to get the value of this Field from
  263. * @return the value of the Field
  264. * @throws IllegalAccessException if you could not normally access this field
  265. * (i.e. it is not public)
  266. * @throws IllegalArgumentException if this is not a byte field of
  267. * <code>o</code>, or if <code>o</code> is not an instance of the
  268. * declaring class of this field
  269. * @throws NullPointerException if <code>o</code> is null and this field
  270. * requires an instance
  271. * @throws ExceptionInInitializerError if accessing a static field triggered
  272. * class initialization, which then failed
  273. * @see #get(Object)
  274. */
  275. public byte getByte(Object o)
  276. throws IllegalAccessException
  277. {
  278. return f.getByte(o);
  279. }
  280. /**
  281. * Get the value of this Field as a char. If the field is static,
  282. * <code>o</code> will be ignored.
  283. *
  284. * @throws IllegalAccessException if you could not normally access this field
  285. * (i.e. it is not public)
  286. * @throws IllegalArgumentException if this is not a char field of
  287. * <code>o</code>, or if <code>o</code> is not an instance
  288. * of the declaring class of this field
  289. * @throws NullPointerException if <code>o</code> is null and this field
  290. * requires an instance
  291. * @throws ExceptionInInitializerError if accessing a static field triggered
  292. * class initialization, which then failed
  293. * @see #get(Object)
  294. */
  295. public char getChar(Object o)
  296. throws IllegalAccessException
  297. {
  298. return f.getChar(o);
  299. }
  300. /**
  301. * Get the value of this Field as a short. If the field is static,
  302. * <code>o</code> will be ignored.
  303. *
  304. * @param o the object to get the value of this Field from
  305. * @return the value of the Field
  306. * @throws IllegalAccessException if you could not normally access this field
  307. * (i.e. it is not public)
  308. * @throws IllegalArgumentException if this is not a byte or short
  309. * field of <code>o</code>, or if <code>o</code> is not an instance
  310. * of the declaring class of this field
  311. * @throws NullPointerException if <code>o</code> is null and this field
  312. * requires an instance
  313. * @throws ExceptionInInitializerError if accessing a static field triggered
  314. * class initialization, which then failed
  315. * @see #get(Object)
  316. */
  317. public short getShort(Object o)
  318. throws IllegalAccessException
  319. {
  320. return f.getShort(o);
  321. }
  322. /**
  323. * Get the value of this Field as an int. If the field is static,
  324. * <code>o</code> will be ignored.
  325. *
  326. * @param o the object to get the value of this Field from
  327. * @return the value of the Field
  328. * @throws IllegalAccessException if you could not normally access this field
  329. * (i.e. it is not public)
  330. * @throws IllegalArgumentException if this is not a byte, short, char, or
  331. * int field of <code>o</code>, or if <code>o</code> is not an
  332. * instance of the declaring class of this field
  333. * @throws NullPointerException if <code>o</code> is null and this field
  334. * requires an instance
  335. * @throws ExceptionInInitializerError if accessing a static field triggered
  336. * class initialization, which then failed
  337. * @see #get(Object)
  338. */
  339. public int getInt(Object o)
  340. throws IllegalAccessException
  341. {
  342. return f.getInt(o);
  343. }
  344. /**
  345. * Get the value of this Field as a long. If the field is static,
  346. * <code>o</code> will be ignored.
  347. *
  348. * @param o the object to get the value of this Field from
  349. * @return the value of the Field
  350. * @throws IllegalAccessException if you could not normally access this field
  351. * (i.e. it is not public)
  352. * @throws IllegalArgumentException if this is not a byte, short, char, int,
  353. * or long field of <code>o</code>, or if <code>o</code> is not an
  354. * instance of the declaring class of this field
  355. * @throws NullPointerException if <code>o</code> is null and this field
  356. * requires an instance
  357. * @throws ExceptionInInitializerError if accessing a static field triggered
  358. * class initialization, which then failed
  359. * @see #get(Object)
  360. */
  361. public long getLong(Object o)
  362. throws IllegalAccessException
  363. {
  364. return f.getLong(o);
  365. }
  366. /**
  367. * Get the value of this Field as a float. If the field is static,
  368. * <code>o</code> will be ignored.
  369. *
  370. * @param o the object to get the value of this Field from
  371. * @return the value of the Field
  372. * @throws IllegalAccessException if you could not normally access this field
  373. * (i.e. it is not public)
  374. * @throws IllegalArgumentException if this is not a byte, short, char, int,
  375. * long, or float field of <code>o</code>, or if <code>o</code> is
  376. * not an instance of the declaring class of this field
  377. * @throws NullPointerException if <code>o</code> is null and this field
  378. * requires an instance
  379. * @throws ExceptionInInitializerError if accessing a static field triggered
  380. * class initialization, which then failed
  381. * @see #get(Object)
  382. */
  383. public float getFloat(Object o)
  384. throws IllegalAccessException
  385. {
  386. return f.getFloat(o);
  387. }
  388. /**
  389. * Get the value of this Field as a double. If the field is static,
  390. * <code>o</code> will be ignored.
  391. *
  392. * @param o the object to get the value of this Field from
  393. * @return the value of the Field
  394. * @throws IllegalAccessException if you could not normally access this field
  395. * (i.e. it is not public)
  396. * @throws IllegalArgumentException if this is not a byte, short, char, int,
  397. * long, float, or double field of <code>o</code>, or if
  398. * <code>o</code> is not an instance of the declaring class of this
  399. * field
  400. * @throws NullPointerException if <code>o</code> is null and this field
  401. * requires an instance
  402. * @throws ExceptionInInitializerError if accessing a static field triggered
  403. * class initialization, which then failed
  404. * @see #get(Object)
  405. */
  406. public double getDouble(Object o)
  407. throws IllegalAccessException
  408. {
  409. return f.getDouble(o);
  410. }
  411. /**
  412. * Set the value of this Field. If it is a primitive field, the value
  413. * will be unwrapped from the passed object (boolean = java.lang.Boolean).<p>
  414. *
  415. * If the field is static, <code>o</code> will be ignored. Otherwise, if
  416. * <code>o</code> is null, you get a <code>NullPointerException</code>,
  417. * and if it is incompatible with the declaring class of the field, you
  418. * get an <code>IllegalArgumentException</code>.<p>
  419. *
  420. * Next, if this Field enforces access control, your runtime context is
  421. * evaluated, and you may have an <code>IllegalAccessException</code> if
  422. * you could not access this field in similar compiled code. This also
  423. * occurs whether or not there is access control if the field is final.
  424. * If the field is primitive, and unwrapping your argument fails, you will
  425. * get an <code>IllegalArgumentException</code>; likewise, this error
  426. * happens if <code>value</code> cannot be cast to the correct object type.
  427. * If the field is static, and its class is uninitialized, you trigger class
  428. * initialization, which may end in a
  429. * <code>ExceptionInInitializerError</code>.<p>
  430. *
  431. * Finally, the field is set with the widened value. This method accesses
  432. * the field of the declaring class, even if the instance passed in belongs
  433. * to a subclass which declares another field to hide this one.
  434. *
  435. * @param o the object to set this Field on
  436. * @param value the value to set this Field to
  437. * @throws IllegalAccessException if you could not normally access this field
  438. * (i.e. it is not public)
  439. * @throws IllegalArgumentException if <code>value</code> cannot be
  440. * converted by a widening conversion to the underlying type of
  441. * the Field, or if <code>o</code> is not an instance of the class
  442. * declaring this field
  443. * @throws NullPointerException if <code>o</code> is null and this field
  444. * requires an instance
  445. * @throws ExceptionInInitializerError if accessing a static field triggered
  446. * class initialization, which then failed
  447. * @see #setBoolean(Object, boolean)
  448. * @see #setByte(Object, byte)
  449. * @see #setChar(Object, char)
  450. * @see #setShort(Object, short)
  451. * @see #setInt(Object, int)
  452. * @see #setLong(Object, long)
  453. * @see #setFloat(Object, float)
  454. * @see #setDouble(Object, double)
  455. */
  456. public void set(Object o, Object value)
  457. throws IllegalAccessException
  458. {
  459. f.set(o, value);
  460. }
  461. /**
  462. * Set this boolean Field. If the field is static, <code>o</code> will be
  463. * ignored.
  464. *
  465. * @param o the object to set this Field on
  466. * @param value the value to set this Field to
  467. * @throws IllegalAccessException if you could not normally access this field
  468. * (i.e. it is not public)
  469. * @throws IllegalArgumentException if this is not a boolean field, or if
  470. * <code>o</code> is not an instance of the class declaring this
  471. * field
  472. * @throws NullPointerException if <code>o</code> is null and this field
  473. * requires an instance
  474. * @throws ExceptionInInitializerError if accessing a static field triggered
  475. * class initialization, which then failed
  476. * @see #set(Object, Object)
  477. */
  478. public void setBoolean(Object o, boolean value)
  479. throws IllegalAccessException
  480. {
  481. f.setBoolean(o, value);
  482. }
  483. /**
  484. * Set this byte Field. If the field is static, <code>o</code> will be
  485. * ignored.
  486. *
  487. * @param o the object to set this Field on
  488. * @param value the value to set this Field to
  489. * @throws IllegalAccessException if you could not normally access this field
  490. * (i.e. it is not public)
  491. * @throws IllegalArgumentException if this is not a byte, short, int, long,
  492. * float, or double field, or if <code>o</code> is not an instance
  493. * of the class declaring this field
  494. * @throws NullPointerException if <code>o</code> is null and this field
  495. * requires an instance
  496. * @throws ExceptionInInitializerError if accessing a static field triggered
  497. * class initialization, which then failed
  498. * @see #set(Object, Object)
  499. */
  500. public void setByte(Object o, byte value)
  501. throws IllegalAccessException
  502. {
  503. f.setByte(o, value);
  504. }
  505. /**
  506. * Set this char Field. If the field is static, <code>o</code> will be
  507. * ignored.
  508. *
  509. * @param o the object to set this Field on
  510. * @param value the value to set this Field to
  511. * @throws IllegalAccessException if you could not normally access this field
  512. * (i.e. it is not public)
  513. * @throws IllegalArgumentException if this is not a char, int, long,
  514. * float, or double field, or if <code>o</code> is not an instance
  515. * of the class declaring this field
  516. * @throws NullPointerException if <code>o</code> is null and this field
  517. * requires an instance
  518. * @throws ExceptionInInitializerError if accessing a static field triggered
  519. * class initialization, which then failed
  520. * @see #set(Object, Object)
  521. */
  522. public void setChar(Object o, char value)
  523. throws IllegalAccessException
  524. {
  525. f.setChar(o, value);
  526. }
  527. /**
  528. * Set this short Field. If the field is static, <code>o</code> will be
  529. * ignored.
  530. *
  531. * @param o the object to set this Field on
  532. * @param value the value to set this Field to
  533. * @throws IllegalAccessException if you could not normally access this field
  534. * (i.e. it is not public)
  535. * @throws IllegalArgumentException if this is not a short, int, long,
  536. * float, or double field, or if <code>o</code> is not an instance
  537. * of the class declaring this field
  538. * @throws NullPointerException if <code>o</code> is null and this field
  539. * requires an instance
  540. * @throws ExceptionInInitializerError if accessing a static field triggered
  541. * class initialization, which then failed
  542. * @see #set(Object, Object)
  543. */
  544. public void setShort(Object o, short value)
  545. throws IllegalAccessException
  546. {
  547. f.setShort(o, value);
  548. }
  549. /**
  550. * Set this int Field. If the field is static, <code>o</code> will be
  551. * ignored.
  552. *
  553. * @param o the object to set this Field on
  554. * @param value the value to set this Field to
  555. * @throws IllegalAccessException if you could not normally access this field
  556. * (i.e. it is not public)
  557. * @throws IllegalArgumentException if this is not an int, long, float, or
  558. * double field, or if <code>o</code> is not an instance of the
  559. * class declaring this field
  560. * @throws NullPointerException if <code>o</code> is null and this field
  561. * requires an instance
  562. * @throws ExceptionInInitializerError if accessing a static field triggered
  563. * class initialization, which then failed
  564. * @see #set(Object, Object)
  565. */
  566. public void setInt(Object o, int value)
  567. throws IllegalAccessException
  568. {
  569. f.setInt(o, value);
  570. }
  571. /**
  572. * Set this long Field. If the field is static, <code>o</code> will be
  573. * ignored.
  574. *
  575. * @param o the object to set this Field on
  576. * @param value the value to set this Field to
  577. * @throws IllegalAccessException if you could not normally access this field
  578. * (i.e. it is not public)
  579. * @throws IllegalArgumentException if this is not a long, float, or double
  580. * field, or if <code>o</code> is not an instance of the class
  581. * declaring this field
  582. * @throws NullPointerException if <code>o</code> is null and this field
  583. * requires an instance
  584. * @throws ExceptionInInitializerError if accessing a static field triggered
  585. * class initialization, which then failed
  586. * @see #set(Object, Object)
  587. */
  588. public void setLong(Object o, long value)
  589. throws IllegalAccessException
  590. {
  591. f.setLong(o, value);
  592. }
  593. /**
  594. * Set this float Field. If the field is static, <code>o</code> will be
  595. * ignored.
  596. *
  597. * @param o the object to set this Field on
  598. * @param value the value to set this Field to
  599. * @throws IllegalAccessException if you could not normally access this field
  600. * (i.e. it is not public)
  601. * @throws IllegalArgumentException if this is not a float or long field, or
  602. * if <code>o</code> is not an instance of the class declaring this
  603. * field
  604. * @throws NullPointerException if <code>o</code> is null and this field
  605. * requires an instance
  606. * @throws ExceptionInInitializerError if accessing a static field triggered
  607. * class initialization, which then failed
  608. * @see #set(Object, Object)
  609. */
  610. public void setFloat(Object o, float value)
  611. throws IllegalAccessException
  612. {
  613. f.setFloat(o, value);
  614. }
  615. /**
  616. * Set this double Field. If the field is static, <code>o</code> will be
  617. * ignored.
  618. *
  619. * @param o the object to set this Field on
  620. * @param value the value to set this Field to
  621. * @throws IllegalAccessException if you could not normally access this field
  622. * (i.e. it is not public)
  623. * @throws IllegalArgumentException if this is not a double field, or if
  624. * <code>o</code> is not an instance of the class declaring this
  625. * field
  626. * @throws NullPointerException if <code>o</code> is null and this field
  627. * requires an instance
  628. * @throws ExceptionInInitializerError if accessing a static field triggered
  629. * class initialization, which then failed
  630. * @see #set(Object, Object)
  631. */
  632. public void setDouble(Object o, double value)
  633. throws IllegalAccessException
  634. {
  635. f.setDouble(o, value);
  636. }
  637. /**
  638. * Return the generic type of the field. If the field type is not a generic
  639. * type, the method returns the same as <code>getType()</code>.
  640. *
  641. * @throws GenericSignatureFormatError if the generic signature does
  642. * not conform to the format specified in the Virtual Machine
  643. * specification, version 3.
  644. * @since 1.5
  645. */
  646. public Type getGenericType()
  647. {
  648. if (p == null)
  649. {
  650. String signature = f.getSignature();
  651. if (signature == null)
  652. return getType();
  653. p = new FieldSignatureParser(getDeclaringClass(),
  654. signature);
  655. }
  656. return p.getFieldType();
  657. }
  658. /**
  659. * Returns the element's annotation for the specified annotation type,
  660. * or <code>null</code> if no such annotation exists.
  661. *
  662. * @param annotationClass the type of annotation to look for.
  663. * @return this element's annotation for the specified type, or
  664. * <code>null</code> if no such annotation exists.
  665. * @throws NullPointerException if the annotation class is <code>null</code>.
  666. */
  667. public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
  668. {
  669. // Inescapable as the VM layer is 1.4 based. T will erase to Annotation anyway.
  670. @SuppressWarnings("unchecked") T ann = (T) f.getAnnotation(annotationClass);
  671. return ann;
  672. }
  673. /**
  674. * Returns all annotations directly defined by the element. If there are
  675. * no annotations directly associated with the element, then a zero-length
  676. * array will be returned. The returned array may be modified by the client
  677. * code, but this will have no effect on the annotation content of this
  678. * class, and hence no effect on the return value of this method for
  679. * future callers.
  680. *
  681. * @return the annotations directly defined by the element.
  682. * @since 1.5
  683. */
  684. public Annotation[] getDeclaredAnnotations()
  685. {
  686. return f.getDeclaredAnnotations();
  687. }
  688. }