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
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.Class<?>forName(java.lang.String s)Returns a class based on its name just like the methodClass.forName(String).static java.lang.Class<?>forNameEx(java.lang.String s)Returns a class based on its name just like the methodClass.forName(String).static java.lang.ObjectgetField(java.lang.Object object, java.lang.String fieldName)Get the value of a field of an object and return it.static voidsetField(java.lang.Object object, java.lang.String fieldName, java.lang.Object valueObject)Set the field of a Java object.
-
-
-
Method Detail
-
setField
public static void setField(java.lang.Object object, java.lang.String fieldName, java.lang.Object valueObject) throws BasicRuntimeExceptionSet 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- parameterfieldName- parametervalueObject- parameter- Throws:
BasicRuntimeException- in case of exception
-
getField
public static java.lang.Object getField(java.lang.Object object, java.lang.String fieldName) throws BasicRuntimeExceptionGet 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- parameterfieldName- parameter- Returns:
- return value
- Throws:
BasicRuntimeException- in case of exception
-
forName
public static java.lang.Class<?> forName(java.lang.String s) throws java.lang.ClassNotFoundExceptionReturns a class based on its name just like the methodClass.forName(String). The search for the class is extended. For example if this method is calledClass<?> 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 locatea.b.c$dbecause it may happen thata.b.cis 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 classesa.b$c$danda$b$c$duntil 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 BasicSyntaxExceptionReturns a class based on its name just like the methodClass.forName(String). If the name of the class isbyte 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
-
-