Introspection: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: added whitespace, changed some comments in the section headers. |
Added Kotlin |
||
Line 910:
julia> VERSION > v"0.3.1"
true</lang>
=={{header|Kotlin}}==
We will use Java reflection for this task as Kotlin's own reflection facilities do not appear to be able to deal generically with top-level entities at the present time (i.e. ::class isn't yet supported):
<lang scala>// version 1.0.6 (intro.kt)
import java.lang.reflect.Method
val bloop = -3
val i = 4
val j = 5
val k = 6
fun main(args: Array<String>) {
// get version of JVM
val version = System.getProperty("java.version")
if (version >= "1.6") println("The current JVM version is $version")
else println("Must use version 1.6 or later")
// check that 'bloop' and 'Math.abs' are available
// note that the class created by the Kotlin compiler for top level declarations will be called 'IntroKt'
val topLevel = Class.forName("IntroKt")
val math = Class.forName("java.lang.Math")
val abs = math.getDeclaredMethod("abs", Int::class.java)
val methods = topLevel.getDeclaredMethods()
for (method in methods) {
// note that the read-only Kotlin property 'bloop' is converted to the static method 'getBloop' in Java
if (method.name == "getBloop" && method.returnType == Int::class.java) {
println("\nabs(bloop) = ${abs.invoke(null, method.invoke(null))}")
break
}
}
// now get the number of global integer variables and their sum
var count = 0
var sum = 0
for (method in methods) {
if (method.returnType == Int::class.java) {
count++
sum += method.invoke(null) as Int
}
}
println("\nThere are $count global integer variables and their sum is $sum")
}</lang>
{{out}}
<pre>
The current JVM version is 1.8.0_121
abs(bloop) = 3
There are 4 global integer variables and their sum is 12
</pre>
=={{header|Lasso}}==
|