ASM: A Bytecode Manipulation Tool – A brief Overview Course : CSE 6329 Spring 2011 University of Texas at Arlington.

Slides:



Advertisements
Similar presentations
Behavioral Pattern: Visitor C h a p t e r 5 – P a g e 224 When an algorithm is separated from an object structure upon which it operates, new operations.
Advertisements

1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Lab#1 (14/3/1431h) Introduction To java programming cs425
1 Basic Object Oriented Concepts Overview l What is Object-Orientation about? l What is an Object? l What is a Class? l Constructing Objects from Classes.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Guide To UNIX Using Linux Third Edition
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department.
Introduction To System Analysis and design
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Aurora: A Conceptual Model for Web-content Adaptation to Support the Universal Accessibility of Web-based Services Anita W. Huang, Neel Sundaresan Presented.
ITEC 352 Lecture 11 ISA - CPU. ISA (2) Review Questions? HW 2 due on Friday ISA –Machine language –Buses –Memory.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
EARTH SCIENCE MARKUP LANGUAGE “Define Once Use Anywhere” INFORMATION TECHNOLOGY AND SYSTEMS CENTER UNIVERSITY OF ALABAMA IN HUNTSVILLE.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Zhonghua Qu and Ovidiu Daescu December 24, 2009 University of Texas at Dallas.
Introducing Axis2 Eran Chinthaka. Agenda  Introduction and Motivation  The “big picture”  Key Features of Axis2 High Performance XML Processing Model.
Understanding the CORBA Model. What is CORBA?  The Common Object Request Broker Architecture (CORBA) allows distributed applications to interoperate.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
CSC 212 Object-Oriented Programming and Java Part 1.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
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.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CIS 644 Aug. 25, 1999 tour of Java. First … about the media lectures… we are experimenting with the media format please give feedback.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
POS 406 Java Technology And Beginning Java Code
1 Software Design Overview Reference: Software Engineering, by Ian Sommerville, Ch. 12 & 13.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Unit 1: Java and Eclipse The Eclipse Development Environment.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Object Oriented Software Development
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.
SilkTest 2008 R2 SP1: Silk4J Introduction. ConfidentialCopyright © 2008 Borland Software Corporation. 2 What is Silk4J? Silk4J enables you to create functional.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
ODS – Introduction to Web Services and BPEL Vakgroep Informatietechnologie Web Services & BPEL Design of Distributed Software.
Declarative Languages and Model Based Development of Web Applications Besnik Selimi South East European University DAAD: 15 th Workshop “Software Engineering.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
METADATA IN.NET Presented By Sukumar Manduva. INTRODUCTION  What is Metadata ? Metadata is a binary information which contains the complete description.
RealTimeSystems Lab Jong-Koo, Lim
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Section 10.1 Define scripting
DHTML.
Chapter 0: Introduction
More About Objects and Methods
Module Road Map Refactoring Why Refactoring? Examples
Introduction to Compiler Construction
Java bytecode manipulation
Object-Oriented Analysis and Design
Chapter 3: Using Methods, Classes, and Objects
Knowledge Byte In this section, you will learn about:
Chapter 6 Methods: A Deeper Look
eSeries Entities By Julie Ladner
Presentation transcript:

ASM: A Bytecode Manipulation Tool – A brief Overview Course : CSE 6329 Spring 2011 University of Texas at Arlington

Presented by Sarker Tanveer Ahmed Rumee

Acknowledgement All the tables and figures of this presentation was copied from the publicly available ASM- Guide from the ASM website.  Some contents were also copied from the ASM 2.0 tutorial. 

What is ASM? ASM is an all purpose Java Bytecode manipulation and analysis framework. It can be used to modify existing classes or dynamically generate classes, directly in binary form.

Where to find? The ASM Home Page – – It contains the ASM library for download – Also has links to documents and other resources – To download the current stable version of ASM library go to the following link: 23&release_id= &release_id=4412

Online Documentation and Resources ASM Guide [API Reference and Detail Discussion about the use of ASM]. – guide.pdfhttp://download.forge.objectweb.org/asm/asm- guide.pdf API Reference – ml ml

Bytecode Outline Plugin Start Eclipse Go to: Help->Install new software In the Location bar enter the following address: – update/ update/ – It will show available updates – Select the Bytecode Outline and rest of the steps are trivial

A Very short Overview of JAVA Class File Format

Structure of Compiled Java Class

Few Facts Compiled class describes only one class where a source file may contain several classes Compiled class does not contain comments A compiled class does not contain package and import section, so all type names must be fully qualified. For example: – Fully Qualified name of String is java/lang/String NB: In this form Dots (.) are replaced by Slash (/)

Few Facts (Contd..) Internal names are used for class and interface types. Internal name is nothing but the fully qualified name of the class/interface. In all other situations java types are represented in compiled classes with type descriptors.

Type Descriptors

Method Descriptors

ASM Framework The ASM bytecode manipulation framework is written in Java and uses a visitor-based approach.ASM Allows developers to avoid dealing directly with a class constant pool and offsets within method byte code. Provides better performance, compared to other tools such as BCEL, SERP, orJavassist.BCELSERPJavassist

ASM package structure ASM is divided into several packages. The package structure is :

ASM package structure (Contd..) The Core package provides an API to read, write, and transform Java bytecode and defines a foundation for the other packages. This package is sufficient to generate Java bytecode and to implement the majority of bytecode transformations. The Tree package provides in-memory representation of Java bytecode. The Analysis package provides basic data-flow analyses and type-checking algorithms for Java method bytecode stored in structures from the tree package.

The Commons package (added in ASM 2.0) provides several commonly used bytecode transformations and adapters to simplify bytecode generation. The Util package contains several helper classes and simple bytecode verifiers that can help in development or testing. The XML package provides an adapter to convert bytecode structures to and from XML

CORE PAKCAGE

The Core Package The Core package uses a push approach (similar to the "Visitor" design pattern) to walk trough complex bytecode structures.Visitor ASM defines several interfaces, such as ClassVisitor, FieldVisitor, MethodVisitor and AnnotationVisitor. These interfaces interact with each other to implement bytecode transformations and/or capture information from the bytecode.

Core package [cont..] The Core package can be logically divided into two major parts: – Bytecode producers, such as a ClassReader or a custom class that can fire the proper sequence of calls to the methods of the above visitor classes. – Bytecode consumers, such as writers (ClassWriter, FieldWriter, MethodWriter, and AnnotationWriter), adapters (ClassAdapter and MethodAdapter), or any other classes implementing the above visitor interfaces.

Visitor Design Pattern ASM follows the Visitor Design Pattern for its operation. To understand how it works we need to investigate how the Visitor Design Pattern fits in Object Oriented Programming.

Visitor Design Pattern In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure it operates on.object-oriented programmingsoftware engineeringdesign patternalgorithm A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures.

The participating classes  Visitor - This is an interface used to declare the visit operations for all the types of visitable classes. Usually the name of the operation is the same and the operations are differentiated by the method signature.  ConcreteVisitor - An implementation of Visitor that implements all the visit method  Visitable - is an abstraction which declares the accept operation. This is the entry point which enables an object to be "visited" by the visitor object.  ConcreteVisitable - Those classes implementing the Visitable interface or a class and defines the accept operation.

Visitor Design Pattern [cont..] We want to add “print” method for each of the following classes representing different parts of a Car – Wheel, Engine, and Body – Instead of creating "print" methods for each subclass (Wheel, Engine, Body), a single class (CarElementPrintVisitor) will be used to perform the required printing action

Example interface CarElementVisitor { void visit(Wheel wheel); void visit(Engine engine); void visit(Body body); } interface CarElement { void accept(CarElementVisitor visitor); // CarElements have to provide accept(). }  Visitor  Visitable

class Wheel implements CarElement { public void accept(CarElementVisitor visitor) { visitor.visit(this); } class Engine implements CarElement { public void accept(CarElementVisitor visitor) { visitor.visit(this); } } class Body implements CarElement { accept(..) {} }  Concrete Visitables

class CarElementPrintVisitor implements CarElementVisitor { public void visit(Wheel wheel) { System.out.println("Visiting wheel"); } public void visit(Engine engine) { System.out.println("Visiting engine"); } public void visit(Body body) { System.out.println("Visiting body"); } }  Concrete Visitor

public class VisitorDemo { static public void main(String[] args){ Wheel ob1 = new Wheel(); Body ob2 = new Body(); Engine ob3 = new Engine(); ob1.accept(new CarElementVisitor()); ob2.accept(new CarElementVisitor()); ob3.accept(new CarElementVisitor()); }

Using ASM API to write Code ASM API for generating and transforming compiled classes is based on ClassVisitor interface. Each method of this interface correspond to the class file structure described earlier The ClassVisitor interface is depicted in the next slide.

ClassVisitor Interface

ClassVisitor (cont..) Simple sections of a class are visited by a single method call and it returns void Sections whose content can be of arbitrary length are visited with a initial method call that returns an auxiliary interface. This happens for visitAnnotation, visitField and visitMethod. The auxiliary visitor interfaces will be described later.

How it works A client application creates ClassReader and calls the accept() method, passing a concrete ClassVisitor instance as a parameter. Then ClassReader parses the class and fires "visit" events to ClassVisitor for each bytecode fragment For repeated contexts, such as fields, methods, or annotations, a ClassVisitor may create child visitors derived from the corresponding interface (FieldVisitor,MethodVisitor, or AnnotationVisitor) and return them to the producer.

The methods of the ClassVisitor interface must be called in the following order: visit visitSource? visitOuterClass? (visitAnnotation | visitAttribute) * (visitInnerClass | visitField | visit Method )* visitEnd

Three Core Components ASM provides three core components based on ClassVisitor interface in order to generate and transform classes. These are: – ClassReader – ClassWriter – ClassAdapter

Three Main tasks of ASM Tool Parse a class Generate a class Transform a class – We discuss these three tasks in light of the three core components depicted in the previous class.

Parsing Classes Only required component to parse and existing class is the ClassReader It does so by calling the corresponding visitXxx methods on ClassVisitor instance passed as an argument to its accept method. Let’s see an example how it works.

Parsing a Class using ClassReader Example:

Generating Classes The only required component – ClassWriter It is an implementation of the ClassVisitor interface that builds compiled classes directly in binary form. It produce as output a byte array that contains the compiled class Lets’ see and example:

Generating Classes (cont..) Consider the following interface

Generating Classes (cont..) The Above interface can be generated with 6 method calls to a ClassVisitor Lets’ see the example..

Transforming classes So far we have used ClassReader and ClassWriter separately. The events were produced by “hand ” and consumed directly by a ClassWriter or, Symmetrically, or Events were produced by ClassReader and consumed by “hand”. i.e, by a custom ClassVisitor implementation.

Transforming Classes (cont..) Now we want to use both ClassReader and ClassWriter together. The first step would be to direct the events produced by a ClassReader to a ClassWriter, the result is that the class parsed by the ClassReader is reconstructed by the ClassWriter

Transforming Classes (cont..) So the corresponding code fragment will be:

Introduction of ClassAdapter Now we introduce a middle entity named ClassAdapter. The idea will be:

ClassAdapter (cont..)

According to the code fragment depicted above the outcome does not change, because the ClassAdapter does not filter anything But now we can easily filter some events using the ClassAdapter to Transform a class.

Example on Class Transformation

Conclusion We have discussed so far three basic operations that can be done by ASM But ASM library has a large collection of useful API to help analysis Compiled java classes in great detail. The ASM –GUIDE is very good resource in this regard.

Questions??