1 / 18

Reflections

Explore how programs are interpreted as data, the significance of reflection in meta-programming, and accessing class information in Java using reflection.

haney
Download Presentation

Reflections

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Reflections CSC207 – Software Design

  2. 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.

  3. 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”.

  4. 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.

  5. . . . 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”.

  6. How Objects Work

  7. 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.

  8. Showing a Type

  9. Output for Type Example

  10. 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); } }

  11. Output

  12. 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.

  13. 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); } }

  14. Point p = new Point("origin", 0, 0); showField( p , "name"); name : origin

  15. 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

  16. 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

More Related