Class ClassScanningLoader
ClassScanningResult and ClassScanningIndex
instances from a serialized JSON snapshot.
This loader is primarily used by the Naftah Playground environment, where runtime classpath scanning results are precomputed and shipped as JSON instead of being generated at runtime.
The loader supports:
- Plain JSON and GZIP-compressed JSON inputs
- Sync and async reconstruction modes
- Reflection-based restoration of classes, methods, and constructors
Reconstructed metadata includes:
- Class loader mappings
- Class registries (accessible / instantiable classes)
- JVM methods and constructors
- Built-in function descriptors and metadata
Failure strategy: Any unresolved class, method, or constructor is skipped or replaced with safe defaults to ensure partial recovery instead of full failure.
Threading note: Async methods create a temporary thread pool per invocation.
- Author:
- Chakib Daii
-
Field Summary
FieldsModifier and TypeFieldDescriptionThread-safe cache of resolvedClassobjects indexed by fully qualified class name. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionprivate static NaftahFunctiondeserializeFunction(javax.json.JsonObject o) Reconstructs aNaftahFunctiondescriptor from JSON.private static NaftahFunctionProviderdeserializeProvider(javax.json.JsonObject o) Reconstructs aNaftahFunctionProviderdescriptor from JSON.private static Constructor<?>findConstructor(Class<?> clazz, Class<?>[] params) Attempts to locate a declared constructor.private static MethodfindMethod(Class<?> clazz, String name, Class<?>[] params) Attempts to locate a declared method.static ClassScanningIndexloadClassScanningIndexFromJson(Path path, boolean async) Loads a lightweightClassScanningIndexfrom a JSON snapshot file.static ClassScanningIndexloadClassScanningIndexFromJson(javax.json.JsonObject obj) Reconstructs aClassScanningIndexfrom JSON.static ClassScanningIndexloadClassScanningIndexFromJsonAsync(javax.json.JsonObject obj) Asynchronously reconstructs aClassScanningIndex.static ClassScanningResultloadClassScanningResultFromJson(Path path, boolean async) Loads and reconstructs aClassScanningResultfrom a JSON snapshot file.static ClassScanningResultloadClassScanningResultFromJson(javax.json.JsonObject obj) Reconstructs aClassScanningResultfrom its JSON representation.static ClassScanningResultloadClassScanningResultFromJsonAsync(javax.json.JsonObject obj) Reconstructs aClassScanningResultfrom JSON using parallel execution.static <R> RloadFromJson(Path path, Function<javax.json.JsonObject, R> loader) Generic JSON snapshot loader that handles file IO, compression detection, and parsing.private static Class<?>Resolves a class by name usingClassUtils, with caching and safe fallback.private static BuiltinFunctionInfo[]toBuiltinFunctionArray(javax.json.JsonArray arr) Converts a JSON array of serialized built-in function metadata into an array ofBuiltinFunctionInfoobjects.private static Map<String,List<BuiltinFunction>> toBuiltinFunctions(javax.json.JsonObject obj) Reconstructs built-in function metadata from its serialized form.private static Class<?>[]toClassArray(javax.json.JsonArray arr) Converts a JSON array of class names into an array of resolved classes.private static Map<String,ClassLoader> toClassLoaders(javax.json.JsonObject obj) Reconstructs the exported class loader mapping.toClassMap(javax.json.JsonObject obj) Converts a JSON object containing class names into a map of resolvedClassinstances.private static Map<String,List<JvmClassInitializer>> toJvmClassInitializers(javax.json.JsonObject obj) Reconstructs JVM constructor metadata from its serialized form.private static Map<String,List<JvmFunction>> toJvmFunctions(javax.json.JsonObject obj) Reconstructs JVM function metadata from its serialized form.toMap(javax.json.JsonObject obj) Converts a JSON object containing string values into aMap.toSet(javax.json.JsonArray arr) Converts a JSON array of strings into aSet.private static String[]toStringArray(javax.json.JsonArray arr) Converts aJsonArraycontaining string values into aString[].
-
Field Details
-
CLASS_CACHE
Thread-safe cache of resolvedClassobjects indexed by fully qualified class name.This cache is used to avoid repeated and expensive class-loading operations for the same class name during runtime resolution/deserialization.
Entries are stored indefinitely for the lifetime of the JVM.
-
-
Constructor Details
-
ClassScanningLoader
private ClassScanningLoader()Private constructor to prevent instantiation. ThrowsNaftahBugErrorif called.
-
-
Method Details
-
loadClassScanningResultFromJson
public static ClassScanningResult loadClassScanningResultFromJson(Path path, boolean async) throws Exception Loads and reconstructs aClassScanningResultfrom a JSON snapshot file.The input file may be either:
- Plain JSON
- GZIP-compressed JSON (auto-detected via header)
When
async = true, reconstruction of major sections (classes, functions, metadata) is performed in parallel using a temporary executor service.- Parameters:
path- the snapshot file pathasync- whether to reconstruct the result using parallel execution- Returns:
- the reconstructed
ClassScanningResult - Throws:
Exception- if the file cannot be read or parsed
-
loadClassScanningResultFromJson
Reconstructs aClassScanningResultfrom its JSON representation.The JSON must match the structure produced by the Naftah class scanning export system.
This method performs synchronous reconstruction of:
- Class loader mappings
- Class registries
- JVM functions and constructors
- Built-in functions
- Parameters:
obj- serialized class scanning snapshot- Returns:
- reconstructed
ClassScanningResult
-
loadClassScanningResultFromJsonAsync
Reconstructs aClassScanningResultfrom JSON using parallel execution.Each major section of the snapshot is loaded concurrently using a fixed thread pool sized to the available CPU cores.
The executor is created per invocation and shut down after completion.
This method is functionally equivalent to the synchronous loader but optimized for large snapshots and browser-playground workloads.
- Parameters:
obj- serialized class scanning snapshot- Returns:
- reconstructed
ClassScanningResult
-
loadClassScanningIndexFromJson
public static ClassScanningIndex loadClassScanningIndexFromJson(Path path, boolean async) throws Exception Loads a lightweightClassScanningIndexfrom a JSON snapshot file.The index version contains only minimal metadata required for fast lookup:
- Class names
- Class qualifiers
- Localized (Arabic) qualifiers
- Built-in function metadata
This is a reduced form of
ClassScanningResultintended for fast startup and lightweight runtime usage.- Parameters:
path- snapshot file pathasync- whether to use parallel reconstruction- Returns:
- reconstructed
ClassScanningIndex - Throws:
Exception- if loading or parsing fails
-
loadClassScanningIndexFromJson
Reconstructs aClassScanningIndexfrom JSON.This method extracts only lightweight metadata and does not resolve full class or method structures.
- Parameters:
obj- serialized index snapshot- Returns:
- reconstructed index
-
loadClassScanningIndexFromJsonAsync
Asynchronously reconstructs aClassScanningIndex.Uses a temporary thread pool to parallelize parsing of metadata sections.
- Parameters:
obj- serialized index snapshot- Returns:
- reconstructed index
-
loadFromJson
public static <R> R loadFromJson(Path path, Function<javax.json.JsonObject, R> loader) throws ExceptionGeneric JSON snapshot loader that handles file IO, compression detection, and parsing.This method:
- Detects GZIP compression via magic bytes
- Parses JSON using
JsonReader - Delegates conversion to the provided loader function
Performance diagnostics are logged at TRACE level.
- Type Parameters:
R- return type- Parameters:
path- input snapshot fileloader- function that converts parsed JSON into a target object- Returns:
- reconstructed object
- Throws:
Exception- on IO or parsing failure
-
toClassLoaders
Reconstructs the exported class loader mapping.Each entry associates a class name with the URLs that were used to load it when the snapshot was generated.
- Parameters:
obj- the root JSON object- Returns:
- a map of class names to reconstructed class loaders,
or
nullif no class loader information is present
-
toClassMap
Converts a JSON object containing class names into a map of resolvedClassinstances.Classes are loaded without initialization using
safeClass(String).- Parameters:
obj- the JSON representation- Returns:
- a map of aliases to resolved classes, or
null
-
toSet
Converts a JSON array of strings into aSet.- Parameters:
arr- the JSON array- Returns:
- a set containing all string values, or
nullif the array isnull
-
toMap
Converts a JSON object containing string values into aMap.- Parameters:
obj- the JSON object- Returns:
- a map containing all key-value pairs, or
nullif the object isnull
-
toJvmFunctions
Reconstructs JVM function metadata from its serialized form.Each function is restored by resolving its declaring class, parameter types, and backing
Methodinstance.- Parameters:
obj- the serialized JVM functions- Returns:
- the reconstructed JVM function registry
-
toJvmClassInitializers
private static Map<String,List<JvmClassInitializer>> toJvmClassInitializers(javax.json.JsonObject obj) Reconstructs JVM constructor metadata from its serialized form.Each initializer is restored by resolving its declaring class, parameter types, and backing
Constructorinstance.- Parameters:
obj- the serialized JVM class initializers- Returns:
- the reconstructed JVM class initializer registry
-
toBuiltinFunctions
Reconstructs built-in function metadata from its serialized form.Each entry is converted into a
BuiltinFunctionby restoring its backing method, provider information, and function descriptor.- Parameters:
obj- the serialized built-in functions- Returns:
- the reconstructed built-in function registry
-
toBuiltinFunctionArray
Converts a JSON array of serialized built-in function metadata into an array ofBuiltinFunctionInfoobjects.Each JSON element is expected to contain the following structure:
methodName- the JVM method nameclassName- declaring class namemethodParameterTypes- array of parameter type namescanonicalKey- canonical function identifierqualifiedAliases- array of fully qualified alias names
The returned array preserves the original JSON order. Each index corresponds directly to the input array index.
Failure behavior:
- If the input array is
null, an empty array is returned. - If an individual element fails to parse, it is skipped and the corresponding
array slot remains
null. - Parsing errors do not interrupt the overall conversion process.
This method is used as part of the lightweight
ClassScanningIndexreconstruction pipeline where only metadata (not executable reflection objects) is required.- Parameters:
arr- JSON array of built-in function metadata- Returns:
- an array of
BuiltinFunctionInfo, possibly containingnullentries
-
deserializeProvider
Reconstructs aNaftahFunctionProviderdescriptor from JSON.- Parameters:
o- the serialized provider descriptor- Returns:
- the reconstructed provider descriptor, or
null
-
deserializeFunction
Reconstructs aNaftahFunctiondescriptor from JSON.- Parameters:
o- the serialized function descriptor- Returns:
- the reconstructed function descriptor, or
null
-
toStringArray
Converts aJsonArraycontaining string values into aString[].If the provided array is
null, an empty array is returned.- Parameters:
arr- the JSON array to convert, ornull- Returns:
- a new array containing all string values from the JSON array,
or an empty array if
arrisnull
-
toClassArray
Converts a JSON array of class names into an array of resolved classes.Unresolvable classes are replaced with
ObjectviasafeClass(String).- Parameters:
arr- the JSON array of fully qualified class names- Returns:
- the resolved class array, never
null
-
findMethod
Attempts to locate a declared method.- Parameters:
clazz- the declaring classname- the method nameparams- the parameter types- Returns:
- the matching method, or
nullif it cannot be found
-
findConstructor
Attempts to locate a declared constructor.- Parameters:
clazz- the declaring classparams- the constructor parameter types- Returns:
- the matching constructor, or
nullif it cannot be found
-
safeClass
Resolves a class by name usingClassUtils, with caching and safe fallback.If resolution fails,
Objectis returned instead of throwing an exception, ensuring partial reconstruction of the snapshot.Results are cached in
CLASS_CACHEfor the lifetime of the JVM.- Parameters:
name- fully qualified class name- Returns:
- resolved class or
Objectif resolution fails
-