10/18/08 Matt Swatzell. What is Reflection?  Some Definitions….  Reflection is the process by which a program can modify its own behavior.  A program.

Slides:



Advertisements
Similar presentations
Using Java™ Technology Reflection to Improve Design
Advertisements

Object Oriented Programming with Java
Reflection Reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime.
Dynamic Code Generation in Java. Class Loading Class loading is the process of transforming a byte code (e.g., a.class file) into a Java class A Java.
1 InTroToJCL Introduction to Java Class Loaders. 2 class loader l ia an object responsible for loading classes. The class ClassLoader is an abstract class.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Slide 11.1 Advanced Programming 2004, based on LY Stefanus’s Slides Reflection Reflection is the ability for a class or object to examine itself. Java.
Reflection CMSC Issues What is reflection History of reflection How to use reflection Myths about reflection Advanced reflection issues Improvements.
Type-safe Implementation of Java ™ Reflection Nadeem Abdul Hamid February 27, 2001 Yale University Advisor: Prof. Zhong Shao (Work in progress)
1 Java Reflection. 2 Java looking at Java l One of the unusual capabilities of Java is that a program can examine itself »You can determine the class.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 Java Reflection. 2 Java looking at Java l One of the unusual capabilities of Java is that a program can examine itself »You can determine the class.
Reflection (Reflexion in British English ) Mo Alkhafaji.
How is Java different from other languages Less than you think: –Java is an imperative language (like C++, Ada, C, Pascal) –Java is interpreted (like LISP,
1 Java object model Part 3: Serialization & Reflection.
Reflections on Reflection Objects from the inside out...
Problem Solving #3: JVM ICS Outline Review of Key Topics Review of Key Topics Problem 1 Problem 1 Problem 2 Problem 2 Problem 3 Problem 3 Problem.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
13-Jul-15 Reflection. 2 Java looking at Java One of the unusual capabilities of Java is that a program can examine itself You can determine the class.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Advanced Java Programming Lecture 5 Reflection dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Java Reflection. Compile-Time vs Run-Time Some data you know at compile time: int area = radius*radius*3.14; The “3.14” is set – known at compile time.
Object Oriented Programming in Java Lecture 13. Java Reflection Reflection is a feature unique to Java that allows an executing program to examine or.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
The Reflection Lucielle Mendez Antonio Bologna.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Reflection in Java Bibliografie: Sun: The Java Tutorials – The Reflection API Java programming.
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Plug-in Architectures Presented by Truc Nguyen. What’s a plug-in? “a type of program that tightly integrates with a larger application to add a special.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Reflection Bibliografie: Sun: The Java Tutorials – The Reflection API IBM developerWorks:
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
Classes, Interfaces and Packages
CSE 331 Reflection slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
03G-1 Reflection In Java “Reflection is the ability of a program to manipulate as data something representing the state of the program during its own execution.”
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
OOP Basics Classes & Methods (c) IDMS/SQL News
CSH Intro. to Java. The Big Ideas in Computer Science Beyond programming Solving tough problems Creating extensible solutions Teams of “Computational.
Reflection Mehdi Einali Advanced Programming in Java 1.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
RealTimeSystems Lab Jong-Koo, Lim
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Chapter 7: Cloning and RTTI
NESTED CLASSES REFLECTION PROXIES.
Java Programming Course
CSE 413, Autumn 2002 Programming Languages
Running An Encrypted Java Program
Presentation on Object Oriented programming Topic
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
Advanced Programming in Java
Polymorphism and access control
What is Reflection? Some Definitions….
Some Examples Reflection is used for many things:
What is Reflection? Some Definitions….
CSCE 314: Programming Languages Dr. Dylan Shell
Reflection in Java By: Zachary Cava.
Plug-In Architecture Pattern
Presentation transcript:

10/18/08 Matt Swatzell

What is Reflection?  Some Definitions….  Reflection is the process by which a program can modify its own behavior.  A program that can analyze and modify its own parameters at runtime is considered reflective.  Tools for manipulating code at runtime.  In other words… A way for a program/application to learn about itself, change itself, and perhaps even add to itself.

Some Examples  Reflection is used for many things:  IDE’s / software analysis tools  Debuggers  Server Side Code i.e. ASP.NET  Plug-ins  Frameworks i.e. Java Beans  Imitating function pointers in Java

Code Demo  Code Demo  How would you go about doing this?

The java.lang.Class Class  ang/Class.html ang/Class.html  JVM always maintains Runtime Type Definition for all objects.  Determines correct methods to call  Class is used for this purpose.  Unique Class object for each type.

java.lang.Class cont.  3 ways to get the Class class of an object  (any Object).getClass();  For objects that already exist  Scanner scan = new Scanner(System.in);  Class c = scan.getClass();  (Any type).class  When you only know the type  Works for primitives too  Int.class;  Scanner.class;  Class.forName(“fully qualified path”);  Know the name of class you want  Class c1 = Class.forName(“java.util.Scanner”);

Useful methods in Class  Public string Name();  returns name of class  Constructor[] getConstructors()  an Array of constructors (more on these later)  Method[] getMethods()  an Array of methods  Field[] getFields()  an Array of fields  Class[] getInterfaces()  an Array of Interfaces  newInstance()  An instance of the class, only works in certain cases

java.lang.reflect.Constructor  Represents a constructor for a class object  Important Methods  Class getDeclaringClass();  Int getModifiers();  Why int?  Class[] getParameterTypes();  Object newInstance(Object[] args);  Creates a new class if parameters are needed

Java.lang.reflect.Method  Represents a method in a class  Useful Methods in Method  Class[] getExceptionTypes();  Int getModifiers();  String getName();  Class[] getParameterTypes();  Class getReturnType();  Invoke(Object o, Object[] args);  Exceptions, Exceptions, Exceptions

Java.lang.reflect.Field  Represents a Field within a Class  get(type)(Object obj);  Returns the value of the field in Object obj  set(Type)(Object o)  Sets the value

Two more java.lang.reflector.*’s  Java.lang.reflect.AccessibleObject  Base for Constructor, method, field  Allows you to modify accessibility at runtime.  isAccessible();  setAccessible(boolean flag);  Breaks with SecurityManagers  java.lang.reflect.Modifier  Takes int and returns facts about modifers on it  isPrivate(int i);  toString(int i);

Can I do it with Arrays?  Java.lang.reflect.Array  get(type)(Object Array, int index);  set(Type)(Object array, int Index, type value);  newInstance(Class type, int length);  New Instance(Class type, int[] dimensions);

Function Pointers (in a way…)  Cannot pass location of method in java  In C/C++ this is possible  Passing functions as parameters  Void doAwesomeness(function awesome);  Reflection provides a handy workaround if you need this functionality  Simply pass method object and call invoke  Before you get too excited…  It not in Java for a reason  Inheritance / Polymorphism usually safer.

Problems With Reflection  Slow…..  Security Problems  If not security problems, than security risks

A brief intro to ClassLoader  Q: So, other than object analyzers, generic toStrings, and function pointers what is Reflection good for?  A: Loading classes dynamincally  Sometimes you will not know all the classes that will be needed. Used by your application  Sometimes the user will want to add their own  Think of VS, Eclipse, some games

ClassLoader cont.  ClassLoader is responsible for loading classes  Every class has reference to ClassLoader the created it.  Only object w/o class loader are generic Objects in arrays.  Primitives have no class loader  The original ClassLoader, (called the bootstrap class loader) has no parent class loader  all others, as objects, do

Boot strap Class Loader  Where does JVM look for classes?  Some are included with application  Custom Defined Ones  Are all of them?  JVM looks in ClassPath for classes, then in program / application  What if you want to look elsewhere?  On your own machine, you could add to ClassPath  Can’t do this on end-user’s

Other Options  Only one really…  Create a new ClassLoader that extends abstract ClassLoader.  Override loadClass  Get location of file  Get byte array of the class data  Call define class and return the result  You now have a class of a type that was unknown until runtime  Problems with inheritance

An example custom Class Loader  Public class customLoader extends ClassLoader {  public class findClass(String name) {  Byte[] b = openFileAndGetByteArray(name);  Return defineClass(name,b,0,b.length);  }  }

Citations  Horstmann, Cay S., and Gary Cornell. Core Java 2. Volume 1. Santa Clara, CA: Sun Microsystems,  "The Reflection API." java.sun.com. Sun Microsystems, Web. 19 Nov 2009.