180 likes | 192 Views
Explore how programs are interpreted as data, the significance of reflection in meta-programming, and accessing class information in Java using reflection.
E N D
Reflections CSC207 – Software Design
Background • Turing's great insight: programs are just another kind of data. • Source code is text that is interpreted in a specific way, with certain conventions. • Compiled programs are data, too: • Integers and strings are bytes in memory that you interpret a certain way. • Instructions in methods are just bytes, too.
Reflection • If programs are just data, then we can write a program that interprets another program. • That’s what a compiler or interpreter is(CSC488) • We can also build reflection into our languages. • Reflection is the ability to interpret code as data while the program is running. • This leads to “meta-programming”.
Why Study Reflection? • Imagine a system that uses plug-ins to change the basic behaviour of the system. • Reflection allows you to invoke code without hard-coding the class name. • Hence, we can get plug-in names (class names) from config files.
. . . Really? • You probably won’t use this on a daily basis. • The code is bulkier and harder to understand. • Understanding reflection will help us understand how Java works. • In particular, in CSC209, you’ll work with pointers, so we’d like to understand the concept of “memory”.
The Class Class • Instances of the class Class store information about Java classes (types). • Class name, inheritance relationships, interfaces implemented, methods and members, etc. • Can look up the instance of Class for a specific type by name or using an instance of the type.
Examining Class Contents public static void showMembers(String className) throws ClassNotFoundException { Class thisClass = Class.forName(className); Field[] fields = thisClass.getDeclaredFields(); for (Field f : fields) { System.out.println("\t" + f); } Method[] methods = thisClass.getDeclaredMethods(); for (Method m : methods) { System.out.println("\t "+ m); } }
Accessing Members • How to access members of a specific object? • class Field • Encapsulates access to a particular field of instances of a class. • Knows “where the field is” in objects of that class -- so it can get() the value or set() it.
ShowField Code public static void showField( Object obj, String fieldName) { try { Class thisClass = obj.getClass(); Field field = thisClass.getField(fieldName); Object value = field.get(obj); System.out.println(fieldName + " : " + value); } catch (NoSuchFieldException e) { System.out.println("NoSuchField"); System.exit(1); } catch (IllegalAccessException e) { System.out.println("Illegal access"); System.exit(1); } }
Point p = new Point("origin", 0, 0); showField( p , "name"); name : origin
Calling Methods • Look up a method based on its signature: the name and list of parameter types • Specify signature as a comma-separated list of Class objects • Call the method, passing in parameters and capturing return value • Specify parameters as a comma-separated list of Objects
Summary • There is no magic to executing a program! • A class is just a data structure • A method is just a data structure, too • A program just contains bytes that are interpreted as instructions to execute. • The call stack is another data structure