alibaba / alibaba/one-java-agent

如何识别transform 传过来的字节码,是否实现了某一接口,或者是否子类

Open
#9 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
579
Forks
128
PR merge metrics
No merged PRs in 30d

Description

对于ClassFileTransformer的回调函数,`String className, Class classBeingRedefined` 都有可能是空的。因为类在第一次加载时,还没有被加载到JVM里。只能取到 `byte[] classfileBuffer`。

```java
@Override
public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer)
```

但是只从`byte[] classfileBuffer`是没有办法判断它所有实现的接口和继承的父类的。 比如有多重继承时。

只能取到这个类本身显式 `implements`和`extends`的信息。

---

为了解决这个问题,有两种思路:

1. 尝试用ClassLoader去 getResource,把每一个父类,父接口的`.class`读取出来,再一层层向上解析。但这个有可能不能工作。因为有些类并不一定会有 `.class`,并且ClassLoader也并不一定能读到对应的resource。

2. 对于每一个传入的`Class classBeingRedefined` 为 null 的类,都解析`byte[] classfileBuffer`,读取并记录它的信息。因为父类是要先于子类加载的,所以能保证在子类想要解析时,总能读出前面已保存过的父类的信息。 缺点是会占比较多内存,还有多ClassLoader情况,是否能正确工作?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the ClassFileTransformer.transform callback and the classfileBuffer, classBeingRedefined, and className cases described here. Compare the two proposed class-resolution approaches across parent classes, interfaces, unavailable resources, and multiple class loaders. The issue is ready only when a concrete implementation scope and verification criteria are agreed.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.