Class MixinSource

java.lang.Object
cloud.opencode.base.json.MixinSource

public final class MixinSource extends Object
Mixin Source - Manages Mixin Annotation Mappings 混入源 - 管理混入注解映射

This class manages mixin annotations, allowing annotations from one class (the mixin) to be applied to another class (the target) without modifying the target class. This is useful for adding JSON serialization annotations to third-party classes.

此类管理混入注解,允许将一个类(混入)的注解应用到另一个类(目标)上, 而无需修改目标类。这对于向第三方类添加 JSON 序列化注解非常有用。

Example | 示例:

MixinSource mixins = new MixinSource();

// Define a mixin class with annotations
abstract class UserMixin {
    @JsonProperty("user_name")
    abstract String getName();

    @JsonIgnore
    abstract String getPassword();
}

// Apply mixin to target class
mixins.addMixin(User.class, UserMixin.class);

// Check if mixin exists
boolean hasMixin = mixins.hasMixin(User.class); // true
Class<?> mixin = mixins.getMixin(User.class);   // UserMixin.class

Features | 主要功能:

  • Thread-safe mixin registration and lookup - 线程安全的混入注册和查找
  • Add, remove, and query mixin mappings - 添加、移除和查询混入映射
  • Unmodifiable view of all registered mixins - 所有注册混入的不可修改视图

Security | 安全性:

  • Thread-safe: Yes (ConcurrentHashMap) - 线程安全: 是(ConcurrentHashMap)
  • Null-safe: Yes (validates inputs) - 空值安全: 是(验证输入)
Since:
JDK 25, opencode-base-json V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • MixinSource

      public MixinSource()
      Constructs an empty MixinSource. 构造空的 MixinSource。
  • Method Details

    • addMixin

      public void addMixin(Class<?> target, Class<?> mixin)
      Registers a mixin annotation class for a target type. 为目标类型注册混入注解类。
      Parameters:
      target - the target class to apply mixin to - 要应用混入的目标类
      mixin - the mixin class containing annotations - 包含注解的混入类
      Throws:
      NullPointerException - if target or mixin is null - 如果 target 或 mixin 为 null
    • removeMixin

      public void removeMixin(Class<?> target)
      Removes the mixin mapping for a target type. 移除目标类型的混入映射。
      Parameters:
      target - the target class to remove mixin from - 要移除混入的目标类
      Throws:
      NullPointerException - if target is null - 如果 target 为 null
    • getMixin

      public Class<?> getMixin(Class<?> target)
      Returns the mixin class for the given target type. 返回给定目标类型的混入类。
      Parameters:
      target - the target class - 目标类
      Returns:
      the mixin class, or null if no mixin is registered - 混入类,如果未注册混入则返回 null
      Throws:
      NullPointerException - if target is null - 如果 target 为 null
    • hasMixin

      public boolean hasMixin(Class<?> target)
      Returns whether a mixin is registered for the given target type. 返回是否为给定目标类型注册了混入。
      Parameters:
      target - the target class - 目标类
      Returns:
      true if a mixin is registered - 如果注册了混入则返回 true
      Throws:
      NullPointerException - if target is null - 如果 target 为 null
    • getMixins

      public Map<Class<?>,Class<?>> getMixins()
      Returns an unmodifiable view of all mixin mappings. 返回所有混入映射的不可修改视图。
      Returns:
      unmodifiable map of target -> mixin mappings - 目标 -> 混入映射的不可修改 Map
    • clear

      public void clear()
      Removes all mixin mappings. 移除所有混入映射。
    • size

      public int size()
      Returns the number of registered mixin mappings. 返回注册的混入映射数量。
      Returns:
      the number of mixins - 混入数量
    • toString

      public String toString()
      Overrides:
      toString in class Object