Presentation on Object Oriented programming Topic

Slides:



Advertisements
Similar presentations
1 Dynamic Proxies Explained Simply. 2 Dynamic Proxies License Copyright © 2008 Ciaran McHale. Permission is hereby granted, free of charge, to any person.
Advertisements

Using Java™ Technology Reflection to Improve Design
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
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.
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.
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.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Road Map Introduction to object oriented programming. Classes
Dynamic Proxies Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Dynamic Proxies Main idea: The proxy wraps objects and adds functionality.
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.
Reflection (Reflexion in British English ) Mo Alkhafaji.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Dynamic Proxies David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 Dynamic Proxies Support for creating classes at runtime Each such class.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Advanced Java Programming Lecture 5 Reflection dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
SEG4110 – Advanced Software Design and Reengineering TOPIC H Java Reflection.
The Reflection Lucielle Mendez Antonio Bologna.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Philly JUG: May 21, 2002 Dynamic Java Aaron Mulder Chief Technical Officer Chariot Solutions Classes Without Code.
In the name of Allah The Proxy Pattern Elham moazzen.
Reflection in Java Bibliografie: Sun: The Java Tutorials – The Reflection API Java programming.
Java Dynamic Proxy Bibliography: reflection/proxy.html
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
J AVA P ROGRAMMING 2 CH 04: C LASSES, O BJECTS AND M ETHODS (II) 0.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Object-Oriented Design
NESTED CLASSES REFLECTION PROXIES.
JAVA MULTIPLE CHOICE QUESTION.
The Java Dynamic Proxy.
Inheritance and Polymorphism
Dynamic Proxy Proxy: Addition to the Java 1.3 reflection package:
Internet and Java Foundations, Programming and Practice
CSE 413, Autumn 2002 Programming Languages
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Reflection SoftUni Team Technical Trainers C# OOP Advanced
2.1. Compilers and Interpreters
ATS Application Programming: Java Programming
Chapter 13 Abstract Classes and Interfaces
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
Object Oriented Programming
Java Programming Language
Polymorphism and access control
Advanced Java Topics Chapter 9
Sampath Kumar S Assistant Professor, SECE/IT
Inheritance Inheritance is a fundamental Object Oriented concept
Generic Classes and Methods
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Some Examples Reflection is used for many things:
TCSS 143, Autumn 2004 Lecture Notes
Java Remote Method Invocation
Software Design Lecture : 38.
CSCE 314: Programming Languages Dr. Dylan Shell
15. Proxy SE2811 Software Component Design
Presentation transcript:

Presentation on Object Oriented programming Topic The Reflection Application

What is Reflection In computer science,Reflection is the ability of a computer program to examine and modify the structure and behavior of the program at runtime.

Why Reflection? Reflection is useful for developing tools such as: 1.Debuggers. 2.Class browsers. 3.Dynamic analysis. So Reflection is important for Object oriented programming.

Java core Reflection is an API with methods to: 4 Get Java core Reflection is an API with methods to: -Determine the class at runtime. -Get information about a class: modifiers,instance variables,methods,constructors. -Create an instance of a class whose name is not known untill runtime. -Get and set the value of an object’s field,even if the field name is unknown untill runtime.

Exposing a class’ content: 5 Exposing a class’ content: The Class class has methods for getting: -constructors:getconstructors() -methods:getmethods() -Fields:getfields() Notice that you can not break encapsulation using Reflection -For example,trying to invoke private methods illegally will cause an exceotion.

Java stores metadata in classes: - Metadata for a class: java.lang.Class - Metadata for a constructor: java.lang.reflect.Constructor - Metadata for a field: java.lang.reflect.Field - Metadata for a method: java.lang.reflect.Method Two ways to access a Class object for a class: Class c1 = Class.forName(“java.util.Properties”); Object obj = ...; Class c2 = obj.getClass(); Reflection classes are inter-dependent

Reflection Applications (Java) -Applications getting run-time information about objects, use: -getField[s] -getMethod[s] -getConstructors[s] -Applications getting compile-time information about objects (at the level provided by .class files), use: -getDeclaredField[s] -getDeclaredMethod[s] -getDeclaredConstructor[s]

Dynamic Proxies In Java

Dynamic Proxies - Proxy objects are useful in many situations to act as an intermediary between a client object and a target object -Usually, the proxy class is already available as Java bytecodes, having been compiled from the Java source file for the proxy class -When needed, the bytecodes for the proxy class are loaded into the Java Virtual Machine and proxy objects can then be instantiated -But, isome circumstances, it is useful to dynamically generate the bytecodes for the proxy class at runtime

Dynamic Proxies In Java -Java supports the creation of dynamic proxy classes and instances -A dynamic proxy class is a class that implements a list of interfaces specified at runtime when the class is created -A proxy interface is an interface that is implemented by a proxy class -A proxy instance is an instance of a proxy class -Each proxy instance has an associated invocation handler object, which implements the interface InvocationHandler.

Dynamic Proxy Class -Proxy classes are created using the new java.lang.reflect.Proxy class -Proxy classes are public, final, non-abstract subclasses of java.lang.reflect.Proxy. -The unqualified name of a proxy class is unspecified. The space of class names that begin with the string "$Proxy" should be, however, reserved for proxy classes. -A proxy class implements exactly the interfaces specified at its Creation. -Since a proxy class implements all of the interfaces specified at its creation, invoking getInterfaces() on its Class object will return an array containing the same list of interfaces (in the order specified at its creation).

Dynamic Proxy Class -Each proxy class has one public constructor that takes one argument, an implementation of the interface InvocationHandler, to set the invocation handler for a proxy instance. Rather than having to use the reflection API to access the public constructor, a proxy instance can be also be created by calling Proxy.newInstance() method, which combines the actions of calling Proxy.getProxyClass() with invoking the constructor with an invocation handler

The java.lang.reflect.Proxy Class -public static Class getProxyClass(ClassLoader loader, Class[] interfaces) throws IllegalArgumentException -Creates a proxy class defined in the specified class loader and which implements the specified interfaces. Returns the java.lang.Class object for the generated proxy class. protected Proxy(InvocationHandler ih): Constructs a new Proxy instance from a subclass (typically, a dynamic proxy class) with the specified value for its invocation handler public static boolean isProxyClass(Class C): Returns true if and only if the specified class was dynamically generated to be a proxy class using the getProxyClass() method or the newProxyInstance() method of the Proxy class.

The java.lang.reflect.Proxy Class public static Object newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler ih) throws IllegalArgumentException -Creates a proxy class defined in the specified class loader and which implements the specified interfaces. In addition, creates an instance of the proxy by invoking the one public proxy constructor which sets the associated invocation handler to the specified handler. Returns a reference to the proxy instance Proxy.newProxyInstance(cl, interfaces, ih); is equivalent to Proxy.getProxyClass(cl, interfaces).getConstructor(new Class[] { InvocationHandler.class }).newInstance(new Object[] {ih});

The java.lang.reflect.Proxy Class public static InvocationHandler getInvocationHandler (Object proxy) throws IllegalArgumentException -Returns the invocation handler for the specified proxy instance.

-where would we use dynamic proxies: Uses for Dynamic proxies -In the Vehicle example, there seems to be little benefit in dynamically generating the proxy -We still had to write the invocation handler class! -There is now another object layer between the client and the targe. -where would we use dynamic proxies: -Generic Delegation -Dynamic generation of proxies (stubs) for remote objects

THANK YOU