A laser beam for Java investigators

yolpos
HackerNoon.com
Published in
2 min readApr 5, 2017

--

Every Java developer sooner or later has the exact same problem. They wish they could see beyond their public API.

Because information hiding is brilliant but, when things don’t work, it is nice to have a way to bypass it.

xr (short for x-ray) gives you a laser beam to point against any Java object.

Beam your x-rays towards Java objects

Suppose you are given an instance of this class:

public class Account {
private int account_number;
private String first_name;
private String last_name;
private Double balance;
...
};

xr, lets you reveal its structure

>>xr(account);
account_number
balance
first_name
last_name

and inspect its parts:

>>xr(account,"account_number"); 
13342101
>>xr(account,"first_name");
John
>>xr(account,"last_name");
Smith
>>xr(account,"balance");
12,000

Flabbergasted ? I hope not.

Use it now

xr is a ready-to-use Nashorn/JavaScript function which uses introspection to discover all the fields in an object and reach their values.

To use it, you need to run your Java program in an interactive environment. So, you have two alternatives:

  1. Use jjs
  2. Use nudge4j

For both options all you have to do is one call:

load('https://gist.github.com/lorenzoongithub/7273902ac628b2761f46de2d0c2a31cd');

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--