如何拦截Java中的对象创建低于用户类级别 [英] How to intercept object creation in Java lower than user class level

查看:97
本文介绍了如何拦截Java中的对象创建低于用户类级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些方法,通过使用Java代理或检测类(最好是比用户类更低级别的东西)拦截JVM中的所有对象创建( new 或任何替代方法来创建对象),有一个类似问题不关注Java代理或低于检测用户类的东西

I am looking towards some approach where by using Java agent or instrumenting classes (preferably something at lower level than user classes) to intercept all object creation in JVM (new or any alternative ways to create Object), There is a similar question which doesn't focus on Java agent or something lower than instrumenting user classes

推荐答案

可以用几种不同的方式创建Java对象。 / p>

Java Objects can be created in several different ways.


  1. 从Java代码,当Java方法(解释或编译)执行以下字节码指令之一时: new newarray anewarray multianewarray

  2. 从本机代码,当本机方法时,包括标准类库中的那些,调用JNI函数之一: NewObject NewObjectArray NewStringUTF N ewDirectByteBuffer 等。

  3. 直接来自VM运行时,当JVM在内部创建新对象时,例如,响应 Object.clone() Throwable.getStackTrace() Class。 getInterfaces()等。

  1. From Java code, when a Java method, either interpreted or compiled, executes one of the following bytecode instructions: new, newarray, anewarray, multianewarray.
  2. From native code, when native methods, including those in standard class library, call one of JNI functions: NewObject, NewObjectArray, NewStringUTF, NewDirectByteBuffer, etc.
  3. Directly from VM runtime, when a new object is created internally by JVM, for example, in response to Object.clone(), Throwable.getStackTrace(), Class.getInterfaces(), etc.

不幸的是,没有一个点可以从中收集所有对象这些来源。但是,有一些方法可以拦截所有这些。

Unfortunately, there is no single point where you can collect objects from all these sources. However, there are means for intercepting all of them.


  1. 从Java实例化的对象可以被 Instrumentation 代理。代理需要定义 ClassFileTransformer 这将扫描所有已加载类的字节码以获取对象创建指令并进行修改。

  1. Objects instantiated from Java can be caught by an Instrumentation agent. The agent needs to define a ClassFileTransformer that will scan the bytecode of all loaded classes for object-creating instructions and modify it.

注意:无需拦截所有 new 说明,您可以使用 Object()构造函数。但是你仍然需要拦截数组分配指令。

Note: there is no need to intercept all new instructions, you can instrument Object() constructor instead. But you still need to intercept array allocation instructions.

JVMI代理可以拦截JNI函数。您需要为 NewObjectArray NewStringUTF 等定义自己的本机挂钩,然后替换JNI函数表。有关详细信息,请参见 JVMTI参考

JNI functions can be intercepted by JVMTI agent. You need to define your own native hooks for NewObjectArray, NewStringUTF etc. and then replace JNI function table. See JVMTI Reference for the details.

VM创建的对象可以被 JVMTI事件回调机制。所需的事件是 VMObjectAlloc

Objects created by the VM can be caught by JVMTI Event Callback mechanism. The desired event is VMObjectAlloc.

注意:对于从Java或JNI函数分配的对象,JVM不会发布 VMObjectAlloc 事件。

Note: JVM will not post VMObjectAlloc event for objects allocated from Java or by JNI functions.

对象实例化(克隆,反射,反序列化)的所有其他方式都属于上述类别之一。

All other ways of object instantiation (cloning, reflection, deserialization) fall into one of the above categories.

从Oracle获取JDK 8演示和示例 Java SE下载网站。

有一个样本JVMTI代理正是针对这个问题。

Get JDK 8 Demos and Samples from Oracle Java SE Downloads website.
There is a sample JVMTI agent for exactly this question.

查看


  • jvmti / heapTracker

  • jvmti / hprof

  • jvmti/heapTracker
  • jvmti/hprof

这篇关于如何拦截Java中的对象创建低于用户类级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆