public void defineClass(String className, byte[] bytes) { super.defineClass(className, bytes, 0, bytes.length); } }
public Class addClass(final String className, final byte[] definition) { return super.defineClass(className, definition, 0, definition.length); } }
public Class<?> xxxDefine(String type, byte[] data) { return super.defineClass(type, data, 0, data.length); } }
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); } }
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; }
/** * 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); }
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. } }
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 } }
try { byte[] bytes = loadClassData(s); return defineClass(s, bytes, 0, bytes.length); } catch (IOException ioe) { try {
getGenerator(r).generateClass(w); byte[] b = w.toByteArray(); Class c = super.defineClass(name, b, 0, b.length, DOMAIN); postProcess(c); return c;
getGenerator(r).generateClass(w); byte[] b = w.toByteArray(); Class c = super.defineClass(name, b, 0, b.length, DOMAIN); postProcess(c); return c;
getGenerator(r).generateClass(w); byte[] b = w.toByteArray(); Class c = super.defineClass(name, b, 0, b.length, DOMAIN); postProcess(c); return c;
getGenerator(r).generateClass(w); byte[] b = w.toByteArray(); Class c = super.defineClass(name, b, 0, b.length, DOMAIN); postProcess(c); return c;
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
@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); } } } }
@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); } }
byte[] buf = new byte[10000]; int len = is.read(buf); return defineClass(name, buf, 0, len); } catch (IOException e) { throw new ClassNotFoundException("", e);
@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); } };
@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(); } } }
@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(); } } }