= #JxnPortable/docs/programmer_examples/demos/Reflection_Demo.jxn

! simply:
sqrt 2.
 = 1.4142135623730951 (double)
Math.E
 = 2.718281828459045 (double)
sqrt( -1 + 0j )
 = 6.123233995736766E-17 + j 1.0 = 1.0 <) 90.0° (JxnComplexAlgebra)
( -1 + 0j ).pow( 0.5 )
 = 6.123233995736766E-17 + j 1.0 = 1.0 <) 90.0° (JxnComplexAlgebra)
!
! or why not the same most complicated using reflection:
!
className = "java.lang.Math"
 = java.lang.Math (java.lang.String)
methodName = "sqrt"
 = sqrt (java.lang.String)
invocationInstance = null  ! null for static method and static field
 = null
argumentValue = 2.
 = 2.0 (double)
parameterType = Double.TYPE
 = double (java.lang.Class:double)
! or from argumentValue:
parameterType = JxnObject.getClass( argumentValue )
 = double (java.lang.Class:double)
invocationArgument = @Double( argumentValue )
 = 2.0 (java.lang.Double)
!
cls = java.lang.Class.forName( className )
 = class java.lang.Math (java.lang.Class:java.lang.Math)
m = cls.getMethod( methodName, { parameterType } )
 = public static double java.lang.Math.sqrt(double) (java.lang.reflect.Method)
m.invoke( invocationInstance, { invocationArgument } ).doubleValue()
 = 1.4142135623730951 (double)
!
! the same in a single line:
Class.forName( className ).getMethod( methodName, { parameterType } ).invoke( invocationInstance, { invocationArgument } ).doubleValue()
 = 1.4142135623730951 (double)
Class.forName( "java.lang.Math" ).getMethod( "sqrt", { Double.TYPE } ).invoke( null, { @Double(2.) } ).doubleValue()
 = 1.4142135623730951 (double)
! Java: ((Double)Class.forName( "java.lang.Math" ).getMethod( "sqrt", new Class[]{ Double.TYPE } ).invoke( null, new Object[]{ new Double( 2. ) } ) ).doubleValue()
!
!
fieldName = "E"
 = E (java.lang.String)
!
f = cls.getField( fieldName )
 = public static final double java.lang.Math.E (java.lang.reflect.Field)
f.get( invocationInstance ).doubleValue()
 = 2.718281828459045 (double)
!
Class.forName( className ).getField( fieldName ).get( invocationInstance ).doubleValue()
 = 2.718281828459045 (double)
Class.forName( "java.lang.Math" ).getField( "E" ).get( null ).doubleValue()
 = 2.718281828459045 (double)
! Java: ((Double)Class.forName( "java.lang.Math" ).getField( "E" ).get( null ) ).doubleValue()
!
!
className = "JxnComplexAlgebra"
 = JxnComplexAlgebra (java.lang.String)
methodName = "pow"
 = pow (java.lang.String)
invocationInstance = @JxnComplexAlgebra( -1., 0. )
 = -1.0 + j 0.0 = 1.0 <) 180.0° (JxnComplexAlgebra)
argumentValue = 0.5
 = 0.5 (double)
parameterType = JxnObject.getClass( argumentValue )
 = double (java.lang.Class:double)
invocationArgument = @Double( argumentValue )
 = 0.5 (java.lang.Double)
!
Class.forName( className ).getMethod( methodName, { parameterType } ).invoke( invocationInstance, { invocationArgument } )
 = 6.123233995736766E-17 + j 1.0 = 1.0 <) 90.0° (JxnComplexAlgebra)
Class.forName( "JxnComplexAlgebra" ).getMethod( "pow", { Double.TYPE } ).invoke( @JxnComplexAlgebra(-1.,0.), { @Double( 0.5 ) } )
 = 6.123233995736766E-17 + j 1.0 = 1.0 <) 90.0° (JxnComplexAlgebra)
! Java: Class.forName( "JxnComplexAlgebra" ).getMethod( "pow", new Class[]{ Double.TYPE } ).invoke( new JxnComplexAlgebra(-1.,0.), new Object[]{ new Double(0.5) } )