ECE 750 Topic 8 Meta-programming languages, systems, and applications Load-time structural reflection in Java – Shigeru Chiba, 2000 May 27, 2004 Shimin Li University of waterloo
Paper Overview
Contents: Overview Existing reflection facilities in Java - Standard Java Reflection API - Extensions to the reflection ability of Java Javassist – an intercession extension to Java - Implementation technique - Introduction to load-time structural reflection - Javassist API Three applications of Javassist
Contents: Detail Existing reflection facilities in Java - Standard Java Reflection API Restricted to introspection Behavior reflection is very limited - Extensions to Java Reflection API Enable restricted behavior reflection Modify runtime systems or compilers Use hook-insertion technique and metaobjects Runtime overheads can be low Limitation to some language extensions(e.g. BCA)
Contents: Detail Novel reflection architecture – Javassist - An extension to the Java reflection API Enables structural reflection in Java at load time - Is portable Runs with a standard JVM - Provides source-level abstraction for programmers Can be used without knowledge of the Java byte code - Never needs source code Performs structural reflection by instrumenting byte code of a loaded class
Contents: Detail Javassist API - Reification CtClass object and its reflective methods - Introspection Almost compatible with the Java reflection API - Alteration Three design goals and their achievements - Adding a new member Two approaches for adding a new method to a class
Contents: Detail Javassist API cont'd - Altering a method body - Reflective class loader Allows a loaded program to control the class loading - Three ways to use Javassist With a user class loader With a web server Off line
Contents: Detail Application examples of Javasssist - Binary Code Adaptation(BCA) - Behavioral reflection - Remote method invocation Generating stub code for RMI
Related Work Reflection in Java - Behavioral reflection examples: MetaXa, Kava, Java reflection API - Structural reflection example: Linguistic reflection Compile-time metaobject protocol - Source-code basis example: OpenJava Byte code translator - Examples: JOIE, JavaClass API Others - OpenJIT, CLOS MOP, BCA
Contributions and Novelties Architecture for performing structural refection in Java based on byte code - No need for source code - Used with a standard JVM and Java compiler - Source-level abstraction Practical structural reflection API Three application examples - Show implementing some language extensions with Javassist is much easier than other reflection architectures
What I like Javassist does not break security guarantees given by Java Javassist is easy to used - Without knowledge of the Java byte code - Can be used with a user class loader, web server, and offline Javassist architecture can be applied to other object-oriented languages - as long as the compiled binary includes enough symbolic information to construct a class object
Questions
Question 1 What is structural reflection?
Answer Structural reflection is the ability to allow a program to alter the definitions of data structures such as classes and methods.
Question 2 Some languages(e.g. Smalltalk,CLOS) implement structural reflection with support mechanisms embedded in runtime systems. What problems would the application of this implementation technique to Java cause?
Answer Invalidation of portability of Java due to modification of JVM Performance degradation of JVM due to difficulty to employ optimization at runtime Extra runtime type checks or restrictions on the structural reflection Less security
Question 3 What is load-time structural reflection?
Answer Structural reflection is performed only before a program is loaded into a runtime system, that is, at load time.
Question 4 How does Javassist perform structural reflection?
Answer Javassist implementation based on load- time structural reflection architecture Javassist performs structural reflection by translating alteration into equivalent bytecode transformation of the class files After the transformation, the modified class files are loaded into the JVM and then no alteration are allowed after that
Question 5 How does Javassist reify the class needed to be reflected?
Answer Javassist creates a CtClass(compile-time class) object CtClass represents the byte code of the class need to be reflected Then the class is accessible from a program by invocating the methods on CtClass object
Question 6 How is the introspecting facilities of Javassist?
Answer The introspection part of Javassist API is compatible with the Java reflection API But, Javassist does not provide methods for creating an instance or invoking a method
Question 7 What is the main features of the alteration methods in Javassist API?
Answer Provides source-level abstraction for users - Approach to specify a method body is in source level Executes structural reflection efficiently - Copy pre-compiled method body from a class to another Performs structural reflection in a safe manner in terms of types - No methods to remove a method or a field from a class - New superclass must be a subclass of the original superclass - No methods to change the parameters of a method
Question 8 What is the difference between the class loader provided by Javassist and the Java’s standard class loader?
Answer The Javassist class loader allows a loaded program to control the class loading (see Figure 1) - If a program is loaded by Javassist’s class loader L and it includes a class C, then it can intercept the loading of C by L to self-reflectively modify the byte code of C - For avoiding infinite recursion, while the loading of a class is intercepted, further interception is not allowed ∙ Java’s class loader never allows this self-reflective class loading
Question 9 Why Javassist does not break security guarantees given by Java?
Answer Javassist uses the standard JVM The classes processed by Javassist are subject to the bytecode verifier and the SecurityManager of Java
Question 10 Can Javassist and OpenJava both deal with syntax extensions of Java?
Answer OpenJava can deal with syntax extensions. Because OpenJava is source-code basis. It reads source code for creating an object representing a class, a method, or a field, and alteration on the object is translated into corresponding transformation of the source code Javassist can not deal with syntax extensions since Javassist is byte-code basis
Question 11 Can the architecture applied by Javassist be applied to other object-oriented language?
Answer Yes, as long as the compiled binary program contains enough symbolic information to construct a class object. However the API needs to be individually designed for each language to perform structural reflection in a safe manner in terms of types