@Test public void testProxyClassCaching() { Function<ServiceProxyBuilder, PersonInfo> f = pb -> pb.buildProxy(ProxyContext.<PersonInfo, JsonNode>builder(PersonInfo.class) .translator(inputStream2Json()) .proxyObjectClassMethods(false) .factory(n -> new PersonInfoImpl(null, null)).build()); var p1 = f.apply(proxyBuilder); var p2 = f.apply(proxyBuilder); assertThat(p1).isNotEqualTo(p2); assertThat(p1.getClass()).isSameAs(p2.getClass()); var noCacheProxyBuilder = new ServiceProxyBuilder(false); p1 = f.apply(noCacheProxyBuilder); p2 = f.apply(noCacheProxyBuilder); assertThat(p1).isNotEqualTo(p2); assertThat(p1.getClass()).isNotSameAs(p2.getClass()); }
public synchronized <T, D> T buildProxy(ProxyContext<T, D> context) { var serviceProxy = new RessorServiceImpl<>(context.getType(), getFactory(context), context.getTranslator(), context.getErrorHandler(), context.getInitialInstance(), context.getResource()) .state(StateVariables.SOURCE, context.getSource()); Class<? extends T> loadedClass = null; if (isCachePossible(context)) { var gci = classCache.computeIfAbsent(context.getType(), k -> new GeneratedClassInfo(generateProxyClass(context), context.getProxyDefaultArguments(), context.getClassLoader(), context.isProxyObjectClassMethods())); if (gci.isMatches(context)) { loadedClass = (Class<? extends T>) gci.loadedClass; } } if (loadedClass == null) { loadedClass = generateProxyClass(context); } try { var targetConstructor = loadedClass.getDeclaredConstructor(); targetConstructor.setAccessible(true); var instance = targetConstructor.newInstance(); var mf = instance.getClass().getDeclaredField(RS_VAR); mf.setAccessible(true); mf.set(instance, serviceProxy); return instance; } catch (Throwable t) { throw Exceptions.wrap(t); } }
ServiceProxyBuilder { public synchronized <T, D> T buildProxy(ProxyContext<T, D> context) { var serviceProxy = new RessorServiceImpl<>(context.getType(), getFactory(context), context.getTranslator(), context.getErrorHandler(), context.getInitialInstance(), context.getResource()) .state(StateVariables.SOURCE, context.getSource()); Class<? extends T> loadedClass = null; if (isCachePossible(context)) { var gci = classCache.computeIfAbsent(context.getType(), k -> new GeneratedClassInfo(generateProxyClass(context), context.getProxyDefaultArguments(), context.getClassLoader(), context.isProxyObjectClassMethods())); if (gci.isMatches(context)) { loadedClass = (Class<? extends T>) gci.loadedClass; } } if (loadedClass == null) { loadedClass = generateProxyClass(context); } try { var targetConstructor = loadedClass.getDeclaredConstructor(); targetConstructor.setAccessible(true); var instance = targetConstructor.newInstance(); var mf = instance.getClass().getDeclaredField(RS_VAR); mf.setAccessible(true); mf.set(instance, serviceProxy); return instance; } catch (Throwable t) { throw Exceptions.wrap(t); } } }
ServiceProxyBuilder { public synchronized <T, D> T buildProxy(ProxyContext<T, D> context) { var serviceProxy = new RessorServiceImpl<>(context.getType(), getFactory(context), context.getTranslator(), context.getErrorHandler(), context.getInitialInstance(), context.getResource()) .state(StateVariables.SOURCE, context.getSource()); Class<? extends T> loadedClass = null; if (isCachePossible(context)) { var gci = classCache.computeIfAbsent(context.getType(), k -> new GeneratedClassInfo(generateProxyClass(context), context.getProxyDefaultArguments(), context.getClassLoader(), context.isProxyObjectClassMethods())); if (gci.isMatches(context)) { loadedClass = (Class<? extends T>) gci.loadedClass; } } if (loadedClass == null) { loadedClass = generateProxyClass(context); } try { var targetConstructor = loadedClass.getDeclaredConstructor(); targetConstructor.setAccessible(true); var instance = targetConstructor.newInstance(); var mf = instance.getClass().getDeclaredField(RS_VAR); mf.setAccessible(true); mf.set(instance, serviceProxy); return instance; } catch (Throwable t) { throw Exceptions.wrap(t); } } ServiceProxyBuilder(boolean isCacheClasses); }
ServiceProxyBuilder { public synchronized <T, D> T buildProxy(ProxyContext<T, D> context) { var serviceProxy = new RessorServiceImpl<>(context.getType(), getFactory(context), context.getTranslator(), context.getErrorHandler(), context.getInitialInstance(), context.getResource()) .state(StateVariables.SOURCE, context.getSource()); Class<? extends T> loadedClass = null; if (isCachePossible(context)) { var gci = classCache.computeIfAbsent(context.getType(), k -> new GeneratedClassInfo(generateProxyClass(context), context.getProxyDefaultArguments(), context.getClassLoader(), context.isProxyObjectClassMethods())); if (gci.isMatches(context)) { loadedClass = (Class<? extends T>) gci.loadedClass; } } if (loadedClass == null) { loadedClass = generateProxyClass(context); } try { var targetConstructor = loadedClass.getDeclaredConstructor(); targetConstructor.setAccessible(true); var instance = targetConstructor.newInstance(); var mf = instance.getClass().getDeclaredField(RS_VAR); mf.setAccessible(true); mf.set(instance, serviceProxy); return instance; } catch (Throwable t) { throw Exceptions.wrap(t); } } ServiceProxyBuilder(boolean isCacheClasses); synchronized T buildProxy(ProxyContext<T, D> context); }
ServiceProxyBuilder { public synchronized <T, D> T buildProxy(ProxyContext<T, D> context) { var serviceProxy = new RessorServiceImpl<>(context.getType(), getFactory(context), context.getTranslator(), context.getErrorHandler(), context.getInitialInstance(), context.getResource()) .state(StateVariables.SOURCE, context.getSource()); Class<? extends T> loadedClass = null; if (isCachePossible(context)) { var gci = classCache.computeIfAbsent(context.getType(), k -> new GeneratedClassInfo(generateProxyClass(context), context.getProxyDefaultArguments(), context.getClassLoader(), context.isProxyObjectClassMethods())); if (gci.isMatches(context)) { loadedClass = (Class<? extends T>) gci.loadedClass; } } if (loadedClass == null) { loadedClass = generateProxyClass(context); } try { var targetConstructor = loadedClass.getDeclaredConstructor(); targetConstructor.setAccessible(true); var instance = targetConstructor.newInstance(); var mf = instance.getClass().getDeclaredField(RS_VAR); mf.setAccessible(true); mf.set(instance, serviceProxy); return instance; } catch (Throwable t) { throw Exceptions.wrap(t); } } ServiceProxyBuilder(boolean isCacheClasses); synchronized T buildProxy(ProxyContext<T, D> context);  }