Reflection API(Reflection API Example in Java):
Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.
Sample Program which Demonstrate How can we try Reflection API:
package ReflectionAPI;
/**
*
* @author Manjeet Kumar
*/
public class ReflectionAPI {
public static void main(String[] args) throws CloneNotSupportedException
{
System.out.println("we are testing reflection API");
ReflectionAPI rap=new ReflectionAPI();
System.out.println("rap.getClass(): "+rap.getClass());
byte[] bytes = new byte[1024];
System.out.println("rap.getClass(): "+bytes.getClass());
String str="test str";
System.out.println("str.getClass(): "+ str.getClass());
System.out.println("getCanonicalName of class: "+RefAPI.class.getCanonicalName());
System.out.println("getName of class: "+RefAPI.class.getName());
System.out.println("getSimpleName of class: "+RefAPI.class.getSimpleName());
System.out.println("toString gives the name of class: "+RefAPI.class.toString());
System.out.println("desiredAssertionStatus of class: "+RefAPI.class.desiredAssertionStatus());
System.out.println("getAnnotations of class: "+RefAPI.class.getAnnotations());
System.out.println("getClassLoader of class: "+RefAPI.class.getClassLoader());
System.out.println("getClasses of class: "+RefAPI.class.getClasses());
System.out.println("getClasses of class: "+ReflectionAPI.class.getClasses());
System.out.println("getComponentType of class: "+ReflectionAPI.class.getComponentType());
System.out.println("getConstructors of class: "+ReflectionAPI.class.getConstructors());
System.out.println("getDeclaredAnnotations of class: "+ReflectionAPI.class.getDeclaredAnnotations());
System.out.println("getDeclaredFields of class: "+ReflectionAPI.class.getDeclaredFields());
System.out.println("getDeclaredMethods of class: "+ReflectionAPI.class.getDeclaredMethods());
System.out.println("getModifiers of class: "+ReflectionAPI.class.getModifiers());
System.out.println("getPackage of class: "+ReflectionAPI.class.getPackage());
System.out.println("getProtectionDomain of class: "+ReflectionAPI.class.getProtectionDomain());
System.out.println("getEnclosingClass of class: "+ReflectionAPI.class.getEnclosingClass());
System.out.println("getEnclosingClass of class: "+RefAPI.class.getEnclosingClass());
System.out.println("getSigners of class: "+ReflectionAPI.class.getSigners());
System.out.println("getSuperClass of class: "+RefAPI.class.getSuperclass());
System.out.println("getTypeParametrs of class: "+ReflectionAPI.class.getTypeParameters());
System.out.println("checks if class isAnnotation: "+ReflectionAPI.class.isAnnotation());
System.out.println("checks if class isAnonymousClass: "+ReflectionAPI.class.isAnonymousClass());
System.out.println("checks if class isArray: "+ReflectionAPI.class.isArray());
System.out.println("checks if class isEnum: "+ReflectionAPI.class.isEnum());
System.out.println("checks if class isInterface: "+RefInterface.class.isInterface());
System.out.println("checks if class isRefAPIEnum: "+RefAPIEnum.class.isEnum());
System.out.println("checks if class isLocalClass: "+RefAPI.class.isLocalClass());
System.out.println("checks if class isMemberClass: "+RefAPI.class.isMemberClass());
System.out.println("checks if class isPrimitive: "+RefAPI.class.isPrimitive());
System.out.println("checks if class isSynthetic: "+RefAPI.class.isSynthetic());
}
class RefAPI
{
public int method1()
{
return 1+1;
}
}
interface RefInterface
{
public int num=0;
public String str="Reflection API";
public boolean bool=false;
}
enum RefAPIEnum<String,String>
No comments:
Post a Comment