From: <ls...@us...> - 2008-12-13 21:11:18
|
Revision: 4786 http://jnode.svn.sourceforge.net/jnode/?rev=4786&view=rev Author: lsantha Date: 2008-12-13 21:11:12 +0000 (Sat, 13 Dec 2008) Log Message: ----------- Intgerated java.lang.reflect.Field from OpenJDK. Modified Paths: -------------- trunk/core/src/core/org/jnode/vm/classmgr/VmConstantPool.java trunk/core/src/core/org/jnode/vm/classmgr/VmField.java trunk/core/src/openjdk/vm/sun/misc/NativeUnsafe.java trunk/core/src/openjdk/vm/sun/misc/UnsafeHelper.java trunk/core/src/openjdk/vm/sun/reflect/NativeReflection.java Added Paths: ----------- trunk/core/src/openjdk/java/java/lang/reflect/Field.java trunk/core/src/openjdk/vm/sun/misc/NativeGC.java trunk/core/src/openjdk/vm/sun/misc/NativeNativeSignalHandler.java trunk/core/src/openjdk/vm/sun/misc/NativePerf.java trunk/core/src/openjdk/vm/sun/misc/NativeSignal.java trunk/core/src/openjdk/vm/sun/misc/NativeVMSupport.java trunk/core/src/openjdk/vm/sun/misc/NativeVersion.java trunk/core/src/openjdk/vm/sun/reflect/NativeNativeConstructorAccessorImpl.java trunk/core/src/openjdk/vm/sun/reflect/NativeNativeMethodAccessorImpl.java Removed Paths: ------------- trunk/core/src/classpath/vm/java/lang/reflect/Field.java Deleted: trunk/core/src/classpath/vm/java/lang/reflect/Field.java =================================================================== --- trunk/core/src/classpath/vm/java/lang/reflect/Field.java 2008-12-13 04:47:08 UTC (rev 4785) +++ trunk/core/src/classpath/vm/java/lang/reflect/Field.java 2008-12-13 21:11:12 UTC (rev 4786) @@ -1,783 +0,0 @@ -/* java.lang.reflect.Field - reflection of Java fields - Copyright (C) 1998, 2001, 2005 Free Software Foundation, Inc. - -This file is part of GNU Classpath. - -GNU Classpath is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. - -GNU Classpath is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with GNU Classpath; see the file COPYING. If not, write to the -Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -02110-1301 USA. - -Linking this library statically or dynamically with other modules is -making a combined work based on this library. Thus, the terms and -conditions of the GNU General Public License cover the whole -combination. - -As a special exception, the copyright holders of this library give you -permission to link this library with independent modules to produce an -executable, regardless of the license terms of these independent -modules, and to copy and distribute the resulting executable under -terms of your choice, provided that you also meet, for each linked -independent module, the terms and conditions of the license of that -module. An independent module is a module which is not derived from -or based on this library. If you modify this library, you may extend -this exception to your version of the library, but you are not -obligated to do so. If you do not wish to do so, delete this -exception statement from your version. */ - - -package java.lang.reflect; - -import gnu.java.lang.ClassHelper; -import gnu.java.lang.reflect.FieldSignatureParser; - -import org.jnode.vm.VmReflection; -import org.jnode.vm.classmgr.VmField; -import java.lang.annotation.Annotation; - -/** - * The Field class represents a member variable of a class. It also allows - * dynamic access to a member, via reflection. This works for both - * static and instance fields. Operations on Field objects know how to - * do widening conversions, but throw {@link IllegalArgumentException} if - * a narrowing conversion would be necessary. You can query for information - * on this Field regardless of location, but get and set access may be limited - * by Java language access controls. If you can't do it in the compiler, you - * can't normally do it here either.<p> - * - * <B>Note:</B> This class returns and accepts types as Classes, even - * primitive types; there are Class types defined that represent each - * different primitive type. They are <code>java.lang.Boolean.TYPE, - * java.lang.Byte.TYPE,</code>, also available as <code>boolean.class, - * byte.class</code>, etc. These are not to be confused with the - * classes <code>java.lang.Boolean, java.lang.Byte</code>, etc., which are - * real classes.<p> - * - * Also note that this is not a serializable class. It is entirely feasible - * to make it serializable using the Externalizable interface, but this is - * on Sun, not me. - * - * @author John Keiser - * @author Eric Blake <eb...@em...> - * @see Member - * @see Class - * @see Class#getField(String) - * @see Class#getDeclaredField(String) - * @see Class#getFields() - * @see Class#getDeclaredFields() - * @since 1.1 - * @status updated to 1.4 - */ -public final class Field -extends AccessibleObject implements Member -{ - private final VmField vmField; - - private static final int FIELD_MODIFIERS - = Modifier.FINAL | Modifier.PRIVATE | Modifier.PROTECTED - | Modifier.PUBLIC | Modifier.STATIC | Modifier.TRANSIENT - | Modifier.VOLATILE; - - /** - * This class is uninstantiable except natively. - */ - public Field(VmField vmField) { - this.vmField = vmField; - } - - public Field(Class declaringClass, String name, Class type, int modifiers, int slot, String signature, byte[] annotations) { - //todo implement it - throw new UnsupportedOperationException(); - } - - /** - * Gets the class that declared this field, or the class where this field - * is a non-inherited member. - * @return the class that declared this member - */ - public Class<?> getDeclaringClass() - { - return vmField.getDeclaringClass().asClass(); - } - - /** - * Gets the name of this field. - * - * @return the name of this field - */ - public String getName() { - return vmField.getName(); - } - - /** - * Return the raw modifiers for this field. - * @return the field's modifiers - */ - public int getModifiersInternal() { - return vmField.getModifiers(); - } - - /** - * Gets the modifiers this field uses. Use the <code>Modifier</code> - * class to interpret the values. A field can only have a subset of the - * following modifiers: public, private, protected, static, final, - * transient, and volatile. - * - * @return an integer representing the modifiers to this Member - * @see Modifier - */ - public int getModifiers() - { - return getModifiersInternal() & FIELD_MODIFIERS; - } - - /** - * Return true if this field is synthetic, false otherwise. - * @since 1.5 - */ - public boolean isSynthetic() - { - return (getModifiersInternal() & Modifier.SYNTHETIC) != 0; - } - - /** - * Return true if this field represents an enum constant, - * false otherwise. - * @since 1.5 - */ - public boolean isEnumConstant() - { - return (getModifiersInternal() & Modifier.ENUM) != 0; - } - - /** - * Gets the type of this field. - * - * @return the type of this field - */ - public Class getType() { - return vmField.getType().asClass(); - } - - /** - * Compare two objects to see if they are semantically equivalent. - * Two Fields are semantically equivalent if they have the same declaring - * class, name, and type. Since you can't creat a Field except through - * the VM, this is just the == relation. - * - * @param o the object to compare to - * @return <code>true</code> if they are equal; <code>false</code> if not - */ - public boolean equals(Object o) - { - if (!(o instanceof Field)) - return false; - Field that = (Field)o; - if (this.getDeclaringClass() != that.getDeclaringClass()) - return false; - if (!this.getName().equals(that.getName())) - return false; - if (this.getType() != that.getType()) - return false; - return true; - } - - /** - * Get the hash code for the Field. The Field hash code is the hash code - * of its name XOR'd with the hash code of its class name. - * - * @return the hash code for the object. - */ - public int hashCode() - { - return getDeclaringClass().getName().hashCode() ^ getName().hashCode(); - } - - /** - * Get a String representation of the Field. A Field's String - * representation is "<modifiers> <type> - * <class>.<fieldname>".<br> Example: - * <code>public transient boolean gnu.parse.Parser.parseComplete</code> - * - * @return the String representation of the Field - */ - public String toString() { - // 64 is a reasonable buffer initial size for field - StringBuilder sb = new StringBuilder(64); - sb.append(Modifier.toString(getModifiers())).append(' '); - sb.append(ClassHelper.getUserName(getType())).append(' '); - sb.append(getDeclaringClass().getName()).append('.'); - sb.append(getName()); - return sb.toString(); - } - - public String toGenericString() - { - StringBuilder sb = new StringBuilder(64); - sb.append(Modifier.toString(getModifiers())).append(' '); - sb.append(getGenericType()).append(' '); - sb.append(getDeclaringClass().getName()).append('.'); - sb.append(getName()); - return sb.toString(); - } - - /** - * Get the value of this Field. If it is primitive, it will be wrapped - * in the appropriate wrapper type (boolean = java.lang.Boolean).<p> - * - * If the field is static, <code>o</code> will be ignored. Otherwise, if - * <code>o</code> is null, you get a <code>NullPointerException</code>, - * and if it is incompatible with the declaring class of the field, you - * get an <code>IllegalArgumentException</code>.<p> - * - * Next, if this Field enforces access control, your runtime context is - * evaluated, and you may have an <code>IllegalAccessException</code> if - * you could not access this field in similar compiled code. If the field - * is static, and its class is uninitialized, you trigger class - * initialization, which may end in a - * <code>ExceptionInInitializerError</code>.<p> - * - * Finally, the field is accessed, and primitives are wrapped (but not - * necessarily in new objects). This method accesses the field of the - * declaring class, even if the instance passed in belongs to a subclass - * which declares another field to hide this one. - * - * @param o the object to get the value of this Field from - * @return the value of the Field - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if <code>o</code> is not an instance of - * the class or interface declaring this field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #getBoolean(Object) - * @see #getByte(Object) - * @see #getChar(Object) - * @see #getShort(Object) - * @see #getInt(Object) - * @see #getLong(Object) - * @see #getFloat(Object) - * @see #getDouble(Object) - */ - public Object get(Object o) throws IllegalAccessException { - if (vmField.isPrimitive()) { - switch (vmField.getSignature().charAt(0)) { - case 'Z': - return Boolean.valueOf(VmReflection.getBoolean(vmField, o)); - case 'B': - return new Byte(VmReflection.getByte(vmField, o)); - case 'C': - return new Character(VmReflection.getChar(vmField, o)); - case 'S': - return new Short(VmReflection.getShort(vmField, o)); - case 'I': - return new Integer(VmReflection.getInt(vmField, o)); - case 'J': - return new Long(VmReflection.getLong(vmField, o)); - case 'F': - return new Float(VmReflection.getFloat(vmField, o)); - case 'D': - return new Double(VmReflection.getDouble(vmField, o)); - default: - throw new IllegalArgumentException("Unknown primitive type"); - } - } else { - return VmReflection.getObject(vmField, o); - } - } - - /** - * Get the value of this boolean Field. If the field is static, - * <code>o</code> will be ignored. - * - * @param o the object to get the value of this Field from - * @return the value of the Field - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a boolean field of - * <code>o</code>, or if <code>o</code> is not an instance of the - * declaring class of this field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #get(Object) - */ - public boolean getBoolean(Object o) throws IllegalAccessException { - return VmReflection.getBoolean(vmField, o); - } - - /** - * Get the value of this byte Field. If the field is static, - * <code>o</code> will be ignored. - * - * @param o the object to get the value of this Field from - * @return the value of the Field - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a byte field of - * <code>o</code>, or if <code>o</code> is not an instance of the - * declaring class of this field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #get(Object) - */ - public byte getByte(Object o) throws IllegalAccessException { - return VmReflection.getByte(vmField, o); - } - - /** - * Get the value of this Field as a char. If the field is static, - * <code>o</code> will be ignored. - * - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a char field of - * <code>o</code>, or if <code>o</code> is not an instance - * of the declaring class of this field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #get(Object) - */ - public char getChar(Object o) throws IllegalAccessException { - return VmReflection.getChar(vmField, o); - } - - /** - * Get the value of this Field as a short. If the field is static, - * <code>o</code> will be ignored. - * - * @param o the object to get the value of this Field from - * @return the value of the Field - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a byte or short - * field of <code>o</code>, or if <code>o</code> is not an instance - * of the declaring class of this field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #get(Object) - */ - public short getShort(Object o) throws IllegalAccessException { - return VmReflection.getShort(vmField, o); - } - - /** - * Get the value of this Field as an int. If the field is static, - * <code>o</code> will be ignored. - * - * @param o the object to get the value of this Field from - * @return the value of the Field - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a byte, short, char, or - * int field of <code>o</code>, or if <code>o</code> is not an - * instance of the declaring class of this field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #get(Object) - */ - public int getInt(Object o) throws IllegalAccessException { - return VmReflection.getInt(vmField, o); - } - - /** - * Get the value of this Field as a long. If the field is static, - * <code>o</code> will be ignored. - * - * @param o the object to get the value of this Field from - * @return the value of the Field - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a byte, short, char, int, - * or long field of <code>o</code>, or if <code>o</code> is not an - * instance of the declaring class of this field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #get(Object) - */ - public long getLong(Object o) throws IllegalAccessException { - return VmReflection.getLong(vmField, o); - } - - /** - * Get the value of this Field as a float. If the field is static, - * <code>o</code> will be ignored. - * - * @param o the object to get the value of this Field from - * @return the value of the Field - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a byte, short, char, int, - * long, or float field of <code>o</code>, or if <code>o</code> is - * not an instance of the declaring class of this field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #get(Object) - */ - public float getFloat(Object o) throws IllegalAccessException { - return VmReflection.getFloat(vmField, o); - } - - /** - * Get the value of this Field as a double. If the field is static, - * <code>o</code> will be ignored. - * - * @param o the object to get the value of this Field from - * @return the value of the Field - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a byte, short, char, int, - * long, float, or double field of <code>o</code>, or if - * <code>o</code> is not an instance of the declaring class of this - * field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #get(Object) - */ - public double getDouble(Object o) throws IllegalAccessException { - return VmReflection.getDouble(vmField, o); - } - - /** - * Set the value of this Field. If it is a primitive field, the value - * will be unwrapped from the passed object (boolean = java.lang.Boolean).<p> - * - * If the field is static, <code>o</code> will be ignored. Otherwise, if - * <code>o</code> is null, you get a <code>NullPointerException</code>, - * and if it is incompatible with the declaring class of the field, you - * get an <code>IllegalArgumentException</code>.<p> - * - * Next, if this Field enforces access control, your runtime context is - * evaluated, and you may have an <code>IllegalAccessException</code> if - * you could not access this field in similar compiled code. This also - * occurs whether or not there is access control if the field is final. - * If the field is primitive, and unwrapping your argument fails, you will - * get an <code>IllegalArgumentException</code>; likewise, this error - * happens if <code>value</code> cannot be cast to the correct object type. - * If the field is static, and its class is uninitialized, you trigger class - * initialization, which may end in a - * <code>ExceptionInInitializerError</code>.<p> - * - * Finally, the field is set with the widened value. This method accesses - * the field of the declaring class, even if the instance passed in belongs - * to a subclass which declares another field to hide this one. - * - * @param o the object to set this Field on - * @param value the value to set this Field to - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if <code>value</code> cannot be - * converted by a widening conversion to the underlying type of - * the Field, or if <code>o</code> is not an instance of the class - * declaring this field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #setBoolean(Object, boolean) - * @see #setByte(Object, byte) - * @see #setChar(Object, char) - * @see #setShort(Object, short) - * @see #setInt(Object, int) - * @see #setLong(Object, long) - * @see #setFloat(Object, float) - * @see #setDouble(Object, double) - */ - public void set(Object o, Object value) throws IllegalAccessException { - if (vmField.isPrimitive()) { - switch (vmField.getSignature().charAt(0)) { - case 'Z': - VmReflection.setBoolean(vmField, o, ((Boolean) value).booleanValue()); - break; - case 'B': - VmReflection.setByte(vmField, o, ((Byte) value).byteValue()); - break; - case 'C': - VmReflection.setChar(vmField, o, ((Character) value).charValue()); - break; - case 'S': - VmReflection.setShort(vmField, o, ((Short) value).shortValue()); - break; - case 'I': - VmReflection.setInt(vmField, o, ((Integer) value).intValue()); - break; - case 'J': - VmReflection.setLong(vmField, o, ((Long) value).longValue()); - break; - case 'F': - VmReflection.setFloat(vmField, o, ((Float) value).floatValue()); - break; - case 'D': - VmReflection.setDouble(vmField, o, ((Double) value).doubleValue()); - break; - default: - throw new IllegalArgumentException("Unknown primitive type"); - } - } else { - VmReflection.setObject(vmField, o, value); - } - } - - /** - * Set this boolean Field. If the field is static, <code>o</code> will be - * ignored. - * - * @param o the object to set this Field on - * @param value the value to set this Field to - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a boolean field, or if - * <code>o</code> is not an instance of the class declaring this - * field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #set(Object, Object) - */ - public void setBoolean(Object o, boolean value) - throws IllegalAccessException { - VmReflection.setBoolean(vmField, o, value); - } - - /** - * Set this byte Field. If the field is static, <code>o</code> will be - * ignored. - * - * @param o the object to set this Field on - * @param value the value to set this Field to - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a byte, short, int, long, - * float, or double field, or if <code>o</code> is not an instance - * of the class declaring this field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #set(Object, Object) - */ - public void setByte(Object o, byte value) throws IllegalAccessException { - VmReflection.setByte(vmField, o, value); - } - - /** - * Set this char Field. If the field is static, <code>o</code> will be - * ignored. - * - * @param o the object to set this Field on - * @param value the value to set this Field to - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a char, int, long, - * float, or double field, or if <code>o</code> is not an instance - * of the class declaring this field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #set(Object, Object) - */ - public void setChar(Object o, char value) throws IllegalAccessException { - VmReflection.setChar(vmField, o, value); - } - - /** - * Set this short Field. If the field is static, <code>o</code> will be - * ignored. - * - * @param o the object to set this Field on - * @param value the value to set this Field to - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a short, int, long, - * float, or double field, or if <code>o</code> is not an instance - * of the class declaring this field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #set(Object, Object) - */ - public void setShort(Object o, short value) throws IllegalAccessException { - VmReflection.setShort(vmField, o, value); - } - - /** - * Set this int Field. If the field is static, <code>o</code> will be - * ignored. - * - * @param o the object to set this Field on - * @param value the value to set this Field to - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not an int, long, float, or - * double field, or if <code>o</code> is not an instance of the - * class declaring this field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #set(Object, Object) - */ - public void setInt(Object o, int value) throws IllegalAccessException { - VmReflection.setInt(vmField, o, value); - } - - /** - * Set this long Field. If the field is static, <code>o</code> will be - * ignored. - * - * @param o the object to set this Field on - * @param value the value to set this Field to - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a long, float, or double - * field, or if <code>o</code> is not an instance of the class - * declaring this field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #set(Object, Object) - */ - public void setLong(Object o, long value) throws IllegalAccessException { - VmReflection.setLong(vmField, o, value); - } - - /** - * Set this float Field. If the field is static, <code>o</code> will be - * ignored. - * - * @param o the object to set this Field on - * @param value the value to set this Field to - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a float or long field, or - * if <code>o</code> is not an instance of the class declaring this - * field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #set(Object, Object) - */ - public void setFloat(Object o, float value) throws IllegalAccessException { - VmReflection.setFloat(vmField, o, value); - } - - /** - * Set this double Field. If the field is static, <code>o</code> will be - * ignored. - * - * @param o the object to set this Field on - * @param value the value to set this Field to - * @throws IllegalAccessException if you could not normally access this field - * (i.e. it is not public) - * @throws IllegalArgumentException if this is not a double field, or if - * <code>o</code> is not an instance of the class declaring this - * field - * @throws NullPointerException if <code>o</code> is null and this field - * requires an instance - * @throws ExceptionInInitializerError if accessing a static field triggered - * class initialization, which then failed - * @see #set(Object, Object) - */ - public void setDouble(Object o, double value) throws IllegalAccessException { - VmReflection.setDouble(vmField, o, value); - } - - /** - * Return the generic type of the field. If the field type is not a generic - * type, the method returns the same as <code>getType()</code>. - * - * @throws GenericSignatureFormatError if the generic signature does - * not conform to the format specified in the Virtual Machine - * specification, version 3. - * @since 1.5 - */ - public Type getGenericType() - { - String signature = getSignature(); - if (signature == null) - return getType(); - FieldSignatureParser p = new FieldSignatureParser(getDeclaringClass(), - signature); - return p.getFieldType(); - } - - /** - * Return the String in the Signature attribute for this field. If there - * is no Signature attribute, return null. - */ - private String getSignature() { - return vmField.getSignature(); - } - - - - /** - * @see java.lang.reflect.AnnotatedElement#getAnnotation(java.lang.Class) - */ - public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { - return vmField.getAnnotation(annotationClass); - } - - /** - * @see java.lang.reflect.AnnotatedElement#getAnnotations() - */ - public Annotation[] getAnnotations() { - return vmField.getAnnotations(); - } - - /** - * @see java.lang.reflect.AnnotatedElement#getDeclaredAnnotations() - */ - public Annotation[] getDeclaredAnnotations() { - return vmField.getDeclaredAnnotations(); - } - - /** - * @see java.lang.reflect.AnnotatedElement#isAnnotationPresent(java.lang.Class) - */ - public boolean isAnnotationPresent(Class< ? extends Annotation> annotationClass) { - return vmField.isAnnotationPresent(annotationClass); - } - - public Field copy() { - //todo implement it - throw new UnsupportedOperationException(); - } - - //todo hide it - public VmField getVmField(){ - return vmField; - } -} Modified: trunk/core/src/core/org/jnode/vm/classmgr/VmConstantPool.java =================================================================== --- trunk/core/src/core/org/jnode/vm/classmgr/VmConstantPool.java 2008-12-13 04:47:08 UTC (rev 4785) +++ trunk/core/src/core/org/jnode/vm/classmgr/VmConstantPool.java 2008-12-13 21:11:12 UTC (rev 4786) @@ -47,7 +47,7 @@ VmConstFieldRef f = cp.getConstFieldRef(index); f.doResolve(loader); VmField vmf = f.getResolvedVmField(); - return new Field(vmf); + return vmf.asField(); } @Override @@ -55,7 +55,7 @@ VmConstFieldRef f = cp.getConstFieldRef(index); try { VmField vmf = f.getResolvedVmField(); - return new Field(vmf); + return vmf.asField(); } catch (NotResolvedYetException x) { return null; } Modified: trunk/core/src/core/org/jnode/vm/classmgr/VmField.java =================================================================== --- trunk/core/src/core/org/jnode/vm/classmgr/VmField.java 2008-12-13 04:47:08 UTC (rev 4785) +++ trunk/core/src/core/org/jnode/vm/classmgr/VmField.java 2008-12-13 21:11:12 UTC (rev 4786) @@ -122,15 +122,16 @@ } Field javaField = javaFieldHolder.get(); if (javaField == null) { - javaFieldHolder.set(javaField = new Field(this)); + //todo add annotations + javaField = new Field(getDeclaringClass().asClass(), getName(), getType().asClass(), getModifiers(), -1, + getSignature(), null); + javaFieldHolder.set(javaField); } return javaField; } /** * Resolve the type of this field - * - * @param cl */ protected final synchronized void resolve() { try { Added: trunk/core/src/openjdk/java/java/lang/reflect/Field.java =================================================================== --- trunk/core/src/openjdk/java/java/lang/reflect/Field.java (rev 0) +++ trunk/core/src/openjdk/java/java/lang/reflect/Field.java 2008-12-13 21:11:12 UTC (rev 4786) @@ -0,0 +1,1041 @@ +/* + * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package java.lang.reflect; + +import sun.reflect.FieldAccessor; +import sun.reflect.Reflection; +import sun.reflect.generics.repository.FieldRepository; +import sun.reflect.generics.factory.CoreReflectionFactory; +import sun.reflect.generics.factory.GenericsFactory; +import sun.reflect.generics.scope.ClassScope; +import java.lang.annotation.Annotation; +import java.util.Map; +import sun.reflect.annotation.AnnotationParser; + + +/** + * A {@code Field} provides information about, and dynamic access to, a + * single field of a class or an interface. The reflected field may + * be a class (static) field or an instance field. + * + * <p>A {@code Field} permits widening conversions to occur during a get or + * set access operation, but throws an {@code IllegalArgumentException} if a + * narrowing conversion would occur. + * + * @see Member + * @see java.lang.Class + * @see java.lang.Class#getFields() + * @see java.lang.Class#getField(String) + * @see java.lang.Class#getDeclaredFields() + * @see java.lang.Class#getDeclaredField(String) + * + * @author Kenneth Russell + * @author Nakul Saraiya + */ +public final +class Field extends AccessibleObject implements Member { + + private Class clazz; + private int slot; + // This is guaranteed to be interned by the VM in the 1.4 + // reflection implementation + private String name; + private Class type; + private int modifiers; + // Generics and annotations support + private transient String signature; + // generic info repository; lazily initialized + private transient FieldRepository genericInfo; + private byte[] annotations; + // Cached field accessor created without override + private FieldAccessor fieldAccessor; + // Cached field accessor created with override + private FieldAccessor overrideFieldAccessor; + // For sharing of FieldAccessors. This branching structure is + // currently only two levels deep (i.e., one root Field and + // potentially many Field objects pointing to it.) + private Field root; + + // More complicated security check cache needed here than for + // Class.newInstance() and Constructor.newInstance() + private Class securityCheckCache; + private Class securityCheckTargetClassCache; + + // Generics infrastructure + + private String getGenericSignature() {return signature;} + + // Accessor for factory + private GenericsFactory getFactory() { + Class<?> c = getDeclaringClass(); + // create scope and factory + return CoreReflectionFactory.make(c, ClassScope.make(c)); + } + + // Accessor for generic info repository + private FieldRepository getGenericInfo() { + // lazily initialize repository if necessary + if (genericInfo == null) { + // create and cache generic info repository + genericInfo = FieldRepository.make(getGenericSignature(), + getFactory()); + } + return genericInfo; //return cached repository + } + + //jnode + /** + * Package-private constructor used by ReflectAccess to enable + * instantiation of these objects in Java code from the java.lang + * package via sun.reflect.LangReflectAccess. + */ + public Field(Class declaringClass, + String name, + Class type, + int modifiers, + int slot, + String signature, + byte[] annotations) + { + this.clazz = declaringClass; + this.name = name; + this.type = type; + this.modifiers = modifiers; + this.slot = slot; + this.signature = signature; + this.annotations = annotations; + } + + /** + * Package-private routine (exposed to java.lang.Class via + * ReflectAccess) which returns a copy of this Field. The copy's + * "root" field points to this Field. + */ + Field copy() { + // This routine enables sharing of FieldAccessor objects + // among Field objects which refer to the same underlying + // method in the VM. (All of this contortion is only necessary + // because of the "accessibility" bit in AccessibleObject, + // which implicitly requires that new java.lang.reflect + // objects be fabricated for each reflective call on Class + // objects.) + Field res = new Field(clazz, name, type, modifiers, slot, signature, annotations); + res.root = this; + // Might as well eagerly propagate this if already present + res.fieldAccessor = fieldAccessor; + res.overrideFieldAccessor = overrideFieldAccessor; + return res; + } + + /** + * Returns the {@code Class} object representing the class or interface + * that declares the field represented by this {@code Field} object. + */ + public Class<?> getDeclaringClass() { + return clazz; + } + + /** + * Returns the name of the field represented by this {@code Field} object. + */ + public String getName() { + return name; + } + + /** + * Returns the Java language modifiers for the field represented + * by this {@code Field} object, as an integer. The {@code Modifier} class should + * be used to decode the modifiers. + * + * @see Modifier + */ + public int getModifiers() { + return modifiers; + } + + /** + * Returns {@code true} if this field represents an element of + * an enumerated type; returns {@code false} otherwise. + * + * @return {@code true} if and only if this field represents an element of + * an enumerated type. + * @since 1.5 + */ + public boolean isEnumConstant() { + return (getModifiers() & Modifier.ENUM) != 0; + } + + /** + * Returns {@code true} if this field is a synthetic + * field; returns {@code false} otherwise. + * + * @return true if and only if this field is a synthetic + * field as defined by the Java Language Specification. + * @since 1.5 + */ + public boolean isSynthetic() { + return Modifier.isSynthetic(getModifiers()); + } + + /** + * Returns a {@code Class} object that identifies the + * declared type for the field represented by this + * {@code Field} object. + * + * @return a {@code Class} object identifying the declared + * type of the field represented by this object + */ + public Class<?> getType() { + return type; + } + + /** + * Returns a {@code Type} object that represents the declared type for + * the field represented by this {@code Field} object. + * + * <p>If the {@code Type} is a parameterized type, the + * {@code Type} object returned must accurately reflect the + * actual type parameters used in the source code. + * + * <p>If the type of the underlying field is a type variable or a + * parameterized type, it is created. Otherwise, it is resolved. + * + * @return a {@code Type} object that represents the declared type for + * the field represented by this {@code Field} object + * @throws GenericSignatureFormatError if the generic field + * signature does not conform to the format specified in the Java + * Virtual Machine Specification, 3rd edition + * @throws TypeNotPresentException if the generic type + * signature of the underlying field refers to a non-existent + * type declaration + * @throws MalformedParameterizedTypeException if the generic + * signature of the underlying field refers to a parameterized type + * that cannot be instantiated for any reason + * @since 1.5 + */ + public Type getGenericType() { + if (getGenericSignature() != null) + return getGenericInfo().getGenericType(); + else + return getType(); + } + + + /** + * Compares this {@code Field} against the specified object. Returns + * true if the objects are the same. Two {@code Field} objects are the same if + * they were declared by the same class and have the same name + * and type. + */ + public boolean equals(Object obj) { + if (obj != null && obj instanceof Field) { + Field other = (Field)obj; + return (getDeclaringClass() == other.getDeclaringClass()) + && (getName() == other.getName()) + && (getType() == other.getType()); + } + return false; + } + + /** + * Returns a hashcode for this {@code Field}. This is computed as the + * exclusive-or of the hashcodes for the underlying field's + * declaring class name and its name. + */ + public int hashCode() { + return getDeclaringClass().getName().hashCode() ^ getName().hashCode(); + } + + /** + * Returns a string describing this {@code Field}. The format is + * the access modifiers for the field, if any, followed + * by the field type, followed by a space, followed by + * the fully-qualified name of the class declaring the field, + * followed by a period, followed by the name of the field. + * For example: + * <pre> + * public static final int java.lang.Thread.MIN_PRIORITY + * private int java.io.FileDescriptor.fd + * </pre> + * + * <p>The modifiers are placed in canonical order as specified by + * "The Java Language Specification". This is {@code public}, + * {@code protected} or {@code private} first, and then other + * modifiers in the following order: {@code static}, {@code final}, + * {@code transient}, {@code volatile}. + */ + public String toString() { + int mod = getModifiers(); + return (((mod == 0) ? "" : (Modifier.toString(mod) + " ")) + + getTypeName(getType()) + " " + + getTypeName(getDeclaringClass()) + "." + + getName()); + } + + /** + * Returns a string describing this {@code Field}, including + * its generic type. The format is the access modifiers for the + * field, if any, followed by the generic field type, followed by + * a space, followed by the fully-qualified name of the class + * declaring the field, followed by a period, followed by the name + * of the field. + * + * <p>The modifiers are placed in canonical order as specified by + * "The Java Language Specification". This is {@code public}, + * {@code protected} or {@code private} first, and then other + * modifiers in the following order: {@code static}, {@code final}, + * {@code transient}, {@code volatile}. + * + * @return a string describing this {@code Field}, including + * its generic type + * + * @since 1.5 + */ + public String toGenericString() { + int mod = getModifiers(); + Type fieldType = getGenericType(); + return (((mod == 0) ? "" : (Modifier.toString(mod) + " ")) + + ((fieldType instanceof Class) ? + getTypeName((Class)fieldType): fieldType.toString())+ " " + + getTypeName(getDeclaringClass()) + "." + + getName()); + } + + /** + * Returns the value of the field represented by this {@code Field}, on + * the specified object. The value is automatically wrapped in an + * object if it has a primitive type. + * + * <p>The underlying field's value is obtained as follows: + * + * <p>If the underlying field is a static field, the {@code obj} argument + * is ignored; it may be null. + * + * <p>Otherwise, the underlying field is an instance field. If the + * specified {@code obj} argument is null, the method throws a + * {@code NullPointerException}. If the specified object is not an + * instance of the class or interface declaring the underlying + * field, the method throws an {@code IllegalArgumentException}. + * + * <p>If this {@code Field} object enforces Java language access control, and + * the underlying field is inaccessible, the method throws an + * {@code IllegalAccessException}. + * If the underlying field is static, the class that declared the + * field is initialized if it has not already been initialized. + * + * <p>Otherwise, the value is retrieved from the underlying instance + * or static field. If the field has a primitive type, the value + * is wrapped in an object before being returned, otherwise it is + * returned as is. + * + * <p>If the field is hidden in the type of {@code obj}, + * the field's value is obtained according to the preceding rules. + * + * @param obj object from which the represented field's value is + * to be extracted + * @return the value of the represented field in object + * {@code obj}; primitive values are wrapped in an appropriate + * object before being returned + * + * @exception IllegalAccessException if the underlying field + * is inaccessible. + * @exception IllegalArgumentException if the specified object is not an + * instance of the class or interface declaring the underlying + * field (or a subclass or implementor thereof). + * @exception NullPointerException if the specified object is null + * and the field is an instance field. + * @exception ExceptionInInitializerError if the initialization provoked + * by this method fails. + */ + public Object get(Object obj) + throws IllegalArgumentException, IllegalAccessException + { + return getFieldAccessor(obj).get(obj); + } + + /** + * Gets the value of a static or instance {@code boolean} field. + * + * @param obj the object to extract the {@code boolean} value + * from + * @return the value of the {@code boolean} field + * + * @exception IllegalAccessException if the underlying field + * is inaccessible. + * @exception IllegalArgumentException if the specified object is not + * an instance of the class or interface declaring the + * underlying field (or a subclass or implementor + * thereof), or if the field value cannot be + * converted to the type {@code boolean} by a + * widening conversion. + * @exception NullPointerException if the specified object is null + * and the field is an instance field. + * @exception ExceptionInInitializerError if the initialization provoked + * by this method fails. + * @see Field#get + */ + public boolean getBoolean(Object obj) + throws IllegalArgumentException, IllegalAccessException + { + return getFieldAccessor(obj).getBoolean(obj); + } + + /** + * Gets the value of a static or instance {@code byte} field. + * + * @param obj the object to extract the {@code byte} value + * from + * @return the value of the {@code byte} field + * + * @exception IllegalAccessException if the underlying field + * is inaccessible. + * @exception IllegalArgumentException if the specified object is not + * an instance of the class or interface declaring the + * underlying field (or a subclass or implementor + * thereof), or if the field value cannot be + * converted to the type {@code byte} by a + * widening conversion. + * @exception NullPointerException if the specified object is null + * and the field is an instance field. + * @exception ExceptionInInitializerError if the initialization provoked + * by this method fails. + * @see Field#get + */ + public byte getByte(Object obj) + throws IllegalArgumentException, IllegalAccessException + { + return getFieldAccessor(obj).getByte(obj); + } + + /** + * Gets the value of a static or instance field of type + * {@code char} or of another primitive type convertible to + * type {@code char} via a widening conversion. + * + * @param obj the object to extract the {@code char} value + * from + * @return the value of the field converted to type {@code char} + * + * @exception IllegalAccessException if the underlying field + * is inaccessible. + * @exception IllegalArgumentException if the specified object is not + * an instance of the class or interface declaring the + * underlying field (or a subclass or implementor + * thereof), or if the field value cannot be + * converted to the type {@code char} by a + * widening conversion. + * @exception NullPointerException if the specified object is null + * and the field is an instance field. + * @exception ExceptionInInitializerError if the initialization provoked + * by this method fails. + * @see Field#get + */ + public char getChar(Object obj) + throws IllegalArgumentException, IllegalAccessException + { + return getFieldAccessor(obj).getChar(obj); + } + + /** + * Gets the value of a static or instance field of type + * {@code short} or of another primitive type convertible to + * type {@code short} via a widening conversion. + * + * @param obj the object to extract the {@code short} value + * from + * @return the value of the field converted to type {@code short} + * + * @exception IllegalAccessException if the underlying field + * is inaccessible. + * @exception IllegalArgumentException if the specified object is not + * an instance of the class or interface declaring the + * underlying field (or a subclass or implementor + * thereof), or if the field value cannot be + * converted to the type {@code short} by a + * widening conversion. + * @exception NullPointerException if the specified object is null + * and the field is an instance field. + * @exception ExceptionInInitializerError if the initialization provoked + * by this method fails. + * @see Field#get + */ + public short getShort(Object obj) + throws IllegalArgumentException, IllegalAccessException + { + return getFieldAccessor(obj).getShort(obj); + } + + /** + * Gets the value of a static or instance field of type + * {@code int} or of another primitive type convertible to + * type {@code int} via a widening conversion. + * + * @param obj the object to extract the {@code int} value + * from + * @return the value of the field converted to type {@code int} + * + * @exception IllegalAccessException if the underlying field + * is inaccessible. + * @exception IllegalArgumentException if the specified object is not + * an instance of the class or interface declaring the + * underlying field (or a subclass or implementor + * thereof), or if the field value cannot be + * converted to the type {@code int} by a + * widening conversion. + * @exception NullPointerException if the specified object is null + * and the field is an instance field. + * @exception ExceptionInInitializerError if the initialization provoked + * by this method fails. + * @see Field#get + */ + public int getInt(Object obj) + throws IllegalArgumentException, IllegalAccessException + { + return getFieldAccessor(obj).getInt(obj); + } + + /** + * Gets the value of a static or instance field of type + * {@code long} or of another primitive type convertible to + * type {@code long} via a widening conversion. + * + * @param obj the object to extract the {@code long} value + * from + * @return the value of the field converted to type {@code long} + * + * @exception IllegalAccessException if the underlying field + * is inaccessible. + * @exception IllegalArgumentException if the specified object is not + * an instance of the class or interface declaring the + * underlying field (or a subclass or implementor + * thereof), or if the field value cannot be + * converted to the type {@code long} by a + * widening conversion. + * @exception NullPointerException if the specified object is null + * and the field is an instance field. + * @exception ExceptionInInitializerError if the initialization provoked + * by this method fails. + * @see Field#get + */ + public long getLong(Object obj) + throws IllegalArgumentException, IllegalAccessException + { + return getFieldAccessor(obj).getLong(obj); + } + + /** + * Gets the value of a static or instance field of type + * {@code float} or of another primitive type convertible to + * type {@code float} via a widening conversion. + * + * @param obj the object to extract the {@code float} value + * from + * @return the value of the field converted to type {@code float} + * + * @exception IllegalAccessException if the underlying field + * is inaccessible. + * @exception IllegalArgumentException if the specified object is not + * an instance of the class or interface declaring the + * underlying field (or a subclass or implementor + * thereof), or if the field value cannot be + * converted to the type {@code float} by a + * widening conversion. + * @exception NullPointerException if the specified object is null + * and the field is an instance field. + * @exception ExceptionInInitializerError if the initialization provoked + * by this method fails. + * @see Field#get + */ + public float getFloat(Object obj) + throws IllegalArgumentException, IllegalAccessException + { + return getFieldAccessor(obj).getFloat(obj); + } + + /** + * Gets the value of a static or instance field of type + * {@code double} or of another primitive type convertible to + * type {@code double} via a widening conversion. + * + * @param obj the object to extract the {@code double} value + * from + * @return the value of the field converted to type {@code double} + * + * @exception IllegalAccessException if the underlying field + * is inaccessible. + * @exception IllegalArgumentException if the specified object is not + * an instance of the class or interface declaring the + * underlying field (or a subclass or implementor + * thereof), or if the field value cannot be + * converted to the type {@code double} by a + * widening conversion. + * @exception NullPointerException if the specified object is null + * and the field is an instance field. + * @exception ExceptionInInitializerError if the initialization provoked + * by this method fails. + * @see Field#get + */ + public double getDouble(Object obj) + throws IllegalArgumentException, IllegalAccessException + { + return getFieldAccessor(obj).getDouble(obj); + } + + /** + * Sets the field represented by this {@code Field} object on the + * specified object argument to the specified new value. The new + * value is automatically unwrapped if the underlying field has a + * primitive type. + * + * <p>The operation proceeds as follows: + * + * <p>If the underlying field is static, the {@code obj} argument is + * ignored; it may be null. + * + * <p>Otherwise the underlying field is an instance field. If the + * specified object argument is null, the method throws a + * {@code NullPointerException}. If the spec... [truncated message content] |