Java Doc for Loader.java in  » Byte-Code » Javassist » javassist » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Byte Code » Javassist » javassist 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java.lang.ClassLoader
      javassist.Loader

All known Subclasses:   javassist.tools.reflect.Loader,
Loader
public class Loader extends ClassLoader (Code)
The class loader for Javassist.

This is a sample class loader using ClassPool. Unlike a regular class loader, this class loader obtains bytecode from a ClassPool.

Note that Javassist can be used without this class loader; programmers can define their own versions of class loader. They can run a program even without any user-defined class loader if that program is statically translated with Javassist. This class loader is just provided as a utility class.

Suppose that an instance of MyTranslator implementing the interface Translator is responsible for modifying class files. The startup program of an application using MyTranslator should be something like this:

     import javassist.*;
     public class Main {
     public static void main(String[] args) throws Throwable {
     MyTranslator myTrans = new MyTranslator();
     ClassPool cp = ClassPool.getDefault();
     Loader cl = new Loader(cp);
     cl.addTranslator(cp, myTrans);
     cl.run("MyApp", args);
     }
     }
     

Class MyApp is the main program of the application.

This program should be executed as follows:

     % java Main arg1 arg2...
     

It modifies the class MyApp with a MyTranslator object before the JVM loads it. Then it calls main() in MyApp with arguments arg1, arg2, ...

This program execution is equivalent to:

     % java MyApp arg1 arg2...
     

except that classes are translated by MyTranslator at load time.

If only a particular class must be modified when it is loaded, the startup program can be simpler; MyTranslator is unnecessary. For example, if only a class test.Rectangle is modified, the main() method above will be the following:

     ClassPool cp = ClassPool.getDefault();
     Loader cl = new Loader(cp);
     CtClass ct = cp.get("test.Rectangle");
     ct.setSuperclass(cp.get("test.Point"));
     cl.run("MyApp", args);

This program changes the super class of the test.Rectangle class.

Note 1:

This class loader does not allow the users to intercept the loading of java.* and javax.* classes (and sun.*, org.xml.*, ...) unless Loader.doDelegation is false. This is because the JVM prohibits a user class loader from loading a system class. Also see Note 2. If this behavior is not appropriate, a subclass of Loader must be defined and loadClassByDelegation() must be overridden.

Note 2:

If classes are loaded with different class loaders, they belong to separate name spaces. If class C is loaded by a class loader CL, all classes that the class C refers to are also loaded by CL. However, if CL delegates the loading of the class C to CL', then those classes that the class C refers to are loaded by a parent class loader CL' instead of CL.

If an object of class C is assigned to a variable of class C belonging to a different name space, then a ClassCastException is thrown.

Because of the fact above, this loader delegates only the loading of javassist.Loader and classes included in package java.* and javax.* to the parent class loader. Other classes are directly loaded by this loader.

For example, suppose that java.lang.String would be loaded by this loader while java.io.File is loaded by the parent class loader. If the constructor of java.io.File is called with an instance of java.lang.String, then it may throw an exception since it accepts an instance of only the java.lang.String loaded by the parent class loader.
See Also:   javassist.ClassPool
See Also:   javassist.Translator



Field Summary
public  booleandoDelegation
     Specifies the algorithm of class loading.

Constructor Summary
public  Loader()
     Creates a new class loader.
public  Loader(ClassPool cp)
     Creates a new class loader.
public  Loader(ClassLoader parent, ClassPool cp)
     Creates a new class loader using the specified parent class loader for delegation.

Method Summary
public  voidaddTranslator(ClassPool cp, Translator t)
     Adds a translator, which is called whenever a class is loaded.
public  voiddelegateLoadingOf(String classname)
     Records a class so that the loading of that class is delegated to the parent class loader.

If the given class name ends with . (dot), then that name is interpreted as a package name.

protected  ClassdelegateToParent(String classname)
    
protected  ClassfindClass(String name)
     Finds the specified class using ClassPath. If the source throws an exception, this returns null.

This method can be overridden by a subclass of Loader.

protected  PackagegetPackage(String name)
    
protected  ClassloadClass(String name, boolean resolve)
     Requests the class loader to load a class.
protected  ClassloadClassByDelegation(String name)
    
public static  voidmain(String[] args)
     Loads a class with an instance of Loader and calls main() of that class.
public  voidrun(String[] args)
     Loads a class and calls main() in that class.
public  voidrun(String classname, String[] args)
     Loads a class and calls main() in that class.
public  voidsetClassPool(ClassPool cp)
     Sets the soruce ClassPool.
public  voidsetDomain(ProtectionDomain d)
     Sets the protection domain for the classes handled by this class loader.

Field Detail
doDelegation
public boolean doDelegation(Code)
Specifies the algorithm of class loading.

This class loader uses the parent class loader for java.* and javax.* classes. If this variable doDelegation is false, this class loader does not delegate those classes to the parent class loader.

The default value is true.





Constructor Detail
Loader
public Loader()(Code)
Creates a new class loader.



Loader
public Loader(ClassPool cp)(Code)
Creates a new class loader.
Parameters:
  cp - the source of class files.



Loader
public Loader(ClassLoader parent, ClassPool cp)(Code)
Creates a new class loader using the specified parent class loader for delegation.
Parameters:
  parent - the parent class loader.
Parameters:
  cp - the source of class files.




Method Detail
addTranslator
public void addTranslator(ClassPool cp, Translator t) throws NotFoundException, CannotCompileException(Code)
Adds a translator, which is called whenever a class is loaded.
Parameters:
  cp - the ClassPool object for obtaininga class file.
Parameters:
  t - a translator.
throws:
  NotFoundException - if t.start() throws an exception.
throws:
  CannotCompileException - if t.start() throws an exception.



delegateLoadingOf
public void delegateLoadingOf(String classname)(Code)
Records a class so that the loading of that class is delegated to the parent class loader.

If the given class name ends with . (dot), then that name is interpreted as a package name. All the classes in that package and the sub packages are delegated.




delegateToParent
protected Class delegateToParent(String classname) throws ClassNotFoundException(Code)



findClass
protected Class findClass(String name) throws ClassNotFoundException(Code)
Finds the specified class using ClassPath. If the source throws an exception, this returns null.

This method can be overridden by a subclass of Loader. Note that the overridden method must not throw an exception when it just fails to find a class file. null if the specified class could not be found.
throws:
  ClassNotFoundException - if an exception is thrown whileobtaining a class file.




getPackage
protected Package getPackage(String name)(Code)



loadClass
protected Class loadClass(String name, boolean resolve) throws ClassFormatError, ClassNotFoundException(Code)
Requests the class loader to load a class.



loadClassByDelegation
protected Class loadClassByDelegation(String name) throws ClassNotFoundException(Code)



main
public static void main(String[] args) throws Throwable(Code)
Loads a class with an instance of Loader and calls main() of that class.

This method calls run().
Parameters:
  args - command line parameters.

    args[0] is the class name to be loaded.
    args[1..n] are parameters passedto the target main().

See Also:   javassist.Loader.run(String[])



run
public void run(String[] args) throws Throwable(Code)
Loads a class and calls main() in that class.
Parameters:
  args - command line parameters.
    args[0] is the class name to be loaded.
    args[1..n] are parameters passedto the target main().



run
public void run(String classname, String[] args) throws Throwable(Code)
Loads a class and calls main() in that class.
Parameters:
  classname - the loaded class.
Parameters:
  args - parameters passed to main().



setClassPool
public void setClassPool(ClassPool cp)(Code)
Sets the soruce ClassPool.



setDomain
public void setDomain(ProtectionDomain d)(Code)
Sets the protection domain for the classes handled by this class loader. Without registering an appropriate protection domain, the program loaded by this loader will not work with a security manager or a signed jar file.



Methods inherited from java.lang.ClassLoader
public synchronized void clearAssertionStatus()(Code)(Java Doc)
final protected Class defineClass(byte[] b, int off, int len) throws ClassFormatError(Code)(Java Doc)
final protected Class defineClass(String name, byte[] b, int off, int len) throws ClassFormatError(Code)(Java Doc)
final protected Class defineClass(String name, byte[] b, int off, int len, ProtectionDomain protectionDomain) throws ClassFormatError(Code)(Java Doc)
final protected Class defineClass(String name, java.nio.ByteBuffer b, ProtectionDomain protectionDomain) throws ClassFormatError(Code)(Java Doc)
protected Package definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) throws IllegalArgumentException(Code)(Java Doc)
protected Class findClass(String name) throws ClassNotFoundException(Code)(Java Doc)
protected String findLibrary(String libname)(Code)(Java Doc)
final protected Class findLoadedClass(String name)(Code)(Java Doc)
protected URL findResource(String name)(Code)(Java Doc)
protected Enumeration<URL> findResources(String name) throws IOException(Code)(Java Doc)
final protected Class findSystemClass(String name) throws ClassNotFoundException(Code)(Java Doc)
protected Package getPackage(String name)(Code)(Java Doc)
protected Package[] getPackages()(Code)(Java Doc)
final public ClassLoader getParent()(Code)(Java Doc)
public URL getResource(String name)(Code)(Java Doc)
public InputStream getResourceAsStream(String name)(Code)(Java Doc)
public Enumeration<URL> getResources(String name) throws IOException(Code)(Java Doc)
public static ClassLoader getSystemClassLoader()(Code)(Java Doc)
public static URL getSystemResource(String name)(Code)(Java Doc)
public static InputStream getSystemResourceAsStream(String name)(Code)(Java Doc)
public static Enumeration<URL> getSystemResources(String name) throws IOException(Code)(Java Doc)
public Class loadClass(String name) throws ClassNotFoundException(Code)(Java Doc)
protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException(Code)(Java Doc)
final protected void resolveClass(Class c)(Code)(Java Doc)
public synchronized void setClassAssertionStatus(String className, boolean enabled)(Code)(Java Doc)
public synchronized void setDefaultAssertionStatus(boolean enabled)(Code)(Java Doc)
public synchronized void setPackageAssertionStatus(String packageName, boolean enabled)(Code)(Java Doc)
final protected void setSigners(Class c, Object[] signers)(Code)(Java Doc)

Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.