1*0c56280aSSorin Basca<?xml version="1.0"?> 2*0c56280aSSorin Basca<!-- 3*0c56280aSSorin Basca * Licensed to the Apache Software Foundation (ASF) under one 4*0c56280aSSorin Basca * or more contributor license agreements. See the NOTICE file 5*0c56280aSSorin Basca * distributed with this work for additional information 6*0c56280aSSorin Basca * regarding copyright ownership. The ASF licenses this file 7*0c56280aSSorin Basca * to you under the Apache License, Version 2.0 (the 8*0c56280aSSorin Basca * "License"); you may not use this file except in compliance 9*0c56280aSSorin Basca * with the License. You may obtain a copy of the License at 10*0c56280aSSorin Basca * 11*0c56280aSSorin Basca * http://www.apache.org/licenses/LICENSE-2.0 12*0c56280aSSorin Basca * 13*0c56280aSSorin Basca * Unless required by applicable law or agreed to in writing, 14*0c56280aSSorin Basca * software distributed under the License is distributed on an 15*0c56280aSSorin Basca * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*0c56280aSSorin Basca * KIND, either express or implied. See the License for the 17*0c56280aSSorin Basca * specific language governing permissions and limitations 18*0c56280aSSorin Basca * under the License. 19*0c56280aSSorin Basca--> 20*0c56280aSSorin Basca<document> 21*0c56280aSSorin Basca <properties> 22*0c56280aSSorin Basca <title>Application areas</title> 23*0c56280aSSorin Basca </properties> 24*0c56280aSSorin Basca 25*0c56280aSSorin Basca <body> 26*0c56280aSSorin Basca <section name="Application areas"> 27*0c56280aSSorin Basca <p> 28*0c56280aSSorin Basca There are many possible application areas for <font 29*0c56280aSSorin Basca face="helvetica,arial">BCEL</font> ranging from class 30*0c56280aSSorin Basca browsers, profilers, byte code optimizers, and compilers to 31*0c56280aSSorin Basca sophisticated run-time analysis tools and extensions to the Java 32*0c56280aSSorin Basca language. 33*0c56280aSSorin Basca </p> 34*0c56280aSSorin Basca 35*0c56280aSSorin Basca <p> 36*0c56280aSSorin Basca Compilers like the <a 37*0c56280aSSorin Basca href="http://barat.sourceforge.net">Barat</a> compiler use <font 38*0c56280aSSorin Basca face="helvetica,arial">BCEL</font> to implement a byte code 39*0c56280aSSorin Basca generating back end. Other possible application areas are the 40*0c56280aSSorin Basca static analysis of byte code or examining the run-time behavior of 41*0c56280aSSorin Basca classes by inserting calls to profiling methods into the 42*0c56280aSSorin Basca code. Further examples are extending Java with Eiffel-like 43*0c56280aSSorin Basca assertions, automated delegation, or with the concepts of <a 44*0c56280aSSorin Basca href="http://www.eclipse.org/aspectj/">Aspect-Oriented Programming</a>.<br/> A 45*0c56280aSSorin Basca list of projects using <font face="helvetica,arial">BCEL</font> can 46*0c56280aSSorin Basca be found <a href="../projects.html">here</a>. 47*0c56280aSSorin Basca </p> 48*0c56280aSSorin Basca 49*0c56280aSSorin Basca <subsection name="Class loaders"> 50*0c56280aSSorin Basca <p> 51*0c56280aSSorin Basca Class loaders are responsible for loading class files from the 52*0c56280aSSorin Basca file system or other resources and passing the byte code to the 53*0c56280aSSorin Basca Virtual Machine. A custom <tt>ClassLoader</tt> object may be used 54*0c56280aSSorin Basca to intercept the standard procedure of loading a class, i.e.m the 55*0c56280aSSorin Basca system class loader, and perform some transformations before 56*0c56280aSSorin Basca actually passing the byte code to the JVM. 57*0c56280aSSorin Basca </p> 58*0c56280aSSorin Basca 59*0c56280aSSorin Basca <p> 60*0c56280aSSorin Basca A possible scenario is described in <a href="#Figure 7">figure 61*0c56280aSSorin Basca 7</a>: 62*0c56280aSSorin Basca During run-time the Virtual Machine requests a custom class loader 63*0c56280aSSorin Basca to load a given class. But before the JVM actually sees the byte 64*0c56280aSSorin Basca code, the class loader makes a "side-step" and performs some 65*0c56280aSSorin Basca transformation to the class. To make sure that the modified byte 66*0c56280aSSorin Basca code is still valid and does not violate any of the JVM's rules it 67*0c56280aSSorin Basca is checked by the verifier before the JVM finally executes it. 68*0c56280aSSorin Basca </p> 69*0c56280aSSorin Basca 70*0c56280aSSorin Basca <p align="center"> 71*0c56280aSSorin Basca <a name="Figure 7"> 72*0c56280aSSorin Basca <img src="../images/classloader.gif"/> 73*0c56280aSSorin Basca <br/> 74*0c56280aSSorin Basca Figure 7: Class loaders 75*0c56280aSSorin Basca </a> 76*0c56280aSSorin Basca </p> 77*0c56280aSSorin Basca 78*0c56280aSSorin Basca <p> 79*0c56280aSSorin Basca Using class loaders is an elegant way of extending the Java 80*0c56280aSSorin Basca Virtual Machine with new features without actually modifying it. 81*0c56280aSSorin Basca This concept enables developers to use <em>load-time 82*0c56280aSSorin Basca reflection</em> to implement their ideas as opposed to the static 83*0c56280aSSorin Basca reflection supported by the <a 84*0c56280aSSorin Basca href="http://java.sun.com/j2se/1.3/docs/guide/reflection/index.html">Java 85*0c56280aSSorin Basca Reflection API</a>. Load-time transformations supply the user with 86*0c56280aSSorin Basca a new level of abstraction. He is not strictly tied to the static 87*0c56280aSSorin Basca constraints of the original authors of the classes but may 88*0c56280aSSorin Basca customize the applications with third-party code in order to 89*0c56280aSSorin Basca benefit from new features. Such transformations may be executed on 90*0c56280aSSorin Basca demand and neither interfere with other users, nor alter the 91*0c56280aSSorin Basca original byte code. In fact, class loaders may even create classes 92*0c56280aSSorin Basca <em>ad hoc</em> without loading a file at all.<br/> <font 93*0c56280aSSorin Basca face="helvetica,arial">BCEL</font> has already builtin support for 94*0c56280aSSorin Basca dynamically creating classes, an example is the ProxyCreator class. 95*0c56280aSSorin Basca </p> 96*0c56280aSSorin Basca 97*0c56280aSSorin Basca </subsection> 98*0c56280aSSorin Basca 99*0c56280aSSorin Basca <subsection name="Example: Poor Man's Genericity"> 100*0c56280aSSorin Basca <p> 101*0c56280aSSorin Basca The former "Poor Man's Genericity" project that extended Java with 102*0c56280aSSorin Basca parameterized classes, for example, used <font 103*0c56280aSSorin Basca face="helvetica,arial">BCEL</font> in two places to generate 104*0c56280aSSorin Basca instances of parameterized classes: During compile-time (with the 105*0c56280aSSorin Basca standard <tt>javac</tt> with some slightly changed classes) and at 106*0c56280aSSorin Basca run-time using a custom class loader. The compiler puts some 107*0c56280aSSorin Basca additional type information into class files (attributes) which is 108*0c56280aSSorin Basca evaluated at load-time by the class loader. The class loader 109*0c56280aSSorin Basca performs some transformations on the loaded class and passes them 110*0c56280aSSorin Basca to the VM. The following algorithm illustrates how the load method 111*0c56280aSSorin Basca of the class loader fulfills the request for a parameterized 112*0c56280aSSorin Basca class, e.g., <tt>Stack<String></tt> 113*0c56280aSSorin Basca </p> 114*0c56280aSSorin Basca 115*0c56280aSSorin Basca <p> 116*0c56280aSSorin Basca <ol type="1"> 117*0c56280aSSorin Basca <li> Search for class <tt>Stack</tt>, load it, and check for a 118*0c56280aSSorin Basca certain class attribute containing additional type 119*0c56280aSSorin Basca information. I.e. the attribute defines the "real" name of the 120*0c56280aSSorin Basca class, i.e., <tt>Stack<A></tt>.</li> 121*0c56280aSSorin Basca 122*0c56280aSSorin Basca <li>Replace all occurrences and references to the formal type 123*0c56280aSSorin Basca <tt>A</tt> with references to the actual type <tt>String</tt>. For 124*0c56280aSSorin Basca example the method 125*0c56280aSSorin Basca </li> 126*0c56280aSSorin Basca 127*0c56280aSSorin Basca <source> 128*0c56280aSSorin Basca void push(A obj) { ... } 129*0c56280aSSorin Basca </source> 130*0c56280aSSorin Basca 131*0c56280aSSorin Basca <p> 132*0c56280aSSorin Basca becomes 133*0c56280aSSorin Basca </p> 134*0c56280aSSorin Basca 135*0c56280aSSorin Basca <source> 136*0c56280aSSorin Basca void push(String obj) { ... } 137*0c56280aSSorin Basca </source> 138*0c56280aSSorin Basca 139*0c56280aSSorin Basca <li> Return the resulting class to the Virtual Machine.</li> 140*0c56280aSSorin Basca </ol> 141*0c56280aSSorin Basca </p> 142*0c56280aSSorin Basca 143*0c56280aSSorin Basca </subsection> 144*0c56280aSSorin Basca </section> 145*0c56280aSSorin Basca </body> 146*0c56280aSSorin Basca</document>