Java Program to Implement Reflection Class | IT2305 - Java Programming Lab


AIM:
To write a Java program to Implement the Reflection Class in IT2305 - Java Programming Laboratory.

SOURCE CODE:
import java.util.*;
import java.lang.reflect.*;
class Mycol
{
private final int id=90;
public static String univ;
private String name;
protected int n;
public double passp;
private Mycol()
{
}
public Mycol(int x)
{
}
public Mycol(Mycol m1)
{
}
private void set(int v)
{

}
public final String getname()
{
return "mepco";
}
public static void main(String args[])
{
}
}

public class New
{
public static void main(String args[])
{
int i;
Class c1=Mycol.class;
//FIELDS

Field []fc=c1.getDeclaredFields();

for(i=0;i<fc.length;i++)
{
   int x=fc[i].getModifiers();
   Class Type=fc[i].getType();
   System.out.println(Modifier.toString(x)+" "+Type.getName()+" "+fc[i].getName()+";");
}

//METHODS
Method []mc=c1.getDeclaredMethods();

for(Method z:mc)
{
  int x=z.getModifiers();
  Class ReType=z.getReturnType();
  Class para[]=z.getParameterTypes();
  System.out.print(Modifier.toString(x)+" "+ReType.getName()+" "+z.getName()+"(");
  for(Class pa:para)
  System.out.print(pa.getName());
  System.out.println(");");

 }

//CONSTRUCTOR
Constructor []c2=c1.getDeclaredConstructors();
for(Constructor d:c2)
 {
 int x=d.getModifiers();
Class []para=d.getParameterTypes();

System.out.print(Modifier.toString(x)+" "+d.getName()+"(");
for(Class pa:para)
    {
     System.out.print(pa.getName());
     System.out.println(");");
   }
 }
cat Studen.java
package person;
import java.util.*;
import java.io.*;
public class Studen
{
 private int rollno;
 private String Studname;
 private String Sdept;
 public void readData()
 {
  Scanner sin=new Scanner(System.in);
  System.out.println("enter the name,dept,rollno");
  Studname=sin.nextLine();
  Sdept=sin.nextLine();
  rollno=sin.nextInt();
 }
public void printDetails()
{
 System.out.println("name:"+Studname);
System.out.println("dept" +Sdept);
System.out.println("id" +rollno);
}
}
[08cs083@mepcolinux:~/java]cat Employee.java
package person;
import java.util.*;
import java.io.*;
public class Employee
{
 private int eid;
 private String ename,edept;
 public void readDetails()
 {
   Scanner sin=new Scanner(System.in);
   System.out.println("Enter Employee id,name,dept");
    eid=sin.nextInt();
    ename=sin.next();
    edept=sin.next();
   }
 public void print_Data()
 {
  System.out.println("Employee id:" +eid);
  System.out.println("Employee name:" +ename);
  System.out.println("Employee dept:" +edept);
  }
}
Previous
Next Post »

Still not found what you are looking for? Try again here.