Skip to main content

Posts

Showing posts with the label Reflection API

Java : How to check if the field of a class is STATIC by Reflection

Points To Remember You can use Reflection  to check if the field of a class is static or not. You need to get the modifiers of the field to check if the field is static. How to check if the field of a class is static by Reflection The following is the only way to check if the field is static or not field.getModifiers()& Modifier.STATIC) == Modifier.STATIC Following program shows how to get the static fields from a class. package com.ekiras.demo; import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class Test { public static void main(String args[]){ Field[] fields = Person.class.getDeclaredFields(); for(Field field : fields){ if((field.getModifiers()& Modifier.STATIC) == Modifier.STATIC ){ System.out.println("final field :: " + field.getName()); } } } } class Person{ public static final int someConstant = 2; private String name; private String email; public String getName() { return name; } public void setName(String...

Java : How to check if the field of a class is FINAL by Reflection

Points To Remember You can use Reflection  to check if the field of a class is final or not. You need to get the modifiers of the field to check if the field is final How to check if the field of a class is final by Reflection The following is the only way to check if the field is final or not field.getModifiers()& Modifier.FINAL) == Modifier.FINAL Following program shows how to get the final fields from a class. package com.ekiras.demo; import java.lang.reflect.Field; import java.lang.reflect.Modifier; public class Test { public static void main(String args[]){ Field[] fields = Person.class.getDeclaredFields(); for(Field field : fields){ if((field.getModifiers()& Modifier.FINAL) == Modifier.FINAL ){ System.out.println("final field :: " + field.getName()); } } } } class Person{ public static final int someConstant = 2; private String name; private String email; public String getName() { return name; } public void setName(String name) { th...

How to create a Custom Annotation in Java

Points To Remember An annotation can be used as a replacement of interfaces. You can make both empty/marker annotations and annotations with methods. You can define the retention policy for the annotation and where the annotation can be used. Program : Create A Custom Annotation The following program creates a custom empty annotation. import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.annotation.ElementType; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Table { public String name() default "table"; } The above class can be used as a custom annotation with only one method name with a default value. This annotation can be applied only to the class i.e it is class level annotation and can not be applied to any member of a class. The following program creates a custom annotation with methods defined. import java.lang.annotation.ElementType; import java.lang.ann...

How to find all the packages in a Project with a Prefix

Points To Remember You to need to add the Reflections jar in the class path.  You will not be able to load the packages other than the current package using Package.getPackages()  since current class loader will not be able to reach them. Program : Scan all packages with a Prefix You can use  Package.getPackages()  approach to find all the packages that are in the current class loader. But you will not be able to get the packages that are not in the class path of the current class loader. So you need to include the jar "Reflections"  to do this for you. All the dependency to the project via pom.xml  or you can add the jar in the class path of the project. The following code will list all the packages in the project that start with the prefix in the project. public static Set<String> findAllPackagesStartingWith(String prefix) { List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>(); classLoadersList.add(Classpat...

How to get all Declared Fields in a Class

Points To Remember We can use reflection to find all the fields available within a class. We can get all the declared fields in a class by getDeclaredFields() . We can search for any field in a class by using getField().  It throws  NoSuchFieldException  if the field does not exist in the class. Program : Get all the declared fields in a class import java.lang.reflect.Field; class SampleClass{ String s1 = "Class variable"; int a = 123; } class Test{ public static void main(String args[]){ Test obj = new Test(); try{ Class clazz = Class.forName("SampleClass"); Field feilds[] = obj.getDeclaredFields(clazz); for(Field feild : feilds){ System.out.println(feild); } }catch(Exception e){ e.printStackTrace(); } } public Field[] getDeclaredFields(Class clazz){ return clazz.getDeclaredFields(); } } java.lang.String SampleClass.s1 int SampleClass.a Program : Get a particular field declared in a class. import java.la...

How to get all Methods available in a Class

Points To Remember We can use Reflection API to get all methods available in a class. Method class is present in java.land.reflect.Method class. We can use getDeclaredMethods() to get all the declared Methods in a class. We can use getMethods()  to get all the methods available in a class. It also includes methods from its super classes. Program : Get All Methods Declared in a Class import java.lang.reflect.Method; class SampleClass{ String s1 = "Class variable"; int a = 123; public SampleClass(){ System.out.println("SampleClass Default Constructor"); } public SampleClass(String str){ System.out.println("SampleClass Overloaded Constructor"); } public void show(){ System.out.println("SampleClass Show Method"); } public void print(){ System.out.println("SampleClass Print Method"); } } class Test{ public static void main(String args[]){ Test obj = new Test(); try{ Class clazz = Class.forName("SampleClass...

How to get all Constructors declared in a Class

Points To Remember We can get the constructors declared in a class using Reflection API. Constructor class is present in java.lang.reflect.Constructor  class. The  getDeclaredConstructors()  method will return an array of Constructors in a class. Program : Get all constructors declared in a class import java.lang.reflect.Constructor; class SampleClass{ String s1 = "Class variable"; int a = 123; public SampleClass(){ System.out.println("SampleClass Default Constructor"); } public SampleClass(String str){ System.out.println("SampleClass Overloaded Constructor"); } public void show(){ System.out.println("SampleClass Show Method"); } public void print(){ System.out.println("SampleClass Print Method"); } } class Test{ public static void main(String args[]){ Test obj = new Test(); try{ Class clazz = Class.forName("SampleClass"); Constructor cc[] = obj.getConstructors(clazz); for(Constructor ...

How to make Object of a class By Reflection API

Points To Remember Reflection API is a powerful technique to find out environment of a class as well as to inspect the class itself. Reflection API was introduced in Java v1.1 It allows user to get full information about the classes, interfaces, constructors, methods, variables etc. It is available in java.lang.reflect package. Example : Creating Object Using Reflection API In the example below we are trying to create the object of class "A" in class "Test" using reflection api. We will first get the class of "A" by using .class  and then call the newInstance() method to get the object of this class. class A{ public A(){ System.out.println("class A constructor"); } public void show(){ System.out.println("class A show() "); } public void show(String s){ System.out.println(s); } } public class Test{ public static void main(String args[]){ try{ Class clazz = A.class; A object = (A)clazz.ne...