Class ReflectionMap<V>

java.lang.Object
org.apache.juneau.commons.reflect.ReflectionMap<V>
Type Parameters:
V - The type of values stored in this map.
Direct Known Subclasses:
DebugEnablementMap

public class ReflectionMap<V> extends Object
Maps arbitrary values to classes, methods, fields, and constructors based on name-based patterns.

This utility allows you to create flexible mappings between reflection elements and custom values. It supports various pattern formats for identifying classes and their members, with intelligent matching that handles inner classes, method signatures, and field names.

Example:

// Create a map that associates visibility levels with specific classes/methods ReflectionMap<Visibility> map = ReflectionMap .create(Visibility.class) .append("com.foo.MyClass", Visibility.PUBLIC) .append("com.foo.MyClass.secretMethod", Visibility.PRIVATE) .append("com.foo.MyClass.internalField", Visibility.PACKAGE) .build(); // Find values for specific reflection elements Stream<Visibility> classVisibility = map.find(MyClass.class); Stream<Visibility> methodVisibility = map.find(secretMethod);

Supported Pattern Formats:

The following pattern formats are supported for mapping keys:

  • Classes:
    • Fully qualified:
      • "com.foo.MyClass"
    • Fully qualified inner class:
      • "com.foo.MyClass$Inner1$Inner2"
    • Simple class name:
      • "MyClass"
    • Simple inner class:
      • "MyClass$Inner1$Inner2"
      • "Inner1$Inner2"
      • "Inner2"
    • All classes wildcard:
      • "*"
  • Methods:
    • Fully qualified with args:
      • "com.foo.MyClass.myMethod(String,int)"
      • "com.foo.MyClass.myMethod(java.lang.String,int)"
      • "com.foo.MyClass.myMethod()"
    • Fully qualified without args (matches any args):
      • "com.foo.MyClass.myMethod"
    • Simple with args:
      • "MyClass.myMethod(String,int)"
      • "MyClass.myMethod(java.lang.String,int)"
      • "MyClass.myMethod()"
    • Simple without args (matches any args):
      • "MyClass.myMethod"
    • Simple inner class:
      • "MyClass$Inner1$Inner2.myMethod"
      • "Inner1$Inner2.myMethod"
      • "Inner2.myMethod"
  • Fields:
    • Fully qualified:
      • "com.foo.MyClass.myField"
    • Simple:
      • "MyClass.myField"
    • Simple inner class:
      • "MyClass$Inner1$Inner2.myField"
      • "Inner1$Inner2.myField"
      • "Inner2.myField"
  • Constructors:
    • Fully qualified with args:
      • "com.foo.MyClass(String,int)"
      • "com.foo.MyClass(java.lang.String,int)"
      • "com.foo.MyClass()"
    • Simple with args:
      • "MyClass(String,int)"
      • "MyClass(java.lang.String,int)"
      • "MyClass()"
    • Simple inner class:
      • "MyClass$Inner1$Inner2()"
      • "Inner1$Inner2()"
      • "Inner2()"
  • Multiple patterns:
    • A comma-delimited list of any patterns above:
      • "com.foo.MyClass, com.bar.OtherClass"
      • "MyClass.method1, MyClass.method2, MyClass.field1"
Notes:
  • Method and constructor patterns without parentheses match any method signature.
  • Method and constructor argument types can be specified with simple or fully qualified names.
  • Array types in signatures use either "Type[]" or JVM notation like "[LType;".
  • Generic type parameters are stripped from method signatures during matching.
  • Patterns are case-sensitive.
See Also:
  • Constructor Details

  • Method Details

    • create

      public static <V> ReflectionMap.Builder<V> create(Class<V> c)
      Creates a new builder for constructing a ReflectionMap.
      Example:

      ReflectionMap<String> map = ReflectionMap .create(String.class) .append("com.foo.MyClass", "value1") .append("com.foo.MyClass.myMethod", "value2") .build();

      Type Parameters:
      V - The type of values stored in the map.
      Parameters:
      c - The class type of values (used for type safety, not stored).
      Returns:
      A new builder instance.
    • find

      public Stream<V> find(Class<?> c)
      Finds all values associated with the specified class.

      This method searches for mappings that match the given class, including patterns for the fully qualified name, simple name, inner class names, and wildcard patterns.

      Example:

      ReflectionMap<String> map = ReflectionMap .create(String.class) .append("com.foo.MyClass", "value1") .append("MyClass", "value2") .build(); // Find all values for MyClass Stream<String> values = map.find(MyClass.class); // Returns stream containing ["value1", "value2"]

      Parameters:
      c - The class to find mappings for. Can be null.
      Returns:
      A stream of all values associated with the class. Empty stream if no matches found.
    • find

      public Stream<V> find(Constructor<?> c)
      Finds all values associated with the specified constructor.

      This method searches for mappings that match the given constructor, considering both the declaring class and parameter types.

      Example:

      ReflectionMap<String> map = ReflectionMap .create(String.class) .append("MyClass(String,int)", "value1") .build(); // Find value for specific constructor Constructor<?> ctor = MyClass.class.getConstructor(String.class, int.class); Stream<String> values = map.find(ctor);

      Parameters:
      c - The constructor to find mappings for. Can be null.
      Returns:
      A stream of all values associated with the constructor. Empty stream if no matches found.
    • find

      public Stream<V> find(Field f)
      Finds all values associated with the specified field.

      This method searches for mappings that match the given field, considering both the declaring class and field name.

      Example:

      ReflectionMap<String> map = ReflectionMap .create(String.class) .append("MyClass.myField", "value1") .build(); // Find value for specific field Field field = MyClass.class.getField("myField"); Stream<String> values = map.find(field);

      Parameters:
      f - The field to find mappings for. Can be null.
      Returns:
      A stream of all values associated with the field. Empty stream if no matches found.
    • find

      public Stream<V> find(Method m)
      Finds all values associated with the specified method.

      This method searches for mappings that match the given method, considering the declaring class, method name, and parameter types.

      Example:

      ReflectionMap<String> map = ReflectionMap .create(String.class) .append("MyClass.myMethod", "value1") .append("MyClass.myMethod(String)", "value2") .build(); // Find values for specific method Method method = MyClass.class.getMethod("myMethod", String.class); Stream<String> values = map.find(method); // Returns stream containing ["value1", "value2"] - first matches any signature

      Parameters:
      m - The method to find mappings for. Can be null.
      Returns:
      A stream of all values associated with the method. Empty stream if no matches found.
    • properties

    • toString

      public String toString()
      Overrides:
      toString in class Object