Dynamic Proxies Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Dynamic Proxies Main idea: The proxy wraps objects and adds functionality.

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

Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Java Review Interface, Casting, Generics, Iterator.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Java Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Review Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Review What have we done during the course? Where to learn more? What is for the.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
RTTI and Reflection David Talby. Overview Static typing Run-Time Type Information in C++ Reflection in Java Dynamic Proxies in Java Syntax, uses and misuses.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Exceptions David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 The Role of Exceptions Definition: a method succeeds if it terminates in.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Dynamic Proxies David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 Dynamic Proxies Support for creating classes at runtime Each such class.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
17. Python Exceptions Handling Python provides two very important features to handle any unexpected error in your Python programs and to add debugging.
Exceptions1 Syntax, semantics, and pragmatics. Exception create If (some error){ throw new SomeException(”some message”); } Exceptions2.
Lecture objectives  Differences between Java and C#  Understand and use abstract classes  Casting in Java: why is it important?  Master exceptions.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
Dynamic Architectures (Component Reconfiguration) with Fractal.
Java Dynamic Proxy Bibliography: reflection/proxy.html
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Types in programming languages1 What are types, and why do we need them?
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
PerlNET: The Camel Talks.NET Jan Dubois The Perl Conference 6 San Diego, July 26 th 2002.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
(1) Dynamic Proxies in Java And so on into winter Till even I have ceased To come as a foot printer, And only some slight beast So mousy or foxy Shall.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Exception Handling How to handle the runtime errors.
CSE 1020: Exceptions and A multiclass application Mark Shtern 1-1.
Martin Kruliš Who is General Failure and why is he reading my disk? by Martin Kruliš (v1.0)1.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
© 2001 by Ashby M. Woolf Revision 2 Exceptions for Exceptional Circumstances Passing a Problem up the Chain of Command.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
RTTI and Reflection David Talby. 11/20/2016 Object Oriented Design Course 2 Overview Static typing Run-Time Type Information in C++ Reflection in Java.
Topic: Classes and Objects
The Java Dynamic Proxy.
Dynamic Proxy Proxy: Addition to the Java 1.3 reflection package:
Presentation on Object Oriented programming Topic
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Handling Exceptions.
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Java Programming Language
More Object-Oriented Programming
Sampath Kumar S Assistant Professor, SECE/IT
CSC 143 Java Errors and Exceptions.
TCSS 143, Autumn 2004 Lecture Notes
„Lambda expressions, Optional”
SPL – PS3 C++ Classes.
Presentation transcript:

Dynamic Proxies Amit Shabtay

March 3rd, 2004 Object Oriented Design Course 2 Dynamic Proxies Main idea: The proxy wraps objects and adds functionality. All proxy methods invocations are dispatched to the invoke(...) method of the instance invocation handler. These methods will be handled by the proxy instance.

March 3rd, 2004 Object Oriented Design Course 3 Dynamic Proxies Scheme Create Object Proxy instance Create Object The proxy also receives an invocation handler Wrap Calling a method foo() calls invoke() Before foo... Invoke foo() after foo... Mast implement interface(s) The heart is the invocation handler

March 3rd, 2004 Object Oriented Design Course 4 Dynamic Proxies Support for creating classes at runtime Each such class implements interface(s) Every method call to the class will be delegated to a handler, using reflection The created class is a proxy for its handler Applications Aspect-Oriented Programming: standard error handling, log & debug for all objects Creating dynamic event handlers

March 3rd, 2004 Object Oriented Design Course 5 Invocation Handlers Start by defining the handler: interface java.lang.reflect.InvocationHandler With a single method: Object invoke( // return value of call Object proxy, // call’s target Method method, // the method called Object[] args) // method’s arguments The “real” call made: proxy.method(args) Simplest invoke(): method.invoke(proxy,args)

March 3rd, 2004 Object Oriented Design Course 6 Creating a Proxy Class Define the proxy interface: interface Foo { Object bar(Object obj); } Use java.lang.reflect.Proxy static methods to create the proxy class: Class proxyClass = Proxy.getProxyClass( Foo.class.getClassLoader(), new Class[] { Foo.class }); First argument – the new class’s class loader 2nd argument – list of implemented interfaces The expression C.class for a class C is the static version of C_obj.getClass()

March 3rd, 2004 Object Oriented Design Course 7 Creating a Proxy Instance A proxy class has one constructor which takes one argument – the invocation handler Given a proxy class, find and invoke this constructor: Foo foo = (Foo)proxyClass. getConstructor(new Class[] { InvocationHandler.class }). newInstance(new Object[] { new MyInvocationHandler() });

March 3rd, 2004 Object Oriented Design Course 8 Class Proxy provides a shortcut: Foo f = (Foo) Proxy.newProxyInstance( Foo.class.getClassLoader(), new Class[] { Foo.class }, new MyInvocationHandler()); Note that all members of Class[] should be interfaces only.

March 3rd, 2004 Object Oriented Design Course 9 A Few More Details I We ignored a bunch of exceptions IllegalArgumentException if proxy class can’t exist UndeclaredThrowableException if the handler throws an exception the interface didn’t declare ClassCastException if return value type is wrong InvocationTargetException wraps checked exceptions A proxy class’s name is undefined But begins with Proxy$ The syntax is very unreadable! Right, but it can be encapsulated inside the handler

March 3rd, 2004 Object Oriented Design Course 10 A Few More Details II Primitive types are wrapped by Integer, Boolean, and so on for argument This is also true for the return values. If null is returned for a primitive type, then a NullPointerException will be thrown by the method invocation.

March 3rd, 2004 Object Oriented Design Course 11 A Debugging Example We’ll write an extremely generic class, that can wrap any object and print a debug message before and after every method call to it Instead of a public constructor, it will have a static factory method to encapsulate the proxy instance creation It will use InvocationTargetException to be exception-neutral to the debugged object

March 3rd, 2004 Object Oriented Design Course 12 A Debugging Example II The class’s definition and construction: public class DebugProxy implements java.lang.reflect.InvocationHandler { private Object obj; public static Object newInstance(Object obj) { return java.lang.reflect.Proxy.newProxyInstance( obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new DebugProxy(obj)); } private DebugProxy(Object obj) { this.obj = obj; }

March 3rd, 2004 Object Oriented Design Course 13 A Debugging Example III The invoke() method: public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { Object result; try { System.out.println("before method " + m.getName()); result = m.invoke(obj, args); } catch (InvocationTargetException e) { throw e.getTargetException(); } catch (Exception e) { throw new RuntimeException("unexpected:” + e.getMessage()); } finally { System.out.println("after method " + m.getName()); } return result; }

March 3rd, 2004 Object Oriented Design Course 14 A Debugging Example IV Now that the handler is written, it’s very simple to use. Just define an interface: interface Foo { Object bar(Object o); } class FooImpl implements Foo { … } And wrap it with a DebugProxy: Foo foo = (Foo)DebugProxy.newInstance(new FooImpl()); This is not much different than using any proxy or decorator Just much, much slower

March 3rd, 2004 Object Oriented Design Course 15 Dynamic Proxies: Summary Applications similar to above example: Log every exception to a file and re-throw it Apply an additional security policy Other kinds of applications exist as well Dynamic event listeners in Swing In general, being an observer to many different objects or interfaces at once It’s a relatively new feature – from JDK 1.3 There may be other future applications

March 3rd, 2004 Object Oriented Design Course 16 More Information guide/reflection/proxy.html guide/reflection/proxy.html api/java/lang/reflect/Proxy.html api/java/lang/reflect/Proxy.html