Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Object.toString alternative. A universal "toString" JavaScript function for any Java Object. Works on nashorn, nudge4j and can be called from Java 8
function toString(oj) {
if (oj == null) return 'null';
if (Java.isJavaObject(oj)==false) return 'Error: not a Java Object';
try {
java.lang.Class.forName('org.apache.commons.lang3.builder.ReflectionToStringBuilder');
} catch (e) {
var url = new java.net.URL('http://central.maven.org/maven2/org/apache/commons/commons-lang3/3.0/commons-lang3-3.0.jar');
var ucl = java.lang.Class.forName('java.net.URLClassLoader');
var addURL = ucl.getDeclaredMethod("addURL", [ url.getClass() ]);
addURL.setAccessible(true);
addURL.invoke(java.lang.ClassLoader.getSystemClassLoader(), url );
addURL.setAccessible(false);
}
var MULTI_LINE_STYLE = Java.type('org.apache.commons.lang3.builder.ToStringStyle').MULTI_LINE_STYLE;
var ReflectionToStringBuilder = Java.type('org.apache.commons.lang3.builder.ReflectionToStringBuilder');
return ReflectionToStringBuilder.toString(oj, MULTI_LINE_STYLE, true,true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.