Skip to content

Commit 6e092ed

Browse files
Merge pull request #4 from NasdaqGodzilla/develop
Develop
2 parents 797a5ef + 4e3d0ec commit 6e092ed

33 files changed

+3025
-0
lines changed

Android.bp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
java_binary_host {
2+
name: "framework_injector",
3+
manifest: "manifest.txt",
4+
srcs: ["src/**/*.java"],
5+
static_libs: [
6+
"javassist_default",
7+
"jackson_2.13.1",
8+
],
9+
}

MANIFEST.MF

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Main-Class: peacemaker.frameworkinjector.Main
2+
Class-Path: libs/jackson-annotations-2.13.1.jar libs/jackson-core-2.13.1.jar libs/jackson-databind-2.13.1.jar libs/javassist.jar

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# FrameworkInjector
2+
3+
![](https://cdn.jsdelivr.net/gh/NasdaqGodzilla/PeacePicture/img/logo_frameworkInjector.png)
4+
5+
[中文说明](https://github.com/NasdaqGodzilla/FrameworkInjector/blob/develop/README_CN.md)
6+
7+
`FrameworkInjector` is an AOP framework for Java which is managing pointcut configurations by json and performing code weaving with `Javassist`.
8+
9+
_At the begining, `FrameworkInjector` is designed for AOP code weaving of Android framework. This is the origin of the name._
10+
11+
# Feature
12+
1. Managing AOP pointcut configurations by JSON
13+
2. Perform code weaving on specified Java class method
14+
3. Support wildcard while matching class and method name
15+
4. [BUG][TODO] Skipping interface class so that interface currently would not perform code weaving.
16+
17+
# Setup
18+
```
19+
source build.sh
20+
```
21+
22+
Run `build.sh` to compile by `javac` and package by `jar`. The final target is runnable jar.
23+
24+
# Usage
25+
The runnable jar of this program support parameters:
26+
1. `--pointcuts_json` Sets path of json that managing pointcut configurations
27+
2. `-i` Sets the input jar file which performing weaving from
28+
3. `-o` Sets the output jar file which is been injected
29+
4. `-cplist` Class Path List for Javassist. In case of class that exists in the input jar is not able to found in system class path, passing the path of the class to Javassist with this params to help Javassist load it. Every path is concat by ':'(Check it on JDK specifications).
30+
31+
## Run the sample
32+
```
33+
source build_and_run_sample.sh
34+
```
35+
36+
`build_and_run_sample.sh` contains a lot of steps. It will make this program by calling `build.sh`. And then runs the inside sample(`sample/sample.json`). The sample will make this program perform code weaving on the jar generated by `build.sh`
37+
38+
## Run
39+
40+
Perform code weaving on foo.jar:
41+
42+
```
43+
java -jar frameworkinjector.jar -cplist "libs" -pointcuts_json myPointcuts.json -i foo.jar -o injected_foo.jar
44+
```
45+
46+
When error like "Class NotFound" happends, it is useful that add class path:
47+
48+
```
49+
java -Xbootclasspath/a:/path/myPath:<other> <...other cmd parts...>
50+
```
51+
52+
# How it works
53+
54+
![](https://cdn.jsdelivr.net/gh/NasdaqGodzilla/PeacePicture/img/FrameworkInjector架构.drawio.png)
55+
56+
- Perform code weaving with [Javasist](https://github.com/jboss-javassist/javassist)
57+
- Parse json with [Jackson](https://github.com/FasterXML/jackson)
58+
59+
# Sample
60+
1. Run `build_and_run_sample.sh`, which will perform code weaving configed by [sample.json](https://github.com/NasdaqGodzilla/FrameworkInjector/blob/main/sample/sample.json)
61+
2. A new jar that is injected will be produced from step 1. It containes codes injected by this program. In the case of the sample, a new `System.out.println` call is injected which shown as below.
62+
63+
![](https://cdn.jsdelivr.net/gh/NasdaqGodzilla/PeacePicture/img/FrameworkInject_Wildcard_result.png)
64+
65+
# LICENSE
66+
Copyright (C) <2022> <copyright Niko Zhong>
67+
68+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
69+
70+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
71+
72+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README_CN.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# FrameworkInjector
2+
3+
![](https://cdn.jsdelivr.net/gh/NasdaqGodzilla/PeacePicture/img/logo_frameworkInjector.png)
4+
5+
[English Edition](https://github.com/NasdaqGodzilla/FrameworkInjector)
6+
7+
`FrameworkInjector`是一个Java的AOP框架。它通过json文件配置AOP切点信息,使用`Javassist`对Jar包进行插桩实现AOP。
8+
9+
_最初,`FrameworkInjector`被设计用于对Android framework进行AOP插桩,这是其命名的渊源。_
10+
11+
# Feature
12+
1. JSON文件管理AOP插桩切点配置
13+
2. 对指定方法进行插桩
14+
3. 支持通配符匹配类和方法
15+
4. [BUG][TODO] 目前暂时跳过了接口类,对接口类(interface)不执行插桩
16+
17+
# Setup
18+
```
19+
source build.sh
20+
```
21+
22+
执行`build.sh`即可编译得到`frameworkinjector.jar`,它通过`javac`编译,`jar`打包。
23+
24+
# Usage
25+
本工具通过java执行编译得到的jar包,其中需要传递参数:
26+
1. `--pointcuts_json` 设置切点配置文件json的路径
27+
2. `-i` 输入的被插桩的jar包
28+
3. `-o` 输出的被插桩后的jar包
29+
4. `-cplist` Class Path List。作为额外的类加载路径提供给Javassist。当被插桩的类依赖了其他jar包且该jar包不在ClassLoader的默认搜索路径时,通过该参数告知Javassist以使其能够找到并加载类。多个路径使用冒号分隔(详见对应平台的JDK标准)
30+
31+
## Run the sample
32+
```
33+
source build_and_run_sample.sh
34+
```
35+
36+
`build_and_run_sample.sh`集成了一些步骤,它能完成一次编译打包,并运行一个内置的样本。该样本是对本工程编译出来的jar包进行插桩,即自己对自己进行插桩。
37+
38+
## Run
39+
40+
对foo.jar进行插桩:
41+
42+
```
43+
java -jar frameworkinjector.jar -cplist "libs" -pointcuts_json myPointcuts.json -i foo.jar -o injected_foo.jar
44+
```
45+
46+
当出现Class NotFound时,尝试加入对应类的jar包的路径:
47+
48+
```
49+
java -Xbootclasspath/a:/path/myPath:<other> <...other cmd parts...>
50+
```
51+
52+
# How it works
53+
54+
![](https://cdn.jsdelivr.net/gh/NasdaqGodzilla/PeacePicture/img/FrameworkInjector架构.drawio.png)
55+
56+
- Perform code weaving with [Javasist](https://github.com/jboss-javassist/javassist)
57+
- Parse json with [Jackson](https://github.com/FasterXML/jackson)
58+
59+
# Sample
60+
1. Run `build_and_run_sample.sh`, which will perform code weaving configed by [sample.json](https://github.com/NasdaqGodzilla/FrameworkInjector/blob/main/sample/sample.json)
61+
2. A new jar that is injected will be produced from step 1. It containes codes injected by this program. In the case of the sample, a new `System.out.println` call is injected which shown as below.
62+
63+
![](https://cdn.jsdelivr.net/gh/NasdaqGodzilla/PeacePicture/img/FrameworkInject_Wildcard_result.png)
64+
65+
# LICENSE
66+
Copyright (C) <2022> <copyright Niko Zhong>
67+
68+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
69+
70+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
71+
72+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

build.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
OUT="out""$RANDOM"
4+
LIB="libs"
5+
MANIFEST="MANIFEST.MF"
6+
TARGET="frameworkinjector"_"$OUT"".jar"
7+
8+
function setup() {
9+
rm -rf "$OUT"
10+
mkdir "$OUT"
11+
}
12+
13+
function clean() {
14+
rm -rf "$OUT"
15+
}
16+
17+
function build_impl() {
18+
javac -cp "$LIB""/*" -d "$OUT" src/frameworkinjector/*
19+
jar cvfm "$TARGET" "$MANIFEST" -C "$OUT" peacemaker/frameworkinjector/
20+
}
21+
22+
setup
23+
build_impl
24+
clean
25+
26+
echo
27+
ls "$TARGET"

build_and_run_sample.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
source build.sh
4+
5+
INJECTED_TARGET="injected_""$TARGET"
6+
7+
java -jar "$TARGET" -cplist "$LIB" -pointcuts_json sample/sample.json -i "$TARGET" -o "$INJECTED_TARGET"
8+
9+
echo
10+
ls "$INJECTED_TARGET"
11+
73.9 KB
Binary file not shown.

libs/jackson-core-2.13.1.jar

366 KB
Binary file not shown.

libs/jackson-databind-2.13.1.jar

1.46 MB
Binary file not shown.

libs/javassist.jar

754 KB
Binary file not shown.

0 commit comments

Comments
 (0)