paint-brush
A laser beam for Java investigatorsby@yolpogists
800 reads
800 reads

A laser beam for Java investigators

by yolposMarch 7th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Every Java developer sooner or later has the exact same problem. They wish they could see <strong>beyond their public API</strong>.
featured image - A laser beam for Java investigators
yolpos HackerNoon profile picture

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_numberbalancefirst_namelast_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');