You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2019/08/06 06:17:48 UTC

[servicecomb-java-chassis] branch master updated: [SCB-1402] BeanUtils.getImplClassFromBean can not return correctly info in cglib proxy situation

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

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 32c3e01  [SCB-1402] BeanUtils.getImplClassFromBean can not return correctly info in cglib proxy situation
32c3e01 is described below

commit 32c3e018e5b12b70578187fa40218f7ce64f0e3e
Author: AngLi2 <li...@husky.neu.edu>
AuthorDate: Mon Jul 29 16:07:49 2019 +0800

    [SCB-1402] BeanUtils.getImplClassFromBean can not return correctly info in cglib proxy situation
---
 .../foundation/common/utils/BeanUtils.java          | 21 ++++++++++++---------
 .../foundation/common/utils/TestBeanUtils.java      | 21 +++++++++++++++++++++
 2 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/BeanUtils.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/BeanUtils.java
index f7f4647..cefe6f7 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/BeanUtils.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/BeanUtils.java
@@ -17,16 +17,16 @@
 
 package org.apache.servicecomb.foundation.common.utils;
 
-import java.util.LinkedHashSet;
-import java.util.Set;
-
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.aop.TargetClassAware;
+import org.springframework.aop.framework.AopProxyUtils;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
+import java.util.LinkedHashSet;
+import java.util.Set;
+
 
 public final class BeanUtils {
 
@@ -109,11 +109,14 @@ public final class BeanUtils {
     return (T) context.getBean(name);
   }
 
+  /**
+   * Get the implemented class of the given instance
+   * @param bean the instance to get implemented class from
+   * @return the implemented class (if the checked class is proxied, return the ultimate target class)
+   * @see org.springframework.aop.framework.AopProxyUtils#ultimateTargetClass
+   */
   public static Class<?> getImplClassFromBean(Object bean) {
-    if (TargetClassAware.class.isInstance(bean)) {
-      return ((TargetClassAware) bean).getTargetClass();
-    }
-
-    return bean.getClass();
+    return AopProxyUtils.ultimateTargetClass(bean);
   }
+
 }
diff --git a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestBeanUtils.java b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestBeanUtils.java
index 70c888e..826ac22 100644
--- a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestBeanUtils.java
+++ b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestBeanUtils.java
@@ -23,6 +23,7 @@ import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.springframework.aop.SpringProxy;
 import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
@@ -38,6 +39,16 @@ public class TestBeanUtils {
 
   }
 
+  // 被代理的类
+  static class TestBean{
+
+  }
+
+  // 模拟 CgLib 代理产生的子类
+  static class TestBean$$TestBeanByCGLIB$$e1a36bab extends TestBean implements SpringProxy {
+
+  }
+
   @Aspect
   static class MyAspect {
   }
@@ -147,4 +158,14 @@ public class TestBeanUtils {
 
     Assert.assertEquals("org.apache.servicecomb", System.getProperty(BeanUtils.SCB_SCAN_PACKAGE));
   }
+
+  @Test
+  public void testGetImplClassFromBeanFromCglib(){
+    TestBean testBeanByCGLIB = new TestBean$$TestBeanByCGLIB$$e1a36bab();
+    Class<?> generatedClass = BeanUtils.getImplClassFromBean(testBeanByCGLIB);
+    Assert.assertNotNull(generatedClass);
+    Assert.assertEquals(TestBean.class, generatedClass);
+  }
+
+
 }