本文整理了Java中java.lang.ClassLoader.defineClass()
方法的一些代码示例,展示了ClassLoader.defineClass()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ClassLoader.defineClass()
方法的具体详情如下:
包路径:java.lang.ClassLoader
类名称:ClassLoader
方法名:defineClass
[英]Defines a new class with the specified name, byte code from the byte buffer and the optional protection domain. If the provided protection domain is null then a default protection domain is assigned to the class.
[中]使用指定的名称、字节缓冲区中的字节代码和可选的保护域定义一个新类。如果提供的保护域为空,则会将默认保护域分配给该类。
代码示例来源:origin: Graylog2/graylog2-server
public void defineClass(String className, byte[] bytes) {
super.defineClass(className, bytes, 0, bytes.length);
}
}
代码示例来源:origin: apache/geode
public Class addClass(final String className, final byte[] definition) {
return super.defineClass(className, definition, 0, definition.length);
}
}
代码示例来源:origin: pxb1988/dex2jar
public Class<?> xxxDefine(String type, byte[] data) {
return super.defineClass(type, data, 0, data.length);
}
}
代码示例来源:origin: stackoverflow.com
public static class ByteClassLoader extends URLClassLoader {
private final Map<String, byte[]> extraClassDefs;
public ByteClassLoader(URL[] urls, ClassLoader parent, Map<String, byte[]> extraClassDefs) {
super(urls, parent);
this.extraClassDefs = new HashMap<String, byte[]>(extraClassDefs);
}
@Override
protected Class<?> findClass(final String name) throws ClassNotFoundException {
byte[] classBytes = this.extraClassDefs.remove(name);
if (classBytes != null) {
return defineClass(name, classBytes, 0, classBytes.length);
}
return super.findClass(name);
}
}
代码示例来源:origin: voldemort/voldemort
public Class<?> loadClass(String className, byte[] classBuffer, int offset, int length) {
Class<?> loadedClass = super.findLoadedClass(className);
if(null == loadedClass) {
return super.defineClass(className, classBuffer, offset, length);
}
return loadedClass;
}
代码示例来源:origin: robovm/robovm
/**
* Defines a new class with the specified name, byte code from the byte
* buffer and the optional protection domain. If the provided protection
* domain is {@code null} then a default protection domain is assigned to
* the class.
*
* @param name
* the expected name of the new class, may be {@code null} if not
* known.
* @param b
* the byte buffer containing the byte code of the new class.
* @param protectionDomain
* the protection domain to assign to the loaded class, may be
* {@code null}.
* @return the {@code Class} object created from the data in {@code b}.
* @throws ClassFormatError
* if {@code b} does not contain a valid class.
* @throws NoClassDefFoundError
* if {@code className} is not equal to the name of the class
* contained in {@code b}.
*/
protected final Class<?> defineClass(String name, ByteBuffer b,
ProtectionDomain protectionDomain) throws ClassFormatError {
byte[] temp = new byte[b.remaining()];
b.get(temp);
return defineClass(name, temp, 0, temp.length, protectionDomain);
}
代码示例来源:origin: stackoverflow.com
public class DatabaseClassLoader extends ClassLoader {
public DatabaseClassLoader(ClassLoader parent, ... /* connection to database */ ) {
super(parent);
// store the connection
}
public Class findClass(String name) {
byte[] data = loadDataFromDatabase(name);
return defineClass(name, data, 0, data.length);
}
private byte[] loadDataFromDatabase(String name) {
// this is your job.
}
}
代码示例来源:origin: stackoverflow.com
class MyClassLoader extends ClassLoader {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
if (name is white-listed JDK class) return super.loadClass(name);
return findClass(name);
}
@Override
public Class findClass(String name) {
byte[] b = loadClassData(name);
return defineClass(name, b, 0, b.length);
}
private byte[] loadClassData(String name) {
// load the untrusted class data here
}
}
代码示例来源:origin: stackoverflow.com
try {
byte[] bytes = loadClassData(s);
return defineClass(s, bytes, 0, bytes.length);
} catch (IOException ioe) {
try {
代码示例来源:origin: google/j2objc
getGenerator(r).generateClass(w);
byte[] b = w.toByteArray();
Class c = super.defineClass(name, b, 0, b.length, DOMAIN);
postProcess(c);
return c;
代码示例来源:origin: cglib/cglib
getGenerator(r).generateClass(w);
byte[] b = w.toByteArray();
Class c = super.defineClass(name, b, 0, b.length, DOMAIN);
postProcess(c);
return c;
代码示例来源:origin: org.easymock/easymock
getGenerator(r).generateClass(w);
byte[] b = w.toByteArray();
Class c = super.defineClass(name, b, 0, b.length, DOMAIN);
postProcess(c);
return c;
代码示例来源:origin: cglib/cglib
getGenerator(r).generateClass(w);
byte[] b = w.toByteArray();
Class c = super.defineClass(name, b, 0, b.length, DOMAIN);
postProcess(c);
return c;
代码示例来源:origin: stackoverflow.com
java.lang.NoClassDefFoundError: com/my/package/MyClass
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at Main$MyClassLoader.getClasses(Main.java:78)
at Main.main(Main.java:109)
Caused by: java.lang.ClassNotFoundException: com.my.package.MyClass
代码示例来源:origin: inferred/FreeBuilder
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (name.startsWith(JAVAC_METHOD_INTROSPECTOR)) {
String path = name.replace('.', '/').concat(".class");
URL resource = processorLoader.getResource(path);
try (InputStream stream = resource.openStream()) {
byte[] bytes = ByteStreams.toByteArray(stream);
return super.defineClass(name, bytes, 0, bytes.length);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
try {
return processingEnvironmentLoader.loadClass(name);
} catch (ClassNotFoundException e) {
return processorLoader.loadClass(name);
}
}
}
}
代码示例来源:origin: biezhi/learn-java8
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
Iterator<ClassJavaFileObject> itr = files.iterator();
while (itr.hasNext()) {
ClassJavaFileObject file = itr.next();
if (file.getClassName().equals(name)) {
itr.remove();
byte[] bytes = file.getBytes();
return super.defineClass(name, bytes, 0, bytes.length);
}
}
return super.findClass(name);
}
}
代码示例来源:origin: stackoverflow.com
byte[] buf = new byte[10000];
int len = is.read(buf);
return defineClass(name, buf, 0, len);
} catch (IOException e) {
throw new ClassNotFoundException("", e);
代码示例来源:origin: inferred/FreeBuilder
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
InMemoryJavaFile classFile = javaFiles.get(FileKey.forClass(location, name, CLASS));
if (classFile == null) {
throw new ClassNotFoundException();
}
return super.defineClass(name, classFile.getBuffer(), null);
}
};
代码示例来源:origin: inferred/FreeBuilder
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
if (testFiles.contains(name.split("[$]", 0)[0])) {
throw new ClassNotFoundException();
}
try {
JavaFileObject classFile = fileManager.getJavaFileForInput(
StandardLocation.CLASS_OUTPUT, name, CLASS);
if (classFile == null) {
throw new ClassNotFoundException();
}
byte[] bytes = ByteStreams.toByteArray(classFile.openInputStream());
return super.defineClass(name, bytes, 0, bytes.length);
} catch (IOException e) {
throw new ClassNotFoundException();
}
}
}
代码示例来源:origin: inferred/FreeBuilder
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
if (!testFiles.contains(name.split("[$]", 0)[0])) {
return SingleBehaviorTester.class.getClassLoader().loadClass(name);
}
try {
JavaFileObject classFile = fileManager.getJavaFileForInput(
StandardLocation.CLASS_OUTPUT, name, CLASS);
if (classFile == null) {
throw new ClassNotFoundException();
}
byte[] bytes = ByteStreams.toByteArray(classFile.openInputStream());
return super.defineClass(name, bytes, 0, bytes.length);
} catch (IOException e) {
throw new ClassNotFoundException();
}
}
}
内容来源于网络,如有侵权,请联系作者删除!