Class AllowList

java.lang.Object
cloud.opencode.base.expression.sandbox.AllowList

public class AllowList extends Object
Allow List 白名单

A flexible whitelist for controlling access to classes, methods, and properties. Supports exact matches and wildcard patterns.

用于控制对类、方法和属性访问的灵活白名单。支持精确匹配和通配符模式。

Usage | 用法

AllowList allowList = AllowList.builder()
    .allowClass("java.lang.String")
    .allowClass("java.util.*")  // All classes in java.util
    .allowMethod("get*")        // All methods starting with "get"
    .allowProperty("name")
    .build();

Features | 主要功能:

  • Exact match and wildcard pattern support for classes, methods, properties - 类、方法、属性的精确匹配和通配符模式支持
  • Configurable default allow/deny policy - 可配置的默认允许/拒绝策略
  • Immutable after construction - 构造后不可变
  • Factory methods for empty and allow-all lists - 空列表和允许所有列表的工厂方法

Security | 安全性:

  • Thread-safe: Yes, immutable after construction with defensive copies - 线程安全: 是,构造后不可变,使用防御性拷贝
  • Null-safe: Yes, null names return false - 空值安全: 是,null名称返回false

Usage Examples | 使用示例:

AllowList allowList = AllowList.builder()
    .allowClass("java.lang.String")
    .allowClass("java.util.*")
    .allowMethod("get*")
    .allowProperty("name")
    .build();
boolean allowed = allowList.isClassAllowed("java.lang.String");  // true
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • isClassAllowed

      public boolean isClassAllowed(String className)
      Check if class is allowed 检查类是否被允许
      Parameters:
      className - the class name | 类名
      Returns:
      true if allowed | 如果允许返回 true
    • isClassAllowed

      public boolean isClassAllowed(Class<?> clazz)
      Check if class is allowed 检查类是否被允许
      Parameters:
      clazz - the class | 类
      Returns:
      true if allowed | 如果允许返回 true
    • isMethodAllowed

      public boolean isMethodAllowed(String methodName)
      Check if method is allowed 检查方法是否被允许
      Parameters:
      methodName - the method name | 方法名
      Returns:
      true if allowed | 如果允许返回 true
    • isPropertyAllowed

      public boolean isPropertyAllowed(String propertyName)
      Check if property is allowed 检查属性是否被允许
      Parameters:
      propertyName - the property name | 属性名
      Returns:
      true if allowed | 如果允许返回 true
    • getAllowedClasses

      public Set<String> getAllowedClasses()
      Get all allowed classes 获取所有允许的类
      Returns:
      the allowed classes | 允许的类
    • getAllowedMethods

      public Set<String> getAllowedMethods()
      Get all allowed methods 获取所有允许的方法
      Returns:
      the allowed methods | 允许的方法
    • getAllowedProperties

      public Set<String> getAllowedProperties()
      Get all allowed properties 获取所有允许的属性
      Returns:
      the allowed properties | 允许的属性
    • builder

      public static AllowList.Builder builder()
      Create a builder 创建构建器
      Returns:
      the builder | 构建器
    • empty

      public static AllowList empty()
      Create an empty allow list (denies all) 创建空白名单(拒绝所有)
      Returns:
      the empty allow list | 空白名单
    • allowAll

      public static AllowList allowAll()
      Create an allow-all list (allows everything) 创建允许所有的白名单
      Returns:
      the allow-all list | 允许所有的白名单