|
Download lang/lang-0042.jsonl from winapiadmin/tinycodinglm: direct link, hf CLI and curl.
- Browser
- Download file 2.84 kB
-
https://huggingface.co/datasets/winapiadmin/tinycodinglm/resolve/main/lang/lang-0042.jsonl
- Command line
-
hf download hf://datasets/winapiadmin/tinycodinglm/lang/lang-0042.jsonl
-
curl -L -o lang-0042.jsonl https://huggingface.co/datasets/winapiadmin/tinycodinglm/resolve/main/lang/lang-0042.jsonl
2.84 kB
| {"type": "session", "harness": "opencode", "id": "lang-0042", "name": "Creating a custom ClassLoader from scratch"} | |
| {"type": "message", "message": {"role": "system", "content": "You are an expert Java developer specializing in reflection, dynamic class loading, and modern Java APIs for runtime introspection."}} | |
| {"type": "message", "message": {"role": "user", "content": "Create a custom ClassLoader from scratch. Show how to override findClass, defineClass, and handle class loading from a byte array. Include a complete working example."}} | |
| {"type": "message", "message": {"role": "assistant", "content": "Here is a complete custom ClassLoader that loads classes from byte arrays.\n\n```java\nimport java.nio.file.*;\nimport java.util.*;\n\npublic class CustomClassLoader extends ClassLoader {\n private final Map<String, Class<?>> classCache = new java.util.concurrent.ConcurrentHashMap<>();\n private final Path classPath;\n public CustomClassLoader(Path classPath, ClassLoader parent) { super(parent); this.classPath = classPath; }\n @Override\n protected Class<?> findClass(String name) throws ClassNotFoundException {\n if (classCache.containsKey(name)) return classCache.get(name);\n try { return super.findClass(name); } catch (ClassNotFoundException e) { }\n String fileName = name.replace('.', java.io.File.separatorChar) + \".class\";\n try {\n Path classFile = classPath.resolve(fileName);\n byte[] classBytes = java.nio.file.Files.readAllBytes(classFile);\n return defineClass(name, classBytes, 0, classBytes.length);\n } catch (java.io.IOException e) { throw new ClassNotFoundException(\"Could not load class: \" + name, e); }\n }\n public void loadClassFromBytes(String name, byte[] bytes) {\n Class<?> clazz = defineClass(name, bytes, 0, bytes.length);\n classCache.put(name, clazz);\n }\n public static void main(String[] args) throws Exception {\n Path currentDir = Paths.get(\".\");\n CustomClassLoader loader = new CustomClassLoader(currentDir, ClassLoader.getSystemClassLoader());\n String className = \"com.example.MyClass\";\n try {\n Class<?> clazz = loader.loadClass(className);\n System.out.println(\"Loaded: \" + clazz.getName());\n System.out.println(\"ClassLoader: \" + clazz.getClassLoader());\n } catch (ClassNotFoundException e) { System.out.println(\"Class not found: \" + e.getMessage()); }\n }\n}\n```\nKey concepts: findClass is called by loadClass after parent delegation fails. defineClass converts a byte array to a Class object. You can override loadClass for full control over delegation. Class caching prevents re-loading the same class. Custom ClassLoaders are essential for hot-reloading and plugin systems."}} | |