Skip to content

Commit 4019423

Browse files
authored
Merge pull request #538 from yanhom1314/master
[ISSUE #530 #533] replace cglib with bytebuddy
2 parents 7cae3db + e99cbe8 commit 4019423

File tree

25 files changed

+485
-498
lines changed

25 files changed

+485
-498
lines changed

common/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
</dependency>
5353

5454
<dependency>
55-
<groupId>cglib</groupId>
56-
<artifactId>cglib</artifactId>
55+
<groupId>net.bytebuddy</groupId>
56+
<artifactId>byte-buddy</artifactId>
5757
</dependency>
5858

5959
<dependency>

common/src/main/java/org/dromara/dynamictp/common/plugin/DtpInterceptorProxyFactory.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
package org.dromara.dynamictp.common.plugin;
1919

2020
import com.google.common.collect.Maps;
21-
import net.sf.cglib.proxy.Enhancer;
21+
import net.bytebuddy.ByteBuddy;
22+
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
23+
import net.bytebuddy.implementation.InvocationHandlerAdapter;
24+
import net.bytebuddy.implementation.attribute.MethodAttributeAppender;
25+
import net.bytebuddy.matcher.ElementMatchers;
26+
import org.dromara.dynamictp.common.util.UUIDUtil;
2227

2328
import java.lang.reflect.Method;
2429
import java.util.HashSet;
@@ -43,21 +48,32 @@ public static Object enhance(Object target, Class<?>[] argumentTypes, Object[] a
4348
if (!signatureMap.containsKey(target.getClass())) {
4449
return target;
4550
}
46-
Enhancer enhancer = new Enhancer();
47-
enhancer.setSuperclass(target.getClass());
48-
enhancer.setCallback(new DtpInterceptorProxy(target, interceptor, signatureMap));
49-
if (Objects.isNull(argumentTypes) || Objects.isNull(arguments)) {
50-
return enhancer.create();
51+
try {
52+
Class<?> proxyClass = new ByteBuddy()
53+
.subclass(target.getClass())
54+
.name(String.format("%s$ByteBuddy$%s", target.getClass().getName(), UUIDUtil.genUuid(5)))
55+
.method(ElementMatchers.any())
56+
.intercept(InvocationHandlerAdapter.of(new DtpInvocationHandler(target, interceptor, signatureMap)))
57+
.attribute(MethodAttributeAppender.ForInstrumentedMethod.INCLUDING_RECEIVER)
58+
.annotateType(target.getClass().getAnnotations())
59+
.make()
60+
.load(DtpInterceptorProxyFactory.class.getClassLoader(), ClassLoadingStrategy.Default.INJECTION)
61+
.getLoaded();
62+
63+
if (Objects.isNull(argumentTypes) || Objects.isNull(arguments)) {
64+
return proxyClass.getDeclaredConstructor().newInstance();
65+
}
66+
return proxyClass.getDeclaredConstructor(argumentTypes).newInstance(arguments);
67+
} catch (Exception e) {
68+
throw new PluginException("Failed to create proxy instance", e);
5169
}
52-
return enhancer.create(argumentTypes, arguments);
5370
}
5471

5572
private static Map<Class<?>, Set<Method>> getSignatureMap(DtpInterceptor interceptor) {
5673
DtpIntercepts interceptsAnno = interceptor.getClass().getAnnotation(DtpIntercepts.class);
5774
if (interceptsAnno == null) {
5875
throw new PluginException("No @DtpIntercepts annotation was found in interceptor " + interceptor.getClass().getName());
5976
}
60-
6177
DtpSignature[] signatures = interceptsAnno.signatures();
6278
Map<Class<?>, Set<Method>> signatureMap = Maps.newHashMap();
6379
for (DtpSignature signature : signatures) {

common/src/main/java/org/dromara/dynamictp/common/plugin/DtpInterceptorProxy.java renamed to common/src/main/java/org/dromara/dynamictp/common/plugin/DtpInvocationHandler.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,39 @@
1717

1818
package org.dromara.dynamictp.common.plugin;
1919

20-
import net.sf.cglib.proxy.MethodInterceptor;
21-
import net.sf.cglib.proxy.MethodProxy;
2220
import org.apache.commons.collections4.CollectionUtils;
2321

22+
import java.lang.reflect.InvocationHandler;
2423
import java.lang.reflect.Method;
2524
import java.util.Map;
2625
import java.util.Set;
2726

2827
/**
29-
* @author windsearcher.lq
30-
* @since 1.1.4
28+
* DtpInvocationHandler related
29+
*
30+
* @author yanhom
31+
* @since 1.2.1
3132
*/
32-
public class DtpInterceptorProxy implements MethodInterceptor {
33+
public class DtpInvocationHandler implements InvocationHandler {
3334

3435
private final Object target;
3536

3637
private final DtpInterceptor interceptor;
3738

3839
private final Map<Class<?>, Set<Method>> signatureMap;
3940

40-
public DtpInterceptorProxy(Object target, DtpInterceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {
41+
public DtpInvocationHandler(Object target, DtpInterceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {
4142
this.target = target;
4243
this.interceptor = interceptor;
4344
this.signatureMap = signatureMap;
4445
}
4546

4647
@Override
47-
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
48+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
4849
Set<Method> methods = signatureMap.get(method.getDeclaringClass());
4950
if (CollectionUtils.isNotEmpty(methods) && methods.contains(method)) {
5051
return interceptor.intercept(new DtpInvocation(target, method, args));
5152
}
52-
5353
return method.invoke(target, args);
5454
}
5555
}

common/src/main/java/org/dromara/dynamictp/common/util/BeanCopierUtil.java

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.dromara.dynamictp.common.util;
19+
20+
import java.util.UUID;
21+
22+
/**
23+
* UUID util.
24+
*
25+
* @author yanhom
26+
*/
27+
public class UUIDUtil {
28+
29+
private UUIDUtil() { }
30+
31+
public static String genUuid() {
32+
return UUID.randomUUID().toString();
33+
}
34+
35+
public static String genUuid(int length) {
36+
return genUuid().replace("-", "").substring(0, length);
37+
}
38+
}

core/src/main/java/org/dromara/dynamictp/core/monitor/DtpMonitor.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
import org.dromara.dynamictp.common.event.CollectEvent;
2525
import org.dromara.dynamictp.common.event.CustomContextRefreshedEvent;
2626
import org.dromara.dynamictp.common.manager.EventBusManager;
27-
2827
import org.dromara.dynamictp.common.properties.DtpProperties;
29-
3028
import org.dromara.dynamictp.core.DtpRegistry;
3129
import org.dromara.dynamictp.core.converter.ExecutorConverter;
3230
import org.dromara.dynamictp.core.handler.CollectorHandler;
@@ -80,8 +78,12 @@ public synchronized void onContextRefreshedEvent(CustomContextRefreshedEvent eve
8078

8179
private void run() {
8280
Set<String> executorNames = DtpRegistry.getAllExecutorNames();
83-
checkAlarm(executorNames);
84-
collectMetrics(executorNames);
81+
try {
82+
checkAlarm(executorNames);
83+
collectMetrics(executorNames);
84+
} catch (Exception e) {
85+
log.error("DynamicTp monitor, run error", e);
86+
}
8587
}
8688

8789
private void checkAlarm(Set<String> executorNames) {

core/src/main/java/org/dromara/dynamictp/core/monitor/collector/MicroMeterCollector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
package org.dromara.dynamictp.core.monitor.collector;
1919

20+
import cn.hutool.core.bean.BeanUtil;
2021
import io.micrometer.core.instrument.Metrics;
2122
import io.micrometer.core.instrument.Tag;
2223
import lombok.extern.slf4j.Slf4j;
2324
import org.dromara.dynamictp.common.em.CollectorTypeEnum;
2425
import org.dromara.dynamictp.common.entity.ThreadPoolStats;
25-
import org.dromara.dynamictp.common.util.BeanCopierUtil;
2626
import org.dromara.dynamictp.common.util.CommonUtil;
2727

2828
import java.util.ArrayList;
@@ -61,7 +61,7 @@ public void collect(ThreadPoolStats threadPoolStats) {
6161
if (Objects.isNull(oldStats)) {
6262
GAUGE_CACHE.put(threadPoolStats.getPoolName(), threadPoolStats);
6363
} else {
64-
BeanCopierUtil.copyProperties(threadPoolStats, oldStats);
64+
BeanUtil.copyProperties(threadPoolStats, oldStats);
6565
}
6666
gauge(GAUGE_CACHE.get(threadPoolStats.getPoolName()));
6767
}

core/src/main/java/org/dromara/dynamictp/core/monitor/collector/jmx/JMXCollector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
package org.dromara.dynamictp.core.monitor.collector.jmx;
1919

20+
import cn.hutool.core.bean.BeanUtil;
2021
import lombok.extern.slf4j.Slf4j;
2122
import org.dromara.dynamictp.common.em.CollectorTypeEnum;
2223
import org.dromara.dynamictp.common.entity.ThreadPoolStats;
23-
import org.dromara.dynamictp.common.util.BeanCopierUtil;
2424
import org.dromara.dynamictp.core.monitor.collector.AbstractCollector;
2525

2626
import javax.management.JMException;
@@ -49,7 +49,7 @@ public class JMXCollector extends AbstractCollector {
4949
public void collect(ThreadPoolStats threadPoolStats) {
5050
if (GAUGE_CACHE.containsKey(threadPoolStats.getPoolName())) {
5151
ThreadPoolStats poolStats = GAUGE_CACHE.get(threadPoolStats.getPoolName());
52-
BeanCopierUtil.copyProperties(threadPoolStats, poolStats);
52+
BeanUtil.copyProperties(threadPoolStats, poolStats);
5353
} else {
5454
try {
5555
MBeanServer server = ManagementFactory.getPlatformMBeanServer();

core/src/main/java/org/dromara/dynamictp/core/notifier/AbstractDtpNotifier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.dromara.dynamictp.core.notifier;
1919

20+
import cn.hutool.core.bean.BeanUtil;
2021
import com.google.common.base.Joiner;
2122
import lombok.extern.slf4j.Slf4j;
2223
import lombok.val;
@@ -28,7 +29,6 @@
2829
import org.dromara.dynamictp.common.entity.NotifyPlatform;
2930
import org.dromara.dynamictp.common.entity.TpMainFields;
3031
import org.dromara.dynamictp.common.notifier.Notifier;
31-
import org.dromara.dynamictp.common.util.BeanCopierUtil;
3232
import org.dromara.dynamictp.common.util.CommonUtil;
3333
import org.dromara.dynamictp.common.util.DateUtil;
3434
import org.dromara.dynamictp.core.notifier.alarm.AlarmCounter;
@@ -182,7 +182,7 @@ protected String formatReceivers(String receives) {
182182

183183
private NotifyPlatform newTargetPlatform(NotifyPlatform platform) {
184184
NotifyPlatform targetPlatform = new NotifyPlatform();
185-
BeanCopierUtil.copyProperties(platform, targetPlatform);
185+
BeanUtil.copyProperties(platform, targetPlatform);
186186

187187
BaseNotifyCtx context = DtpNotifyCtxHolder.get();
188188
NotifyItem item = context.getNotifyItem();

core/src/main/java/org/dromara/dynamictp/core/support/ExecutorWrapper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
package org.dromara.dynamictp.core.support;
1919

20+
import cn.hutool.core.bean.BeanUtil;
2021
import lombok.Data;
2122
import org.dromara.dynamictp.common.em.NotifyItemEnum;
2223
import org.dromara.dynamictp.common.entity.NotifyItem;
23-
import org.dromara.dynamictp.common.util.BeanCopierUtil;
2424
import org.dromara.dynamictp.core.aware.AwareManager;
2525
import org.dromara.dynamictp.core.aware.RejectHandlerAware;
2626
import org.dromara.dynamictp.core.aware.TaskEnhanceAware;
@@ -166,8 +166,8 @@ public static ExecutorWrapper of(DtpExecutor executor) {
166166
*/
167167
public ExecutorWrapper capture() {
168168
ExecutorWrapper executorWrapper = new ExecutorWrapper();
169-
BeanCopierUtil.copyProperties(this, executorWrapper);
170-
executorWrapper.executor = new CapturedExecutor(this.getExecutor());
169+
BeanUtil.copyProperties(this, executorWrapper);
170+
executorWrapper.setExecutor(new CapturedExecutor(this.getExecutor()));
171171
return executorWrapper;
172172
}
173173
/**

0 commit comments

Comments
 (0)