Skip to content

The API module

German Vekhorev edited this page May 27, 2021 · 6 revisions

Access Warden API

me.darksidecode.accesswarden.api

Including the API (Gradle)

repositories {
    maven {
        name 'Public Reflex Repository'
        url 'https://archiva.reflex.rip/repository/public/'
    }
}

dependencies {
    implementation group: 'me.darksidecode.accesswarden.api', name: 'access-warden-api', version: '1'
}

Including the API (Maven)

<repositories>
    <repository>
        <id>reflex.public</id>
        <name>Public Reflex Repository</name>
        <url>https://archiva.reflex.rip/repository/public/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>me.darksidecode.accesswarden.api</groupId>
        <artifactId>access-warden-api</artifactId>
        <version>1</version>
    </dependency>
</dependencies>

Capabilities of the API

This module contains two types of objects: high-level annotations (such as @RestrictedAccess) and low-level call stack and environment inspection facilities.

Typically you want to simply include the API module for the former part of it — just for it to be used internally by Access Warden code generation tools.

The latter is used internally by other Access Warden modules and is used at runtime along with the generated code. However, in rare cases when you need something really specific to your application, you can use those low-level facilities as well. For example, these APIs allow you to easily retrieve the current call stack with all reflection and native frames filtered out:

FilteredContext ctx;

try {
    // Options are specified as a 32-bit bitfield. Use bitwise OR ("|") to combine them.
    int options = ContextResolution.Options.FILTER_REFLECTION_FRAMES |
                  ContextResolution.Options.FILTER_NATIVE_FRAMES     ;

    ctx = ContextResolution.resolve(options);
} catch (UnexpectedSetupException ex) {
    // Current filtered call stack is too small, or something else is invalid.
    // Print exception with ex.printStackTrace() or rethrow it for details.
}

Then, for example, you can retrieve the most recent call frame where class name starts with "java.lang" as follows:

StackTraceElement filteredFrame = ctx.mostRecentCall(
        frame -> frame.getClassName().startsWith("java.lang"));

if (filteredFrame != null) {
    // Found.
} else {
    // No frames matching the given criteria (in lambda above) in the filtered call stack.
}

There are other useful methods in the API module as well. But, again, only use them in you really cannot rely on standard, built-in features of Access Warden for some reason.