BytebuddyProxy implements Proxy { @Override public <T> T getProxy(Class<T> interfaceClass, Invoker proxyInvoker) { Class<? extends T> cls = PROXY_CLASS_MAP.get(interfaceClass); if (cls == null) { cls = new ByteBuddy() .subclass(interfaceClass) .method( ElementMatchers.isDeclaredBy(interfaceClass).or(ElementMatchers.isEquals()) .or(ElementMatchers.isToString().or(ElementMatchers.isHashCode()))) .intercept(MethodDelegation.to(new BytebuddyInvocationHandler(proxyInvoker), "handler")) .make() .load(interfaceClass.getClassLoader(), ClassLoadingStrategy.Default.INJECTION) .getLoaded(); PROXY_CLASS_MAP.put(interfaceClass, cls); } try { return cls.newInstance(); } catch (Throwable t) { throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_PROXY_CONSTRUCT, "bytebuddy"), t); } }  @Override T getProxy(Class<T> interfaceClass, Invoker proxyInvoker); @Override Invoker getInvoker(Object proxyObject); static Invoker parseInvoker(Object proxyObject);  }
@Test public void getProxy() throws Exception { BytebuddyProxy proxy = new BytebuddyProxy(); AbstractTestClass testClass = null; try { testClass = proxy.getProxy(AbstractTestClass.class, new TestInvoker()); } catch (Exception e) { LOGGER.info(e.getMessage()); } Assert.assertNotNull(testClass); TestInvoker invoker = new TestInvoker(); TestInterface testInterface = proxy.getProxy(TestInterface.class, invoker); Assert.assertNotNull(testInterface); Class clazz = testInterface.getClass().getInterfaces()[0]; Assert.assertEquals(TestInterface.class, clazz); Assert.assertEquals(proxy.getInvoker(testInterface).getClass(), TestInvoker.class); Assert.assertEquals(testInterface.toString(), invoker.toString()); Assert.assertEquals(testInterface.hashCode(), invoker.hashCode()); TestInterface another1 = proxy.getProxy(TestInterface.class, invoker); TestInterface another2 = proxy.getProxy(TestInterface.class, new TestInvoker()); Assert.assertFalse(testInterface.equals(invoker)); Assert.assertTrue(testInterface.equals(another2)); Assert.assertEquals(testInterface, another1); Assert.assertEquals(678, another1.sayNum(true)); SofaRequest request = invoker.getRequest(); Assert.assertEquals(TestInterface.class.getCanonicalName(), request.getInterfaceName()); Assert.assertEquals("sayNum", request.getMethodName()); Assert.assertEquals("boolean", request.getMethodArgSigs()[0]); Assert.assertEquals(true, request.getMethodArgs()[0]); Assert.assertNotNull(request.getMethod()); Assert.assertEquals("sayHello", another1.sayHello("xxxx")); another1.sayNoting(); Assert.assertArrayEquals(new int[] { 6, 7, 8 }, another1.sayNums(null, new HashMap())); Assert.assertNull(another1.sayNum2(1.2D)); boolean error = false; try { another1.throwbiz1(); } catch (Throwable e) { error = true; } Assert.assertFalse(error); error = false; try { another1.throwbiz2(); } catch (Throwable e) { error = true; } Assert.assertFalse(error); try { another1.throwRPC(); } catch (Throwable e) { error = true; } Assert.assertTrue(error); }