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