本文整理了Java中javassist.Loader
类的一些代码示例,展示了Loader
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Loader
类的具体详情如下:
包路径:javassist.Loader
类名称:Loader
[英]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.
[中]Javassist的类装入器。
这是一个使用ClassPool
的示例类装入器。与常规类加载器不同,该类加载器从ClassPool
获取字节码。
注意,Javassist可以在没有此类装入器的情况下使用;程序员可以定义自己版本的类加载器。如果程序是用Javassist静态翻译的,那么即使没有任何用户定义的类加载器,它们也可以运行该程序。这个类加载器只是作为一个实用程序类提供的。
假设实现接口Translator
的MyTranslator
的实例负责修改类文件。使用MyTranslator
的应用程序的启动程序应如下所示:
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);
}
}
类MyApp
是应用程序的主程序。
该程序应按如下方式执行:
% java Main arg1 arg2...
在JVM加载类MyApp
之前,它使用MyTranslator
对象修改类MyApp
。然后它使用参数arg1、arg2、。。。
此程序执行相当于:
% java MyApp arg1 arg2...
除了类在加载时转换为MyTranslator
。
如果加载时只需修改特定类,则启动程序可以更简单;MyTranslator
是不必要的。例如,如果只修改了类test.Rectangle
,则上面的main()
方法如下所示:
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);
此程序更改test.Rectangle
类的超类。
注1:
此类加载器不允许用户拦截java.*
和javax.*
类(以及sun.*
、org.xml.*
、…)的加载除非Loader.doDelegation
是false
。这是因为JVM禁止用户类加载器加载系统类。另见注2。如果此行为不合适,则必须定义Loader
的子类,并且必须重写loadClassByDelegation()
。
注2:
如果类由不同的类加载器加载,则它们属于不同的名称空间。如果类C
由类加载器CL
加载,则类C
引用的所有类也由CL
加载。但是,如果CL
将类C
的加载委托给CL'
,则类C
引用的那些类将由父类加载器CL'
而不是CL
加载。
如果类C
的对象被分配给属于不同名称空间的类C
的变量,则会抛出ClassCastException
。
由于上述事实,此加载器仅将javassist.Loader
和包java.*
和javax.*
中包含的类的加载委托给父类加载器。其他类由该加载器直接加载。
例如,假设java.lang.String
将由该加载器加载,而java.io.File
由父类加载器加载。如果使用java.lang.String
的实例调用java.io.File
的构造函数,则它可能会引发异常,因为它只接受父类装入器装入的java.lang.String
的实例。
代码示例来源:origin: redisson/redisson
private <T> Class loadProxyClass(Class<T> clazz, ClassPool pool, final CtClass cc) throws ClassNotFoundException {
Class ccClz;
Loader cl = new Loader(clazz.getClassLoader(), pool) {
protected Class loadClassByDelegation(String name)
throws ClassNotFoundException
{
if ( name.equals(cc.getName()) )
return null;
return delegateToParent(name);
}
};
ccClz = cl.loadClass(cc.getName());
return ccClz;
}
代码示例来源:origin: redisson/redisson
/**
* Loads a class with an instance of <code>Loader</code>
* and calls <code>main()</code> of that class.
*
* <p>This method calls <code>run()</code>.
*
* @param args command line parameters.
* <br> {@code args[0]} is the class name to be loaded.
* <br> {@code args[1..n]} are parameters passed
* to the target {@code main()}.
*
* @see javassist.Loader#run(String[])
*/
public static void main(String[] args) throws Throwable {
Loader cl = new Loader();
cl.run(args);
}
代码示例来源:origin: redisson/redisson
private void init(ClassPool cp) {
notDefinedHere = new Hashtable();
notDefinedPackages = new Vector();
source = cp;
translator = null;
domain = null;
delegateLoadingOf("javassist.Loader");
}
代码示例来源:origin: redisson/redisson
/**
* Requests the class loader to load a class.
*/
protected Class loadClass(String name, boolean resolve)
throws ClassFormatError, ClassNotFoundException {
name = name.intern();
synchronized (name) {
Class c = findLoadedClass(name);
if (c == null)
c = loadClassByDelegation(name);
if (c == null)
c = findClass(name);
if (c == null)
c = delegateToParent(name);
if (resolve)
resolveClass(c);
return c;
}
}
代码示例来源:origin: redisson/redisson
protected Class loadClassByDelegation(String name)
throws ClassNotFoundException
{
/* The swing components must be loaded by a system
* class loader.
* javax.swing.UIManager loads a (concrete) subclass
* of LookAndFeel by a system class loader and cast
* an instance of the class to LookAndFeel for
* (maybe) a security reason. To avoid failure of
* type conversion, LookAndFeel must not be loaded
* by this class loader.
*/
Class c = null;
if (doDelegation)
if (name.startsWith("java.")
|| name.startsWith("javax.")
|| name.startsWith("sun.")
|| name.startsWith("com.sun.")
|| name.startsWith("org.w3c.")
|| name.startsWith("org.xml.")
|| notDelegated(name))
c = delegateToParent(name);
return c;
}
代码示例来源:origin: org.openimaj/core-aop-support
protected static Loader run(final ClassLoader parent, ClassPool pool, MultiTransformClassFileTransformer tf,
String mainClass, String[] args)
throws Throwable
{
final Loader cl = new Loader(parent, pool);
// Set the correct app name on OSX. Are there similar controls for other
// platforms?
System.setProperty("com.apple.mrj.application.apple.menu.about.name", mainClass);
// skip args4j
cl.delegateLoadingOf("org.apache.log4j.");
cl.addTranslator(pool, tf);
cl.run(mainClass, args);
return cl;
}
代码示例来源:origin: stackoverflow.com
var loader:Loader = new Loader();
var request:URLRequest = new URLRequest("twviewer.swf");
request.data = getFlashVars(this.loaderInfo);
loader.load(request);
addChild(loader);
代码示例来源:origin: sherif181/java-sequence-diagram-generator
Translator xlat = new InjectorTranslator();
Loader loader = new Loader();
loader.addTranslator(pool, xlat);
classes[i] = loader.loadClass(classnames.get(i));
Class<?> jucore = loader.loadClass(JUnitCore.class.getName());
代码示例来源:origin: redisson/redisson
if (i != -1) {
String pname = name.substring(0, i);
if (getPackage(pname) == null)
try {
definePackage(
pname, null, null, null, null, null, null, null);
return defineClass(name, classfile, 0, classfile.length);
else
return defineClass(name, classfile, 0, classfile.length, domain);
代码示例来源:origin: sc.fiji/fiji-compat
public static void main(String[] args) throws Throwable {
Thread.currentThread().setContextClassLoader(MemoryProfiler.class.getClassLoader());
if (args.length == 0) {
System.err.println("Usage: java " + MemoryProfiler.class + " <main-class> [<argument>...]");
System.exit(1);
}
String mainClass = args[0];
String[] mainArgs = new String[args.length - 1];
System.arraycopy(args, 1, mainArgs, 0, mainArgs.length);
Loader loader = new Loader();
loader.addTranslator(ClassPool.getDefault(), new MemoryProfiler());
gc();
loader.run(mainClass, mainArgs);
}
}
代码示例来源:origin: redisson/redisson
/**
* Loads a class and calls <code>main()</code> in that class.
*
* @param args command line parameters.
*
* <br> {@code args[0]} is the class name to be loaded.
* <br> {@code args[1..n]} are parameters passed
* to the target {@code main()}.
*/
public void run(String[] args) throws Throwable {
int n = args.length - 1;
if (n >= 0) {
String[] args2 = new String[n];
for (int i = 0; i < n; ++i)
args2[i] = args[i + 1];
run(args[0], args2);
}
}
代码示例来源:origin: org.javassist/javassist
if (i != -1) {
String pname = name.substring(0, i);
if (isDefinedPackage(pname))
try {
definePackage(
pname, null, null, null, null, null, null, null);
return defineClass(name, classfile, 0, classfile.length);
return defineClass(name, classfile, 0, classfile.length, domain);
代码示例来源:origin: ago1024/WurmServerModLauncher
public static void main(String[] args) {
try {
initLogger();
Loader loader = HookManager.getInstance().getLoader();
loader.delegateLoadingOf("javafx.");
loader.delegateLoadingOf("com.sun.");
loader.delegateLoadingOf("org.controlsfx.");
loader.delegateLoadingOf("impl.org.controlsfx");
loader.delegateLoadingOf("com.mysql.");
loader.delegateLoadingOf("org.sqlite.");
loader.delegateLoadingOf("org.gotti.wurmunlimited.modloader.classhooks.");
loader.delegateLoadingOf("javassist.");
Thread.currentThread().setContextClassLoader(loader);
loader.run("org.gotti.wurmunlimited.serverlauncher.DelegatedLauncher", args);
} catch (Throwable e) {
Logger.getLogger(ServerLauncher.class.getName()).log(Level.SEVERE, e.getMessage(), e);
e.printStackTrace();
System.exit(-1);
}
}
代码示例来源:origin: stackoverflow.com
Loader loader = new Loader();
loader.loadStrings ();
foo = loader.initMatrix ();
代码示例来源:origin: com.google.gwt.gwtmockito/gwtmockito
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
// If the class is in a blacklisted package, load it with the parent or default classloader.
for (String blacklistedPackage : getPackagesToLoadViaStandardClassloader()) {
if (name.startsWith(blacklistedPackage)) {
return null;
}
}
// Otherwise load it with our custom classloader.
return super.findClass(name);
}
代码示例来源:origin: stackoverflow.com
Loader loader = new Loader();
GuiTexture texture = new GuiTexture(loader.loadTexture("some/file/here.png", new Vector2f(0, 0), new Vector2f(1, 1));
List<GuiTexture> guis = new ArrayList<GuiTexture>();
guis.add(texture);
GuiRenderer renderer = new GuiRenderer(loader);
while(Frame is open) {
renderer.render(guis);
}
renderer.cleanUp();
代码示例来源:origin: net.sf.jour/jour-instrument
private void init(String configFileName, String[] path) {
config = new Config(configFileName);
translator = new InstrumentingTranslator(config);
ClassPool pool = ClassPool.getDefault();
try {
for (int i = 0; (path != null) && (i < path.length); i++) {
pool.appendClassPath(path[i]);
}
super.addTranslator(pool, translator);
} catch (NotFoundException e) {
throw new Error(e);
} catch (CannotCompileException e) {
throw new Error(e);
}
}
代码示例来源:origin: org.javassist/javassist
/**
* Requests the class loader to load a class.
*/
@Override
protected Class<?> loadClass(String name, boolean resolve)
throws ClassFormatError, ClassNotFoundException {
name = name.intern();
synchronized (name) {
Class<?> c = findLoadedClass(name);
if (c == null)
c = loadClassByDelegation(name);
if (c == null)
c = findClass(name);
if (c == null)
c = delegateToParent(name);
if (resolve)
resolveClass(c);
return c;
}
}
代码示例来源:origin: openimaj/openimaj
protected static Loader run(final ClassLoader parent, ClassPool pool, MultiTransformClassFileTransformer tf,
String mainClass, String[] args)
throws Throwable
{
final Loader cl = new Loader(parent, pool);
// Set the correct app name on OSX. Are there similar controls for other
// platforms?
System.setProperty("com.apple.mrj.application.apple.menu.about.name", mainClass);
// skip args4j
cl.delegateLoadingOf("org.apache.log4j.");
cl.addTranslator(pool, tf);
cl.run(mainClass, args);
return cl;
}
代码示例来源:origin: stackoverflow.com
Collection<Loader> loaders = new Mapper<File, Loader>(files) {
@Override public Loader map(File source) {
return new Loader(source);
}
}.apply();
内容来源于网络,如有侵权,请联系作者删除!