友情支持
如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜
有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。
| 公众号的微信号是: jikerizhi。因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。 |
|" {}
35 | find . -name "*.html" | grep -v "index.html" | xargs -I {} sed -i 's|||' {}
36 |
37 | - name: Setup Node.js 🕸
38 | uses: actions/setup-node@v3
39 | with:
40 | node-version: '16'
41 |
42 | - name: Compress Style 🍭
43 | run: |
44 | npm install cssnano-cli --location=global
45 | cd target/docs/multipage/assets/styles
46 | echo -e '\na{text-decoration:none;}p>code,strong>code{color: #d14 !important;background-color: #f5f5f5 !important;border: 1px solid #e1e1e8;white-space: nowrap;border-radius: 3px;}' >> asciidoctor.css
47 | for f in `ls *.css`;
48 | do
49 | fn="${f%.*}.min.css";
50 | cssnano $f $fn;
51 | rm -rf $f;
52 | mv $fn $f
53 | done
54 |
55 | - name: Rsync Deploy 🏹
56 | uses: burnett01/rsync-deployments@5.2
57 | with:
58 | switches: -avzr --delete
59 | path: target/docs/multipage/
60 | remote_path: ${{ secrets.DEPLOY_PATH }}
61 | remote_host: ${{ secrets.DEPLOY_HOST }}
62 | remote_port: ${{ secrets.DEPLOY_PORT }}
63 | remote_user: ${{ secrets.DEPLOY_USER }}
64 | remote_key: ${{ secrets.DEPLOY_KEY }}
65 |
66 | - name: Change Files Mod 🔐
67 | uses: appleboy/ssh-action@master
68 | with:
69 | host: ${{ secrets.DEPLOY_HOST }}
70 | port: ${{ secrets.DEPLOY_PORT }}
71 | username: ${{ secrets.DEPLOY_USER }}
72 | key: ${{ secrets.DEPLOY_KEY }}
73 | script: |
74 | cd ${{ secrets.DEPLOY_PATH }}
75 | sudo chmod -R 777 *
76 |
77 | - name: Deploy 🚀
78 | uses: JamesIves/github-pages-deploy-action@v4
79 | with:
80 | # The branch the action should deploy to.
81 | branch: gh-pages
82 | # The folder the action should deploy.
83 | folder: target/docs/multipage/
84 | single-commit: true
--------------------------------------------------------------------------------
/docs/custom-instrumentation.adoc:
--------------------------------------------------------------------------------
1 | [#custom-instrumentation]
2 | = 定制化仪表
3 |
4 | [{java_source_attr}]
5 | ----
6 | LDC 10 // stack contains 10
7 | LDC 50 // stack contains 10, 50
8 | IADD // stack contains 60
9 | IRETURN // stack is empty
10 | ----
11 |
12 |
13 | [{java_source_attr}]
14 | ----
15 | 12 00 01
16 | 12 00 02
17 | 60
18 | AC
19 | ----
20 |
21 | [{java_source_attr}]
22 | ----
23 | enum IntegerSum implements StackManipulation {
24 |
25 | INSTANCE; // singleton
26 |
27 | @Override
28 | public boolean isValid() {
29 | return true;
30 | }
31 |
32 | @Override
33 | public Size apply(MethodVisitor methodVisitor,
34 | Implementation.Context implementationContext) {
35 | methodVisitor.visitInsn(Opcodes.IADD);
36 | return new Size(-1, 0);
37 | }
38 | }
39 | ----
40 |
41 |
42 | [{java_source_attr}]
43 | ----
44 | enum SumMethod implements ByteCodeAppender {
45 |
46 | INSTANCE; // singleton
47 |
48 | @Override
49 | public Size apply(MethodVisitor methodVisitor,
50 | Implementation.Context implementationContext,
51 | MethodDescription instrumentedMethod) {
52 | if (!instrumentedMethod.getReturnType().asErasure().represents(int.class)) {
53 | throw new IllegalArgumentException(instrumentedMethod + " must return int");
54 | }
55 | StackManipulation.Size operandStackSize = new StackManipulation.Compound(
56 | IntegerConstant.forValue(10),
57 | IntegerConstant.forValue(50),
58 | IntegerSum.INSTANCE,
59 | MethodReturn.INTEGER
60 | ).apply(methodVisitor, implementationContext);
61 | return new Size(operandStackSize.getMaximalSize(),
62 | instrumentedMethod.getStackSize());
63 | }
64 | }
65 | ----
66 |
67 | [{java_source_attr}]
68 | ----
69 | enum SumImplementation implements Implementation {
70 |
71 | INSTANCE; // singleton
72 |
73 | @Override
74 | public InstrumentedType prepare(InstrumentedType instrumentedType) {
75 | return instrumentedType;
76 | }
77 |
78 | @Override
79 | public ByteCodeAppender appender(Target implementationTarget) {
80 | return SumMethod.INSTANCE;
81 | }
82 | }
83 | ----
84 |
85 |
86 | [{java_source_attr}]
87 | ----
88 | abstract class SumExample {
89 | public abstract int calculate();
90 | }
91 |
92 | new ByteBuddy()
93 | .subclass(SumExample.class)
94 | .method(named("calculate"))
95 | .intercept(SumImplementation.INSTANCE)
96 | .make()
97 | ----
98 |
99 | [#creating-a-custom-assigner]
100 | == 创建自定义分配器
101 |
102 | [{java_source_attr}]
103 | ----
104 | enum ToStringAssigner implements Assigner {
105 |
106 | INSTANCE; // singleton
107 |
108 | @Override
109 | public StackManipulation assign(TypeDescription.Generic source,
110 | TypeDescription.Generic target,
111 | Assigner.Typing typing) {
112 | if (!source.isPrimitive() && target.represents(String.class)) {
113 | MethodDescription toStringMethod = new TypeDescription.ForLoadedType(Object.class)
114 | .getDeclaredMethods()
115 | .filter(named("toString"))
116 | .getOnly();
117 | return MethodInvocation.invoke(toStringMethod).virtual(sourceType);
118 | } else {
119 | return StackManipulation.Illegal.INSTANCE;
120 | }
121 | }
122 | }
123 | ----
124 |
125 |
126 | [{java_source_attr}]
127 | ----
128 | new ByteBuddy()
129 | .subclass(Object.class)
130 | .method(named("toString"))
131 | .intercept(FixedValue.value(42)
132 | .withAssigner(new PrimitiveTypeAwareAssigner(ToStringAssigner.INSTANCE),
133 | Assigner.Typing.STATIC))
134 | .make()
135 | ----
136 |
137 | [#creating-a-custom-parameter-binder]
138 | == 创建自定义参数绑定器
139 |
140 | [{java_source_attr}]
141 | ----
142 | @Retention(RetentionPolicy.RUNTIME)
143 | @interface StringValue {
144 | String value();
145 | }
146 | ----
147 |
148 | [{java_source_attr}]
149 | ----
150 | enum StringValueBinder
151 | implements TargetMethodAnnotationDrivenBinder.ParameterBinder
{
152 |
153 | INSTANCE; // singleton
154 |
155 | @Override
156 | public Class getHandledType() {
157 | return StringValue.class;
158 | }
159 |
160 | @Override
161 | public MethodDelegationBinder.ParameterBinding> bind(AnnotationDescription.Loaded annotation,
162 | MethodDescription source,
163 | ParameterDescription target,
164 | Implementation.Target implementationTarget,
165 | Assigner assigner,
166 | Assigner.Typing typing) {
167 | if (!target.getType().asErasure().represents(String.class)) {
168 | throw new IllegalStateException(target + " makes illegal use of @StringValue");
169 | }
170 | StackManipulation constant = new TextConstant(annotation.loadSilent().value());
171 | return new MethodDelegationBinder.ParameterBinding.Anonymous(constant);
172 | }
173 | }
174 | ----
175 |
176 | [{java_source_attr}]
177 | ----
178 | class ToStringInterceptor {
179 | public static String makeString(@StringValue("Hello!") String value) {
180 | return value;
181 | }
182 | }
183 |
184 | new ByteBuddy()
185 | .subclass(Object.class)
186 | .method(named("toString"))
187 | .intercept(MethodDelegation.withDefaultConfiguration()
188 | .withBinders(StringValueBinder.INSTANCE)
189 | .to(ToStringInterceptor.class))
190 | .make()
191 | ----
192 |
--------------------------------------------------------------------------------
/docs/preliminary.adoc:
--------------------------------------------------------------------------------
1 | [#preliminary]
2 | = 为什么需要在运行时生成代码?
3 |
4 | Java 语言带有一套比较严格的类型系统。Java 要求所有变量和对象都有一个确定的类型,并且任何赋值不兼容类型的尝试都会抛出错误。这些错误通常都会被编译器检查出来,或者极少情况下,在非法转换类型的时候由Java运行时抛出。如此严格的类型限制在大多数情况下是可取的,比如在编写业务应用时。在业务领域,通常可以以明确的方式去描述其中任何元素,各个元素都有自己明确的类型。通过这种方式,我们可以用 Java 构建具有非常强可读性和稳定性的应用,应用中的错误也非常贴近源码。除此之外,Java 严格的类型系统造就 Java 在企业编程中的普及。
5 |
6 | _然而,通过强制实施其严格的类型系统,Java 限制了自己在其他领域的应用范围。_ 比如,当编写一个供其他 Java 应用使用的通用库时,我们通常不能引用用户应用中定义的任何类型,因为当这个通用库被编译时,我们还不知道这些类型。为了调用用户未知代码的方法或者访问其属性,Java 类库提供了一套反射 API。使用这套反射 API,我们就可以反省未知类型,进而调用方法或者访问属性。不幸的是,这套反射 API 的用法有两个明显的缺点:
7 |
8 | * 相比硬编码的方法调用,使用 http://docs.oracle.com/javase/tutorial/reflect/index.html[反射 API 非常慢]:首先,需要执行一个相当昂贵的方法查找来获取描述特定方法的对象。同时,当一个方法被调用时,这要求 Java 虚拟机去运行本地代码,相比直接调用,这需要一个很长的运行时间。然而,现代 Java 虚拟机知道一个被称为“类型膨胀”的概念:基于 JNI 的方法调用会被动态生成的字节码给替换掉,而这些方法调用的字节码被注入到一个动态生成的类中。(即使 Java 虚拟机自身也使用代码生成!)毕竟,Java 的类型膨胀系统仍存在生成非常一般的代码的缺点,例如,仅能使用基本类型的装箱类型以至于性能缺陷不能完全解决。
9 | * 反射 API 能绕过类型安全检查:即使 Java 虚拟机支持通过反射进行代码调用,但反射 API 自身并不是类型安全的。当编写一个类库时,只要我们不需要把反射 API 暴露给库的用户,就不会有什么大问题。毕竟,当我们编译类库时,我们不知道用户代码,而且也不能校验我们的库与用户类型是否匹配。__有时,需要通过让一个库为我们自己调用我们自己的方法之一来向用户显示反射 API 示例。__这是使用反射 API 变得有问题的地方,因为 Java 编译器将具有所有信息来验证我们的程序的类型安全性。例如,当实现方法级安全库时,这个库的用户将希望这个库做到强制执行安全限制才能调用方法。为此,在用户传递过来方法所需的参数后,这个库将反射性地调用方法。这样,就没有编译时类型检查这些方法参数是否与方法的反射调用相匹配。方法调用依然会校验,只是被推迟到了运行时。这样做,我们就错失了 Java 编程语言的一大特性。
10 |
11 | 这正是运行时代码生成能帮助我们的地方。它允许我们模拟一些只有使用动态编程语言编程才有的特性,而且不丢失 Java 的静态类型检查。这样,我们就可以两全其美并且还可以提高运行时性能。为了更好地理解这个问题,让我们实现一个方法级安全库。
12 |
13 | [#writing-a-security-library]
14 | == 编写一个安全的库
15 |
16 | 业务应用程序可能会增长,有时很难在我们的应用程序中概述调用堆栈。当我们在应用程序中使用至关重要的方法时,而这些方法只能在特定条件下调用,这可能会变得有问题。 设想一下,实现重置功能的业务应用程序可以从应用程序的数据库中删除所有内容。
17 |
18 | [{java_source_attr}]
19 | ----
20 | class Service {
21 | void deleteEverything() {
22 | // delete everything ...
23 | }
24 | }
25 | ----
26 |
27 | 这样的复位操作当然只能由管理员执行,而不是由应用程序的普通用户执行。通过分析源代码,我们当然可以确保这将永远不会发生。但是,我们期望我们的应用能够在未来发展壮大。因此,我们希望实现更紧密的安全模型,其中通过对应用程序的当前用户的显式检查来保护方法调用。我们通常会使用一个安全框架来确保该方法从不被除管理员外的任何人调用。
28 |
29 | 为此,假设我们使用具有公共 API 如下的安全框架:
30 |
31 | [{java_source_attr}]
32 | ----
33 | @Retention(RetentionPolicy.RUNTIME)
34 | @interface Secured {
35 | String user();
36 | }
37 |
38 | class UserHolder {
39 | static String user;
40 | }
41 |
42 | interface Framework {
43 | T secure(Class type);
44 | }
45 | ----
46 |
47 | 在此框架中,`Secured` 注解应用于标记只能由给定用户访问的方法。`UserHolder` 用于在全局范围内定义当前登录到应用程序的用户。`Framework` 接口允许通过调用给定类型的默认构造函数来创建安全实例。当然,这个框架过于简单,但是,从本质上来说,即使流行的安全框架,例如 http://projects.spring.io/spring-security/[Spring Security],也是这样实现的。这个安全框架的一个特点是我们过滤用户的类型。通过调用我们框架的接口,我们承诺返回给用户任何类型 `T` 的实例。幸亏这样,用户能够透明地他自己的类型进行交互,就像安全框架根本不存在一样。在测试环境中,用户甚至可以创建其类型的不安全实例,使用这些实例来代替安全实例。你会同意这真的很方便!已知这种框架使用 POJO,普通的旧 Java 对象进行交互,这是一种用于描述不侵入框架的术语,这些框架不会将自己的类型强加给用户。
48 |
49 | 现在,想象一下,假如我们知道传递给 `Framework` 的类型只能是 `T = Service`,而且 `deleteEverything` 方法用 `@Secured("ADMIN")` 注解。这样,我们可以通过简单的子类化来轻松实现这种特定类型的安全版本:
50 |
51 | [{java_source_attr}]
52 | ----
53 | class SecuredService extends Service {
54 | @Override
55 | void deleteEverything() {
56 | if(UserHolder.user.equals("ADMIN")) {
57 | super.deleteEverything();
58 | } else {
59 | throw new IllegalStateException("Not authorized");
60 | }
61 | }
62 | }
63 | ----
64 |
65 | 通过这个额外的类,我们可以实现框架如下:
66 |
67 | [{java_source_attr}]
68 | ----
69 | class HardcodedFrameworkImpl implements Framework {
70 | @Override
71 | public T secure(Class type) {
72 | if(type == Service.class) {
73 | return (T) new SecuredService();
74 | } else {
75 | throw new IllegalArgumentException("Unknown: " + type);
76 | }
77 | }
78 | }
79 | ----
80 |
81 | 当然这个实现并没有太多的用处。通过标注 `secure` 方法签名,我们建议该方法可以为任何类型提供安全性,但实际上,一旦遇到其他事情,我们将抛出一个异常,然后是已知的 `Service`。此外,当编译库时,这将需要我们的安全库知道有关此特定 `Service` 类型的信息。显然,这不是实现框架的可行解决方案。那么我们如何解决这个问题呢?好吧,由于这是一个关于代码生成库的教程,你可能已经猜到答案:当通过调用 `secure` 方法, `Service` 类第一次被我们安全框架知道时,我们会在运行时后台地创建一个子类。通过使用代码生成,我们可以使用任何给定的类型,在运行时将其子类化,并覆盖我们要保护的方法。在我们的例子中,我们覆盖所有被 `@Secured` 注解标注的方法,并从注解的 `user` 属性中读取所需的用户。许多流行的 Java 框架都使用类似的方法实现。
82 |
83 | [#general-information]
84 | == 基本信息
85 | 在学习代码生成和 Byte Buddy 之前,请注意,应该谨慎使用代码生成。Java 类型对于 Java 虚拟机来说,是相当特别的东西,通常不能当做垃圾被回收。因此,不应该过度使用代码生成,而应该只在生成代码是解决问题的唯一出路时使用。但是,如果需要像上面的示例那样增强未知类型时,则代码生成很可能是你唯一的选择。用于安全性,事务管理,对象关系映射或类型模拟(mock)等框架是代码生成库的典型用户。
86 |
87 | 当然,Byte Buddy 不是 Java 虚拟机上第一个代码生成库。不过,我们认为 Byte Buddy 拥有其他框架没有的技巧。Byte Buddy 的总体目标是通过专注于其领域特定语言和注解的使用来声明式地进行工作。据我们所知,没有其他针对 Java 虚拟机的代码生成库以这种方式工作。不过,你可能希望看一下其他代码生成框架,以找出最适合你的套件。以下库在 Java 中很流行:
88 |
89 | http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Proxy.html[*Java Proxy*]::
90 | Java 类库自带了一个代理工具,它允许为实现了一系列接口的类创建代理。这个内置的代理供应商非常方便,但局限性也特别明显。 上面提到的安全框架就不能用这样的方式来实现的,因为我们想扩展是类而不是扩展接口。
91 |
92 | https://github.com/cglib/cglib[*cglib*]::
93 | 代码生成库(注:这里指 `cglib`)诞生于 Java 初期,但不幸的是没有跟上 Java 平台的发展。然而,cglib 仍然是一个相当强大的库,但其积极的开发却变得相当模糊。鉴于此,其许多用户已经离开了 cglib。
94 |
95 | https://github.com/jboss-javassist/javassist[*Javassist*]::
96 | 该库附带一个编译器,它使用包含 Java 源代码的字符串,这些字符串在应用程序的运行时被转换为 Java 字节码。这是非常有前途的,本质上是一个好主意,因为 Java 源代码显然是描述 Java 类的好方法。但是,Javassist 编译器在功能上比不了 javac 编译器,并且在动态组合字符串以实现比较复杂的逻辑时容易出错。此外,Javassist 还提供了一个类似于 Java 类库中的代理工具,但允许扩展类,并不限于接口。然而,Javassist 的代理工具的范围在其 API 和功能上仍然受到限制。
97 |
98 | 即使评估完这些框架,但我们相信 Byte Buddy 提供了功能和便利,可以减少徒劳地搜索。Byte Buddy 提供了一种具有表现力的领域特定语言,允许通过编写简单的 Java 代码和使用强大的类型为你自己的代码创建非常自定义的运行时类。与此同时,Byte Buddy 还具有非常开放的定制性,并不限制开箱即用的功能。如果需要,你甚至可以为任何实现的方法定义自定义字节码。但即使不知道什么字节代码是或它如何工作,你可以做很多,而不深入到框架。你有没有看看 http://bytebuddy.net/#/[`Hello World!` example?],使用 Byte Buddy 是如此简单。
99 |
100 | 当然,在选择代码生成库时,一个愉快的 API 不是唯一需要考虑的特性。对于许多应用程序,生成代码的运行时特性更有可能确定最佳选择。__而在生成的代码本身的运行时间之外,用于创建动态类的运行时也是一个问题。__声称“我们是最快的!”很容易,但是为库的速度提供有效的评比指标却很难。不过,我们希望提供这样的指标作为基本方向。但是,请注意,这些结果并不一定会转化为更具体的用例,此时你应该采用单独的指标。
101 |
102 | 在讨论我们的指标之前,让我们来看一下原始数据。下表显示了一个操作的平均运行时间,以纳秒为单位,标准偏差在括号内附加:
103 |
104 | [cols="h,>,>,>,>,>",options="header"]
105 | |===
106 | | | 基线 | Byte Buddy | cglib | Javassist | Java proxy
107 |
108 | | 简单的类创建 | 0.003 (0.001) | 142.772 (1.390) | 515.174 (26.753) | 193.733 (4.430) | 70.712 (0.645)
109 | | 接口实现 | 0.004 (0.001) | 1'126.364 (10.328) | 960.527 (11.788) | 1'070.766 (59.865) | 1'060.766 (12.231)
110 | | 方法调用 | 0.002 (0.001) | 0.002 (0.001) | 0.003 (0.001) | 0.011 (0.001) | 0.008 (0.001)
111 | | 类型扩展 | 0.004 (0.001) | 885.983 (7.901) | 1'632.730 (52.737) | 683.478 (6.735) | 5'408.329 (52.437)
112 | | 父类方法调用 | 0.004 (0.001) | 0.004 (0.001) | 0.021 (0.001) | 0.025 (0.001) | 0.004 (0.001)
113 | |===
114 |
115 | 与静态编译器类似,代码生成库在生成快速代码和快速生成代码之间面临着折衷。当在这些冲突的目标之间进行选择时,Byte Buddy 的主要侧重点在于以最少的运行时生成代码。通常,类型创建或操作不是任何程序中的常见步骤,并不会对任何长期运行的应用程序产生重大影响;特别是因为类加载或类构建(class instrumentation)是运行此类代码时最耗时且不可避免的步骤。
116 |
117 | WARNING: 按照这个逻辑,D瓜哥觉得应该选择“生成快速代码”,毕竟很少生成而且只生成一次,但是生成的代码却可能运行多次。不过,考虑到 Java 虚拟机的优化,选择“生成快速代码”是否是更好的选择呢?
118 |
119 | 上表中的第一个基准测试测量一个库在运行时子类化类,并且不实现或覆盖任何方法。这给我们一个库在代码生成时的一般开销的印象。在这个基准测试中,Java 代理执行得比其他库更好,这是因为存在着一种优化,假设总是扩展接口。Byte Buddy 还会检查类的泛型和注解类别,从而导致额外的运行时间。这个性能开销在创建类的其他基准中也是可见的。基准(2a)展示了运行时创建类,这个类实现了一个有 18 个方法的接口;(2b)显示为此类生成的方法的执行时间。类似地,(3a)显示了扩展类的基准,这个拥有相同的 18 种被实现的方法。 Byte Buddy 提供了两个基准测试,因为对于总是执行超类方法的拦截器来说,可能的优化是可能的。除了在类创建期间花费一段时间,Byte Buddy 创建类的执行时间通常达到基线,这意味着构建根本不会产生开销。应该注意的是,如果元数据处理被禁用,则在类创建期间,Byte Buddy 也会胜过任何其他代码生成库。由于代码生成的运行时间与程序的总运行时间相比微乎其微,所以这种性能优化是不可取的,因为它虽然获得了极少的性能,但却使库代码复杂很多。
120 |
121 | 最后,请注意,我们这些衡量 Java 代码性能的测试,都由 Java 虚拟机即时编译器优化过。如果你的代码只能偶尔执行,那么性能将会比上述表格指标略差。在这种情况下,你的代码并不是性能攸关的开始。这些性能测试代码与 Byte Buddy 一起发布,你可以在自己的计算机上运行这些指标,其中可能会根据你的机器的处理能力对上述数字进行涨跌。因此,不要绝对地解释上述数字,而是将它们视为不同库的对比方式。当进一步开发 Byte Buddy 时,我们希望监控这些指标,以避免在添加新功能时造成性能损失。
122 |
123 | 在下面的教程中,我们将会逐步说明 Byte Buddy 的功能。我们将从其更广泛的功能开始,这些功能最有可能被大多数用户使用。然后,我们将考虑越来越多的高级主题,并简要介绍 Java 字节码和类文件格式。即使你快速跳过这以后的材料,也不要灰心!你可以通过使用 Byte Buddy 的标准 API 来完成任何操作,而无需了解任何 JVM 规范。要了解标准 API,只需继续阅读。
124 |
--------------------------------------------------------------------------------
/mvnw.cmd:
--------------------------------------------------------------------------------
1 | @REM ----------------------------------------------------------------------------
2 | @REM Licensed to the Apache Software Foundation (ASF) under one
3 | @REM or more contributor license agreements. See the NOTICE file
4 | @REM distributed with this work for additional information
5 | @REM regarding copyright ownership. The ASF licenses this file
6 | @REM to you under the Apache License, Version 2.0 (the
7 | @REM "License"); you may not use this file except in compliance
8 | @REM with the License. You may obtain a copy of the License at
9 | @REM
10 | @REM https://www.apache.org/licenses/LICENSE-2.0
11 | @REM
12 | @REM Unless required by applicable law or agreed to in writing,
13 | @REM software distributed under the License is distributed on an
14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | @REM KIND, either express or implied. See the License for the
16 | @REM specific language governing permissions and limitations
17 | @REM under the License.
18 | @REM ----------------------------------------------------------------------------
19 |
20 | @REM ----------------------------------------------------------------------------
21 | @REM Maven Start Up Batch script
22 | @REM
23 | @REM Required ENV vars:
24 | @REM JAVA_HOME - location of a JDK home dir
25 | @REM
26 | @REM Optional ENV vars
27 | @REM M2_HOME - location of maven2's installed home dir
28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
31 | @REM e.g. to debug Maven itself, use
32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
34 | @REM ----------------------------------------------------------------------------
35 |
36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
37 | @echo off
38 | @REM set title of command window
39 | title %0
40 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on'
41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
42 |
43 | @REM set %HOME% to equivalent of $HOME
44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
45 |
46 | @REM Execute a user defined script before this one
47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending
49 | if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %*
50 | if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %*
51 | :skipRcPre
52 |
53 | @setlocal
54 |
55 | set ERROR_CODE=0
56 |
57 | @REM To isolate internal variables from possible post scripts, we use another setlocal
58 | @setlocal
59 |
60 | @REM ==== START VALIDATION ====
61 | if not "%JAVA_HOME%" == "" goto OkJHome
62 |
63 | echo.
64 | echo Error: JAVA_HOME not found in your environment. >&2
65 | echo Please set the JAVA_HOME variable in your environment to match the >&2
66 | echo location of your Java installation. >&2
67 | echo.
68 | goto error
69 |
70 | :OkJHome
71 | if exist "%JAVA_HOME%\bin\java.exe" goto init
72 |
73 | echo.
74 | echo Error: JAVA_HOME is set to an invalid directory. >&2
75 | echo JAVA_HOME = "%JAVA_HOME%" >&2
76 | echo Please set the JAVA_HOME variable in your environment to match the >&2
77 | echo location of your Java installation. >&2
78 | echo.
79 | goto error
80 |
81 | @REM ==== END VALIDATION ====
82 |
83 | :init
84 |
85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
86 | @REM Fallback to current working directory if not found.
87 |
88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
90 |
91 | set EXEC_DIR=%CD%
92 | set WDIR=%EXEC_DIR%
93 | :findBaseDir
94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound
95 | cd ..
96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound
97 | set WDIR=%CD%
98 | goto findBaseDir
99 |
100 | :baseDirFound
101 | set MAVEN_PROJECTBASEDIR=%WDIR%
102 | cd "%EXEC_DIR%"
103 | goto endDetectBaseDir
104 |
105 | :baseDirNotFound
106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
107 | cd "%EXEC_DIR%"
108 |
109 | :endDetectBaseDir
110 |
111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
112 |
113 | @setlocal EnableExtensions EnableDelayedExpansion
114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
116 |
117 | :endReadAdditionalConfig
118 |
119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
122 |
123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
124 |
125 | FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
126 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
127 | )
128 |
129 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
130 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data.
131 | if exist %WRAPPER_JAR% (
132 | if "%MVNW_VERBOSE%" == "true" (
133 | echo Found %WRAPPER_JAR%
134 | )
135 | ) else (
136 | if not "%MVNW_REPOURL%" == "" (
137 | SET DOWNLOAD_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
138 | )
139 | if "%MVNW_VERBOSE%" == "true" (
140 | echo Couldn't find %WRAPPER_JAR%, downloading it ...
141 | echo Downloading from: %DOWNLOAD_URL%
142 | )
143 |
144 | powershell -Command "&{"^
145 | "$webclient = new-object System.Net.WebClient;"^
146 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
147 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
148 | "}"^
149 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^
150 | "}"
151 | if "%MVNW_VERBOSE%" == "true" (
152 | echo Finished downloading %WRAPPER_JAR%
153 | )
154 | )
155 | @REM End of extension
156 |
157 | @REM Provide a "standardized" way to retrieve the CLI args that will
158 | @REM work with both Windows and non-Windows executions.
159 | set MAVEN_CMD_LINE_ARGS=%*
160 |
161 | %MAVEN_JAVA_EXE% ^
162 | %JVM_CONFIG_MAVEN_PROPS% ^
163 | %MAVEN_OPTS% ^
164 | %MAVEN_DEBUG_OPTS% ^
165 | -classpath %WRAPPER_JAR% ^
166 | "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^
167 | %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
168 | if ERRORLEVEL 1 goto error
169 | goto end
170 |
171 | :error
172 | set ERROR_CODE=1
173 |
174 | :end
175 | @endlocal & set ERROR_CODE=%ERROR_CODE%
176 |
177 | if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost
178 | @REM check for post script, once with legacy .bat ending and once with .cmd ending
179 | if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat"
180 | if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd"
181 | :skipRcPost
182 |
183 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
184 | if "%MAVEN_BATCH_PAUSE%"=="on" pause
185 |
186 | if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE%
187 |
188 | cmd /C exit /B %ERROR_CODE%
189 |
--------------------------------------------------------------------------------
/src/main/java/com/diguage/cafe/jiadao/ByteBuddyTest.java:
--------------------------------------------------------------------------------
1 | package com.diguage.cafe.jiadao;
2 |
3 | import net.bytebuddy.ByteBuddy;
4 | import net.bytebuddy.dynamic.ClassFileLocator;
5 | import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
6 | import net.bytebuddy.implementation.FixedValue;
7 | import net.bytebuddy.pool.TypePool;
8 | import org.junit.jupiter.api.Test;
9 |
10 | import java.lang.reflect.InvocationTargetException;
11 | import java.lang.reflect.Method;
12 | import java.util.Arrays;
13 |
14 | import static net.bytebuddy.matcher.ElementMatchers.*;
15 | import static org.assertj.core.api.Assertions.assertThat;
16 |
17 | public class ByteBuddyTest {
18 | //
19 | // @Test
20 | // public void test1() {
21 | // DynamicType.Unloaded