Reflection (Reflexion in British English ) Mo Alkhafaji.

Slides:



Advertisements
Similar presentations
JavAssist X.java X.class Class Loader My Loader X compile load JavAssist enables preload byte code modification.
Advertisements

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.
ECE 750 Topic 8 Meta-programming languages, systems, and applications Load-time structural reflection in Java – Shigeru Chiba, 2000 May 27, 2004 Shimin.
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
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.
Confined Types Encapsulation and modularity Seminar November, 2005 presented by: Guy Gueta.
1 Java (vs. C++). 2 Object-oriented Programming Java and C++ are the most popular object-oriented programming languages Java Sun Microsystems in 1990.
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,
03G-1 Reflection In Java For every loaded class, the Java Runtime Environment (JRE) maintains an associated Class object The Class object “ reflects ”
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.
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
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,
Load Time Structural Reflection in Java Shigeru Chiba Institute of Information Science and Electronics University of Tsukuba.
Protection of Agent Teamwork By Jeremy Hall. Agent Teamwork Overview ● Mobile agent framework  AgentTeamwork 2 is a mobile-agent based middleware system.
Java Beans Component Technology Integrated, Visual Development Environments Reusable Platform-Independent Modular.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Advanced Java Programming Lecture 5 Reflection dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Bruno Cabral Summary Vision Statement Process and Methodology Key Features Application scenarios What can we really do with RAIL?
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Names Variables Type Checking Strong Typing Type Compatibility 1.
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.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
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.
Course contents Basic concepts –What is software architecture ? Fundamental architectural styles –Pipes and filters –Layers –Blackboard –Event-driven Architectural.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
1 First BlueJ Day, Houston, Texas, 1st March 2006 Writing BlueJ Extensions Ian Utting University of Kent.
Reflection in Java Bibliografie: Sun: The Java Tutorials – The Reflection API Java programming.
Working with arrays (we will use an array of double as example)
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
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.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
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.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Chapter 3 JSP Overview. The Problem with Servlets processing the request and generating the response are both handled by a single servlet class Java programming.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Reflection Bibliografie: Sun: The Java Tutorials – The Reflection API IBM developerWorks:
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Introduction to Software Components: the JavaBeans specs Babak Esfandiari.
Bruno Cabral “Reflection, Code Generation and Instrumentation in the.NET platform” University of Coimbra.
(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.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
METADATA IN.NET Presented By Sukumar Manduva. INTRODUCTION  What is Metadata ? Metadata is a binary information which contains the complete description.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Debuggers. Errors in Computer Code Errors in computer programs are commonly known as bugs. Three types of errors in computer programs –Syntax errors –Runtime.
Object-Oriented Design
Reflection API, JDBC, Hibernate, RMI
Inheritance and Polymorphism
Internet and Java Foundations, Programming and Practice
CSE 413, Autumn 2002 Programming Languages
Presentation on Object Oriented programming Topic
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
Inheritance Inheritance is a fundamental Object Oriented concept
Final Jim Brucker.
Chapter 14 Abstract Classes and Interfaces
Object Oriented Programming in java
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
Plug-In Architecture Pattern
Presentation transcript:

Reflection (Reflexion in British English ) Mo Alkhafaji

Reflection A powerful Java API that allows for dynamic introspection of classes, interfaces, methods, and fields. No similar built-in mechanism in Pascal, C, or C++. What can you do with Reflection? It gives you the ability to: * Discover interfaces, classes, methods and fields. * Examine and call classes, methods and constructors. * Discover types of objects. * Write very generic code.

The API *java.lang.Class — This class is used to represent classes. *java.lang.reflect.Constructor — A special class for inspecting any of a class's constructor methods. *java.lang.reflect.Method — This class provides an abstract means of invoking a Java method from a given class or object.

The API – Cont. *java.lang.reflect.Modifier — The Modifier class is used to determine whether special access modifiers (such as private, public, or protected) have been applied to a method or attribute. *java.lang.reflect.Array — This class is used as an abstract representation of array objects. *java.lang.reflect.Field — This class is used to represent an attribute field.

Practical Examples * JavaBeans. * Debuggers. * Class browsers. * Introspection. * Too generic tasks that cannot be grouped into interfaces or abstract classes.

Practical Examples - Cont. * A proxy to remote API that can change. * Changing code without recompiling it! * Specific requirements. * Artificial Intelligence (AI) in learning and dynamic generation of logic.

Disadvantages *Complex. You cannot use by only looking at the API. *Error prone. *Slow because classes, methods, and fields need to be loaded at runtime for introspection. *Security. *It does not update byte code.

Javassist *Unlike Reflection, Javassist API lets you manipulate and change byte code generated by Java compilers. *CtClass, CtMethod, and CtField. *Classes do not have to be loaded into JVM! *You can load using toClass() method in CtClass to load into JVM. *You can generate new classes and methods during runtime.

Javassist – Cont. 1. ClassPool pool = ClassPool.getDefault(); 2. CtClass pt = pool.get(“mypackage.PNLParser"); 3. pt.setSuperclass(pool.get(“ReportParser")); 4. CtMethod m = CtNewMethod.make("public void printType() { System.out.println(“PNL”); }", pt); 5. pt.addMethod(m); 6. pt.writeFile();