


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 4
This page cannot be seen from the preview
Don't miss anything!



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;
/**
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); }
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) */