EPITA Première Année Cycle Ingénieur. Atelier Java - J3

Similar documents
EPITA Première Année Cycle Ingénieur. Atelier Java - J3

EPITA Première Année Cycle Ingénieur. Atelier Java - J3

EPITA Première Année Cycle Ingénieur. Atelier Java - J3

JAVA. Reflection API. Java, summer semester

Reflection. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 28

Lecture 9 : Basics of Reflection in Java

JAVA.LANG.CLASS CLASS

Advanced programming for Java platform. Introduction

Introduction to Reflection

Introflection. Dave Landers BEA Systems, Inc.

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: reflection

Reflection. Based on

INTROSPECTION. We need to begin with a more basic concept called type introspection

Software-Architecture Annotations, Reflection and Frameworks

Programming Language Concepts: Lecture 10

Some @SuppressWarnings

Introspec*on and reflexivity

OOP Reflection. Kasper Østerbye Mette Jaquet Carsten Schuermann. IT University Copenhagen

CS5233 Components Models and Engineering

Ira R. Forman Nate Forman

Concepts of Object-Oriented Programming Peter Müller

Java Object Model. Or, way down the rabbit hole

Reflection (in fact, Java introspection)

RTTI and Reflection. Run-Time Type Information. Overview. David Talby. Advantages of Static Typing. Typing. Advantages II.

LINGI2252 PROF. KIM MENS REFLECTION (IN JAVA)*

Dynamic Generated Adapters from Adaptive Object Models to Static APIs

CSC Java Programming, Fall Java Data Types and Control Constructs

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Programming Language Concepts: Lecture 9

Java reflection. alberto ferrari university of parma

Pieter van den Hombergh Thijs Dorssers Richard van den Ham. May 17, 2018

Background. Reflection. The Class Class. How Objects Work

interface MyAnno interface str( ) val( )

Java Reflection. adapted from original slides by Tim Lethbridge University of Ottawa. Examining Classes. What is Reflection?

Material Java type system Reflection

CSCE 314 Programming Languages

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

An overview of Java, Data types and variables

Java Security. Compiler. Compiler. Hardware. Interpreter. The virtual machine principle: Abstract Machine Code. Source Code

public static boolean isoutside(int min, int max, int value)

Preview from Notesale.co.uk Page 13 of 72

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Java Reflection

Introduction to Java

1 Shyam sir JAVA Notes

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Getting started with Java

CSC 1214: Object-Oriented Programming

Explained Simply. CiaranMcHale.com

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016

3. Java - Language Constructs I

Java Programming. Atul Prakash

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Java Class Loading and Bytecode Verification

Java Classes, Inheritance, and Interfaces

Polymorphism. return a.doublevalue() + b.doublevalue();

Certification In Java Language Course Course Content

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

What is Serialization?

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Class, Variable, Constructor, Object, Method Questions

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

CSC 1351 The Twelve Hour Exam From Hell

Rules and syntax for inheritance. The boring stuff

Atelier Java - J2. Marwan Burelle. EPITA Première Année Cycle Ingénieur.


Outline. 1 Compilers. 2 Why Study Compilers? 3 The Phases of Compilation. 4 Overview of the j-- to JVM Compiler. 5 The j-- Compiler Source Tree 2 / 29

Preview from Notesale.co.uk Page 9 of 108

CompSci 125 Lecture 02

Java Persistence API (JPA) Entities

Coverage of Part 2. A Brief Introduction to Java for C++ Programmers: Part 2. import: using packages

CN208 Introduction to Computer Programming

University of Palestine. Mid Exam Total Grade: 100

Declarations and Access Control SCJP tips

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

Emulating the Java ME Platform on Java SE

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

Majustic - Evaluation Test With Answers. Time: 1 hour 30 minutes

JAVA WRAPPER CLASSES

Comp Assignment 3: Extendible and Multi-Platform Object (De)Serialization in GIPC Date Assigned: October 8, 2015

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Definition of DJ (Diminished Java)

Java Code Coverage Mechanics. by Evgeny Mandrikov at EclipseCon Europe 2017

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Compaq Interview Questions And Answers

Java Code Coverage Mechanics Evgeny Mandrikov Marc Hoffmann #JokerConf 2017, Saint-Petersburg

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

type conversion polymorphism (intro only) Class class

Programming Languages and Techniques (CIS120)

Timing for Interfaces and Abstract Classes

Kurt Schmidt. November 13, 2017

JAVA. Note about the Reflection API

Course Status Polymorphism Containers Exceptions Midterm Review. CS Java. Introduction to Java. Andy Mroczkowski

COE318 Lecture Notes Week 8 (Oct 24, 2011)

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing

public static void negate2(list<integer> t)

Transcription:

EPITA Première Année Cycle Ingénieur marwan.burelle@lse.epita.fr http://www.lse.epita.fr

Overview 1 Imagination From 2

Plan 1 Imagination From

Plan 1 Imagination From

What is Reflection is the process by which a computer program can observe and modify its own structure and behavior. Reflective language should be able of introspection Using reflective language, we should be able to call functions, methods or whatever, without direct access (for example, be able to call a function using a string representing its name.) Example: public static <T> void inspect(t t){ System. out. println(t. getclass(). getname ()); public static <T> Object call(t t, String fun){ Method m = t. getclass(). getmethod( fun); return (m. invoke(t));

Why /Unserialization Auto-adaptative programs Metaprogrammation Calling entity dynamicaly by their name Example: public static <T> Object call(t t, String fun, Class <? >[] paramtypes, Object[] params) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method m = t. getclass(). getmethod(fun, paramtypes); return (m. invoke(t, params));

Plan 1 Imagination From

The getclass Method Example: Class c = "foo".getclass(); System. out. println(c); class java.lang.string Example: public class MyReflectClass { public void autoinspect (){ System. out. println( this. getclass ()); public static void main( String[] args){ MyReflectClass o = new MyReflectClass (); o. autoinspect (); class MyReflectClass

From Class Example: // Accessing Class object from a class Class c = MyReflectClass. class; // Or from an atomic type Class boolclass = boolean. class; System. out. println(c); System. out. println( boolclass); class MyReflectClass boolean The.class notation is the only way to acces the class of a native type (you can t dereference a variable of native type.) You can also get the class of a native type from the wrapping class: System. out. println( Boolean. TYPE); boolean

From its Name If we have the fully-qualified name we can get the corresponding Class object: Example: Class sc = Class. forname(" java. lang. String"); System. out. println( String. class == sc); true Example: public Foo { static Object getobjfromname( String classname) { return ( Class. forname( classname). newinstance())

Other Ways of es Some methods from java.lang.class: public Class <? super T> getsuperclass (); public Class <? >[] getclasses (); public Class <? >[] getdeclaredclasses (); public Class <?> getenclosingclass (); We also have the method getdeclaringclass() which can be called from Class or Fields or...

Getting Modifiers The following method of the class Class retrieve modifiers of the class: public int getmodifiers() The class java.lang.reflect.modifier offers some methods to decode the integer encoding modifiers. Modifier for classes are: public, protected, private, final, static, abstract and interface

ClassSpy: A Class Inspector ClassSpy goals is to extract all informations from a given class; We will use most of the reflection API on class; It will introduce us with Member API; This example is directly extract from the Java Tutorial: http://java.sun.com/docs/books/tutorial/ reflect/class/classmembers.html

ClassSpy: preamble Example: import java. lang. reflect. Constructor; import java. lang. reflect. Field; import java. lang. reflect. Method; import java. lang. reflect. Member; import static java. lang. System. out; enum ClassMember{ CONSTRUCTOR,FIELD, METHOD,CLASS, ALL

ClassSpy: printclass Example: public class ClassSpy { private static Class <?> printclass( String cname) throws ClassNotFoundException { Class <?> c = Class. forname( cname); out. format(" Class:%n %s%n%n",c. getcanonicalname ()); Package p = c. getpackage (); out. format(" Package:%n %s%n%n", (p!= null? p.getname() : "-- No Package --")); return c;

ClassSpy: printclasses Example: private static void printclasses(class <?> c) { out.format("classes:%n"); Class <? >[] clss = c. getclasses (); for (Class <?> cls : clss) out. format(" %s%n", cls. getcanonicalname ()); if ( clss. length == 0) out. format( "--No member interfaces, classes,or enums --%n"); out.format("%n");

ClassSpy: printmembers Example: private static void printmembers( Member[] mbrs, String s) { out.format("%s:%n", s); for ( Member mbr : mbrs) { if ( mbr instanceof Field) out.format(" %s%n", (( Field) mbr). togenericstring ()); else if ( mbr instanceof Constructor) out.format(" %s%n", (( Constructor) mbr). togenericstring ()); else if ( mbr instanceof Method) out.format(" %s%n", (( Method) mbr). togenericstring ()); if ( mbrs. length == 0) out.format(" -- No %s --%n", s); out.format("%n");

ClassSpy: main Example: public static void main( String... args) { try { Class <?> c = printclass( args[0]); for ( int i = 1; i < args. length; i++) { switch ( ClassMember. valueof( args[i])) { case ALL: printmembers(c. getconstructors()," Constuctors"); printmembers(c. getfields(), " Fields"); printmembers(c. getmethods(), " Methods"); printclasses(c); break; default: assert false; catch ( ClassNotFoundException x) { x. printstacktrace ();

ClassSpy : result > java ClassSpy HelloWorld ALL Class: HelloWorld Package: -- No Package -- Constuctors: public HelloWorld() Fields: -- No Fields -- Methods: public static void HelloWorld.main(java.lang.String[]) public final native void java.lang.object.wait(long) throws java.lang.interruptedexception public final void java.lang.object.wait(long,int) throws java.lang.interruptedexception public final void java.lang.object.wait() throws java.lang.interruptedexception public native int java.lang.object.hashcode() public final native java.lang.class<?> java.lang.object.getclass() public boolean java.lang.object.equals(java.lang.object) public java.lang.string java.lang.object.tostring() public final native void java.lang.object.notify() public final native void java.lang.object.notifyall() Classes: -- No member interfaces, classes, or enums --

Plan 1 Imagination From

How to access the inner content Member Class API List Inherited Private Field getdeclaredfield no no yes getfield no yes no getdeclaredfields yes no yes getfields yes yes no Method getdeclaredmethod no no yes getmethod no yes no getdeclaredmethods yes no yes getmethods yes yes no Constructor getdeclaredconstructor no - yes getconstructor no - no getdeclaredconstructors yes - yes getconstructors yes - no

Member API To represent members of a class we have a generic interface java.lang.reflect.member; java.lang.reflect.field: implements Member for properties; java.lang.reflect.method: implements Member for methods; java.lang.reflect.constructor: implements Member for constructors; Each implementation has some specific operations such as setting a fields or invoking a method.

Field class Field: public Object get( Object obj) throws IllegalArgumentException, IllegalAccessException public <T extends Annotation > T getannotation(class <T> annotationclass) public Type getgenerictype() public Class <?> gettype() public void set( Object obj, Object value) throws IllegalArgumentException, IllegalAccessException

Method class Method: public Class <? >[] getexceptiontypes() public Type[] getgenericexceptiontypes() public Type[] getgenericparametertypes() public Class <? >[] getparametertypes() public Type getgenericreturntype() public Class <?> getreturntype() public TypeVariable <Method >[] gettypeparameters() public Object invoke( Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException

Constructor class Constructor <T>: public T newinstance( Object... initargs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException public boolean isvarargs()

Another Use Case import java.lang.reflect.*; public class View { private Object engine; public void addengine(object e) { engine = e; private Object enginegetres() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method getres = engine.getclass().getmethod("getres"); return (getres.invoke(engine)); public void start() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { System.out.println("Welcome"); System.out.println("Res: "+ this.enginegetres ());

Another Use Case import java.lang.reflect.*; public class Engine { private Object view; public void addview(object v) { view = v; ; private int fact(int i, int accu) { return (i>0?fact(i-1,accu*i):accu); public Integer getres() { return (new Integer(fact(5,1))); public void startup() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method s = view.getclass().getmethod("start"); s.invoke(view); public static void main(string[] args) { Engine engine = new Engine(); try { View view = new View(); view.addengine(engine); engine.addview(view); engine.startup(); catch // [snip error handling here]

Plan 2

Serialize: translating an instantiated object into a form adapted for transmitting the object; Unserialize: retrieving an object from its serialized form; Many use cases: Simple, easy but efficient data persistance Object exchange with other applications RPC...

What do we need? In order to be serialized, an object need a notion of state: the values of its fields It must some how be able to include code into the result Unserializing implies creating the object (a new instance) Unserializing implies restoring the state of the object Somehow retrieving information on the real type of the object

What is available? Interface Serializable: binary simple serialization (beware of non-serializable ancestors... ) Interface Externalizable: minimal serialization, implies the definition of saving and restoring into the class itself XStream: a side project offering XML serialization Reflection offers good tools for homemade serialization.