Loadtime Structural Reflection in Java - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Loadtime Structural Reflection in Java

Description:

Class Calendar implements Printable { public void write(PrintStream s) ... { interfaces[i] = CtClass.forName('Printable'); c.setInterfaces(interfaces) ... – PowerPoint PPT presentation

Number of Views:111
Avg rating:3.0/5.0
Slides: 22
Provided by: salmosa
Category:

less

Transcript and Presenter's Notes

Title: Loadtime Structural Reflection in Java


1
Load-time Structural Reflection in Java
  • Shigeru Chiba
  • in Proceedings of ECOOP'2000, 2000,
    SpringerVerlag LNCS 1850
  • 2002/11/12 Kim, Sang-Woon

2
Contents
  • Introduction Reflection
  • Reflection techniques
  • Introspection
  • Behavioral reflection
  • Structural reflection
  • Load-time in Java
  • Javassist
  • APIs
  • Examples
  • Related works
  • Reflection view
  • Performance view
  • Conclusion

3
Introduction
  • Reflection
  • A technique for changing the program behavior
    according to another program
  • To gather information about its own execution
    environment
  • To modify this environment
  • Major categories
  • Introspection
  • Behavioral reflection
  • Structural reflection

4
Reflection in Java
  • Java is the only programming language supporting
    reflection
  • Java Reflection API mainly introspects data
    structure such as classes
  • Examining classes
  • Determine the class of an object
  • Get information about the classes and interfaces
  • Manipulating objects
  • Instantiate a class
  • Get/set a field value
  • Invoke a method

5
Example Invoke a method
String concatnation new
String(firstWord) concatnation.concat(secondWo
rd) ...
Method concatMethod Class c
String.class Class parameterTypes new
Class String.class Object arguments
new Object secondWord try
concatMethod c.getMethod("concat",
parameterTypes) concatMethod.invoke(firstW
ord, arguments) catch (NoSuchMethodExcepti
on e) ...
6
Behavioral reflection
  • Alters behavior such as method call, field
    access, and object creation
  • Extends Java Reflection API
  • In the SE viewpoint, can support High-level
    abstraction with good modularity

7
Behavioral reflection approach
  • Common behavioral reflection process
  • Intercepting the method
  • Altering the behavior of the method
  • Using metaobject protocol
  • Implementing a metaobject
  • Metaobject executes the intercepted operation
    with customized semantics
  • Connecting the method to metaobjects methods
  • intercepting a method call and controlling the
    methods behavior in runtime
  • Inserting a hook calling metaobjects methods to
    the method in original file in compile time

8
Structural reflection
  • Alters definitions of data structure, statically
    fixed at compile time
  • Add/delete
  • Renames
  • In the implementation viewpoints,
  • Effectiveness of dynamic recompilation
  • Depends on the implementation way
  • Correctness of types
  • Needs to an extra runtime type checker
  • Requires a great deal of efforts
  • Existing architectures for this type of
    reflection are inefficient

9
Load-time in Java
  • Bytecodes in a class file are executed by loading
    on the classloader
  • loaded bytecodes are not changed on the execution
  • Structural reflection performs before a program
    is loaded into a runtime system

Import String Class A()
Classloader
Source Code
String
A
compile
execute
Machine
Byte Codes
10
Javassist
  • A new architecture for structural reflection
  • Does not modify an existing runtime or a compiler
  • Reflects the object only at load time
  • How to perform structural reflection
  • By translating alterations into an equivalent
    bytecode transformation of the class files
  • By verifiers checking on JVM
  • Design goals
  • Providing source level abstraction
  • Executing as efficiently as possible
  • Performing reflection in a safe manner in terms
    of types

11
How to use Javassist?
  • Operation sequence
  • Reification step Creating a CtClass (Compile
    time Class) object representing the bytecodes
  • Modification step Introspecting and altering
    the class definition
  • Translation step Obtaining the bytecodes for
    loading the altered class into the JVM
  • Reflection step Loading the obtained bytecodes
    into the JVM

CtClass c new CtClass(stream) //alters cs
behavior or data structure Byte bytecode
c.toBytecode() //loads on the JVM
12
Methods for introspection
  • Compatible with the Java Reflection API except
    for creating an instance or invoking a method
  • Defined in CtClass, CtMathod, and CtField

13
Methods for alteration
  • Make Javassist different from Java reflection API
  • changing class modifiers
  • changing class hierarchy
  • adding a new member

14
Examples(1/2)
  • BCA( binary code adaptation)

Class Calendar implements Writable public
void write(PrintStream s)
delta class implements Writable rename
Writable Printable add public void print()
write(System.out)
Class Calendar implements Printable public
void write(PrintStream s) public void
print() write(System.out)
15
BCA program using Javassist
  • Class Adaptor
  • public void adapt(CtClass c)
  • CtMethod printM / method print() in
    Calender /
  • CtClass interfaces c.getInterfaces()
  • for (int i 0 i lt interfaces.length i)
  • if(interfacesi.getName().equals(Writable
    ))
  • interfacesi CtClass.forName(Printable
    )
  • c.setInterfaces(interfaces)
  • c.addMethod(printM, new ClassMap())
  • return

preparing
collecting
finding
renaming
setting
adding
16
Examples(2/2)
  • Behavioral reflection

public class C public int m(int x) return x
f public int f
Public class MyMetaobject extends Metaobject
public Object trapMethodcall() / called
if a method call is intercepted / public
Object trapFieldRead() / called if the
value of a field is read / public Object
trapFieldWrite() / called if a field is
set /
17
Behavioral reflection using Javassist
  • 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 /

original code
for accessing field f
18
Related works Reflection view
  • From Dalang to Kava - The evolution of
    reflective Java Extension, in Proc. of
    Reflection 99, by Welch, I. and R. Stroud
  • Supporting behavioral reflection
  • By translating bytecodes to bytecodes at
    load-time
  • Inserted only hooks for dynamic reflection
  • Suitable for implementing different kinds of
    language extensions
  • Covered by the classloader of Javassist
  • Linguistic Reflection in Java in SPE, 98, by
    Kirby et al
  • Supporting structural reflection
  • defining a new class dynamically
  • Without altering a given class definition
  • Producing a source code of a new class

19
Related works - Performance view
  • OpenJava
  • Performs structural reflection at compile time
  • By translating original source code to modified
    source code

20
Reflective Java Implementations
21
Conclusion
  • Presented an extension to the Java reflection
    API, Javassist
  • Allows a program to alter a given class
    definition and to define a new class dynamically
  • Performs by instrumenting bytecode of a loaded
    class
  • Emphasizes three features
  • Portability, Source-level abstraction, and
    Independency of source code
  • Future work
  • Applying to other object oriented languages
Write a Comment
User Comments (0)
About PowerShow.com