Java Reflection: Analyzing Classes, Fields, Constructors, and Methods, Lecture notes of Java Programming

This java code demonstrates the use of reflection api to inspect various elements of a class such as package, class name, parent class, interfaces, fields, constructors, and methods. It also shows how to invoke private methods with given parameters.

Typology: Lecture notes

2011/2012

Uploaded on 08/09/2012

dhanyaa
dhanyaa 🇮🇳

4.7

(3)

60 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ameed.khalid
*/
public class ReflectionMainClass {
public static void main(String[] args)
{
try {
Class c = Class.forName("lab01.Student"); //creating a
reflective object of student class
//retrieving name of package, class, parent and interfaces
System.out.println("Package: " + c.getPackage().getName());
System.out.println("Class: " + c.getName());
System.out.println("Parent Class: " +
c.getSuperclass().getName());
System.out.println("Interfaces: ");
printInterfaces(c);
//retrieving fields, their types and modifiers
System.out.println("Fields: ");
printFields(c);
//retrieving Constructors, their types and modifiers
System.out.println("Constructors: ");
printConstructors(c);
//printing method info
System.out.println("Methods: ");
printMethods(c);
try {
//invoking private method
executePrivateMethods(c);
} catch (Exception ex) {
System.out.println("Error Invoking Private Methods: " +
ex);
}
} catch (ClassNotFoundException ex) {
System.out.println("Class Not Found: " + ex);
}
}
docsity.com
pf3
pf4

Partial preview of the text

Download Java Reflection: Analyzing Classes, Fields, Constructors, and Methods and more Lecture notes Java Programming in PDF only on Docsity!

import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.logging.Level; import java.util.logging.Logger;

  • To change this template, choose Tools | Templates
  • and open the template in the editor. */

/**

  • @author ameed.khalid */ public class ReflectionMainClass { public static void main(String[] args) { try { Class c = Class.forName("lab01.Student"); //creating a reflective object of student class //retrieving name of package, class, parent and interfaces System.out.println("Package: " + c.getPackage().getName()); System.out.println("Class: " + c.getName()); System.out.println("Parent Class: " + c.getSuperclass().getName()); System.out.println("Interfaces: "); printInterfaces(c); //retrieving fields, their types and modifiers System.out.println("Fields: "); printFields(c); //retrieving Constructors, their types and modifiers System.out.println("Constructors: "); printConstructors(c); //printing method info System.out.println("Methods: "); printMethods(c); try { //invoking private method executePrivateMethods(c); } catch (Exception ex) { System.out.println("Error Invoking Private Methods: " + ex); } } catch (ClassNotFoundException ex) { System.out.println("Class Not Found: " + ex); } }

public static void printInterfaces(Class cl) { Class[] interfaces = cl.getInterfaces(); for(int i= 0; i<interfaces.length; i++) { System.out.println("\t" + interfaces[i].getName()); } } public static void printFields(Class cl) { Field[] fields = cl.getDeclaredFields(); for(int i= 0; i<fields.length; i++) { System.out.print("\t" + Modifier.toString(fields[i].getModifiers())); System.out.print(" " + fields[i].getType().getName()); System.out.println(" " + fields[i].getName()); } }

public static void printConstructors(Class cl) { Constructor[] constructors = cl.getConstructors(); for(int i= 0; i<constructors.length; i++) { System.out.print("\t" + Modifier.toString(constructors[i].getModifiers())); System.out.print(" " + constructors[i].getName()); Class[] paramTypes = constructors[i].getParameterTypes(); System.out.print("("); for (int j = 0; j < paramTypes.length; j++) { if (j > 0) System.out.print(", "); System.out.print(paramTypes[j].getName()); } System.out.println(");"); } } public static void printMethods(Class cl) { Method[] methods = cl.getDeclaredMethods();

for (int i = 0; i < methods.length; i++) { Method m = methods[i]; Class retType = m.getReturnType(); Class[] paramTypes = m.getParameterTypes(); String name = m.getName(); System.out.print("\t" + Modifier.toString(m.getModifiers())); System.out.print(" " + retType.getName() + " " + name + "("); for (int j = 0; j < paramTypes.length; j++) { if (j > 0) System.out.print(", "); System.out.print(paramTypes[j].getName()); } System.out.println(");"); } }

System.out.println("Value Returned: " + retValue); } } } catch (Exception ex) { System.out.println("Error Invoking Private Methods: " + ex); }

/* OUTPUT:

run: Package: lab Class: lab01.Student Parent Class: lab01.Person Interfaces: java.io.Serializable Fields: private java.lang.String degree_ private char section_ private short batch_ private short semester_ private float cgpa_ private int regNo_ Constructors: public lab01.Student(); public lab01.Student(java.lang.String, char, short, int); Methods: public void setCgpa(float); public float getCgpa(); public void setBatch(short); public short getBatch(); public void setDegree(java.lang.String); public java.lang.String getDegree(); public void setSection(char); public char getSection(); public void setSemester(short); public short getsemester(); public void setRegno(int); public int getregNo(); private float calculateCgpa(int, short); Creating default person Creating default Student Executing Method: private float calculateCgpa(int, short); private method: Calcualte CGPA for regNo: 1 semester 1 Value Returned: 3. BUILD SUCCESSFUL (total time: 0 seconds) */