Class SandboxException

All Implemented Interfaces:
OpenExceptionMeta, Serializable

public class SandboxException extends OpenExpressionException
Sandbox Exception 沙箱异常

Thrown when a security violation is detected during expression evaluation.

在表达式求值过程中检测到安全违规时抛出。

Features | 主要功能:

  • Typed violation categories via ViolationType enum - 通过ViolationType枚举的类型化违规分类
  • Violated resource tracking - 被违规资源跟踪
  • Static factory methods for common violations - 常见违规的静态工厂方法

Usage Examples | 使用示例:

try {
    OpenExpression.eval("Runtime.exec('cmd')", sandboxedCtx);
} catch (SandboxException e) {
    SandboxException.ViolationType type = e.getViolationType();
    String resource = e.getViolatedResource();
}

// Create specific violations
throw SandboxException.classNotAllowed("java.lang.Runtime");
throw SandboxException.methodNotAllowed("exec");

Security | 安全性:

  • Thread-safe: Yes, immutable after construction - 线程安全: 是,构造后不可变
  • Null-safe: Yes, null resource handled gracefully - 空值安全: 是,null资源优雅处理
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • SandboxException

      public SandboxException(String message)
      Create sandbox exception 创建沙箱异常
      Parameters:
      message - the error message | 错误消息
    • SandboxException

      public SandboxException(String message, SandboxException.ViolationType violationType, String violatedResource)
      Create sandbox exception with violation type 创建带违规类型的沙箱异常
      Parameters:
      message - the error message | 错误消息
      violationType - the violation type | 违规类型
      violatedResource - the violated resource | 被违规的资源
    • SandboxException

      public SandboxException(String message, Throwable cause)
      Create sandbox exception with cause 创建带原因的沙箱异常
      Parameters:
      message - the error message | 错误消息
      cause - the cause | 原因
  • Method Details

    • getViolationType

      public SandboxException.ViolationType getViolationType()
      Get the violation type 获取违规类型
      Returns:
      the violation type | 违规类型
    • getViolatedResource

      public String getViolatedResource()
      Get the violated resource 获取被违规的资源
      Returns:
      the violated resource | 被违规的资源
    • classNotAllowed

      public static SandboxException classNotAllowed(String className)
      Create exception for denied class access 创建拒绝类访问的异常
      Parameters:
      className - the class name | 类名
      Returns:
      the exception | 异常
    • classNotAllowed

      public static SandboxException classNotAllowed(Class<?> clazz)
      Create exception for denied class access 创建拒绝类访问的异常
      Parameters:
      clazz - the class | 类
      Returns:
      the exception | 异常
    • methodNotAllowed

      public static SandboxException methodNotAllowed(String methodName)
      Create exception for denied method access 创建拒绝方法访问的异常
      Parameters:
      methodName - the method name | 方法名
      Returns:
      the exception | 异常
    • methodNotAllowed

      public static SandboxException methodNotAllowed(String className, String methodName)
      Create exception for denied method access with class context 创建带类上下文的拒绝方法访问异常
      Parameters:
      className - the class name | 类名
      methodName - the method name | 方法名
      Returns:
      the exception | 异常
    • propertyNotAllowed

      public static SandboxException propertyNotAllowed(String propertyName)
      Create exception for denied property access 创建拒绝属性访问的异常
      Parameters:
      propertyName - the property name | 属性名
      Returns:
      the exception | 异常
    • functionNotAllowed

      public static SandboxException functionNotAllowed(String functionName)
      Create exception for denied function access 创建拒绝函数访问的异常
      Parameters:
      functionName - the function name | 函数名
      Returns:
      the exception | 异常
    • timeout

      public static SandboxException timeout(long timeoutMs)
      Create exception for execution timeout 创建执行超时异常
      Parameters:
      timeoutMs - the timeout in milliseconds | 超时毫秒数
      Returns:
      the exception | 异常
    • iterationLimitExceeded

      public static SandboxException iterationLimitExceeded(int maxIterations)
      Create exception for iteration limit exceeded 创建迭代限制超出异常
      Parameters:
      maxIterations - the max iterations | 最大迭代次数
      Returns:
      the exception | 异常
    • expressionTooLong

      public static SandboxException expressionTooLong(int maxLength, int actualLength)
      Create exception for expression length exceeded 创建表达式长度超出异常
      Parameters:
      maxLength - the max length | 最大长度
      actualLength - the actual length | 实际长度
      Returns:
      the exception | 异常
    • depthLimitExceeded

      public static SandboxException depthLimitExceeded(int maxDepth)
      Create exception for depth limit exceeded 创建深度限制超出异常
      Parameters:
      maxDepth - the max depth | 最大深度
      Returns:
      the exception | 异常