The Class class
Obtain the java.lang.Class instance corresponding to a class using the following techniques:
- Class.forName(String),
- MyClass.class,
- classInstance.getClass(),
- primitive.class, and,
- PrimitiveWrapperClass.TYPE
Display ability to correctly use the following methods to identify the class and identify its relationship with other Class instances:
- getPackage(),
- getInterfaces(),
- getSuperclass(),
- isPrimitive(),
- isArray(),
- isAssignableFrom(), and
- isInstance()
Member Access and Method Invocation
Show how to use the Class methods for acquiring references to fields and methods:
- getField(),
- getDeclaredField(),
- getFields(),
- getMethod(),
- getDeclaredMethod(), and,
- getMethods()
Display correct usage of following Method methods, including exception handling:
- getParameterTypes(),
- getReturnType(),
- getExceptionTypes(),
- invoke(), and,
- getModifiers()
Demonstrate correct usage of following Field methods, including exception handling:
- set(), setBoolean(), setByte(), etc.
- get(), getBoolean(), getByte(), etc.
- getType()
- getModifiers()
Identify the role played by the SecurityManager when accessing non-public fields and methods.
Write code using the Modifier.is*() methods, to check accessibility prior to potentially causing an IllegalAccessException to be thrown.
Finally, display safe usage of setAccessible() to circumvent access checks.
Object Creation
Show how to use the Class reflection methods for accessing constructors:
- getConstructor(),
- getConstructors(),
- getDeclaredConstructor(), and,
- getDeclaredConstructors()
Demonstrate correct usage of Class.newInstance(), including exception handling, show understanding that this method implicitly calls the default constructor of the target Class instance.
Display correct usage of the following Constructor methods, including exception handling:
- getParameterTypes(),
- getExceptionTypes(),
- newInstance(), and,
- getModifiers()
Write code using the Modifier.is*() methods, to check accessibility prior to potentially causing an IllegalAccessException to be thrown, additionally display use of the setAccessible() method to circumvent access checking.
Finally, display full and proper usage of the java.reflect.Array class to create (and initialise the contents of) an array of a given Class instance, using the Array.newInstance() method.
Multi dimensional arrays are not covered.
Dynamic Proxy
This category specifically covers the java.lang.reflect.Proxy class and java.lang.reflect.InvocationHandler interface.
Display the knowledge required to:
- Create a Class instance of Proxy implementing one or more interfaces using getProxyClass(),
- Create an instance of Proxy implementing one or more interfaces using newProxyInstance()
- Identify a proxy using the Proxy.isProxyClass() method.
Demonstrate that:
- An instance of Proxy cannot extend a specified class.
- A Proxy instance always extends Proxy.
- An instance of Proxy can be used to implement one or more interfaces.
- A non-public interface will cause the proxy to be created in the same package as the non-public interface.
- Where a method is defined in more then one interface, understand that the first interface defining the method is assumed to be the correct method. Know that this means exceptions thrown will relate to the first definition of the method, not later definitions.
- Calls to a Proxy Class instance's methods getInterfaces(), getMethods() and getMethod() will act as if the Proxy were an instance of a class that implemented the same interfaces and methods as it was defined with.
Explain correct usage of the InvocationHandler interface:
- Expect primitive conversion into the invoke method
- Expect back conversion of return values that are primitives
- Avoid ClassCastExceptions and NullPointerExceptions, by returning the correct class and correct primitive wrapper from invoke().
- Throw the correct exceptions, avoiding UndeclaredThrowableExceptions being thrown when a method is implemented by two interfaces.
Provide understanding of a typical Proxy behaviour in either of the two following conditions:
- As a mock object, or,
- As a performance/profiling monitor