THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

16.4. The Member classes


The classes Field, Constructor, and Method all implement the interface Member, which has four
methods for properties that all members share:


ClassgeTDeclaringClass()

Returns the Class object for the class in which this member is declared.

StringgetName()

Returns the name of this member.

int getModifiers()

Returns the language modifiers for the member encoded as an integer. This
value should be decoded using the Modifier class.

booleanisSynthetic()

Returns true if this member is a synthetic member that was created by the
compiler. For example, synthetic fields are often created in an inner class to
hold a reference to the enclosing instance, and synthetic "bridge" methods are
generated to support generic typessee Section A.3.1 on page 745.

Although a class or interface can be a member of another class, for historical reasons the class Class does
not implement Member, although it supports methods of the same name and contract.


The toString method of all Member classes includes the complete declaration for the member, similar to
how it would appear in your source code, including modifiers, type, and parameter types (where applicable).
For historical reasons, toString does not include generic type information. The toGenericString
method provides a more complete representation of the declaration of a member, including type parameters,
and all use of parameterized types and type variables. For methods and constructors, the tHRows list is also
included in the string.


16.5. Access Checking and AccessibleObject


The classes Field, Constructor, and Method are also all subclasses of the class
AccessibleObject, which lets you enable or disable the checking of the language-level access modifiers,
such as public and private. Normally, attempts to use reflection to access a member are subject to the
same access checks that would be required for regular, explicit codefor example, if you cannot access a field
directly in code, you cannot access it indirectly via reflection. You can disable this check by invoking
setAccessible on the object with a value of TRuethe object is now accessible, regardless of
language-level access control. This would be required, for example, if you were writing a debugger. The
methods are:


public voidsetAccessible(boolean flag)

Sets the accessible flag for this object to the indicated boolean value. A value
of true means that the object should suppress language-level access control
(and so will always be accessible); false means the object should enforce
language-level access control. If you are not allowed to change the
accessibility of an object a SecurityException is thrown.
Free download pdf