Skip to content

io microsphere util ClassUtils

github-actions[bot] edited this page Jul 31, 2026 · 18 revisions

ClassUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.util | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/util/ClassUtils.java

Overview

Class utility class

Declaration

public abstract class ClassUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.3.16-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

isArray

boolean isPrimitive = ClassUtils.PRIMITIVE_TYPES.contains(int.class);   // true
  boolean isNotPrimitive = ClassUtils.PRIMITIVE_TYPES.contains(Integer.class); // false
  int count = ClassUtils.PRIMITIVE_TYPES.size(); // 9 (includes void)
boolean isWrapper = ClassUtils.WRAPPER_TYPES.contains(Integer.class); // true
  boolean isNotWrapper = ClassUtils.WRAPPER_TYPES.contains(int.class);  // false
  for (Class<?> wrapperType : ClassUtils.WRAPPER_TYPES) {
      System.out.println(wrapperType.getSimpleName());
  }
boolean isPrimArray = ClassUtils.PRIMITIVE_ARRAY_TYPES.contains(int[].class);      // true
  boolean isNotPrimArray = ClassUtils.PRIMITIVE_ARRAY_TYPES.contains(Integer[].class); // false
  int count = ClassUtils.PRIMITIVE_ARRAY_TYPES.size(); // 8
// Internal cache used by isConcreteClass(Class):
  boolean concrete = ClassUtils.isConcreteClass(String.class); // true, result cached
  boolean abstractType = ClassUtils.isConcreteClass(Number.class); // false, result cached
boolean result1 = ClassUtils.isArray(new int[]{1, 2, 3});  // returns true
boolean result2 = ClassUtils.isArray("Hello");             // returns false
boolean result3 = ClassUtils.isArray(null);                // returns false

isArray

boolean result1 = ClassUtils.isArray(int[].class);     // returns true
boolean result2 = ClassUtils.isArray(String.class);    // returns false
boolean result3 = ClassUtils.isArray(null);           // returns false

isConcreteClass

boolean result1 = ClassUtils.isConcreteClass(String.class);  // returns true
boolean result2 = ClassUtils.isConcreteClass(List.class);    // returns false (interface)
boolean result3 = ClassUtils.isConcreteClass(null);         // returns false

isAbstractClass

abstract class AbstractExample { }
class ConcreteExample { }

boolean result1 = ClassUtils.isAbstractClass(AbstractExample.class); // returns true
boolean result2 = ClassUtils.isAbstractClass(ConcreteExample.class); // returns false
boolean result3 = ClassUtils.isAbstractClass(null);                  // returns false

isInterface

boolean result1 = ClassUtils.isInterface(List.class);    // returns true
boolean result2 = ClassUtils.isInterface(String.class);  // returns false
boolean result3 = ClassUtils.isInterface(null);          // returns false

isGeneralClass

class ExampleClass {}
abstract class AbstractExample {}
interface ExampleInterface {}

boolean result1 = ClassUtils.isGeneralClass(ExampleClass.class);      // returns true
boolean result2 = ClassUtils.isGeneralClass(AbstractExample.class);   // returns false (abstract)
boolean result3 = ClassUtils.isGeneralClass(ExampleInterface.class);  // returns false (interface)
boolean result4 = ClassUtils.isGeneralClass(null);                   // returns false

isTopLevelClass

class TopLevelClass {}

class OuterClass {
    static class StaticNestedClass {}
    class InnerClass {}

    void method() {
        class LocalClass {}
    }
}

boolean result1 = ClassUtils.isTopLevelClass(TopLevelClass.class);     // returns true
boolean result2 = ClassUtils.isTopLevelClass(OuterClass.InnerClass.class); // returns false
boolean result3 = ClassUtils.isTopLevelClass(OuterClass.StaticNestedClass.class); // returns true
boolean result4 = ClassUtils.isTopLevelClass(null);                    // returns false

isSyntheticClass

// Assuming MyClass has an inner class or lambda expression
Object syntheticObject = new MyClass$1(); // Example of a synthetic class instance

boolean result1 = ClassUtils.isSyntheticClass(syntheticObject); // returns true
boolean result2 = ClassUtils.isSyntheticClass(new MyClass());   // returns false
boolean result3 = ClassUtils.isSyntheticClass(null);            // returns false

isSyntheticClass

// Assuming MyClass has an inner class or lambda expression
Class<?> syntheticClass = MyClass$1.class; // Example of a synthetic class

boolean result1 = ClassUtils.isSyntheticClass(syntheticClass); // returns true
boolean result2 = ClassUtils.isSyntheticClass(MyClass.class);  // returns false
boolean result3 = ClassUtils.isSyntheticClass(null);           // returns false

isLambdaClass

// Assuming MyClass has a lambda expression
Object lambdaObject = (Runnable) () -> System.out.println("Hello, Lambda!");

boolean result1 = ClassUtils.isLambdaClass(lambdaObject); // returns true
boolean result2 = ClassUtils.isLambdaClass(new MyClass()); // returns false
boolean result3 = ClassUtils.isLambdaClass(null);          // returns false

isLambdaClass

// Assuming MyClass has a lambda expression
Class<?> lambdaClass = MyClass$$Lambda$1.class; // Example of a lambda class

boolean result1 = ClassUtils.isLambdaClass(lambdaClass); // returns true
boolean result2 = ClassUtils.isLambdaClass(MyClass.class); // returns false
boolean result3 = ClassUtils.isLambdaClass(null);          // returns false

isLambdaClassName

String lambdaClassName = "com.example.MyClass$$Lambda$1"; // Example of a lambda class name

boolean result1 = ClassUtils.isLambdaClassName(lambdaClassName); // returns true
boolean result2 = ClassUtils.isLambdaClassName("com.example.MyClass"); // returns false
boolean result3 = ClassUtils.isLambdaClassName(null);          // returns false

isFunctionalInterface

@FunctionalInterface
interface MyFunctionalInterface {
    void doSomething();
}

boolean result1 = ClassUtils.isFunctionalInterface(MyFunctionalInterface.class); // returns true
boolean result2 = ClassUtils.isFunctionalInterface(Runnable.class);              // returns true
boolean result3 = ClassUtils.isFunctionalInterface(List.class);                  // returns false (not a functional interface)
boolean result4 = ClassUtils.isFunctionalInterface(null);                        // returns false

isPrimitive

boolean result1 = ClassUtils.isPrimitive(int.class);        // returns true
boolean result2 = ClassUtils.isPrimitive(Integer.class);    // returns false (wrapper class)
boolean result3 = ClassUtils.isPrimitive(int[].class);      // returns true (primitive array)
boolean result4 = ClassUtils.isPrimitive(String.class);     // returns false
boolean result5 = ClassUtils.isPrimitive(null);            // returns false

isFinal

final class FinalExample {}
class NonFinalExample {}

boolean result1 = ClassUtils.isFinal(FinalExample.class);   // returns true
boolean result2 = ClassUtils.isFinal(NonFinalExample.class); // returns false
boolean result3 = ClassUtils.isFinal(null);                 // returns false

isSimpleType

boolean result1 = ClassUtils.isSimpleType("Hello");  // returns true
boolean result2 = ClassUtils.isSimpleType(123);      // returns true
boolean result3 = ClassUtils.isSimpleType(new ArrayList<>()); // returns false
boolean result4 = ClassUtils.isSimpleType(null);     // returns false

isSimpleType

boolean result1 = ClassUtils.isSimpleType(String.class);  // returns true
boolean result2 = ClassUtils.isSimpleType(List.class);    // returns false
boolean result3 = ClassUtils.isSimpleType(null);         // returns false

isCharSequence

boolean result1 = ClassUtils.isCharSequence("Hello");     // returns true
boolean result2 = ClassUtils.isCharSequence(new StringBuilder("World")); // returns true
boolean result3 = ClassUtils.isCharSequence(123);         // returns false
boolean result4 = ClassUtils.isCharSequence(null);        // returns false

isCharSequence

boolean result = ClassUtils.isCharSequence(String.class); // returns true
    boolean result2 = ClassUtils.isCharSequence(Integer.class); // returns false

isNumber

boolean result1 = ClassUtils.isNumber(Integer.valueOf(10));  // returns true
boolean result2 = ClassUtils.isNumber(10.5);                 // returns true
boolean result3 = ClassUtils.isNumber("123");                // returns false
boolean result4 = ClassUtils.isNumber(null);                 // returns false

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.util.ClassUtils;

API Reference

Public Methods

Method Description
isArray Suffix for array class names: "[]"
isArray Checks if the specified type is an array.
isConcreteClass Checks if the specified type is a concrete class.
isAbstractClass Checks if the specified type is an abstract class.
isInterface Checks if the specified type is an interface.
isGeneralClass Checks if the specified type is a general class.
isTopLevelClass Is the specified type a general class or not?
isSyntheticClass Checks if the specified object is a synthetic class.
isSyntheticClass Checks if the specified class is a synthetic class.
isLambdaClass Checks if the specified object is a lambda class.
isLambdaClass Checks if the specified class is a lambda class.
isLambdaClassName Checks if the specified class name is a lambda class name.
isFunctionalInterface Checks if the specified class is a functional interface.
isPrimitive Checks if the specified class is a primitive type.
isFinal Checks if the specified class is a final class.
isSimpleType Checks if the specified object is a simple type.
isSimpleType Checks if the specified type is a simple type.
isCharSequence Checks if the given object is an instance of CharSequence.
isCharSequence Checks if the given type is a subtype of CharSequence.
isNumber Checks if the given object is an instance of Number.

Method Details

isArray

public static boolean isArray(@Nullable Class<?> type)

Checks if the specified type is an array.

Example Usage

`boolean result1 = ClassUtils.isArray(int[].class);     // returns true
boolean result2 = ClassUtils.isArray(String.class);    // returns false
boolean result3 = ClassUtils.isArray(null);           // returns false
`

isConcreteClass

public static boolean isConcreteClass(@Nullable Class<?> type)

Checks if the specified type is a concrete class.

A concrete class is a class that:

  • Is not an interface
  • Is not an annotation
  • Is not an enum
  • Is not a synthetic class
  • Is not a primitive type
  • Is not an array
  • Is not abstract

Example Usage

`boolean result1 = ClassUtils.isConcreteClass(String.class);  // returns true
boolean result2 = ClassUtils.isConcreteClass(List.class);    // returns false (interface)
boolean result3 = ClassUtils.isConcreteClass(null);         // returns false
`

isAbstractClass

public static boolean isAbstractClass(@Nullable Class<?> type)

Checks if the specified type is an abstract class.

An abstract class is a class that:

  • Is not an interface
  • Is not an annotation
  • Is not an enum
  • Is not a synthetic class
  • Is not a primitive type
  • Is not an array
  • Is abstract

Example Usage

`abstract class AbstractExample { `
class ConcreteExample { }

boolean result1 = ClassUtils.isAbstractClass(AbstractExample.class); // returns true
boolean result2 = ClassUtils.isAbstractClass(ConcreteExample.class); // returns false
boolean result3 = ClassUtils.isAbstractClass(null);                  // returns false
}

isInterface

public static boolean isInterface(@Nullable Class<?> type)

Checks if the specified type is an interface.

Example Usage

`boolean result1 = ClassUtils.isInterface(List.class);    // returns true
boolean result2 = ClassUtils.isInterface(String.class);  // returns false
boolean result3 = ClassUtils.isInterface(null);          // returns false
`

isGeneralClass

public static boolean isGeneralClass(@Nullable Class<?> type)

Checks if the specified type is a general class.

A general class is a class that:

  • Is not an interface
  • Is not an annotation
  • Is not an enum
  • Is not a synthetic class
  • Is not a primitive type
  • Is not an array
  • Is not abstract (by default)

Example Usage

`class ExampleClass {`
abstract class AbstractExample {}
interface ExampleInterface {}

boolean result1 = ClassUtils.isGeneralClass(ExampleClass.class);      // returns true
boolean result2 = ClassUtils.isGeneralClass(AbstractExample.class);   // returns false (abstract)
boolean result3 = ClassUtils.isGeneralClass(ExampleInterface.class);  // returns false (interface)
boolean result4 = ClassUtils.isGeneralClass(null);                   // returns false
}

isTopLevelClass

public static boolean isTopLevelClass(@Nullable Class<?> type)

Is the specified type a general class or not?

If isAbstract == null, it will not check type is abstract or not.

isSyntheticClass

public static boolean isSyntheticClass(@Nullable Object object)

Checks if the specified object is a synthetic class.

A synthetic class is a class that is generated by the Java compiler and does not have a corresponding source code representation. This method returns true if the given object's class is marked as synthetic, and false otherwise. It also returns false if the object is null.

Example Usage

`// Assuming MyClass has an inner class or lambda expression
Object syntheticObject = new MyClass$1(); // Example of a synthetic class instance

boolean result1 = ClassUtils.isSyntheticClass(syntheticObject); // returns true
boolean result2 = ClassUtils.isSyntheticClass(new MyClass());   // returns false
boolean result3 = ClassUtils.isSyntheticClass(null);            // returns false
`

isSyntheticClass

public static boolean isSyntheticClass(@Nullable Class<?> type)

Checks if the specified class is a synthetic class.

A synthetic class is a class that is generated by the Java compiler and does not have a corresponding source code representation. This method returns true if the given class is marked as synthetic, and false otherwise. It also returns false if the class is null.

Example Usage

`// Assuming MyClass has an inner class or lambda expression
Class syntheticClass = MyClass$1.class; // Example of a synthetic class

boolean result1 = ClassUtils.isSyntheticClass(syntheticClass); // returns true
boolean result2 = ClassUtils.isSyntheticClass(MyClass.class);  // returns false
boolean result3 = ClassUtils.isSyntheticClass(null);           // returns false
`

isLambdaClass

public static boolean isLambdaClass(@Nullable Object object)

Checks if the specified object is a lambda class.

A lambda class is a synthetic class generated by the Java compiler to represent a lambda expression. This method returns true if the given object's class is marked as synthetic and its name contains the "$$Lambda/" pattern, indicating that it is a lambda class. It returns false otherwise, including for non-synthetic classes or classes that do not follow the naming convention for lambda classes. It also returns false if the object is null.

Example Usage

`// Assuming MyClass has a lambda expression
Object lambdaObject = (Runnable) () -> System.out.println("Hello, Lambda!");

boolean result1 = ClassUtils.isLambdaClass(lambdaObject); // returns true
boolean result2 = ClassUtils.isLambdaClass(new MyClass()); // returns false
boolean result3 = ClassUtils.isLambdaClass(null);          // returns false
`

isLambdaClass

public static boolean isLambdaClass(@Nullable Class<?> type)

Checks if the specified class is a lambda class.

A lambda class is a synthetic class generated by the Java compiler to represent a lambda expression. This method returns true if the given class is marked as synthetic and its name contains the "$$Lambda/" pattern, indicating that it is a lambda class. It returns false otherwise, including for non-synthetic classes or classes that do not follow the naming convention for lambda classes. It also returns false if the class is null.

Example Usage

`// Assuming MyClass has a lambda expression
Class lambdaClass = MyClass$$Lambda$1.class; // Example of a lambda class

boolean result1 = ClassUtils.isLambdaClass(lambdaClass); // returns true
boolean result2 = ClassUtils.isLambdaClass(MyClass.class); // returns false
boolean result3 = ClassUtils.isLambdaClass(null);          // returns false
`

isLambdaClassName

public static boolean isLambdaClassName(@Nullable String className)

Checks if the specified class name is a lambda class name.

A lambda class name is a synthetic class name generated by the Java compiler to represent a lambda expression. This method returns true if the given class name contains the "$$Lambda/" pattern, indicating that it is a lambda class name. It returns false otherwise, including for non-synthetic class names or class names that do not follow the naming convention for lambda classes. It also returns false if the class name is null.

Example Usage

`String lambdaClassName = "com.example.MyClass$$Lambda$1"; // Example of a lambda class name

boolean result1 = ClassUtils.isLambdaClassName(lambdaClassName); // returns true
boolean result2 = ClassUtils.isLambdaClassName("com.example.MyClass"); // returns false
boolean result3 = ClassUtils.isLambdaClassName(null);          // returns false
`

isFunctionalInterface

public static boolean isFunctionalInterface(@Nullable Class<?> type)

Checks if the specified class is a functional interface.

A functional interface is an interface that has exactly one abstract method. This method returns true if the given class is an interface and has exactly one abstract method, or if it is annotated with FunctionalInterface. It returns false otherwise, including for non-interface classes or interfaces with more than one abstract method. It also returns false if the class is null.

Example Usage

{@code

#### `isPrimitive`

```java
public static boolean isPrimitive(@Nullable Class> type)
```

Checks if the specified class is a primitive type.



This method returns `true` if the given class is one of the eight primitive types
(`boolean`, `byte`, `char`, `short`, `int`, `long`, `float`, `double`)
or their corresponding array types. It returns `false` otherwise, including for wrapper classes.


### Example Usage
`boolean result1 = ClassUtils.isPrimitive(int.class);        // returns true
boolean result2 = ClassUtils.isPrimitive(Integer.class);    // returns false (wrapper class)
boolean result3 = ClassUtils.isPrimitive(int[].class);      // returns true (primitive array)
boolean result4 = ClassUtils.isPrimitive(String.class);     // returns false
boolean result5 = ClassUtils.isPrimitive(null);            // returns false
`

isFinal

public static boolean isFinal(@Nullable Class<?> type)

Checks if the specified class is a final class.

A final class is a class that cannot be extended by other classes. This method returns true if the given class is marked with the final modifier, and false otherwise. It also returns false if the class is null.

Example Usage

`final class FinalExample {`
class NonFinalExample {}

boolean result1 = ClassUtils.isFinal(FinalExample.class);   // returns true
boolean result2 = ClassUtils.isFinal(NonFinalExample.class); // returns false
boolean result3 = ClassUtils.isFinal(null);                 // returns false
}

isSimpleType

public static boolean isSimpleType(@Nullable Object object)

Checks if the specified object is a simple type.

A simple type is defined as one of the following:

  • Void
  • Boolean
  • Character
  • Byte
  • Short
  • Integer
  • Long
  • Float
  • Double
  • String
  • BigDecimal
  • BigInteger
  • Date
  • Object

Example Usage

`boolean result1 = ClassUtils.isSimpleType("Hello");  // returns true
boolean result2 = ClassUtils.isSimpleType(123);      // returns true
boolean result3 = ClassUtils.isSimpleType(new ArrayList<>()); // returns false
boolean result4 = ClassUtils.isSimpleType(null);     // returns false
`

isSimpleType

public static boolean isSimpleType(@Nullable Class<?> type)

Checks if the specified type is a simple type.

A simple type is defined as one of the following:

  • Void
  • Boolean
  • Character
  • Byte
  • Short
  • Integer
  • Long
  • Float
  • Double
  • String
  • BigDecimal
  • BigInteger
  • Date
  • Object

Example Usage

`boolean result1 = ClassUtils.isSimpleType(String.class);  // returns true
boolean result2 = ClassUtils.isSimpleType(List.class);    // returns false
boolean result3 = ClassUtils.isSimpleType(null);         // returns false
`

isCharSequence

public static boolean isCharSequence(@Nullable Object value)

Checks if the given object is an instance of CharSequence.

Example Usage

`boolean result1 = ClassUtils.isCharSequence("Hello");     // returns true
boolean result2 = ClassUtils.isCharSequence(new StringBuilder("World")); // returns true
boolean result3 = ClassUtils.isCharSequence(123);         // returns false
boolean result4 = ClassUtils.isCharSequence(null);        // returns false
`

isCharSequence

public static boolean isCharSequence(@Nullable Class<?> type)

Checks if the given type is a subtype of CharSequence.

Example Usage

`boolean result = ClassUtils.isCharSequence(String.class); // returns true
    boolean result2 = ClassUtils.isCharSequence(Integer.class); // returns false
`

isNumber

public static boolean isNumber(@Nullable Object value)

Checks if the given object is an instance of Number.

Example Usage

`boolean result1 = ClassUtils.isNumber(Integer.valueOf(10));  // returns true
boolean result2 = ClassUtils.isNumber(10.5);                 // returns true
boolean result3 = ClassUtils.isNumber("123");                // returns false
boolean result4 = ClassUtils.isNumber(null);                 // returns false
`

See Also

  • Class
  • ClassLoaderUtils
  • ConstructorUtils

This documentation was auto-generated from the source code of microsphere-java.

Home

annotation-processor

annotation-test

java-annotations

java-core

java-test

jdk-tools

lang-model

Clone this wiki locally