Reflections CSC 207 Software Design Background Turings great
Reflections CSC 207 – 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(CSC 488) • 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 CSC 209, you’ll work with pointers, so we’d like to understand the concept of “memory”.
How Objects Work
The 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.
Showing a Type
Output for Type Example
Examining Class Contents public static void show. Members(String class. Name) throws Class. Not. Found. Exception { Class this. Class = Class. for. Name(class. Name); Field[] fields = this. Class. get. Declared. Fields(); for (Field f : fields) { System. out. println("t" + f); } Method[] methods = this. Class. get. Declared. Methods(); for (Method m : methods) { System. out. println("t "+ m); } }
Output
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.
Show. Field Code public static void show. Field( Object obj, String field. Name) { try { Class this. Class = obj. get. Class(); Field field = this. Class. get. Field(field. Name); Object value = field. get(obj); System. out. println(field. Name + " : " + value); } catch (No. Such. Field. Exception e) { System. out. println("No. Such. Field"); System. exit(1); } catch (Illegal. Access. Exception e) { System. out. println("Illegal access"); System. exit(1); } }
Point p = new Point("origin", 0, 0); show. Field( 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
- Slides: 18