Class ClassUtils
- Author:
- Chakib Daii
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringRegex pattern to split class names by dot '.' or dollar '$' sign.Commonly recognized factory-style static method names in Java.private static final String[]Common prefixes used to identify factory-like methods when scanning class members.static final PatternRegex to match strings with at least one ':' and exactly one '::', with '::' before the last segment.static final StringSeparator used to join qualified calls (class::method).static final StringSeparator used to join qualified names. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringclassToDetailedString(Class<?> clazz) Returns a detailed, Arabic-formatted string representation of the specified Java class.static StringcleanBuiltinFunctionName(String name, String replacement, boolean removeDiacritics) Normalizes a function or provider name by optionally removing Arabic diacritics and replacing whitespace with underscores.Filters a map of classes by applying a predicate on the Class objects.getArabicClassQualifiers(Collection<String[]> classQualifiers) Transliterates a collection of class qualifiers parts arrays into Arabic qualified names.getArabicClassQualifiersMapping(Collection<Pair<String, String[]>> classQualifiers) Generates a mapping from Arabic-transliterated fully qualified class names to the original fully qualified class names.static StringgetBuiltinFunctionName(boolean useQualifiedName, String providerName, String functionName, boolean removeNameDiacritics) Constructs the canonical name for a built-in function, optionally using a qualified name that includes the provider name.static List<BuiltinFunction>getBuiltinMethods(Class<?> clazz) Retrieves built-in methods annotated with @NaftahFn from a single class annotated with @NaftahFnProvider.static Map<String,List<BuiltinFunction>> getBuiltinMethods(Map<String, Class<?>> classes) Retrieves all built-in methods from classes annotated with @NaftahFnProvider.static Map<String,List<BuiltinFunction>> Retrieves built-in methods annotated with @NaftahFn from classes annotated with @NaftahFnProvider, filtered by a method predicate.static List<BuiltinFunction>getBuiltinMethods(Set<Class<?>> classes) Retrieves built-in functions from multiple classes annotated withNaftahFnProvider.static List<JvmClassInitializer>getClassConstructors(String qualifiedName, Class<?> clazz) Retrieves all public constructors of the specified class and wraps them asJvmClassInitializerinstances.static Map<String,List<JvmClassInitializer>> getClassConstructors(Map<String, Class<?>> classes) Retrieves all public constructors from the given classes without applying any filtering, mapping each qualified class name to a list ofJvmClassInitializerwrappers.static Map<String,List<JvmClassInitializer>> getClassConstructors(Map<String, Class<?>> classes, Predicate<Constructor<?>> constructorPredicate) Retrieves and groups constructors from multiple classes, filtered by a specified predicate, mapping each qualified class name to a list ofJvmClassInitializerwrappers.static List<JvmFunction>getClassMethods(String qualifiedName, Class<?> clazz) Retrieves all methods of the given class, wrapped asJvmFunctioninstances.static List<JvmFunction>getClassMethods(String qualifiedName, Class<?> clazz, Predicate<Method> methodPredicate) Retrieves methods of the given class that satisfy a specified predicate, wrapped asJvmFunctioninstances.static Map<String,List<JvmFunction>> getClassMethods(Map<String, Class<?>> classes) Returns all methods from given classes without filtering.static Map<String,List<JvmFunction>> Retrieves methods from given classes filtered by a predicate, grouped by their qualified call strings.getClassQualifiers(Set<String> classNames, boolean flattened) Extracts parts of class names and returns a mapping based on the specified mode.static StringgetQualifiedCall(String qualifiedName, Method method) Returns a qualified method call string by combining the qualified class name and the method name, transliterated into Arabic script.static StringgetQualifiedCall(String qualifiedName, String methodName) Returns a qualified method call string by combining the qualified class name and the method name, transliterated into Arabic script.static StringgetQualifiedName(String className) Returns a qualified name by transliterating each part of the class name to Arabic.static booleanhasFactoryMethod(Class<?> clazz) Determines whether the specified class defines at least one public static factory method capable of producing an instance of that class (or a subclass of it).static booleanisAccessibleClass(Class<?> clazz) Checks if the class has at least one public static factory method.private static booleanisFactoryName(String name) Determines whether a given method name matches a known factory method pattern.static booleanisInstantiableClass(Class<?> clazz) Checks if the class is instantiable: public, not abstract/interface, and has a public no-arg constructor.static booleanisInvocable(Executable methodOrConstructor) Determines whether a givenExecutable(method or constructor) can be invoked dynamically.static booleanChecks if a method is static.static Class<?>normalizePrimitive(Class<?> clazz) Normalizes a primitiveClassto its corresponding wrapper type.
-
Field Details
-
CLASS_SEPARATORS_REGEX
Regex pattern to split class names by dot '.' or dollar '$' sign.- See Also:
-
QUALIFIED_NAME_SEPARATOR
Separator used to join qualified names.- See Also:
-
QUALIFIED_CALL_SEPARATOR
Separator used to join qualified calls (class::method).- See Also:
-
QUALIFIED_CALL_REGEX
Regex to match strings with at least one ':' and exactly one '::', with '::' before the last segment. -
FACTORY_NAMES
Commonly recognized factory-style static method names in Java.These names are used by
hasFactoryMethod(Class<?>)to detect whether a class provides a static creation method that returns instances of itself or its subclasses. This allows Naftah to treat such methods as "constructors" in its interop layer.The list includes canonical JDK factories (e.g.,
of,from), builder-style factories, parser/deserializer names, and provider patterns from the JDK, Guava, Jackson, and Spring ecosystems. -
FACTORY_PREFIXES
Common prefixes used to identify factory-like methods when scanning class members.Unlike
FACTORY_NAMES, these are matched as prefixes, so methods such asnewBuilder(),createStream(),fromJsonString(), andgetInstanceSafe()are also recognized.This prefix-based detection is useful for frameworks that generate descriptive factory names dynamically or through code generation.
-
-
Constructor Details
-
ClassUtils
private ClassUtils()Private constructor to prevent instantiation. Always throws aNaftahBugErrorwhen called.
-
-
Method Details
-
getQualifiedName
Returns a qualified name by transliterating each part of the class name to Arabic. For example, "java.lang.String" becomes Arabic transliteration joined by colon.- Parameters:
className- the fully qualified Java class name- Returns:
- the qualified name transliterated to Arabic script
-
getQualifiedCall
Returns a qualified method call string by combining the qualified class name and the method name, transliterated into Arabic script.The format of the returned string is:
qualifiedName::methodName, where the method name is transliterated.- Parameters:
qualifiedName- the fully qualified name of the class (e.g.,com.example.MyClass)method- theMethodinstance representing the method- Returns:
- a string in the form
qualifiedName::methodNamewith the method name in Arabic script
-
getQualifiedCall
Returns a qualified method call string by combining the qualified class name and the method name, transliterated into Arabic script.The format of the returned string is:
qualifiedName::methodName, where the method name is transliterated.- Parameters:
qualifiedName- the fully qualified name of the class (e.g.,com.example.MyClass)methodName- the name of the method as a string- Returns:
- a string in the form
qualifiedName::methodNamewith the method name in Arabic script
-
getClassQualifiers
public static Map<String,Pair<String, getClassQualifiersString[]>> (Set<String> classNames, boolean flattened) Extracts parts of class names and returns a mapping based on the specified mode.If
flattenedistrue, the returned map contains each individual class part as a key, mapped to a pair of the original class name and its array of parts. Duplicate keys are resolved by keeping the first occurrence.If
flattenedisfalse, the returned map uses the fully qualified class name as the key, mapped to a pair of the original class name and its array of parts.- Parameters:
classNames- a set of fully qualified class names to processflattened- iftrue, returns a map of individual parts; iffalse, returns a map keyed by fully qualified names- Returns:
- a map where keys are either class parts or fully qualified class names,
and values are
Pairobjects containing the original class name and its parts array
-
getArabicClassQualifiers
Transliterates a collection of class qualifiers parts arrays into Arabic qualified names.- Parameters:
classQualifiers- collection of class parts arrays- Returns:
- set of Arabic transliterated qualified names joined by colon
-
getArabicClassQualifiersMapping
public static Map<String,String> getArabicClassQualifiersMapping(Collection<Pair<String, String[]>> classQualifiers) Generates a mapping from Arabic-transliterated fully qualified class names to the original fully qualified class names.Each entry in the returned map uses the Arabic transliteration of the class parts (joined by
QUALIFIED_NAME_SEPARATOR) as the key, and the original fully qualified name as the value. If multiple original names transliterate to the same Arabic string, the first occurrence is kept.- Parameters:
classQualifiers- a collection ofPairobjects, where the left element is the original class name and the right element is an array of class name parts- Returns:
- a map of Arabic-transliterated qualified names to original qualified names
-
getClassMethods
public static Map<String,List<JvmFunction>> getClassMethods(Map<String, Class<?>> classes, Predicate<Method> methodPredicate) Retrieves methods from given classes filtered by a predicate, grouped by their qualified call strings.Usage example:
Set<Class> classes =ClassUtils.getClasses();Predicate<Method> instancePublicMethodPredicate = method ->Modifier.isPublic(method.getModifiers());Map<Class,Method[]> instancePublicMethods =ClassUtils.getClassMethods(classes, instancePublicMethodPredicate);- Parameters:
classes- map of qualified class names to Class objectsmethodPredicate- predicate to filter methods- Returns:
- map from qualified call string to list of JvmFunction wrappers
-
getClassConstructors
public static Map<String,List<JvmClassInitializer>> getClassConstructors(Map<String, Class<?>> classes, Predicate<Constructor<?>> constructorPredicate) Retrieves and groups constructors from multiple classes, filtered by a specified predicate, mapping each qualified class name to a list ofJvmClassInitializerwrappers.For each class in the provided map, this method inspects its public constructors (via
Class.getConstructors()), applies the givenconstructorPredicate, and wraps all matching constructors asJvmClassInitializerinstances. The results are then grouped by their qualified class names.Any classes or constructors that cannot be accessed or processed (e.g., due to security restrictions) are silently skipped.
- Parameters:
classes- a map where each key is the fully qualified class name (e.g.,"com.example.MyClass") and each value is the correspondingClassobjectconstructorPredicate- aPredicateused to filter which constructors should be included- Returns:
- a map where each key is a qualified class name and each value is a list of
JvmClassInitializerinstances representing all constructors of that class that satisfy the given predicate - Throws:
NullPointerException- ifclassesorconstructorPredicateisnull- See Also:
-
getClassMethods
Retrieves all methods of the given class, wrapped asJvmFunctioninstances.This variant retrieves all public methods of the class, including inherited methods, without any filtering.
- Parameters:
qualifiedName- the fully qualified name of the classclazz- theClassobject to inspect- Returns:
- a list of
JvmFunctioninstances representing all class methods
-
getClassMethods
public static List<JvmFunction> getClassMethods(String qualifiedName, Class<?> clazz, Predicate<Method> methodPredicate) Retrieves methods of the given class that satisfy a specified predicate, wrapped asJvmFunctioninstances.The provided
methodPredicateis applied to each public method of the class to determine whether it should be included in the result.- Parameters:
qualifiedName- the fully qualified name of the classclazz- theClassobject to inspectmethodPredicate- aPredicateused to filter methods- Returns:
- a list of
JvmFunctioninstances representing the filtered methods
-
getClassConstructors
Retrieves all public constructors of the specified class and wraps them asJvmClassInitializerinstances.Each
JvmClassInitializerrepresents a single constructor and includes its qualified name and reflective metadata.- Parameters:
qualifiedName- the fully qualified name of the class (e.g., "com.example.MyClass")clazz- theClassobject representing the class whose constructors should be retrieved- Returns:
- a list of
JvmClassInitializerinstances representing all public constructors of the specified class
-
getClassMethods
Returns all methods from given classes without filtering.- Parameters:
classes- map of qualified class names to Class objects- Returns:
- map from qualified call string to list of JvmFunction wrappers
-
getClassConstructors
public static Map<String,List<JvmClassInitializer>> getClassConstructors(Map<String, Class<?>> classes) Retrieves all public constructors from the given classes without applying any filtering, mapping each qualified class name to a list ofJvmClassInitializerwrappers.This method is a convenience overload of
getClassConstructors(Map, java.util.function.Predicate)that includes all available constructors by default.- Parameters:
classes- a map where each key is the fully qualified class name (e.g.,"com.example.MyClass") and each value is the correspondingClassobject- Returns:
- a map from qualified class names to lists of
JvmClassInitializerinstances representing all public constructors of the given classes - Throws:
NullPointerException- ifclassesisnull- See Also:
-
isAccessibleClass
Checks if the class has at least one public static factory method.- Parameters:
clazz- the class to check- Returns:
- true if accessible (has public static method), false otherwise
-
isInstantiableClass
Checks if the class is instantiable: public, not abstract/interface, and has a public no-arg constructor.- Parameters:
clazz- the class to check- Returns:
- true if instantiable, false otherwise
-
hasFactoryMethod
Determines whether the specified class defines at least one public static factory method capable of producing an instance of that class (or a subclass of it).This method is used by the Naftah Java interop layer to identify Java types that are constructible through factory methods rather than public constructors. Examples include:
Optional.of(),LocalDate.parse(), andList.copyOf().The detection relies on a curated set of known factory method names and prefixes (see
FACTORY_NAMESandFACTORY_PREFIXES). The following conditions must all be satisfied for a method to be recognized as a valid factory:- The method must be
public static. - It must not be a compiler-generated bridge or synthetic method.
- Its name must either match a known factory name
(e.g.
of,from,valueOf) or begin with a recognized prefix (e.g.create,new,build). - It must return a non-
voidtype assignable to the declaring class, or whose simple name contains the declaring class’s simple name (to support builder/wrapper patterns).
Example classes detected as factory-capable:
Optional.of("value"); LocalDate.parse("2025-11-07"); List.copyOf(existingList); EnumSet.noneOf(MyEnum.class); HttpRequest.newBuilder().build();Classes that are primitives, arrays, or
voidare excluded immediately.- Parameters:
clazz- the class to inspect- Returns:
trueif the class declares at least one public static factory method that produces instances of itself or a compatible type;falseotherwise
- The method must be
-
isFactoryName
Determines whether a given method name matches a known factory method pattern.This method checks whether the name appears in the
FACTORY_NAMESset or begins with one of theFACTORY_PREFIXES. Matching is case-sensitive and uses simple string comparison for efficiency.- Parameters:
name- the method name to test- Returns:
trueif the name matches a known factory name or prefix,falseotherwise
-
isStatic
Checks if a method is static.- Parameters:
method- the method to check- Returns:
- true if static, false otherwise
-
isInvocable
Determines whether a givenExecutable(method or constructor) can be invoked dynamically.A member is considered invocable if it meets all of the following conditions:
- It is
public. - It is not synthetic or compiler-generated (e.g., bridge methods).
- If it is a
Method, it is eitherstaticor declared in an instantiable class. - If it is a
Constructor, it belongs to an instantiable class.
- Parameters:
methodOrConstructor- theExecutable(method or constructor) to check- Returns:
trueif the executable can be invoked via reflection, otherwisefalse- See Also:
- It is
-
filterClasses
public static Map<String,Class<?>> filterClasses(Map<String, Class<?>> classes, Predicate<Class<?>> classPredicate) Filters a map of classes by applying a predicate on the Class objects.- Parameters:
classes- map of qualified class names to Class objectsclassPredicate- predicate to filter classes- Returns:
- filtered map of classes
-
getBuiltinMethods
public static Map<String,List<BuiltinFunction>> getBuiltinMethods(Map<String, Class<?>> classes, Predicate<Method> methodPredicate) Retrieves built-in methods annotated with @NaftahFn from classes annotated with @NaftahFnProvider, filtered by a method predicate.- Parameters:
classes- map of qualified class names to Class objectsmethodPredicate- predicate to filter methods- Returns:
- map of function names to lists of BuiltinFunction instances
-
getBuiltinFunctionName
public static String getBuiltinFunctionName(boolean useQualifiedName, String providerName, String functionName, boolean removeNameDiacritics) Constructs the canonical name for a built-in function, optionally using a qualified name that includes the provider name.- Parameters:
useQualifiedName- whether to qualify the function name with the provider nameproviderName- the name of the function provider (e.g. extension or library name)functionName- the actual function nameremoveNameDiacritics- whether to remove Arabic diacritics from both provider and function names- Returns:
- the cleaned and optionally qualified function name, with spaces replaced by underscores
-
cleanBuiltinFunctionName
public static String cleanBuiltinFunctionName(String name, String replacement, boolean removeDiacritics) Normalizes a function or provider name by optionally removing Arabic diacritics and replacing whitespace with underscores.- Parameters:
name- the original name to cleanremoveDiacritics- whether to remove Arabic diacritics from the name- Returns:
- a sanitized string with optional diacritics removed and spaces replaced with underscores
-
getBuiltinMethods
Retrieves all built-in methods from classes annotated with @NaftahFnProvider.- Parameters:
classes- map of qualified class names to Class objects- Returns:
- map of function names to lists of BuiltinFunction instances
-
getBuiltinMethods
Retrieves built-in methods annotated with @NaftahFn from a single class annotated with @NaftahFnProvider.- Parameters:
clazz- the class to inspect- Returns:
- list of BuiltinFunction instances or empty list if not annotated
-
getBuiltinMethods
Retrieves built-in functions from multiple classes annotated withNaftahFnProvider.This method aggregates results from all provided classes by invoking
getBuiltinMethods(Class)on each one.- Parameters:
classes- the set of classes to inspect- Returns:
- a combined list of
BuiltinFunctioninstances from all classes, or an empty list if none are found
-
classToDetailedString
Returns a detailed, Arabic-formatted string representation of the specified Java class.The returned string includes the following information about the class:
- Fully qualified class name (e.g.,
java.util.ArrayList) - Simple class name (e.g.,
ArrayList) - Package name (or "(default)" if the class is in the default package)
- Class modifiers (whether it's
public,abstract, or aninterface) - The superclass of the class (if any)
- All interfaces implemented by the class
- Declaring class (if the class is an inner or nested class)
- Whether the class is an
enum,annotation,record, orprimitivetype
Additionally, the method includes Arabic transliteration (phonetic representation in Arabic script) for class names and package names, using
getQualifiedName(String)orScriptUtils.transliterateToArabicScriptDefault(String...). For example, the simple nameArrayListmight appear as:ArrayList - أرَي لِسْتْThe output is formatted as a multi-line string with Arabic labels for each item. Example output (for
java.util.ArrayList):تفاصيل الصنف: - الاسم الكامل: java.util.ArrayList - جاڤا:يوتِل:أرَي_لِسْتْ - الاسم المختصر: ArrayList - أرَي_لِسْتْ - الحزمة: java.util - جاڤا:يوتِل - عام؟: نعم - مجرد؟: لا - واجهة؟: لا - الصنف الأب: java.util.AbstractList - جاڤا:يوتِل:أَبْسْتْرَكْتْ_لِسْتْ - الواجهات: - java.util.List - جاڤا:يوتِل:لِسْتْ - java.util.RandomAccess - جاڤا:يوتِل:رَانْدُم_أَكْسِسْ - java.lang.Cloneable - جاڤا:لانْغ:كْلُونِبْلْ - java.io.Serializable - جاڤا:أَي_أُو:سِرِيَالِيزَابْلْ - تعداد؟: لا - توصيف؟: لا - سجل؟: لا - نوع بدائي؟: لا
- Parameters:
clazz- theClassobject to inspect- Returns:
- a multi-line, Arabic-formatted string describing the class, including Arabic transliterations
- Fully qualified class name (e.g.,
-
normalizePrimitive
Normalizes a primitiveClassto its corresponding wrapper type.If the provided class represents a primitive type (e.g.,
int.class), this method returns the corresponding wrapper class (e.g.,Integer). If the class is not primitive, it is returned unchanged.This is useful for reflective or generic type comparisons where primitive and wrapper types should be considered equivalent.
Examples:
normalizePrimitive(int.class); // returns Integer.class normalizePrimitive(boolean.class); // returns Boolean.class normalizePrimitive(String.class); // returns String.class (unchanged)- Parameters:
clazz- the class to normalize; must not be null- Returns:
- the wrapper class for a primitive type, or the original class if not primitive
-