CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Written by: Dr. JJ Shepherd
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
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.
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.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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,
03G-1 Reflection In Java For every loaded class, the Java Runtime Environment (JRE) maintains an associated Class object The Class object “ reflects ”
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
1 Java object model Part 3: Serialization & Reflection.
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.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Advanced Java Programming Lecture 5 Reflection dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Java and C++, The Difference An introduction Unit - 00.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Topic 3 The Stack ADT.
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.
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.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
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.
The Reflection Lucielle Mendez Antonio Bologna.
JAVA BASICS Prepared by The Smartpath Information Systems
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
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.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
CS884 (Prasad)java11Extn1 Java 1.1 Extensions Nested classes static public/protected/private.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Reflection Bibliografie: Sun: The Java Tutorials – The Reflection API IBM developerWorks:
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
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.”
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
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.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
METADATA IN.NET Presented By Sukumar Manduva. INTRODUCTION  What is Metadata ? Metadata is a binary information which contains the complete description.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
5th MaCS Debrecen On the Turing-Completeness of Generative Metaprograms Zoltán Porkoláb, István Zólyomi Dept. of Programming.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Reflection SoftUni Team Technical Trainers Java OOP Advanced
NESTED CLASSES REFLECTION PROXIES.
Introspection the process by which a builder tool finds out which properties, methods, and events a bean supports. searching for classes and methods that.
Java Programming Language
CSE 413, Autumn 2002 Programming Languages
Reflection SoftUni Team Technical Trainers C# OOP Advanced
Reflection SoftUni Team Technical Trainers C# OOP Advanced
Presentation on Object Oriented programming Topic
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
Advanced Programming in Java
Java Programming Language
Computer Programming with JAVA
CSCE 314: Programming Languages Dr. Dylan Shell
Presentation transcript:

CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1

Lee CSCE 314 TAMU Reflection and Metaprogramming Metaprogramming: Writing (meta)programs that represent and manipulate other programs Reflection: Writing (meta)programs that represent and manipulate themselves Reflection is metaprogramming 2

Lee CSCE 314 TAMU Uses of Reflection Configuring programs for specific deployment environment Writing debuggers, class browsers, GUI tools,... Optimizing programs for specific inputs Analyzing running systems Extending running systems Of course, compilers, interpreters, program generators, even macros, are examples of metaprograms 3

Lee CSCE 314 TAMU Definitions of Reflection General: Reflection: An entity’s integral ability to represent, operate on, and otherwise deal with its self in the same way that it represents, operates on, and deals with its primary subject matter. For computer programs: Reflection is the ability of a program to manipulate as data something representing the state of the program during its own execution. There are two aspects of such manipulation: introspection and intercession. Introspection is the ability of a program to observe and therefore reason about its own state. Intercession is the ability of a program to modify its own execution state or alter its own interpretation or meaning. Both aspects require a mechanism for encoding execution state as data; providing such an encoding is called reification. 4

Lee CSCE 314 TAMU Language Support for Reflection Metaobjects: Objects that represent methods, execution stacks, processor Extreme: Dynamic languages such as Smalltalk and CLOS Language definitions are largely just libraries Smalltalk: Classes are represented as objects, can be manipulated at run time Class objects are objects as well, and thus can be manipulated, for example, to modify the properties of Smalltalk’s object model Regular Smalltalk code can access and modify metaobjects Intermediate: Java, C# Java Reflection API: discover methods and attributes at runtime, create objects of classes whose names discovered only at run time Mostly for introspection, no runtime modification of class’s metaobjects Low: C++ Runtime type identification (RTTI), simple introspection 5

Lee CSCE 314 TAMU Why some tasks are delayed until runtime (and why reflection is useful) The later in the application’s life cycle, the more information is available for adapting the program Compile time Linkin g Loadin g Runtim e Postruntime Static information Execution history 6

Lee CSCE 314 TAMU Java Reflection API Java’s reflection capabilities are offered as a library API, though of course supported by the language java.lang.Class java.lang.reflect Capabilities: Knowing a name of a class, load a class file into memory at runtime examine methods, fields, constructors, annotations, etc.; invoke constructors or methods; and access fields. Create instances of interfaces dynamically (special API for creating “proxy” classes) Large API, we touch just a few points 7

Lee CSCE 314 TAMU Obtaining a class object The entry point to reflection operations: java.lang.Class Various means to obtain an object of Class type 1.From instance (using Object.getClass()): MyClass mc = new Myclass(); Class c = mc.getClass(); 2.From type: c = boolean.class; 3.By name: Class cls = null; try { cls = Class.forName("MyClass"); } catch (ClassNotFoundException e) {... } // use cls 8

Lee CSCE 314 TAMU Obtaining other classes from a Class object Class.getSuperclass() Class.getClasses() returns all public class, interface, enum members as an array. Includes inherited members. Example: Class [] c = Character.class.getClasses(); Class.DeclaredClasses() similar to getClasses() but includes non-public classes and does not recur to base classes Class.getEnclosingClass() immediately enclosing class 9

Lee CSCE 314 TAMU Class Modifiers Java’s class modifiers public, protected, and private abstract static final strictfp Annotations Example: class public static MyClass {... }... int mods = MyClass.class.getModifiers(); if (Modifier.isPublic(mods)) System.out.println("public class"); 10

Lee CSCE 314 TAMU Accessing Members Same set of methods for fields, methods, and constructors. Let X be one of Field, Method, or Constructor: X Class.getDeclaredX(String) X Class.getX(String) X[] Class.getDeclaredXs() X[] Class.getXs() “Declared” versions obtain private members too “non-Declared” versions obtain inherited members Method and constructor versions need the parameter types as parameters too: Method getDeclaredMethod(String name, Class... parameterTypes) throws NoSuchMethodException, SecurityException 11

Lee CSCE 314 TAMU Example 1 Class c = Class.forName(className); System.out.println(c + " {"); int mods; Field fields[] = c.getDeclaredFields(); for (Field f : fields) { if (!Modifier.isPrivate(f.getModifiers()) && !Modifier.isProtected(f.getModifiers())) System.out.println("\t" + f); } Constructor[] constructors = c.getConstructors(); for (Constructor con : constructors) { System.out.println("\t" + con); } Method methods[] = c.getDeclaredMethods(); for (Method m : methods) { if (!Modifier.isPrivate(m.getModifiers())) { System.out.println("\t" + m); } } System.out.println("}"); [Ian Darwin: Java’s Reflection API, 2007] 12

Lee CSCE 314 TAMU Example 2. Dynamic Method Invocation // Let this be a method of object x public void work(int i, String s) { System.out.printf("Called: i=%d, s=%s%n", i, s); } Class clX = x.getClass(); // To find a method, need array of matching Class types. Class[] argTypes = { int.class, String.class }; // Find a Method object for the given method. Method worker = clX.getMethod("work", argTypes); // To invoke the method, need the invocation arguments, as an Object array. Object[] theData = { 42, "Chocolate Chips" }; // The last step: invoke the method. worker.invoke(x, theData); [Ian Darwin: Java’s Reflection API, 2007] 13

Lee CSCE 314 TAMU Notes about Reflection Reflection can break (intentionally) abstraction boundaries set by type system. For example: Reflection allows access to private fields and methods - The reflection API has means to set security policies to control this It allows attempts to calls to methods that do not exist Reflection incurs a performance overhead, because more checking needs to occur at run-time 14