Things that you can (but typically shouldn't!) do with Java reflection


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Reflection {
    public static void main(String [] args) {
        Class c1 = "hello".getClass();
        System.out.println(c1);
        System.out.println(c1.getName());
        Class c2 = "bye".getClass();
        System.out.println("The two classes are the same: " + c1.equals(c2));
        Class c3 = (new StringBuffer("hello")).getClass();
        System.out.println("The two classes are the same: " + c2.equals(c3));

        // classes of other data types:
        String [] arr = {"hello", "bye"};
        System.out.println(arr.getClass());
        int [] i_arr = {6, 7};
        System.out.println(i_arr.getClass());
        System.out.println(int.class); //getClass doesn't work for primitive types

        Circle circle = new Circle(6,7,5);
        System.out.println("the class of a circle: " + circle.getClass());
        System.out.println("the superclass of a circle: " +
                           circle.getClass().getSuperclass());
        ColorCircle cc = new ColorCircle(7, 8, 5, "blue");
        System.out.println("the class of a color circle: " + cc.getClass());
        System.out.println("the superclass of a color circle: " +
                           cc.getClass().getSuperclass());
        System.out.println("Class hierarchy of ColorCircle:");
        Class c = cc.getClass();
        while ( !c.equals((new Object()).getClass())) {
            System.out.println(c);
            c = c.getSuperclass();
        }

        try {
            String s = "hello".getClass().newInstance(); // constructor takes no arguments
            System.out.println(s);
        } catch (InstantiationException e) {
            System.out.println("Cannot create an instance");
        } catch (IllegalAccessException e) {
            System.out.println("Cannot access the object");
        }

        try {
            Class cl = Class.forName("Circle");
            Circle  circ = (Circle) cl.newInstance(); // constructor takes no arguments
            circ.draw();
        } catch (InstantiationException e) {
            System.out.println("Cannot create an instance");
        } catch (IllegalAccessException e) {
            System.out.println("Cannot access the object");
        } catch (ClassNotFoundException e) {
            System.out.println("Cannot find the class");
        }
        
        Method [] methods = c1.getMethods();
        for (Method m: methods) {
        	//System.out.println(m.getModifiers() + " " +  m.getName());
        	System.out.println(m.getName());
        }
        
        
        try {
        	// calling hashcode
			Object result = methods[2].invoke("hello", null);
			System.out.println(result);
			System.out.println(result.getClass().getName());
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    }
}



interface Shape {
    public void draw();
}



class Circle implements Shape {
    private int x;
    private int y;
    private int radius;

    public Circle() {

    }

    public Circle (int x, int y, int radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
    public void draw() {
        System.out.println("x = " + x + " y = " + y + " r = " + radius);
    }
}

class ColorCircle extends Circle{
    private String color;
    public ColorCircle (int x, int y, int radius, String color) {
        super(x,y,radius);
        this.color = color;
            }
    public void draw() {
        super.draw();
        System.out.println(" color = " + color);
    }
}


This is an example from CSci 4651 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.