Class Fields


  • public class Fields
    extends java.lang.Object
    Utilities to access fields.
    Author:
    tomyeh
    • Constructor Summary

      Constructors 
      Constructor Description
      Fields()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object get​(java.lang.Object obj, java.lang.String name)
      Returns the value of the specified public field or public method in the object.
      static java.lang.Object getByCompound​(java.lang.Object obj, java.lang.String name)
      Returns the value of the specified public field or public method of the object.
      static void set​(java.lang.Object obj, java.lang.String name, java.lang.Object val, boolean autoCoerce)
      Sets the value of the specified public filed or public method in the object.
      static void setAccessible​(java.lang.reflect.AccessibleObject f, boolean accessible)
      Changes the accessibility without throwing any exception.
      static void setByCompound​(java.lang.Object obj, java.lang.String name, java.lang.Object val)
      Sets the value of the specified public field or public method in the object, without converting the specified val.
      static void setByCompound​(java.lang.Object obj, java.lang.String name, java.lang.Object val, boolean autoCoerce)
      Sets the value of the specified public field or public method in the object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Fields

        public Fields()
    • Method Detail

      • setAccessible

        public static final void setAccessible​(java.lang.reflect.AccessibleObject f,
                                               boolean accessible)
        Changes the accessibility without throwing any exception.
        Since:
        3.0.3
      • getByCompound

        public static final java.lang.Object getByCompound​(java.lang.Object obj,
                                                           java.lang.String name)
                                                    throws java.lang.NoSuchMethodException
        Returns the value of the specified public field or public method of the object.

        If getByCompound(obj, "a.b.c") is called and obj.getA() or obj.getA().getB() returns null, the result is null. However, NullPointerException is thrown if obj is null.

        Parameters:
        name - the field name. It can be in form of "a.b.c", but cannot be an expression. It must be a public field or public method (prefixed with set).
        Throws:
        java.lang.NoSuchMethodException - if no corresponding field.
      • setByCompound

        public static final void setByCompound​(java.lang.Object obj,
                                               java.lang.String name,
                                               java.lang.Object val,
                                               boolean autoCoerce)
                                        throws java.lang.NoSuchMethodException
        Sets the value of the specified public field or public method in the object.
        Parameters:
        autoCoerce - whether to automatically convert val to the proper class that matches the argument of method or field.
        Throws:
        java.lang.NoSuchMethodException
      • setByCompound

        public static final void setByCompound​(java.lang.Object obj,
                                               java.lang.String name,
                                               java.lang.Object val)
                                        throws java.lang.NoSuchMethodException
        Sets the value of the specified public field or public method in the object, without converting the specified val.

        It is a shortcut of setByCompound(obj, name, val, false).

        Parameters:
        name - the field name. It can be in form of "a.b.c", but cannot be an expression. It must be a public field or public method (prefixed with set).
        Throws:
        java.lang.NoSuchMethodException
      • get

        public static final java.lang.Object get​(java.lang.Object obj,
                                                 java.lang.String name)
                                          throws java.lang.NoSuchMethodException
        Returns the value of the specified public field or public method in the object. Unlike getByCompound(java.lang.Object, java.lang.String), name cannot contain '.'. In other words, it must be the name of a public field or method (prefixed with get).
        Throws:
        java.lang.NoSuchMethodException
      • set

        public static final void set​(java.lang.Object obj,
                                     java.lang.String name,
                                     java.lang.Object val,
                                     boolean autoCoerce)
                              throws java.lang.NoSuchMethodException
        Sets the value of the specified public filed or public method in the object. Unlike setByCompound(java.lang.Object, java.lang.String, java.lang.Object, boolean), name cannot contain '.'. In other words, it must be the name of a public field or method (prefixed with set).
        Throws:
        java.lang.NoSuchMethodException