Interface TypeRequest

All Superinterfaces:
BiPredicate<ElementTypeRequest,String>
All Known Implementing Classes:
TypeRequest.Literal, TypeRequest.Regex, TypeRequest.Wildcard

public interface TypeRequest extends BiPredicate<ElementTypeRequest,String>

An Element-declared predicate that requests access to specific types not otherwise permitted by the system-side PermittedTypes or PermittedPackages visibility rules. Where PermittedTypes is registered by the host system via ServiceLoader and governs what the system chooses to expose, TypeRequest is declared by the Element itself, allowing it to assert that it needs access to a particular type at load time.

How It Works

Implementations are referenced from the ElementTypeRequest annotation, which is placed on a package declaration (package-info.java) within the Element. When the PermittedTypesClassLoader evaluates a class for visibility, it discovers any TypeRequest implementations declared by the Element and invokes BiPredicate.test(Object, Object) passing the annotation instance and the binary class name. If the predicate returns true, the class is permitted regardless of annotation-based rules.

BiPredicate Contract

The two parameters passed to BiPredicate.test(Object, Object) are:

  • ElementTypeRequest — the annotation instance, providing configuration context such as the explicit type name list in ElementTypeRequest.value()
  • String — the binary name of the class as passed to ClassLoader.loadClass()

Passing the annotation instance as the first argument allows a single TypeRequest implementation to behave differently depending on how the annotation is configured at each call site, without needing a separate implementation class per package.

Difference from PermittedTypes

PermittedTypes is a system-side contract: the host registers implementations to declare what it is willing to expose. TypeRequest is an Element-side contract: the Element declares what it needs. This separation of concerns allows the host to control the default visibility surface while still giving Elements a way to opt in to additional types they depend on.

Usage

For simple cases, the built-in TypeRequest.Literal implementation (the default) permits types whose binary names appear in ElementTypeRequest.value():


 @ElementTypeRequest(value = {"com.example.shared.Foo", "com.example.shared.Bar"})
 package com.example.myelement;
 

For more complex matching, supply a custom implementation via ElementTypeRequest.request():


 @ElementTypeRequest(request = MyTypeRequest.class)
 package com.example.myelement;

 public class MyTypeRequest implements TypeRequest {
     {@literal @}Override
     public boolean test(ElementTypeRequest annotation, String binaryName) {
         return binaryName.startsWith("com.example.shared.");
     }
 }
 
See Also: