Class KlassUtility

java.lang.Object
com.scriptbasic.utility.KlassUtility

public final class KlassUtility
extends java.lang.Object
Author:
Peter Verhas date June 28, 2012
  • Method Summary

    Modifier and Type Method Description
    static java.lang.Class<?> forName​(java.lang.String s)
    Returns a class based on its name just like the method Class.forName(String).
    static java.lang.Class<?> forNameEx​(java.lang.String s)
    Returns a class based on its name just like the method Class.forName(String).
    static java.lang.Object getField​(java.lang.Object object, java.lang.String fieldName)
    Get the value of a field of an object and return it.
    static void setField​(java.lang.Object object, java.lang.String fieldName, java.lang.Object valueObject)
    Set the field of a Java object.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • setField

      public static void setField​(java.lang.Object object, java.lang.String fieldName, java.lang.Object valueObject) throws BasicRuntimeException
      Set the field of a Java object. This method is called when a BASIC code has access to an object and assigns value to some of the fields of the Java object.

      TODO: implement ScriptBasic magic bean setting mechanism as a first resort and if that does not work then setter and if there is no setter then direct field access.

      Parameters:
      object - parameter
      fieldName - parameter
      valueObject - parameter
      Throws:
      BasicRuntimeException - in case of exception
    • getField

      public static java.lang.Object getField​(java.lang.Object object, java.lang.String fieldName) throws BasicRuntimeException
      Get the value of a field of an object and return it.

      TODO implement the following algorithm: ScriptBasic magic bean access as a first resort when the object's class implements scriptbasic magic bean if.

      2. If there is a getter for the field, use that

      3. If there is no getter starting with name 'get' but the field is declared as boolean or Boolean then use the getter that starts with 'is'

      4. Use the field.

      Current implementation uses the field.

      TODO implement ScriptBasic magic bean functionality access as a last resort calling the method INVENT_NAME passing the field name as argument if the class of the object implements the interface INVENT_INTERFACE_NAME. The names should include the word BASIC, or better SCRIPTBASIC. Perhaps ScriptBasicMagicBean or something.

      Parameters:
      object - parameter
      fieldName - parameter
      Returns:
      return value
      Throws:
      BasicRuntimeException - in case of exception
    • forName

      public static java.lang.Class<?> forName​(java.lang.String s) throws java.lang.ClassNotFoundException
      Returns a class based on its name just like the method Class.forName(String). The search for the class is extended. For example if this method is called
       Class<?> klass = KlassUtility.forName("a.b.c.d");
       

      the method tries to locate he class a.b.c.d. If it can not locate that it will try to locate a.b.c$d because it may happen that a.b.c is not a package but rather a class itself and the class to be located is an inner class. This continues until the class is located or until the last dot (starting from the end of the class name string) is replaced by the character '$'. In other words the method will try to locate the classes a.b$c$d and a$b$c$d until it finds one.

      If this algorithm can not load any class then the exception caught the first time is thrown.

      Parameters:
      s - the name of the class to be loaded
      Returns:
      the class finally loaded
      Throws:
      java.lang.ClassNotFoundException - the exception thrown by the class loader the first time if the algorithm can not find any class to load.
    • forNameEx

      public static java.lang.Class<?> forNameEx​(java.lang.String s) throws BasicSyntaxException
      Returns a class based on its name just like the method Class.forName(String). If the name of the class is
       byte
       short
       char
       double
       float
       long
       int
       boolean
       

      then the method will return the primitive class named. Otherwise it calls forName(String) to load the class.

      Parameters:
      s - parameter
      Returns:
      return value
      Throws:
      BasicSyntaxException - in case of exception