Java SE Reflection API

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值