1. Overview
1.概述
nudge4j allows developers to see the impact of any operation straight-away and provides an environment in which they can explore, learn, and ultimately spend less time debugging and redeploying their application.
nudge4j允许开发人员直接看到任何操作的影响,并提供一个环境,使他们能够探索、学习,并最终减少调试和重新部署其应用程序的时间。
In this article, we will explore what nudge4j is, how it works, and how any Java application in development might benefit from it.
在本文中,我们将探讨什么是nudge4j,它是如何工作的,以及任何正在开发的Java应用程序如何从它那里受益。
2. How nudge4j Works
2.nudge4j是如何工作的
2.1. A REPL in Disguise
2.1.伪装的REPL
nudge4j is essentially a read-eval-print-loop (REPL) in which you talk to your Java application within a browser window via a simple page containing just two elements:
nudge4j本质上是一个读-评价-打印-循环(REPL),在这个循环中,你通过一个只包含两个元素的简单页面与浏览器窗口中的Java应用程序对话。
- an editor
- the Execute on JVM button
You can talk to your JVM in a typical REPL cycle:
你可以在一个典型的REPL循环中与你的JVM对话。
- Type any code into the editor and press Execute on JVM
- The browser posts the code to your JVM, which then runs the code
- The result is returned (as a string) and displayed below the button
nudge4j comes with a few examples to try straight-away, like querying how long the JVM has been running and how much memory is currently available. I suggest you start with these before writing your own code.
nudge4j附带了一些可以直接尝试的例子,比如查询JVM已经运行了多长时间,以及当前有多少内存可用。我建议你在写你自己的代码之前先从这些开始。
2.2. The JavaScript Engine
2.2.JavaScript引擎
The code which is sent by the browser to the JVM is JavaScript that manipulates Java objects (not to be confused with any JavaScript that runs on the browser). The JavaScript is executed by the built-in JavaScript engine Nashorn.
浏览器发送给JVM的代码是操作Java对象的JavaScript(不要与在浏览器上运行的任何JavaScript相混淆)。这些JavaScript是由内置的JavaScript引擎Nashorn执行的。
Don’t worry if you don’t know (or like) JavaScript – for your nudge4j needs, you can just think of it as an untyped Java dialect.
如果你不知道(或不喜欢)JavaScript,也不用担心–为了你的nudge4j需求,你可以把它看作是一种无类型的Java方言。
Note that I am aware that saying that “JavaScript is untyped Java” is a huge simplification. But I want Java developers (who may be prejudiced towards anything that is JavaScript) to give nudge4j a fair chance.
请注意,我知道说“JavaScript是无类型的Java”是一个巨大的简化。但我希望Java开发者(他们可能对任何属于JavaScript的东西有偏见)能给nudge4j一个公平的机会。
2.3. Scope of JVM Interaction
2.3.JVM相互作用的范围
nudge4j lets you access any Java class which is accessible from your JVM, allowing you to call methods, create objects, etc. This is very powerful, but it might not be sufficient while working with your application.
nudge4j让你访问任何可以从你的JVM访问的Java类,允许你调用方法,创建对象等。这非常强大,但在使用你的应用程序时可能还不够。
In some situations, you might want to reach one or more objects, specific to your application only, so that you can manipulate them. nudge4j allows for that. Any object that needs to be exposed can be passed as an argument at instantiation time.
在某些情况下,你可能想接触一个或多个对象,只针对你的应用程序,这样你就可以对它们进行操作。nudge4j允许这样做。任何需要暴露的对象都可以在实例化时作为一个参数传递。
2.4. Exception Handling
2.4.异常处理
The design of nudge4j recognizes the possibility that users of the tool might make mistakes or cause errors on the JVM. In both of these cases, the tool is designed to report the full stack trace in order to guide the user to rectify the mistake or error.
nudge4j的设计认识到,该工具的用户有可能在JVM上犯错或导致错误。在这两种情况下,该工具被设计为报告完整的堆栈跟踪,以指导用户纠正错误或误差。
Let’s look at a screenshot in which a snippet of code that has been executed results in an Exception being thrown:
让我们看一个截图,其中一个代码片断被执行后导致一个异常被抛出。
3. Adding nudge4j to Your Application
3.将nudge4j添加到您的应用程序
3.1. Just Copy and Paste
3.1.只需复制和粘贴
The integration with nudge4j is achieved somewhat unconventionally, as there are no jar files to add to your classpath, and there are no dependencies to add to a Maven or Gradle build.
与nudge4j的集成是以非传统方式实现的,因为没有jar文件需要添加到classpath中,也没有需要添加到Maven或Gradle构建中的依赖项。
Instead, you are required to simply copy and paste a small snippet of Java code – around 100 lines – anywhere into your own code before you run it.
相反,你需要简单地复制和粘贴一小段Java代码–大约100行–到你自己代码的任何地方,然后再运行。
You’ll find the snippet on the nudge4j home page – there’s even a button on the page that you can click to copy the snippet to your clipboard.
您可以在nudge4j主页上找到该片段–该页面上甚至有一个按钮,您可以点击它将该片段复制到您的剪贴板上。
This snippet of code might appear quite abstruse at first. There are a few reasons for that:
这段代码一开始可能显得很玄乎。这有几个原因。
- The nudge4j snippet can be dropped into any class; therefore, it could not make any assumption regarding the imports, and any class it contained had to be fully qualified
- To avoid potential clashes with variables already defined, the code is wrapped in a function
- Access to the built-in JDK HttpServer is done via introspection in order to avoid restrictions which exist with some IDEs (e.g. Eclipse) for packages beginning with “com.sun.*”
So, even though Java is already a verbose language, it had to be made even more verbose to provide for a seamless integration.
因此,尽管Java已经是一种冗长的语言,但它必须变得更加冗长,以提供一个无缝的整合。
3.2. Sample Application
3.2.应用样本
Let’s start with a standard JVM application where we pretend that a simple java.util.HashMap holds most of the information that we want to play with:
让我们从一个标准的JVM应用程序开始,我们假装一个简单的java.util.HashMap持有我们想玩的大部分信息。
public class MyApp {
public static void main(String args[]) {
Map map = new HashMap();
map.put("health", 60);
map.put("strength", 4);
map.put("tools", Arrays.asList("hammer"));
map.put("places", Arrays.asList("savannah","tundra"));
map.put("location-x", -42 );
map.put("location-y", 32);
// paste original code from nudge4j below
(new java.util.function.Consumer<Object[]>() {
public void accept(Object args[]) {
...
...
}
}).accept(new Object[] {
5050, // <-- the port
map // <-- the map is passed as a parameter.
});
}
}
As you can see from this example, you simply paste in the nudge4j snippet at the end of your own code. Lines 12-20 in the example here serve as a placeholder for an abbreviated version of the snippet.
正如你从这个例子中看到的,你只需在自己的代码末尾粘贴nudge4j片段。这里的例子中的第12-20行是一个占位符,是该片段的缩写版本。
Now, let’s point the browser to http://localhost:5050/. The map is now accessible as args[1] in the editor from the browser by simply typing:
现在,让我们把浏览器指向http://localhost:5050/。现在可以通过简单的键入,在编辑器中以args[1] 的形式访问该地图。
args[1];
This will provide a summary of our Map (in this case relying on the toString() method of the Map and its keys and values).
这将提供我们的Map的摘要(在这种情况下依赖于Map的toString()方法及其键和值)。
Suppose we want to examine and modify the Map entry with the key value “tools”.
假设我们想检查并修改键值为“工具”的Map条目。
To get a list of all available tools in the Map, you would write:
要获得地图中所有可用的工具的列表,你要写。
map = args[1];
map.get("tools");
And to add a new tool to the Map, you would write:
而要在Map上添加一个新的工具,你要写。
map = args[1];
map.get("tools").add("axe");
In general, few lines of code should be sufficient to probe any Java application.
一般来说,几行代码就足以探测任何Java应用程序。
4. Conclusion
4.结论
By combining two simple APIs within the JDK (Nashorn and Http server) nudge4j gives you the ability to probe into any Java 8 Application.
通过结合JDK中两个简单的API(Nashorn和Http server),nudge4j让你有能力探测任何Java 8应用程序。
In a way, nudge4j is just a modern cut off an old idea: give developers access to the facilities of an existing system via a scripting language – an idea that can make an impact on how Java developers could spend their day coding.
在某种程度上,nudge4j只是一个古老想法的现代切割:让开发人员通过脚本语言访问现有系统的设施–这个想法可以对Java开发人员如何度过他们的日常编码产生影响。