How to get all Method information under a Class in Java Reflection?

This is continuing article of my previous post. In this article we will see how can we retrieve Class Related Information using Java Reflection .We will see Method Names .

Spatial Note : I will make a separate reflector utility class where we input a target class in its constructor and we will retrieve information using separate method. In this way ,we can isolate our needs. Please see this before start.

How to get all declared Method Names inside of a class?
This means , we will get the method names which are declared inside of the class(public , private, default, protected). Not inherited methods. 

   1: public String[] getAllOwnMethodNames(){
   2:         ArrayList<String> allMethods = new ArrayList<String>();
   3:         for(Method aMethod : myClass.getDeclaredMethods()){          
   4:         allMethods.add("Method Name : "+aMethod.getName()+" , Full Name : "+aMethod.toString());
   5:         }
   6:         return allMethods.toArray(new String[allMethods.size()]);
   7:     }
How to get all Method Names accessible from of a class 
(which includes inherited, implemented methods of its own, super class, interfaces)? 
Thant means , we will get method names which are included in the class and the method which are taken from its parent class.
(include its parents and interfaces)
   1: public String[] getAllPubliAndInheritedMethodNames(){
   2:         ArrayList<String> allMethods = new ArrayList<String>();
   3:         for(Method aMethod : myClass.getMethods()){            
   4:         allMethods.add("Method Name : "+aMethod.getName()+" , Full Name : "+aMethod.toString());
   5:         }
   6:         return allMethods.toArray(new String[allMethods.size()]);
   7:     }
 
Note: To know information in detail i use getName() and toString() method especially. 
 
For both case, we can specify method name to get that specific method.
myClass.getDeclaredMethod(<Name of the method as string>, parameter of that method)
myClass.getMethod(<Name of the method as string>, parameter of that method)
 
In the both case we need to know the name of the method. 
In a class, often we need to know either the method is a Getter or a setter method. 
We can apply small string filter. While iterating if we add like following 
 
To know if it is a Getter method :
aMethod.getName().startsWith("set"); 

To know if it is a Setter method :
aMethod.getName().startsWith("get"); 

This is only class related method post, for method detail I will provide separate post.
Thanks..:)



How to get all Field info in a Class in Java Reflection?

This is continuing article of my previous post. In this article we will see how can we retrieve Class Related Information using Java Reflection .We will see Filed Names .
Spatial Note : I will make a separate reflector utility class where we input a target class in its constructor and we will retrieve information using separate method. In this way ,we can isolate our needs. Please see this before start.

How to get all declared Fields inside of a class? This means , we will get the fields names which are declared inside of the class(public , private, default, protected). This is similar to what we did while method name extraction.
   1:  public String[] getAllOwnFieldNames(){
   2:          ArrayList<String> allFields = new ArrayList<String>();
   3:          for(Field aField : myClass.getDeclaredFields()){
   4:          allFields.add("Field Name : "+aField.getName()+" , Full Name : "+aField.toString());
   5:          }
   6:          return allFields.toArray(new String[allFields.size()]);
   7:      }
How to get all Fields accessible from of a class (which includes inherited fields of its own, super class and full hierarchy)?
That means , we will get fields which are included in the class and the method which are taken from its parent class.(following full hierarchy)
   1:  public String[] getAllAccessableFields(){
   2:          ArrayList<String> allFields = new ArrayList<String>();
   3:          for(Field aField : myClass.getFields()){
   4:              allFields.add("Field Name : "+aField.getName()+" , Full Name : "+aField.toString());
   5:          }
   6:          return allFields.toArray(new String[allFields.size()]);
   7:      }

Note : in both case, I used getName(), and toString() get full information. And , for both scenarios we can get access to the fields by specific names.
myClass.getDeclaredField("StingName");
myClass.getField("StringName");
And, we need to know the field name .

This is class related post where we can access fields. I will provide filed specific post separately.

Thanks..:)

How to get all Constructor information under a Class in Java Reflection?

This is continuing article of my previous post. In this article we will see how can we retrieve Class Related Information using Java Reflection .We will see Constructor Names .

Spatial Note : I will make a separate reflector utility class where we input a target class in its constructor and we will retrieve information using separate method. In this way ,we can isolate our needs. Please see this before start.

How to get all Constructors of a class?
This is very simple, unlike the method, a class has only its own constructors so, we can have direct access.
   1: public String[] getAllConstructorNames(){
   2:         ArrayList<String> allConstructors = new ArrayList<String>();
   3:         for(Constructor aConstructor: myClass.getDeclaredConstructors()){
   4:         allConstructors.add("Constructor Name : " 
+aConstructor.getName()+" , Full Name : "+aConstructor.toString());
   5:         }
   6:         return allConstructors.toArray(new String[allConstructors.size()]);
   7:     }
How to get only Public Constructors of a class?
In this way we can see only public constructors.
   1: public String[] getAllPublicConstructorNames(){
   2:         ArrayList<String> allConstructors = new ArrayList<String>();
   3:         for(Constructor aConstructor: myClass.getConstructors()){
   4:         allConstructors.add("Constructor Name : "
 +aConstructor.getName()+" , Full Name : "+aConstructor.toString());
   5:         }
   6:         return allConstructors.toArray(new String[allConstructors.size()]);    
   7:     }
Like as filed and method cases, we can get access using parameter names. like as follows.
myClass.getConstructor(<Parameters>);
myClass.getDeclaredConstructor(<Paraeters>);
I will provide separate post for calling and manipulating those constructors.

Thanks..:)