The Reflection Lucielle Mendez Antonio Bologna.

Slides:



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

Reflection Reflection is the ability of a program to examine and modify the structure and behavior of an object at runtime.
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.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
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.
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.
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.
03G-1 Reflection In Java For every loaded class, the Java Runtime Environment (JRE) maintains an associated Class object The Class object “ reflects ”
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.
1 Java reflections API Lecture 1. 2 Assignments Java 1.4 or Java 1.5? Both are ok, but the assignments are written for Java 1.4, and specific Java 1.5.
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.
Advanced Java Programming Lecture 5 Reflection dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Object Oriented Programming
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
SEG4110 – Advanced Software Design and Reengineering TOPIC H Java Reflection.
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.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Course contents Basic concepts –What is software architecture ? Fundamental architectural styles –Pipes and filters –Layers –Blackboard –Event-driven Architectural.
Reflection in Java Bibliografie: Sun: The Java Tutorials – The Reflection API Java programming.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Introduction to Generics
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CS884 (Prasad)java11Extn1 Java 1.1 Extensions Nested classes static public/protected/private.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Reflection Bibliografie: Sun: The Java Tutorials – The Reflection API IBM developerWorks:
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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)
Topics Instance variables, set and get methods Encapsulation
Reflection Mehdi Einali Advanced Programming in Java 1.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Reflections CSC207 – Software Design. Background Turing's great insight: programs are just another kind of data. Source code is text that is interpreted.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Generics.
Chapter 7: Cloning and RTTI
Summary prepared by Kirk Scott
NESTED CLASSES REFLECTION PROXIES.
More Sophisticated Behavior
Java Programming Course
Java as a Dynamic Language
CSE 413, Autumn 2002 Programming Languages
Object Oriented Programming using Java - Class Instance Variables
Chapter 3: Using Methods, Classes, and Objects
Presentation on Object Oriented programming Topic
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
Computer Science II Exam 1 Review.
Advanced Programming in Java
Chapter 9 Object-Oriented Programming: Inheritance
Chapter 6 Methods: A Deeper Look
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.
Presentation transcript:

The Reflection Lucielle Mendez Antonio Bologna

The Meaning of Reflection The Reflection API is located in the java.lang.reflect package. The Reflection API is all about providing a "reflection" of the inner workings of a class.

Examining Classes Why do you want to examine Classes? Why do you want to examine Classes? For example, if you’re writing a class browser application, you may need Reflection to get information at runtime. For example, if you’re writing a class browser application, you may need Reflection to get information at runtime. JRE: Java runtime environment maintains a copy of the class as an object which contains information about the class. JRE: Java runtime environment maintains a copy of the class as an object which contains information about the class.

Examining Classes Retrieving Class Objects Retrieving Class Objects Getting the Class Name Getting the Class Name Finding Superclasses Finding Superclasses Identifying the Interfaces Implemented by a Class Identifying the Interfaces Implemented by a Class Identifying Class Fields Identifying Class Fields Discovering Class Constructors Discovering Class Constructors Obtaining Method Information Obtaining Method Information

Retrieving Class Objects If the instance of a class is available at runtime, you can Object.getClass() to obtain information about that object: If the instance of a class is available at runtime, you can Object.getClass() to obtain information about that object: Class c = drawLine.getClass(); Class c = drawLine.getClass();

Getting the Class Name At runtime, you can determine the name of a Class object by invoking the getName method. The String returned by getName is the fully-qualified name of the class. At runtime, you can determine the name of a Class object by invoking the getName method. The String returned by getName is the fully-qualified name of the class. Class c = o.getClass(); String s = c.getName();

Finding Superclasses To determine the superclass of a class, you invoke the getSuperclass method. This method returns a Class object representing the superclass, or returns null if the class has no superclass. To determine the superclass of a class, you invoke the getSuperclass method. This method returns a Class object representing the superclass, or returns null if the class has no superclass. Class subclass = o.getClass(); Class superclass = subclass.getSuperclass();

Identifying the Interfaces Implemented by a Class With reflection not only we can know an object class and superclass, but also we can know its interfaces. With reflection not only we can know an object class and superclass, but also we can know its interfaces. You invoke the getInterfaces method to determine which interfaces a class implements. You invoke the getInterfaces method to determine which interfaces a class implements. Class c = o.getClass(); Class[] theInterfaces = c.getInterfaces();

Identifying Class Fields You might want to find out what fields belong to a particular class. You might want to find out what fields belong to a particular class. You can identify a class's fields by invoking the getFields method on a Class object. You can identify a class's fields by invoking the getFields method on a Class object. Class c = o.getClass(); Field[] fields = c.getFields();

Discovering Class Constructors Typically to create an instance of a class you invoke the class constructor. Typically to create an instance of a class you invoke the class constructor. To get information about a class's constructors you invoke the getConstructors method, which returns an array of Constructor objects. To get information about a class's constructors you invoke the getConstructors method, which returns an array of Constructor objects. Class c = o.getClass(); Constructor[] theConstructors = c.getConstructors();

Obtaining Method Information To find out what public methods belong to a class, invoke the method named getMethods. To find out what public methods belong to a class, invoke the method named getMethods. You can use a Method object to uncover a method's name, return type, parameter types, set of modifiers, and set of throwable exceptions. You can use a Method object to uncover a method's name, return type, parameter types, set of modifiers, and set of throwable exceptions. Class c = o.getClass(); Method[] theMethods = c.getMethods();

Manipulating Objects Creating Objects Creating Objects Invoking Methods Invoking Methods

Creating Class Objects Simplest way to create an object in the Java : Simplest way to create an object in the Java : Point p1 = new Point(x1, y1); You may not know the class of an object until runtime: You may not know the class of an object until runtime: Point p = (Point) createObject("java.awt.Point"); static Object createObject(String className) { Object object = null; try { Class classDefinition = Class.forName(className); object = classDefinition.newInstance(); …..

Invoking Methods Suppose that you are writing a debugger that allows the user to select and then invoke methods during a debugging session. Since you don't know at compile time which methods the user will invoke, you cannot hardcode the method name in your source code. Suppose that you are writing a debugger that allows the user to select and then invoke methods during a debugging session. Since you don't know at compile time which methods the user will invoke, you cannot hardcode the method name in your source code.

Invoking Methods Steps Create a Class object that corresponds to the object whose method you want to invoke. Create a Class object that corresponds to the object whose method you want to invoke. Create a Method object by invoking getMethod on the Class object. Create a Method object by invoking getMethod on the Class object.Method