org.curjent.agent
Class DelegatingLoader

java.lang.Object
  extended by org.curjent.agent.DelegatingLoader
All Implemented Interfaces:
AgentLoader

public class DelegatingLoader
extends Object
implements AgentLoader

Delegates class creation and loading to an existing ClassLoader. See the class description for AgentLoader.


Field Summary
private  ClassLoader classLoader
          Class loader whose 'defineClass' method is called.
private  Method defineClass
          Cached 'defineClass' method of classLoader.
 
Constructor Summary
DelegatingLoader(Class<?> type)
          Equivalent to DelegatingLoader(type.getClassLoader()).
DelegatingLoader(ClassLoader classLoader)
          Initializes this instance with the given classLoader for creating and loading generated classes.
DelegatingLoader(Object instance)
          Equivalent to DelegatingLoader(instance.getClass().getClassLoader()).
 
Method Summary
 Class<?> defineClass(String name, byte[] bytes)
          Uses the previously cached defineClass method to create a generated class.
 ClassLoader getClassLoader()
          Returns the classLoader instance this instance was constructed with.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

classLoader

private final ClassLoader classLoader
Class loader whose 'defineClass' method is called.


defineClass

private final Method defineClass
Cached 'defineClass' method of classLoader.

Constructor Detail

DelegatingLoader

public DelegatingLoader(Object instance)
Equivalent to DelegatingLoader(instance.getClass().getClassLoader()).

See Also:
DelegatingLoader(ClassLoader)

DelegatingLoader

public DelegatingLoader(Class<?> type)
Equivalent to DelegatingLoader(type.getClassLoader()).

See Also:
DelegatingLoader(ClassLoader)

DelegatingLoader

public DelegatingLoader(ClassLoader classLoader)
Initializes this instance with the given classLoader for creating and loading generated classes. Reflection is used during construction to find and cache the loader's defineClass method.

Throws:
NullPointerException - classLoader == null
IllegalArgumentException - defineClass(String, byte[], int, int) not found in the class of the given classLoader
SecurityException - Reflection is restricted by security settings.
Method Detail

getClassLoader

public ClassLoader getClassLoader()
Returns the classLoader instance this instance was constructed with.

Specified by:
getClassLoader in interface AgentLoader
See Also:
DelegatingLoader(ClassLoader)

defineClass

public Class<?> defineClass(String name,
                            byte[] bytes)
Uses the previously cached defineClass method to create a generated class.

Specified by:
defineClass in interface AgentLoader
Parameters:
name - The class's fully qualified name.
bytes - The generated bytecodes for the class. The offset is 0, and the length is equal to bytes.length.
Throws:
IllegalStateException - Wraps as the cause an IllegalAccessException thrown because the defineClass method is not accessible.
AgentException - Wraps as the cause an exception thrown by the defineClass method.
See Also:
DelegatingLoader(ClassLoader)


Copyright 2009-2011 Tom Landon
Apache License 2.0