Skip to content

Instantly share code, notes, and snippets.

@cidermole
Created October 4, 2019 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cidermole/836abd1c12a59830465e89cba4492ecf to your computer and use it in GitHub Desktop.
Save cidermole/836abd1c12a59830465e89cba4492ecf to your computer and use it in GitHub Desktop.
//
// Obtain class member signatures as required for Jenkins whitelists
// as used in the Script Security plugin,
// https://github.com/jenkinsci/script-security-plugin
//
// Usage example: $ groovy signatures.groovy java.lang.String
//
def resolveArrayType(typeName) {
def arrNames = [
"[Z": "boolean",
"[B": "byte",
"[S": "short",
"[I": "int",
"[J": "long",
"[F": "float",
"[D": "double",
"[C": "char"
]
// array of objects, e.g. [Ljava.lang.String;
while(typeName.contains("[L")) {
typeName = typeName.replaceAll(/\[L([^;]*);/, '$1[]');
}
// array of primitives
def i = 0;
while(typeName.contains("[") && i < 100) {
arrNames.each {
key, val ->
typeName = typeName.replace(key, val + "[]")
}
i++;
}
return typeName
}
// get all public method (and public static method) signatures, in Jenkins whitelist format
def dumpOut(clz, includeClassName = true) {
def signatures = []
clz.metaClass.methods.each { method ->
def typeStr = method.static ? "staticMethod" : "method"
def paramTypes = method.parameterTypes*.name.collect({it -> resolveArrayType(it)}).join(' ')
paramTypes = paramTypes.length() == 0 ? "" : " " + paramTypes
def className = includeClassName ? clz.name : ""
if(method.public)
signatures.add("${typeStr} ${className} ${method.name}${paramTypes}")
}
return signatures
}
// omit java.lang.Object's methods
def dumpHigher(clz) {
def exclude = dumpOut(Object.class, false)
def methods = dumpOut(clz, false)
def signatures = dumpOut(clz)
def result = []
def len = signatures.size()
for(i = 0; i < len; i++) {
if(!exclude.contains(methods[i]))
result.add(signatures[i])
}
return result
}
def clz = Class.forName(args[0])
clz.constructors.each { ctor ->
def paramTypes = ctor.parameterTypes*.name.collect({it -> resolveArrayType(it)}).join(' ')
paramTypes = paramTypes.length() == 0 ? "" : " " + paramTypes
println("new ${clz.name}${paramTypes}")
}
Modifier = java.lang.reflect.Modifier
clz.fields.each { field ->
if(Modifier.isPublic(field.modifiers) && Modifier.isStatic(field.modifiers))
println("staticField ${clz.name} ${field.name}")
}
println dumpHigher(clz).join('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment