THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

This capability is provided for highly specialized contexts and is not intended for general usewe mention it
only for completeness. Changing a final field can have unexpected, possibly tragic consequences unless
performed in specific contexts, such as custom deserialization. Outside those contexts, changes to a final field
are not guaranteed to be visible. Even in such contexts, code using this technique must be guaranteed that
security will not thwart its work. Changing a final field that is a constant variable (see page 46) will never
result in the change being seen, except through the use of reflectiondon't do it!


Exercise 16.6: Create an Interpret program that creates an object of a requested type and allows the user
to examine and modify fields of that object.


16.7. The Method Class


The Method class, together with its inherited Member methods allows you to obtain complete information
about the declaration of a method:


public TypegetGenericReturnType()

Returns the Type object for the type returned by this method. If the method
is declared void the returned object is void.class.

public Type[]getGenericParameterTypes()

Returns an array of Type objects with the type of each parameter of this
method, in the order in which the parameters are declared. If the method has
no parameters an empty array is returned.

public Type[]getGenericExceptionTypes()

Returns an array of Type objects for each of the exception types listed in the
throws clause for this method, in the order they are declared. If there are no
declared exceptions an empty array is returned.

There are also the legacy methods geTReturnType, getParameterTypes, and
getExceptionTypes that return Class objects instead of Type objects. As with Field.getType,
parameterized types and type variables are represented by the Class objects of their erasures.


Method implements AnnotatedElement, and the annotations on a method can be queried as discussed in
Section 16.2 on page 414. Additionally, Method provides the getParameterAnnotations method to
provide access to the annotations applied to the method's parameters. The getParameterAnnotations
method return an array of Annotation arrays, with each element of the outermost array corresponding to
the parameters of the method, in the order they are declared. If a parameter has no annotations then a
zero-length Annotation array is provided for that parameter. If the Method object represents a method
that is itself an element of an annotation, the geTDefaultValue method returns an Object representing
the default value of that element. If it is not an annotation element or if there is no default value, then null is
returned.


The Method class also implements GenericDeclaration and so defines the method
getTypeParameters which returns an array of TypeVariable objects. If a given Method object does
not present a generic method, then an empty array is returned.


You can ask a Method object if it is a varargs (variable-argument) method using the isVarArgs method.
The method isBridge asks if it is a bridge method (see Section A.3.1 on page 745).

Free download pdf