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

Slides:



Advertisements
Similar presentations
AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Introduction To Java Objectives For Today â Introduction To Java â The Java Platform & The (JVM) Java Virtual Machine â Core Java (API) Application Programming.
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.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Supporting Unanticipated Dynamic Adaptation of Application Behaviour Barry Redmond and Vinny Cahill Distributed Systems Group Trinity College, Dublin.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Introduction to Java Programming
Principles of Object-Oriented Software Development The language Java.
Reflection (Reflexion in British English ) Mo Alkhafaji.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
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.
Institute of Computing Jaca: a Reflective Fault Injection Tool based on Patterns Eliane Martins Cecilia Rubira Nelson Leme Institute of Computing State.
Load Time Structural Reflection in Java Shigeru Chiba Institute of Information Science and Electronics University of Tsukuba.
Session-02. Objective In this session you will learn : What is Class Loader ? What is Byte Code Verifier? JIT & JAVA API Features of Java Java Environment.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
Introduction to Aspect Oriented Programming Presented By: Kotaiah Choudary. Ravipati M.Tech IInd Year. School of Info. Tech.
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.
Extensible Plug-ins for Aspect-Oriented Programming Macneil Shonle*Ankit Shah.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Composing Adaptive Software Authors Philip K. McKinley, Seyed Masoud Sadjadi, Eric P. Kasten, Betty H.C. Cheng Presented by Ana Rodriguez June 21, 2006.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
1 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java.
1 Cisco Unified Application Environment Developers Conference 2008© 2008 Cisco Systems, Inc. All rights reserved.Cisco Public Introduction to Etch Scott.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
Celluloid An interactive media sequencing language.
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.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Java FilesOops - Mistake Java lingoSyntax
Classes, Interfaces and Packages
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
SCoPE: an AspectJ Compiler for Supporting User-Defined Analysis-Based Pointcuts Tomoyuki Aotani Hidehiko Masuhara
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
RealTimeSystems Lab Jong-Koo, Lim
Software, IEE Proceedings, Vol.152, Num.3, June 2005,Page(s): Prasanthi.S March, Java-based component framework for dynamic reconfiguration.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Module Road Map Refactoring Why Refactoring? Examples
Inheritance and Polymorphism
Internet and Java Foundations, Programming and Practice
Rean Griffith‡, Gail Kaiser‡ Presented by Rean Griffith
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
null, true, and false are also reserved.
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Sampath Kumar S Assistant Professor, SECE
Units with – James tedder
Units with – James tedder
Inheritance Inheritance is a fundamental Object Oriented concept
VIRTUAL FUNCTIONS RITIKA SHARMA.
CSCE 314: Programming Languages Dr. Dylan Shell
Plug-In Architecture Pattern
Presentation transcript:

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

What is Reflection? If a program can inspect itself while running, it’s said that the runtime system is reflective with respect to that program.

Levels of Reflection Introspection. –No alteration, read only. Behavioral Reflection. –Limited Alteration possible, such as method invocation. –In general : behavior of operations is changeable. Structural Reflection. –The ability to change data structures used by the runtime system, such as Class for example.

Java, AspectJ, and JavAssist Java provides Introspection: –getName. AspectJ provides Behavioral Reflection: –Aspects + Point cuts are used. –Compile time Weaving process integrates behavioral changes to precompiled base code. Javassist provides Structural Reflection: –Javassist API is in Java. –Customized runtime loader changes preloaded byte code.

How is Javassist implemented? CtClass c = new CtClass(“X.class”); //There’s nothing like Class X in the JVM so far! c.notFinal(); c.addField(…) c.addMethod(…) //c contains the altered byte code of X. c.load(); //Altered X is finally loaded into the JVM. cX.class X’ My Loader

Design Goals of Javassist 1.Source Level Abstraction. No knowledge of byte code is needed. API is purely in Java. 2.Efficient Structural Reflection. Reduce runtime overheads due to class reconstruction. 3.Safe Structural Reflection. Protect programmers from defected byte code composition.

Pursuing Javassist Design Goals. 1.Source Level Abstraction. Class rename. 2.Efficient Structural Reflection. Runtime compilation with addMethod. 3.Safe Structural Reflection. Class member removal.

The Javassist API Introspection Alteration. Adding a new Member. Altering a Method Body. Javassist Class Loader. Class{ Fields Constructors Methods } CtClass{ CtFields CtMethods }

CtClass Introspection

CtField & CtMethod Introspection

Alteration

Example: addMethod public class Xvector extends java.util.vector{ public void add(X e){ super.addElement(e); } CtMethod m = // method add() in XVector CtClass c = // class StringVector Classmap = new ClassMap(); map.put(“X”, ”java.lang.String”); c.addMethod(m, “addString”, map); Public void addString(java.lang.String e){ super.addElement(e); }

Example: setBody CtMethod m = // method which body is to be changed CodeConverter conv = new CodeConverter(); Conv.replaceNew(Xclass, Yclass, “create” ); m.instrument(conv); M( … ){ … Xclass xclass = new Xclass(3,4); … } M( … ){ … Yclass.create(3,4); … } Must be Static

Reflective Class Loader JavAssist default loader. User defined loader. Web Server. Off line.

Examples: Binary Code Adaptation. class X implements Writable{ public void write(PrintableStream s){..} } class X implements Printable{ public void (PrintableStream s){..} public void print(){write(System.out);} }

Examples: Binary Code Adaptation. class Exemplar implements Printable { public void write(PrintStream s) { /* dummy */ } public void print() { write(System.out); } } class Adaptor { //  THE JOB IS DONE HERE public void adapt(CtClass c) { CtMethod printM = /* method print() in Exemplar */; CtClass[] interfaces = c.getInterfaces(); for (int i = 0; i < interfaces.length; ++i) if (interfaces[i].getName().equals("Writable")) { interfaces[i] = CtClass.forName("Printable"); c.setInterfaces(interfaces); c.addMethod(printM, new ClassMap()); return; }

Behavioral Reflection. public class MyMetaObject extends MetaObject { public Object trapMethodCall(String methodName, Object[] args) { /* called if a method call is intercepted. */ } public Object trapFieldRead(String fieldName) { /* called if the value of a field is read. */ } public void trapFieldWrite(String fieldName, Object value) { /* called if a field is set. */ } }

Behavioral Reflection. public class C{ public int f; public int m(int x){ return x+f;} } public class C implements MetaLevel { public int m(int x) { /* notify a MetaObject. */ } public int f; private MetaObject _MetaObject = new MyMetaObject(this); public MetaObject _getMetaObject() { return _MetaObject; } public int orig_m(int x) { return x + f; } public static int read_f(Object target) {/* notify a MetaObject. */ } public static void write_f(Object target, int value) { /* notify a MetaObject. */ } }

Behavioral Reflection. class Exemplar { private MetaObject _MetaObject; public Object trap(Object[] args, String methodName) { return _MetaObject.trapMethodcall(methodName, args); } public static Object trapRead(Object[] args, String name) { Metalevel target = (Metalevel)args[0]; return target._getMetaObject().trapFieldRead(name); } public static Object trapWrite(Object[] args, String name) { Metalevel target = (Metalevel)args[0]; Object value = args[1]; target._getMetaObject().trapFieldWrite(name, value); }

Related Work: Reflection in Java. MetaXa and Kava enable behavioral reflection in Java, whereas JavAssist enable structural reflection. JavAssist can be used to achieve behavioral reflection, such as with MetaXa and Kava. Kirby’s system, allows a class definition at load time. It doesn’t support class redefining.

Related Work:Compile-Time MetaObject Protocol. Another architecture enabling structural reflection without modifying an existing runtime system is OpenJava. OpenJava was designed mainly for off-line use at compile time.

Related Work:Compile-Time MetaObject Protocol. OpenJava is source-code based. OpenJava alterations are on source code level. OpenJava involves a performance overhead due to source code manipulations. On the other hand it results with a fine- grained source code.

Related Work:Compile-Time MetaObject Protocol. JavAssist is bytecode based and doesn’t need source code( which is not always available). No performance overhead: the code is already compiled, therefore JavAssist achieves shorter loading time. JavAssist runs 10 times faster than OpenJava.

Related Work: Bytecode Translators. JOIE and JavaClass API provide functionality similar to JavAssist. Using them, the programmer is required to have a deep understanding of the Java byte code. JavAssist provides source-level abstraction for manipulating byte code in a safe manner.

Related Work: Others. OpenJIT: allows a Java program to control how byte code is compiled into native code. It provides better flexibility with method body redefinition, but doesn’t allow to add a new method or field to the class.

Conclusions. JavAssist is an extension to the Java reflection API. It enables structural reflection in Java, allows to alter a given class definition and to dynamically define a new class. Language extensions are more easily implemented with structural reflection than with behavioral reflection.

Conclusions (Cont.) JavAssist is portable. JavAssist provides source-level abstraction for manipulating byte code in a safe manner, while byte code translators, such as JOIE and the JavaClass API, provide no higher-level abstraction. JavAssist processes byte code. OpenJava processes source code.

Conclusions (Cont.) The architecture of JavAssist can be applied to other object-oriented languages if a compiled binary program includes enough symbolic information to construct a class object. API must be individually designed for each language to allow class alteration definition in a safe and meaningful manner.