Skip to content

io microsphere lang Wrapper

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

Wrapper

Type: Interface | Module: microsphere-java-core | Package: io.microsphere.lang

Source: microsphere-java-core/src/main/java/io/microsphere/lang/Wrapper.java

Overview

A Wrapper is an interface that provides the ability to wrap objects and provide access to their underlying implementations, especially when those implementations expose non-standard or extended APIs.

Implementations of this interface must ensure that they can either directly implement a requested type, or delegate to a wrapped object recursively until the appropriate implementation is found. This allows for flexible proxying and unwrapping patterns, particularly useful in frameworks where implementations may be layered with proxies or decorators.

Example Usage

`public class MyWrapper implements Wrapper {
    private final MyService wrapped;

    public MyWrapper(MyService wrapped) {
        this.wrapped = wrapped;
    `

    public  T unwrap(Class type) {
        if (type.isInstance(wrapped)) {
            return type.cast(wrapped);
        }
        throw new IllegalArgumentException("Cannot unwrap to " + type.getName());
    }

    public boolean isWrapperFor(Class type) {
        return type.isInstance(wrapped);
    }
}

// Usage:
MyService service = new MyServiceImpl();
Wrapper wrapper = new MyWrapper(service);

if (wrapper.isWrapperFor(MyService.class)) {
    MyService unwrapped = wrapper.unwrap(MyService.class); // returns service
}
}

The above example demonstrates a basic implementation of the Wrapper interface. It checks whether the wrapped object supports the requested type via #isWrapperFor(Class), and safely returns the underlying instance via #unwrap(Class).

Declaration

public interface Wrapper

Version Information

  • Introduced in: 0.1.10-SNAPSHOT (current)
  • Current Project Version: 0.1.10-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

public class MyWrapper implements Wrapper {
    private final MyService wrapped;

    public MyWrapper(MyService wrapped) {
        this.wrapped = wrapped;
    }

    public <T> T unwrap(Class<T> type) {
        if (type.isInstance(wrapped)) {
            return type.cast(wrapped);
        }
        throw new IllegalArgumentException("Cannot unwrap to " + type.getName());
    }

    public boolean isWrapperFor(Class<?> type) {
        return type.isInstance(wrapped);
    }
}

// Usage:
MyService service = new MyServiceImpl();
Wrapper wrapper = new MyWrapper(service);

if (wrapper.isWrapperFor(MyService.class)) {
    MyService unwrapped = wrapper.unwrap(MyService.class); // returns service
}

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.lang.Wrapper;

See Also

  • #unwrap(Class)
  • #isWrapperFor(Class)
  • #tryUnwrap(Object, Class)

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

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally