diff options
author | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2008-03-03 21:21:30 +0000 |
---|---|---|
committer | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2008-03-03 21:21:30 +0000 |
commit | 22e05c59dcbd7a1641d5c79104fde5a6d5a09c80 (patch) | |
tree | 2ecc4193a51337bf8ff46d713e8a165388955cf0 /vm | |
parent | 2062baf6faf3f19d1c92163d510487c1421c0f80 (diff) | |
download | classpath-22e05c59dcbd7a1641d5c79104fde5a6d5a09c80.tar.gz |
2008-03-03 Andrew John Hughes <gnu_andrew@member.fsf.org>
* java/lang/reflect/Constructor.java,
* java/lang/reflect/Field.java,
* java/lang/reflect/Method.java,
* vm/reference/java/lang/reflect/VMConstructor.java:
(equals(Object)): Added.
* vm/reference/java/lang/reflect/VMField.java:
(equals(Object)): Added.
* vm/reference/java/lang/reflect/VMMethod.java:
(equals(Object)): Added.
Move variables from Classpath classes to VM classes
and make class methods into instance methods.
Diffstat (limited to 'vm')
-rw-r--r-- | vm/reference/java/lang/reflect/VMConstructor.java | 56 | ||||
-rw-r--r-- | vm/reference/java/lang/reflect/VMField.java | 106 | ||||
-rw-r--r-- | vm/reference/java/lang/reflect/VMMethod.java | 84 |
3 files changed, 176 insertions, 70 deletions
diff --git a/vm/reference/java/lang/reflect/VMConstructor.java b/vm/reference/java/lang/reflect/VMConstructor.java index b2eb3ff85..c50291ccf 100644 --- a/vm/reference/java/lang/reflect/VMConstructor.java +++ b/vm/reference/java/lang/reflect/VMConstructor.java @@ -40,46 +40,57 @@ package java.lang.reflect; import java.lang.annotation.Annotation; +import java.util.Arrays; + final class VMConstructor { + Class clazz; + int slot; + + VMConstructor(Class clazz, int slot) + { + this.clazz = clazz; + this.slot = slot; + } + + public Class getDeclaringClass() + { + return clazz; + } + /** * Return the raw modifiers for this constructor. In particular * this will include the synthetic and varargs bits. - * @param c the constructor concerned. * @return the constructor's modifiers */ - static native int getModifiersInternal(Constructor c); + native int getModifiersInternal(); /** * Get the parameter list for this constructor, in declaration order. If the * constructor takes no parameters, returns a 0-length array (not null). * - * @param c the constructor concerned. * @return a list of the types of the constructor's parameters */ - static native Class[] getParameterTypes(Constructor c); + native Class[] getParameterTypes(); /** * Get the exception types this constructor says it throws, in no particular * order. If the constructor has no throws clause, returns a 0-length array * (not null). * - * @param c the constructor concerned. * @return a list of the types in the constructor's throws clause */ - static native Class[] getExceptionTypes(Constructor c); + native Class[] getExceptionTypes(); - static native Object constructNative(Object[] args, Class declaringClass, - int slot) + native Object construct(Object[] args) throws InstantiationException, IllegalAccessException, InvocationTargetException; /** * Return the String in the Signature attribute for this constructor. If there * is no Signature attribute, return null. - * @param c the constructor concerned. */ - static native String getSignature(Constructor c); + native String getSignature(); /** * <p> @@ -96,12 +107,33 @@ final class VMConstructor * no affect on the return value of future calls to this method. * </p> * - * @param c the constructor concerned. * @return an array of arrays which represents the annotations used on the * parameters of this constructor. The order of the array elements * matches the declaration order of the parameters. * @since 1.5 */ - static native Annotation[][] getParameterAnnotations(Constructor c); + native Annotation[][] getParameterAnnotations(); + + /** + * Compare two objects to see if they are semantically equivalent. + * Two Constructors are semantically equivalent if they have the same + * declaring class and the same parameter list. This ignores different + * exception clauses, but since you can't create a Method 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 Constructor)) + return false; + Constructor that = (Constructor)o; + if (clazz != that.getDeclaringClass()) + return false; + if (!Arrays.equals(getParameterTypes(), that.getParameterTypes())) + return false; + return true; + } } diff --git a/vm/reference/java/lang/reflect/VMField.java b/vm/reference/java/lang/reflect/VMField.java index 3b294a368..a4b4dbb58 100644 --- a/vm/reference/java/lang/reflect/VMField.java +++ b/vm/reference/java/lang/reflect/VMField.java @@ -40,20 +40,38 @@ package java.lang.reflect; final class VMField { + Class declaringClass; + String name; + int slot; + + VMField(Class declaringClass, String name, int slot) + { + this.declaringClass = declaringClass; + this.name = name; + this.slot = slot; + } + + public Class getDeclaringClass() + { + return declaringClass; + } + + public String getName() + { + return name; + } /** * Return the raw modifiers for this field. - * @param f the field concerned. * @return the field's modifiers */ - static native int getModifiersInternal(Field f); + native int getModifiersInternal(); /** * Gets the type of this field. - * @param f the field concerned. * @return the type of this field */ - static native Class getType(Field f); + native Class getType(); /** * Get the value of this Field. If it is primitive, it will be wrapped @@ -76,7 +94,6 @@ final class VMField * declaring class, even if the instance passed in belongs to a subclass * which declares another field to hide this one. * - * @param f the field concerned. * @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 @@ -96,14 +113,13 @@ final class VMField * @see #getFloat(Object) * @see #getDouble(Object) */ - static native Object get(Field f, Object o) + native Object get(Object o) throws IllegalAccessException; /** * Get the value of this boolean Field. If the field is static, * <code>o</code> will be ignored. * - * @param f the field concerned. * @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 @@ -117,14 +133,13 @@ final class VMField * class initialization, which then failed * @see #get(Object) */ - static native boolean getBoolean(Field f, Object o) + native boolean getBoolean(Object o) throws IllegalAccessException; /** * Get the value of this byte Field. If the field is static, * <code>o</code> will be ignored. * - * @param f the field concerned. * @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 @@ -138,14 +153,13 @@ final class VMField * class initialization, which then failed * @see #get(Object) */ - static native byte getByte(Field f, Object o) + native byte getByte(Object o) throws IllegalAccessException; /** * Get the value of this Field as a char. If the field is static, * <code>o</code> will be ignored. * - * @param f the field concerned. * @param o the object to get the value of this Field from * @throws IllegalAccessException if you could not normally access this field * (i.e. it is not public) @@ -158,14 +172,13 @@ final class VMField * class initialization, which then failed * @see #get(Object) */ - static native char getChar(Field f, Object o) + native char getChar(Object o) throws IllegalAccessException; /** * Get the value of this Field as a short. If the field is static, * <code>o</code> will be ignored. * - * @param f the field concerned. * @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 @@ -179,14 +192,13 @@ final class VMField * class initialization, which then failed * @see #get(Object) */ - static native short getShort(Field f, Object o) + native short getShort(Object o) throws IllegalAccessException; /** * Get the value of this Field as an int. If the field is static, * <code>o</code> will be ignored. * - * @param f the field concerned. * @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 @@ -200,14 +212,13 @@ final class VMField * class initialization, which then failed * @see #get(Object) */ - static native int getInt(Field f, Object o) + native int getInt(Object o) throws IllegalAccessException; /** * Get the value of this Field as a long. If the field is static, * <code>o</code> will be ignored. * - * @param f the field concerned. * @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 @@ -221,14 +232,13 @@ final class VMField * class initialization, which then failed * @see #get(Object) */ - static native long getLong(Field f, Object o) + native long getLong(Object o) throws IllegalAccessException; /** * Get the value of this Field as a float. If the field is static, * <code>o</code> will be ignored. * - * @param f the field concerned. * @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 @@ -242,14 +252,13 @@ final class VMField * class initialization, which then failed * @see #get(Object) */ - static native float getFloat(Field f, Object o) + native float getFloat(Object o) throws IllegalAccessException; /** * Get the value of this Field as a double. If the field is static, * <code>o</code> will be ignored. * - * @param f the field concerned. * @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 @@ -264,7 +273,7 @@ final class VMField * class initialization, which then failed * @see #get(Object) */ - static native double getDouble(Field f, Object o) + native double getDouble(Object o) throws IllegalAccessException; /** @@ -291,7 +300,6 @@ final class VMField * 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 f the field concerned. * @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 @@ -313,14 +321,13 @@ final class VMField * @see #setFloat(Object, float) * @see #setDouble(Object, double) */ - static native void set(Field f, Object o, Object value) + native void set(Object o, Object value) throws IllegalAccessException; /** * Set this boolean Field. If the field is static, <code>o</code> will be * ignored. * - * @param f the field concerned. * @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 @@ -334,14 +341,13 @@ final class VMField * class initialization, which then failed * @see #set(Object, Object) */ - static native void setBoolean(Field f, Object o, boolean value) + native void setBoolean(Object o, boolean value) throws IllegalAccessException; /** * Set this byte Field. If the field is static, <code>o</code> will be * ignored. * - * @param f the field concerned. * @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 @@ -355,14 +361,13 @@ final class VMField * class initialization, which then failed * @see #set(Object, Object) */ - static native void setByte(Field f, Object o, byte value) + native void setByte(Object o, byte value) throws IllegalAccessException; /** * Set this char Field. If the field is static, <code>o</code> will be * ignored. * - * @param f the field concerned. * @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 @@ -376,14 +381,13 @@ final class VMField * class initialization, which then failed * @see #set(Object, Object) */ - static native void setChar(Field f, Object o, char value) + native void setChar(Object o, char value) throws IllegalAccessException; /** * Set this short Field. If the field is static, <code>o</code> will be * ignored. * - * @param f the field concerned. * @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 @@ -397,14 +401,13 @@ final class VMField * class initialization, which then failed * @see #set(Object, Object) */ - static native void setShort(Field f, Object o, short value) + native void setShort(Object o, short value) throws IllegalAccessException; /** * Set this int Field. If the field is static, <code>o</code> will be * ignored. * - * @param f the field concerned. * @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 @@ -418,14 +421,13 @@ final class VMField * class initialization, which then failed * @see #set(Object, Object) */ - static native void setInt(Field f, Object o, int value) + native void setInt(Object o, int value) throws IllegalAccessException; /** * Set this long Field. If the field is static, <code>o</code> will be * ignored. * - * @param f the field concerned. * @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 @@ -439,14 +441,13 @@ final class VMField * class initialization, which then failed * @see #set(Object, Object) */ - static native void setLong(Field f, Object o, long value) + native void setLong(Object o, long value) throws IllegalAccessException; /** * Set this float Field. If the field is static, <code>o</code> will be * ignored. * - * @param f the field concerned. * @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 @@ -460,14 +461,13 @@ final class VMField * class initialization, which then failed * @see #set(Object, Object) */ - static native void setFloat(Field f, Object o, float value) + native void setFloat(Object o, float value) throws IllegalAccessException; /** * Set this double Field. If the field is static, <code>o</code> will be * ignored. * - * @param f the field concerned. * @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 @@ -481,15 +481,37 @@ final class VMField * class initialization, which then failed * @see #set(Object, Object) */ - static native void setDouble(Field f, Object o, double value) + native void setDouble(Object o, double value) throws IllegalAccessException; /** * Return the String in the Signature attribute for this field. If there * is no Signature attribute, return null. * - * @param f the field concerned. */ - static native String getSignature(Field f); + native String getSignature(); + + /** + * 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 create 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 (declaringClass != that.getDeclaringClass()) + return false; + if (!name.equals(that.getName())) + return false; + if (getType() != that.getType()) + return false; + return true; + } } diff --git a/vm/reference/java/lang/reflect/VMMethod.java b/vm/reference/java/lang/reflect/VMMethod.java index e393780e4..bc8afc11d 100644 --- a/vm/reference/java/lang/reflect/VMMethod.java +++ b/vm/reference/java/lang/reflect/VMMethod.java @@ -40,51 +40,61 @@ package java.lang.reflect; import java.lang.annotation.Annotation; +import java.util.Arrays; + final class VMMethod { + Class declaringClass; + String name; + int slot; + + public Class getDeclaringClass() + { + return declaringClass; + } + + public String getName() + { + return name; + } + /** * Return the raw modifiers for this method. - * @param m the method concerned. * @return the method's modifiers */ - static native int getModifiersInternal(Method m); + native int getModifiersInternal(); /** * Gets the return type of this method. - * @param m the method concerned. * @return the type of this method */ - static native Class getReturnType(Method m); + native Class getReturnType(); /** * Get the parameter list for this method, in declaration order. If the * method takes no parameters, returns a 0-length array (not null). * - * @param m the method concerned. * @return a list of the types of the method's parameters */ - static native Class[] getParameterTypes(Method m); + native Class[] getParameterTypes(); /** * Get the exception types this method says it throws, in no particular * order. If the method has no throws clause, returns a 0-length array * (not null). * - * @param m the method concerned. * @return a list of the types in the method's throws clause */ - static native Class[] getExceptionTypes(Method m); + native Class[] getExceptionTypes(); - static native Object invokeNative(Object o, Object[] args, - Class declaringClass, int slot) + native Object invoke(Object o, Object[] args) throws IllegalAccessException, InvocationTargetException; /** * Return the String in the Signature attribute for this method. If there * is no Signature attribute, return null. - * @param m the method concerned. */ - static native String getSignature(Method m); + native String getSignature(); /** * If this method is an annotation method, returns the default @@ -92,13 +102,12 @@ final class VMMethod * method is not a member of an annotation type, returns null. * Primitive types are wrapped. * - * @param m the method concerned. * @throws TypeNotPresentException if the method returns a Class, * and the class cannot be found * * @since 1.5 */ - static native Object getDefaultValue(Method m); + native Object getDefaultValue(); /** * <p> @@ -115,13 +124,56 @@ final class VMMethod * no affect on the return value of future calls to this method. * </p> * - * @param m the method concerned. * @return an array of arrays which represents the annotations used on the * parameters of this method. The order of the array elements * matches the declaration order of the parameters. * @since 1.5 */ - static native Annotation[][] getParameterAnnotations(Method m); + native Annotation[][] getParameterAnnotations(); + + /** + * Compare two objects to see if they are semantically equivalent. + * Two Methods are semantically equivalent if they have the same declaring + * class, name, parameter list, and return type. + * + * @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) + { + // Implementation note: + // The following is a correct but possibly slow implementation. + // + // This class has a private field 'slot' that could be used by + // the VM implementation to "link" a particular method to a Class. + // In that case equals could be simply implemented as: + // + // if (o instanceof Method) + // { + // Method m = (Method)o; + // return m.declaringClass == this.declaringClass + // && m.slot == this.slot; + // } + // return false; + // + // If a VM uses the Method class as their native/internal representation + // then just using the following would be optimal: + // + // return this == o; + // + if (!(o instanceof Method)) + return false; + Method that = (Method)o; + if (declaringClass != that.getDeclaringClass()) + return false; + if (!name.equals(that.getName())) + return false; + if (getReturnType() != that.getReturnType()) + return false; + if (!Arrays.equals(getParameterTypes(), that.getParameterTypes())) + return false; + return true; + } } |