This is an automated email from the ASF dual-hosted git repository.

tanxinyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new f62d058a796 [IOTDB-6353] replace cglib to byte-buddy (#14426)
f62d058a796 is described below

commit f62d058a7967618ea5f6b6b4db05a0f0d968b4c0
Author: ChaoWang <[email protected]>
AuthorDate: Sun Dec 15 11:10:08 2024 +0800

    [IOTDB-6353] replace cglib to byte-buddy (#14426)
    
    * [IOTDB-6353] replace cglib to byte-buddy
    
    * [IOTDB-6353] replace cglib to byte-buddy
---
 dependencies.json                                  |  2 +-
 iotdb-core/node-commons/pom.xml                    |  4 +-
 .../commons/client/sync/ByteBuddyEnhancer.java     | 75 ++++++++++++++++++++++
 .../sync/SyncThriftClientWithErrorHandler.java     | 30 +--------
 pom.xml                                            |  8 +--
 5 files changed, 85 insertions(+), 34 deletions(-)

diff --git a/dependencies.json b/dependencies.json
index 0c8ff00acdd..dfcaa6a317b 100644
--- a/dependencies.json
+++ b/dependencies.json
@@ -1,6 +1,6 @@
 {
     "dependencies": [
-        "cglib:cglib",
+        "net.bytebuddy:byte-buddy",
         "ch.qos.logback:logback-classic",
         "ch.qos.logback:logback-core",
         "ch.qos.reload4j:reload4j",
diff --git a/iotdb-core/node-commons/pom.xml b/iotdb-core/node-commons/pom.xml
index 2243f340bfc..a9d45250027 100644
--- a/iotdb-core/node-commons/pom.xml
+++ b/iotdb-core/node-commons/pom.xml
@@ -140,8 +140,8 @@
             <artifactId>nimbus-jose-jwt</artifactId>
         </dependency>
         <dependency>
-            <groupId>cglib</groupId>
-            <artifactId>cglib</artifactId>
+            <groupId>net.bytebuddy</groupId>
+            <artifactId>byte-buddy</artifactId>
         </dependency>
         <dependency>
             <groupId>io.jsonwebtoken</groupId>
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/client/sync/ByteBuddyEnhancer.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/client/sync/ByteBuddyEnhancer.java
new file mode 100644
index 00000000000..db72b444c85
--- /dev/null
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/client/sync/ByteBuddyEnhancer.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.commons.client.sync;
+
+import org.apache.iotdb.commons.client.ThriftClient;
+
+import net.bytebuddy.ByteBuddy;
+import net.bytebuddy.description.method.MethodDescription;
+import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
+import net.bytebuddy.implementation.MethodDelegation;
+import net.bytebuddy.implementation.bind.annotation.Origin;
+import net.bytebuddy.implementation.bind.annotation.RuntimeType;
+import net.bytebuddy.implementation.bind.annotation.SuperCall;
+import net.bytebuddy.implementation.bind.annotation.This;
+import net.bytebuddy.matcher.ElementMatcher;
+import net.bytebuddy.matcher.ElementMatchers;
+import org.apache.thrift.TException;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.concurrent.Callable;
+
+public class ByteBuddyEnhancer {
+  public static <V> V createProxy(Class<V> targetClass, Constructor 
constructor, Object[] args)
+      throws NoSuchMethodException,
+          IllegalAccessException,
+          InvocationTargetException,
+          InstantiationException {
+    ElementMatcher.Junction<MethodDescription> matcher =
+        ElementMatchers.noneOf(Object.class.getDeclaredMethods());
+
+    return new ByteBuddy()
+        .subclass(targetClass)
+        .method(matcher)
+        .intercept(MethodDelegation.to(SyncThriftClientWithErrorHandler.class))
+        .make()
+        .load(targetClass.getClassLoader(), 
ClassLoadingStrategy.Default.CHILD_FIRST)
+        .getLoaded()
+        .getDeclaredConstructor(constructor.getParameterTypes())
+        .newInstance(args);
+  }
+
+  public static class SyncThriftClientWithErrorHandler {
+    @RuntimeType
+    public static Object intercept(
+        @This Object targetObject, @SuperCall Callable<?> callable, @Origin 
Method method)
+        throws TException {
+      try {
+        Object result = callable.call();
+        return result;
+      } catch (Throwable t) {
+        ThriftClient.resolveException(t, (ThriftClient) targetObject);
+        throw new TException(
+            "Error in calling method " + method.getName() + ", because: " + 
t.getMessage(), t);
+      }
+    }
+  }
+}
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/client/sync/SyncThriftClientWithErrorHandler.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/client/sync/SyncThriftClientWithErrorHandler.java
index 06097c9c821..9047fb9f3df 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/client/sync/SyncThriftClientWithErrorHandler.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/client/sync/SyncThriftClientWithErrorHandler.java
@@ -21,15 +21,9 @@ package org.apache.iotdb.commons.client.sync;
 
 import org.apache.iotdb.commons.client.ThriftClient;
 
-import net.sf.cglib.proxy.Enhancer;
-import net.sf.cglib.proxy.MethodInterceptor;
-import net.sf.cglib.proxy.MethodProxy;
-import org.apache.thrift.TException;
-
 import java.lang.reflect.Constructor;
-import java.lang.reflect.Method;
 
-public class SyncThriftClientWithErrorHandler implements MethodInterceptor {
+public class SyncThriftClientWithErrorHandler {
 
   /**
    * Note: The caller needs to ensure that the constructor corresponds to the 
class, or the cast
@@ -37,25 +31,7 @@ public class SyncThriftClientWithErrorHandler implements 
MethodInterceptor {
    */
   @SuppressWarnings("unchecked")
   public static <V extends ThriftClient> V newErrorHandler(
-      Class<V> targetClass, Constructor<V> constructor, Object... args) {
-    Enhancer enhancer = new Enhancer();
-    enhancer.setSuperclass(targetClass);
-    enhancer.setCallback(new SyncThriftClientWithErrorHandler());
-    if (constructor == null) {
-      return (V) enhancer.create();
-    }
-    return (V) enhancer.create(constructor.getParameterTypes(), args);
-  }
-
-  @Override
-  public Object intercept(Object o, Method method, Object[] objects, 
MethodProxy methodProxy)
-      throws Throwable {
-    try {
-      return methodProxy.invokeSuper(o, objects);
-    } catch (Throwable t) {
-      ThriftClient.resolveException(t, (ThriftClient) o);
-      throw new TException(
-          "Error in calling method " + method.getName() + ", because: " + 
t.getMessage(), t);
-    }
+      Class<V> targetClass, Constructor<V> constructor, Object... args) throws 
Exception {
+    return ByteBuddyEnhancer.createProxy(targetClass, constructor, args);
   }
 }
diff --git a/pom.xml b/pom.xml
index da51a20417d..6dc3be76131 100644
--- a/pom.xml
+++ b/pom.xml
@@ -55,7 +55,7 @@
         <boost.include.dir/>
         <!-- This was the last version to support Java 8 -->
         <caffeine.version>2.9.3</caffeine.version>
-        <cglib.version>3.3.0</cglib.version>
+        <bytebuddy.version>1.14.19</bytebuddy.version>
         <checker-qual.version>3.38.0</checker-qual.version>
         <cmake.build.type>Release</cmake.build.type>
         <commons-cli.version>1.5.0</commons-cli.version>
@@ -475,9 +475,9 @@
                 <version>${nimbus-jose-jwt.version}</version>
             </dependency>
             <dependency>
-                <groupId>cglib</groupId>
-                <artifactId>cglib</artifactId>
-                <version>${cglib.version}</version>
+                <groupId>net.bytebuddy</groupId>
+                <artifactId>byte-buddy</artifactId>
+                <version>${bytebuddy.version}</version>
             </dependency>
             <dependency>
                 <groupId>org.apache.commons</groupId>

Reply via email to