Type-safe Implementation of Java ™ Reflection Nadeem Abdul Hamid February 27, 2001 Yale University Advisor: Prof. Zhong Shao (Work in progress)

Slides:



Advertisements
Similar presentations
Using Java™ Technology Reflection to Improve Design
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
pa 1 Porting BETA to ROTOR ROTOR Projects Presentation Day, June by Peter Andersen.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Introduction To Java Objectives For Today â Introduction To Java â The Java Platform & The (JVM) Java Virtual Machine â Core Java (API) Application Programming.
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.
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.
Lab#1 (14/3/1431h) Introduction To java programming cs425
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
Introduction to Java Programming
1 Java Reflection. 2 Java looking at Java l One of the unusual capabilities of Java is that a program can examine itself »You can determine the class.
1 Java (vs. C++). 2 Object-oriented Programming Java and C++ are the most popular object-oriented programming languages Java Sun Microsystems in 1990.
Reflection (Reflexion in British English ) Mo Alkhafaji.
Programming in Scala Chapter 1. Scala: both object-oriented and functional Scala blends –object-oriented and –functional programming in a –statically.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
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.
Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
OOP Languages: Java vs C++
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Java Security. Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security Manager.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Advanced Java Programming Lecture 5 Reflection dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
1 The Java Virtual Machine Yearly Programming Project.
Java Introduction Lecture 1. Java Powerful, object-oriented language Free SDK and many resources at
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.
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.
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.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Java Dynamic Proxy Bibliography: reflection/proxy.html
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Classes, Interfaces and Packages
Jun 14, 2004RAM-SE'04 Workshop, Oslo, Norway 1 Negligent Class Loaders for Software Evolution Yoshiki Sato, Shigeru Chiba (Tokyo Institute of Technology.
CSE 331 Reflection slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.”
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Chapter 7: Cloning and RTTI
NESTED CLASSES REFLECTION PROXIES.
Internet and Java Foundations, Programming and Practice
CSE 413, Autumn 2002 Programming Languages
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
Generics, Lambdas, Reflections
Presentation on Object Oriented programming Topic
Java Programming Language
Generics, Lambdas, Reflections
Some Examples Reflection is used for many things:
CSCE 314: Programming Languages Dr. Dylan Shell
Reflection in Java By: Zachary Cava.
Presentation transcript:

Type-safe Implementation of Java ™ Reflection Nadeem Abdul Hamid February 27, 2001 Yale University Advisor: Prof. Zhong Shao (Work in progress)

2 Outline Introduction Java Reflection Implementing Java Reflection Implementing Java Reflection Safely Java Reflection Using ITA (Intensional Type Analysis) Conclusions and Further Work

3 Java Mobile Code Platform Java Bytecode: Platform independent mobile code Verifiable for safety/security properties Problem: Large Trusted Computing Base (TCB) Runtime System Interpreter Verifier Class Library Introduction

4 A More Principled, Flexible Platform FLINT IL Low-level Code Sophisticated Type System = Safety/Security Fast Type Checking Precise Semantics Multiple Source Languages Small TCB Java Class Library Introduction

5 Java-FLINT Current Results Support for large subset of Java: classes, inheritance, interfaces, privacy, mutual recursion, dynamic cast, constructors, super, static,... [League, Shao, Trifonov ’99, ’01] Introduction

6... Reflection? Constraints Adhere to Java Specification Straightforward, efficient (?) encoding Similar to current implementations, but,... Entirely type checked Introduction

7 Java Warm-up class Pt extends Object { public int x; Pt(int newx) { this.x = newx; } public void bump(Pt y) { this.x = y.x; } } main() { Pt p1 = new Pt(1), p2 = new Pt(2); p1.bump(p2); p2.x = p1.x * 2; } Introduction

8 Outline Introduction Java Reflection Implementing Java Reflection Implementing Java Reflection Safely Java Reflection Using ITA (Intensional Type Analysis) Conclusions and Further Work

9 What is Reflection? “Reflection is the ability of a program to manipulate as data something representing the state of the program during its own execution.” [Demers and Malenfant] Reification = “Encoding execution state as data” Reflective Power 1. Introspection 2. Behavioral Reflection 3. Structural Reflection Java Reflection Mostly (1) Java Reflection

10 Who Uses Java Reflection? JavaBeans (component architectures) Database applications Serialization Mobile objects Scripting applications Runtime Debugging/Inspection Tools Jalapeno (IBM) – remote VM debugging tool Frappé (Antony) Java Reflection

11 Why Use Reflection? Dynamically loaded classes Convenience Efficiency Java Reflection

12 How To Use Java Reflection Java Reflection class Pt extends Object { int x; Pt(int newx) { this.x = newx; } void bump(Pt y) { this.x = y.x; } } main() { Class c = getClass(“Pt”); Field f = c.getField(“x”); Method m = c.getMethod(“bump”); Object p = c.newInstance( [3] ); // p = new Pt(3); f.get( p ); // p.x; m.invoke( p, [ p ] ); // p.bump(p); }

13 Another Example Java Reflection main() { Class c = getClass(“Pt”); Field f = c.getField(0); Object obj = ; if (c.isInstanceOf( obj ) { print “It’s a point!”; print “Value: “ + f.get(obj); } }

14 Reflection API Interface Java Reflection class Class extends AccessibleObject { static Class forName(String name); Object newInstance(); Field getField(String name); Method getMethod(String name); boolean isInstance(Object obj); getName(), getInterfaces(), getSuperclass(), getModifiers(), getFields(), getMethods() } class Field extends AccessibleObject { Object get(Object obj); void set(Object obj, Object val); getType(), getDeclaringClass(),... }

15 Reflection API Interface (cont.) Java Reflection class Method extends AccessibleObject { Object invoke(Object obj, Object[] args); getReturnType(), getParameterTypes(), getExceptionTypes(), getDeclaringClass(),... } class Constructor; class AccessibleObject; class Array; class Proxy;...

16 Subtleties Security Access Control Overriding Inheritance Arrays Primitive Types Class Initialization Inner & Anonymous Classes Java Reflection

17 Reflection API Summary Representations for Class, Field, Method check class of an object construct new class instances access and modify field values of an object access and invoke methods of a class Java Reflection

18 Outline Introduction Java Reflection Implementing Java Reflection Implementing Java Reflection Safely Java Reflection Using ITA (Intensional Type Analysis) Conclusions and Further Work

19 Java VM/Compiler Survey Sun J2SE (Java 1.3) Kaffe VM Jalapeno (IBM) OpenJIT (Japan) JavaInJava (Sun 1998) BulletTrain Rivet (MIT) Marmot (Microsoft) Classpath (GNU) Implementing Java Reflection

20 At Runtime Implementing Java Reflection class Pt {Object x; Pt bump(Pt); } Libraries: Object, Class, Field,... JVM Class “Object” fields = / Object vtab class field1... Class “Class”... Class “Field”... Class “Pt” fields Field “x” clazz type offset = 16 Pt x = new Pt(3) ...

21 At Runtime: Methods Implementing Java Reflection class Pt {Object x; Pt bump(Pt); } Libraries: Object, Class, Field,... JVM Object vtab class field new Pt(3) ... Pt vtab class x =... bump... Method “bump” clazz args/rettype code ptr Class “Pt” fields methods...

22 Class and Field Implementation Implementing Java Reflection class Field { String name; Class type; Class clazz; int offset; Object get(Object obj) { if (clazz.isInstance(obj)) f = ((char*)obj) + offset; return (Object)f; } } class Class { String name; Field[] fields; Method[] methods; boolean primitive; bool isInstance... Object newInstance.. }

23 Method Implementation Implementing Java Reflection class Method { String name; Class clazz; CodePtr* code; Class[] argtypes; Class rettype; Object invoke(Object obj, Object args[]) { if (clazz.isInstance(obj)) foreach args[i] CHECK argtypes[i].isInstance(args[i]) return (Object)retvalue; }

24 Primitive Types class Class {... boolean primitive; } class Field { String name; Class type; Class clazz; int offset; Object get(Object obj) { if (clazz.isInstance(obj)) f = ((char*)obj) + offset; return (type.primitive = TRUE ? wrap(f) : (Object)f); } } new Integer( *(int*)f) int  class Integer; boolean  class Boolean; double  class Double;... Implementing Java Reflection

25 Why Native Code is Needed isInstance Implemented using some form of tags Separate topic: Dynamic loading Selecting arbitrary offset Not really arbitrary – checked to make sure object is valid Unrolling arguments and applying to method code Implementing Java Reflection

26 The Problem(s) Implementation is platform-specific passing arguments object layout Logic is straightforward but code is not type checked compiler is part of the TCB- what happens if it makes a mistake Implementing Java Reflection

27 Outline Introduction Java Reflection Implementing Java Reflection Implementing Java Reflection Safely Java Reflection Using ITA (Intensional Type Analysis) Conclusions and Further Work

28 First try: Pure Java Solution Class, Field, Method are abstract JVM loads a class file: Generates an instance of Class, and instances of Field First generates subclass of Class, subclasses of Field and then instances of those Subclasses generated by filling in templates Hard code checks/selection/invocation One subclass for each class, field, method Implementing Java Reflection Safely

29 Subclassing Field class Field_ _ extends Field { String name = “ ”; Class type = ; Class clazz = ; Object get(Object obj) { return ( ( ) obj ). ; } } Template: if (clazz.isInstance(obj)) f = *((char*)obj) + offset; return (Object)f; Implementing Java Reflection Safely

30 Filling in the template Point.getClass().getField(“x”)  (Field) new Field_Point_x(); class Point { public Integer x; } class Field_Point_x extends Field { String name = “x”; Object get(Object obj) { return ( (Point) obj ).x; } } Implementing Java Reflection Safely

31 Nice try, No native code Semantics of Java automatically enforced Independent of object-layout Not part of the TCB! Implementing Java Reflection Safely

32 but... No overriding privacy Consider, if x were a private field: ( (Point) obj ).x would not compile Maybe in FLINT? Slight specification revisions needed Too much overhead? Explosion in number of classes generated, not just class instances Implementing Java Reflection Safely

33 - Enter Intensional Type Analysis... [HM, CWM, SST]

34 Java Encoding (Intuitively) JAVAminiFLINT Objects new Point (x=3) Records Field Selection p.x (also for methods) Field Selection Methods class Pt { void m(int x1) {...} } Lambda terms Method Invocation p.m( x1 ) Self-application Implementing Java Reflection Using ITA

35 Claim Java + Reflection  miniFLINT + ITA Field selection: (p.x) : ? where x unknown at compile time Method selection & invocation: p.m( p, x1 ) : ? where m : ?, p : ?, x1 : ? Implementing Java Reflection Using ITA

36 FLINT Framework Object representation: Implementing Java Reflection Using ITA

37 Class Objects in FLINT Implementing Java Reflection Using ITA

38 newInstance Object newInstance(Object[] args); Implementing Java Reflection Using ITA

39 Field Selection Powerful record select based on first-class labels or offset where the record type maps offsets to types Implementing Java Reflection Using ITA

40 Recursive Types Complication: ObjTy[C] is a recursive type Representation of recursive types for ITA ? Implementing Java Reflection Using ITA

41 Summary: ITA Approach Dynamic cast Name equivalence Field/Method Selection Unrolling arguments and applying to code Using FLINT-based IL ( ) extended with ITA Implementing Java Reflection Using ITA

42 Outline Introduction Java Reflection Implementing Java Reflection Implementing Java Reflection Safely Java Reflection Using ITA (Intensional Type Analysis) Conclusions and Further Work

43 Further work Formalize reflection in Java Extend target language with ITA Representing recursive types Interaction with privacy/security Especially overriding access control TWELF encoding FLINT VM implementation

44 Conclusion Type-safe implementation of Java Reflection in FLINT-based IL Conforms to Java specification Straightforward, efficient* Captures logic of existing native implementations