Skip to content

Instantly share code, notes, and snippets.

View lorenzoongithub's full-sized avatar

lorenzo puccetti lorenzoongithub

  • london, united kingdom
View GitHub Profile
@lorenzoongithub
lorenzoongithub / nudge4j.java
Last active September 6, 2019 19:48
Snippet of Java code to make your Java application accessible to the browser. It's meant for use during development to provide an environment for experimenting with code against a running application.
// nudge4j - bootstrap
try {
new javax.script.ScriptEngineManager()
.getEngineByName("JavaScript")
.eval("load('http://lorenzoongithub.github.io/nudge4j/twigs/n4j.boot.js')");
} catch (javax.script.ScriptException e) {
throw new RuntimeException(e);
}
@lorenzoongithub
lorenzoongithub / xr.js
Last active April 10, 2017 08:31
xr.js - A Nashorn JavaScript function to navigate inside the structure of any Java object. Designed for nudge4j. https://lorenzoongithub.github.io/nudge4j/
function xr(oj, name) {
var arrayLength = java.lang.reflect.Array.getLength;
var isStaticField = function(field) {
return java.lang.reflect.Modifier.isStatic(field.getModifiers());
}
/**
* Returns true if the input appears to represent a Class, false otherwise.
*
@lorenzoongithub
lorenzoongithub / toString.js
Last active August 16, 2019 12:48
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);
package test;
/**
* nudge4j's demo: A stand alone Java 8 program that you can just run.
**/
public class DemoNudge4j {
public static void main(String args[]) {
System.out.println("Demo nudge4j");
//
@lorenzoongithub
lorenzoongithub / nudge4j.PNGEncoder.java
Created June 3, 2016 21:09
requires okio.jar in your classpath. see: https://github.com/square/okio
//
// PNG Encoder. This script requires okio.jar
//
var URL = Java.type('java.net.URL');
var ByteString = Java.type('okio.ByteString');
var Okio = Java.type('okio.Okio');
var Buffer = Java.type('okio.Buffer');
var url = new URL('http://i936.photobucket.com/albums/ad204/0_Marathon_0/Abstract.png');
var mem = java.lang.management.ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
var usd = Math.round( mem.getUsed() / 1048576 )
var cmt = Math.round( mem.getCommitted() / 1048576 )
var max = Math.round( mem.getMax() / 1048576 )
'<div style="font-family:monospace;margin:2%;border-bottom:1px solid #333; border-left:1px solid #333; border-right:1px solid #333">'+
'<div style="border-top:1px solid #333"><div style="width:'+(usd*100/max)+'%;background-color:#dcd;">used:'+usd+'MB</div></div>'+
'<div style="border-top:1px solid #333"><div style="width:'+(cmt*100/max)+'%;background-color:#ddd;">committed:'+cmt+'MB</div></div>'+
'<div style="border-top:1px solid #333"><div style="width:100%; background-color:#ddd;">max:'+max+'MB</div></div></div>';
//
// Using Lambda in java 8
// Inspired by https://blogs.oracle.com/nashorn/entry/nashorn_and_lambda_what_the
//
var copyright =
'/* \n'+
' * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. \n'+
' * \n'+
' * Redistribution and use in source and binary forms, with or without \n'+
//
// Prints all threads
//
var threads = java.lang.Thread.getAllStackTraces().keySet().toArray();
var str ='';
for (var i=0;i<threads.length;i++) {
str+=threads[i]+' - '+threads[i].getState()+'\n';
}
str;
//
// An example of java 8 new Date/Time API.
//
var LocalDate = Java.type('java.time.LocalDate');
var Month = Java.type('java.time.Month');
var ChronoUnit = Java.type('java.time.temporal.ChronoUnit');
var today = LocalDate.now();
var firstDayThisYear = LocalDate.of(today.getYear(), Month.JANUARY, 1);
//
// https://github.com/mailcheck/mailcheck
//
load('https://cdn.jsdelivr.net/mailcheck/1.1/mailcheck.min.js');
Kicksend.mailcheck.run({email:'lo@hotmail.cmo',suggested:function(oj) { x = oj; }});
x;