














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
An overview of java reflection, a powerful feature that allows runtime discovery of an object's class, fields, constructors, and methods. It covers various techniques for retrieving class objects, getting class names, identifying interfaces, discovering class fields, and invoking methods. The document also explains how to create objects dynamically using no-argument constructors and constructors with arguments.
Typology: Slides
1 / 22
This page cannot be seen from the preview
Don't miss anything!















public, protected, private protected, private
Class c = mystery.getClass(); Class sc = c.getSuperclass();
Class c = java.awt.Button.class;
String className = “java.awt.Button”; Class c = Class.forName(className);
Class c = mystery.getClass();
Discovering Class Modifiers
(public/abstract/final)
String s = new String(); printModifiers(s); ... public static void printModifiers(Object o) {
Class c = o.getClass(); int m = c.getModifiers();
if ( Modifier.isPublic(m) ) System.out.println ("public"); if ( Modifier.isAbstract(m) ) System.out.println ("abstract"); if ( Modifier.isFinal(m) ) System.out.println ("final"); } public final
Finding Superclasses:
getSuperclass()
java.awt.Component java.lang.Object
isInterface() checks whether a Class object represents an interface or a class getField() finds public constants of an interface ... Class observer = Observer.class; Class observable = Observable.class; verifyInterface(observer); verifyInterface(observable); ... static void verifyInterface(Class c) { String name = c.getName(); if ( c.isInterface() ) { System.out.println(name + " is an interface."); } else { System.out.println(name + " is a class."); } } (^) java.util.Observer is an interface. java.util.Observable is a class.
Discovering Class Constructors:
getConsructors()
static void showConstructors(Object o) { Class c = o.getClass(); Constructor[] constructors = c.getConstructors(); for ( int i = 0; i < constructors.length; i++) { System.out.print("( "); Class[] paramTypes = constructors[i].getParameterTypes(); for ( int k = 0; k < parameterTypes.length; k++) { String paramString = paramTypes[k].getName(); System.out.print(paramString + " "); } System.out.println(")"); } }
( ) (int int ) (int int int int) (java.awt.Dimension) (java.awt.Point) (java.awt.Dimension) (java.awt.Rectangle)
Using No-Argument Constructors
static Object createObject(String className) { Object object = null; try { Class classDefinition = Class.forName(className); object = classDefinition.newInstance(); } catch (InstantiationException e) { System.out.println(e); } catch (IllegalAccessException e) { System.out.println(e); } catch (ClassNotFoundException e) { System.out.println(e); } return object; }
void printHeight(Rectangle r) { Field heightField; Integer heightValue; Class c = r.getClass();
try { heightField = c.getField ("height"); heightValue = (Integer) heightField.get(r); System.out.println ("Height: “ + heightValue.toString()); } catch (NoSuchFieldException e) { System.out.println(e); } catch (SecurityException e) { System.out.println(e); } catch (IllegalAccessException e) { System.out.println(e); }
Create a Class object Create a Field object by invoking getField on the Class object Invoke the appropriate set method on the Field object
void modifyWidth(Rectangle r, Integer widthParam){ Field widthField; Integer widthValue; Class c = r.getClass();
try { widthField = c.getField ("width"); widthField.set(r, widthParam); } catch (NoSuchFieldException e) { System.out.println(e); } catch (IllegalAccessException e) { System.out.println(e); } }