Retrieve Modifier Information using Java Reflection APIs

Java Reflection has API called getModifiers() which is used to get Modifier information of class. just create object of class and call getModifiers() method. Play with Modifier and it's methods.

Java Program to get Modifier Information using Java Reflection API:

System.out.println("Modifier Information - ");
List list = new ArrayList();
Class originalClassObj = list.getClass();
int modifier = originalClassObj.getModifiers();
if (Modifier.isPublic(modifier)) {
 System.out.println("public");
}
if (Modifier.isAbstract(modifier)) {
 System.out.println("abstract");
}
if (Modifier.isFinal(modifier)) {
 System.out.println("final");
}
Output :

Modifier Information - 
public


Retrieve Constructor Information of class using Java Reflection APIs

In order to retrieve Constructor information, just create object of class and call
getConstructors() api.

Java Program to retrieve Constructor Information using Java Reflection API :

Constructor[] constructor = originalClassObj.getConstructors();
System.out.println("Constructor used - ");
for (int i = 0; i < constructor.length; i++) {
    System.out.println(constructor[i].getName());
}


Retrieve Fields Information of class using java Reflection APIs

Using Java Reflecion APIs you can get fields information of class easily. just create object of class and get fields using getFields();

Java Program to retrieve fields information using java Reflection Apis :
Field[] fields = originalClassObj.getFields();
System.out.println("Fields used - ");
for (int i = 0; i < fields.length; i++) {
    System.out.println(fields[i].getName());
}


Retrieve SuperClass information using Java Reflection API

Using Java Reflection APIs you can retrieve superClass name, it's simple name,packagename, implemented interfaces and many more.

Key points to retrieve SuperClass Information :
  1. Create Object of class which has super class
  2. Get SuperClass using getSuperClass()
  3. Get SuperClass name using getName()
  4. Get SuperClass simple name using getSimpleName().
  5. Get SuperClass PackageName using getPackage()
  6. Get interfaces using getInterfaces()

Retrieve Methods Information of Class using Java Reflection API

    Using Java Reflection API, It's easy to get all methods information of class.

    Key points to retrieve Methods information of class :
    1. Create Object of Class
    2. Retrieve all methods of class in Method[] array using getMethods()
    3. Get Method name using getName()
    4. Get Return Type of method using getReturnType()
    5. Get all parameter of method using getParameterTypes()

    How to check Number is Palindrome number or not using Java

    Number is called Palindrome number if original number and reverse of original number are same.
    ex. 46364. Just reverse the number and compare it with original number. that's it

    Java Program to check number is palindrome or not :

    package com.anuj.algorithms;
    
    /**
     *
     * @author Anuj Patel
     */
    public class Palindrom {
    
        protected void checkPalindrom(int num) {
            int reverse = 0;
            int original = num;
    
            for (int i = 0; i <= num; i++) {
                int rand = num % 10;
                num = num / 10;
                reverse = reverse * 10 + rand;
            }
            System.out.println("Original Number is - " + original);
            System.out.println("Reverse Number is - " + reverse);
    
            if (reverse == original) {
                System.out.println("number is Palindrom\n");
            } else {
                System.out.println("number is not palindrom\n");
            }
        }
    
        public static void main(String[] args) {
            Palindrom palindrom = new Palindrom();
            //palindrom.checkPalindrom(46361);
            palindrom.checkPalindrom(46364);
        }
    }
    

    Output :
    Original Number is - 46361
    Reverse Number is - 16364
    number is not palindrom

    Original Number is - 46364
    Reverse Number is - 46364
    number is Palindrom

    How to Check Number is ArmStrong or not using Java

    Number is called as ArmStrong number if sum of each digits's cube is equal to original number.

    Ex. 153 = (1*1*1) + (5*5*5) + (3*3*3)

    Please follow below Steps to check if number is armstring number or not using java : 

    1. Take Reminder of number
    2.  Cube the reminder and add it to Sum
    3.  Retrieve pending number to be evaluate and set it to Number
    4.  Perform above checks until number is greater than 0.

    Retrieve Implemented Interfaces using Java Reflection API

    Consider that i have some java class and i want to get all interfaces which my class implements and want to list all those interfaces.How can i retrieve information about all implemented interfaces information using Java Reflection API, have a look at below :

    I have create object such as List list = new ArrayList(); and we will retrieve all implemented information using java relection API.

    We already know ArrayList Heirarchy as :
    public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

    Java Program to retrieve Implemented Interfaces :

    List list = new ArrayList();
    Class originalClass = list.getClass();        
    
    //retrieve implemeted interfaces
    Class[] ointerfaces = originalClass.getInterfaces();
    System.out.println("\nImplemented Interfaces - ");
    for (int i = 0; i < ointerfaces.length; i++) {
     if (ointerfaces[i].isInterface()) {
      System.out.println(ointerfaces[i].getName() + " is interface");
     } else {
      System.out.println(ointerfaces[i].getName() + " is Class");
     }
    }
    Output :
    Implemented Interfaces - 
    java.util.List is interface
    java.util.RandomAccess is interface
    java.lang.Cloneable is interface
    java.io.Serializable is interface 
    


    Retrieve Class Information using Java Reflection API

    Reflection API should be used by developers who has strong understanding of language fundamentals.

    Using Reflection APIs , you can

    1. Retrieve Class Information
    2. Retrieve Implemented Interfaces
    3. Retrieve Methods information used in Provided Class
    4. Retrieve Constructors information used in provided class
    5. Retrieve Fields information
    6. Retrieve SuperClass information of provided class
    7. Retrieve Modifier Information

    Here, i am just showing small java code for how you can retrieve class information. Please refer to this blogs for more examples of Java Reflections API explanation and java codes.

    Java Program :
    List list = new ArrayList();
    
    Class originalClass = list.getClass();        
    System.out.println("Original Class Information");
    System.out.println("ClassName - " + originalClass.getName());
    System.out.println("SimpleName -  " + originalClass.getSimpleName());
    System.out.println("PackageName - " + originalClass.getPackage());
    
    Output :
    Original Class Information
    ClassName - java.util.ArrayList
    SimpleName -  ArrayList
    PackageName - package java.util, Java Platform API Specification, version 1.7
    

    Sending Email using Spring MailSender and Gmail SMTP Server

    Spring provides interface called "MailSender" (org.springframework.mail.MailSender) to send mail.
    Developer can use MailSender method send(SimpleMailMessage simpleMessage)and pass SimpleMailMessage with From,To,Subject, and Text set.

    MailSender Documentation : MailSender
    SimpleMailMessage Documentation : SimpleMailMessage


    Java Program to send Mail using Spring MailSender :
    package com.anuj.spring.mail;
    
    import org.springframework.mail.MailSender;
    import org.springframework.mail.SimpleMailMessage;
    
    /**
     *
     * @author Anuj Patel
     */
    public class NotifyImpl {
        
        private MailSender mailsender;
        
        public MailSender getMailsender() {
            return mailsender;
        }
        
        public void setMailsender(MailSender mailsender) {
            this.mailsender = mailsender;
        }
        
        public void sendMail(String from, String to, String subject, String message) {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(from);
            simpleMailMessage.setTo(to);
            simpleMailMessage.setSubject(subject);
            simpleMailMessage.setText(message);
            
            mailsender.send(simpleMailMessage);
        }
    }