Tut5 Reflection CPSC 501 Dr J Hudson University

  • Slides: 11
Download presentation
Tut#5: Reflection CPSC 501 Dr. J. Hudson University of Calgary Arshia Hosseini T 01/T

Tut#5: Reflection CPSC 501 Dr. J. Hudson University of Calgary Arshia Hosseini T 01/T 02

Uses of Reflection • Programs which require the ability to examine or modify runtime

Uses of Reflection • Programs which require the ability to examine or modify runtime behavior of applications • Should only be used by developers who know what they’re doing • If it’s possible to do something without reflection, then it is preferable to avoid using it • Mostly used in frameworks where dynamicity is really important 2

Drawbacks • Performance overhead • Should be avoided in sections that are performancesensitive •

Drawbacks • Performance overhead • Should be avoided in sections that are performancesensitive • Security restrictions • Performing reflection requires run-time permission which is not always possible • Exposure of Internals • Reflections performs otherwise illegal actions such as accessing private fields and methods • Reflection may result in the code being dysfunctional 3

Reflection vs Introspection • The ability to inspect the code in the system and

Reflection vs Introspection • The ability to inspect the code in the system and see object types is not reflection, but rather Type Introspection. • Reflection is then the ability to make modifications at runtime by making use of introspection. 4

Examples • Reflection allows instantiation of new objects, invocation of methods, and get/set operations

Examples • Reflection allows instantiation of new objects, invocation of methods, and get/set operations on class variables dynamically at run time without having prior knowledge of its implementation. 5

 • Reflection allows you to access private member/methods of a class 6

• Reflection allows you to access private member/methods of a class 6

Dump method 7

Dump method 7

Examples – Converting to XML • In order to convert the class to XML,

Examples – Converting to XML • In order to convert the class to XML, you can write something like this: 8 • But this is a lot of boilerplate code, and every time you change the class, you have to update the code.

Example – String Processing • You can test/run your code using Strings • JSON,

Example – String Processing • You can test/run your code using Strings • JSON, XML, etc. { “function name”: “function 1” “argument 1”: { “type”: “type 1” “value”: “value 1” } “argument 1”: { “type”: “type 2” “value”: “value 2” } } Method method = obj. get. Class(). get. Method("function 1", type 1. Class, type 2. Class); Object ret. Obj = method. invoke(obj, "value 1", "value 2"); 9

Example – Spring Framework • In order to create a DB mapping of a

Example – Spring Framework • In order to create a DB mapping of a class: • Spring goes through the class using reflection, finds its fields, finds their relevant types and values, and creates a DB accordingly 10

Exercise • Write a piece of code in Java using reflection, that takes an

Exercise • Write a piece of code in Java using reflection, that takes an object as an argument, and converts the class object (including it’s fields and methods if available) to a String. 11