/** * Creates a proxy class and returns an instance of that class. * * @param paramTypes parameter types for a constructor. * @param args arguments passed to a constructor. */ public Object create(Class[] paramTypes, Object[] args) throws NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Class c = createClass(); Constructor cons = c.getConstructor(paramTypes); return cons.newInstance(args); }
/** * Creates a proxy class and returns an instance of that class. * * @param paramTypes parameter types for a constructor. * @param args arguments passed to a constructor. */ public Object create(Class<?>[] paramTypes, Object[] args) throws NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Class<?> c = createClass(); Constructor<?> cons = c.getConstructor(paramTypes); return cons.newInstance(args); }
@Override public void postInstantiate( final String entityName, final Class persistentClass, final Set<Class> interfaces, final Method getIdentifierMethod, final Method setIdentifierMethod, CompositeType componentIdType) throws HibernateException { this.entityName = entityName; this.persistentClass = persistentClass; this.interfaces = toArray( interfaces ); this.getIdentifierMethod = getIdentifierMethod; this.setIdentifierMethod = setIdentifierMethod; this.componentIdType = componentIdType; this.overridesEquals = ReflectHelper.overridesEquals( persistentClass ); this.proxyClass = buildJavassistProxyFactory().createClass(); }
Object readResolve() throws ObjectStreamException { try { int n = interfaces.length; Class[] infs = new Class[n]; for (int i = 0; i < n; i++) infs[i] = loadClass(interfaces[i]); ProxyFactory f = new ProxyFactory(); f.setSuperclass(loadClass(superClass)); f.setInterfaces(infs); Proxy proxy = (Proxy)f.createClass(filterSignature).newInstance(); proxy.setHandler(handler); return proxy; } catch (ClassNotFoundException e) { throw new java.io.InvalidClassException(e.getMessage()); } catch (InstantiationException e2) { throw new java.io.InvalidObjectException(e2.getMessage()); } catch (IllegalAccessException e3) { throw new java.io.InvalidClassException(e3.getMessage()); } } }
factory.createClass().getConstructor().newInstance() : factory.create(constructorParamTypes, constructorArguments)); proxy.setHandler((self, overridden, proceed, args) -> {
f.setSuperclass(loadClass(superClass)); f.setInterfaces(infs); Proxy proxy = (Proxy)f.createClass(filterSignature).getConstructor().newInstance(); proxy.setHandler(handler); return proxy;
public BasicProxyFactoryImpl(Class superClass, Class[] interfaces) { if ( superClass == null && ( interfaces == null || interfaces.length < 1 ) ) { throw new AssertionFailure( "attempting to build proxy without any superclass or interfaces" ); } final javassist.util.proxy.ProxyFactory factory = new javassist.util.proxy.ProxyFactory(); factory.setFilter( FINALIZE_FILTER ); if ( superClass != null ) { factory.setSuperclass( superClass ); } if ( interfaces != null && interfaces.length > 0 ) { factory.setInterfaces( interfaces ); } proxyClass = factory.createClass(); }
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { boolean isProxy = readBoolean(); if (isProxy) { String name = (String)readObject(); Class superClass = loader.loadClass(name); int length = readInt(); Class[] interfaces = new Class[length]; for (int i = 0; i < length; i++) { name = (String)readObject(); interfaces[i] = loader.loadClass(name); } length = readInt(); byte[] signature = new byte[length]; read(signature); ProxyFactory factory = new ProxyFactory(); // we must always use the cache and never use writeReplace when using // ProxyObjectOutputStream and ProxyObjectInputStream factory.setUseCache(true); factory.setUseWriteReplace(false); factory.setSuperclass(superClass); factory.setInterfaces(interfaces); Class proxyClass = factory.createClass(signature); return ObjectStreamClass.lookup(proxyClass); } else { return super.readClassDescriptor(); } }
@Override protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { boolean isProxy = readBoolean(); if (isProxy) { String name = (String)readObject(); Class<?> superClass = loader.loadClass(name); int length = readInt(); Class<?>[] interfaces = new Class[length]; for (int i = 0; i < length; i++) { name = (String)readObject(); interfaces[i] = loader.loadClass(name); } length = readInt(); byte[] signature = new byte[length]; read(signature); ProxyFactory factory = new ProxyFactory(); // we must always use the cache and never use writeReplace when using // ProxyObjectOutputStream and ProxyObjectInputStream factory.setUseCache(true); factory.setUseWriteReplace(false); factory.setSuperclass(superClass); factory.setInterfaces(interfaces); Class<?> proxyClass = factory.createClass(signature); return ObjectStreamClass.lookup(proxyClass); } return super.readClassDescriptor(); }
public static HibernateProxy deserializeProxy(SerializableProxy serializableProxy) { final JavassistLazyInitializer initializer = new JavassistLazyInitializer( serializableProxy.getEntityName(), serializableProxy.getPersistentClass(), serializableProxy.getInterfaces(), serializableProxy.getId(), resolveIdGetterMethod( serializableProxy ), resolveIdSetterMethod( serializableProxy ), serializableProxy.getComponentIdType(), null, ReflectHelper.overridesEquals( serializableProxy.getPersistentClass() ) ); final javassist.util.proxy.ProxyFactory factory = buildJavassistProxyFactory( serializableProxy.getPersistentClass(), serializableProxy.getInterfaces() ); // note: interface is assumed to already contain HibernateProxy.class try { final Class proxyClass = factory.createClass(); final HibernateProxy proxy = ( HibernateProxy ) proxyClass.newInstance(); ( (Proxy) proxy ).setHandler( initializer ); initializer.constructed(); return proxy; } catch ( Throwable t ) { final String message = LOG.bytecodeEnhancementFailed( serializableProxy.getEntityName() ); LOG.error( message, t ); throw new HibernateException( message, t ); } }
/** * Constructs a Latke bean. * * @param beanManager the specified bean manager * @param name the specified bean name * @param beanClass the specified bean class * @param types the specified bean types * @param stereotypes the specified stereo types */ public Bean(final BeanManager beanManager, final String name, final Class<T> beanClass, final Set<Type> types, final Set<Class<? extends Annotation>> stereotypes) { this.beanManager = beanManager; this.name = name; this.beanClass = beanClass; this.types = types; this.stereotypes = stereotypes; this.configurator = beanManager.getConfigurator(); javassistMethodHandler = new JavassistMethodHandler(beanManager); final ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setSuperclass(beanClass); proxyFactory.setFilter(javassistMethodHandler.getMethodFilter()); proxyClass = (Class<T>) proxyFactory.createClass(); annotatedType = new AnnotatedTypeImpl<T>(beanClass); fieldInjectionPoints = new HashSet<>(); initFieldInjectionPoints(); }
public Class<?> run() { return factory.createClass(); } }
public Class<?> run() { return factory.createClass(); } }
protected static Class<?> getProxyClass(javassist.util.proxy.ProxyFactory factory) { SecurityManager sm = System.getSecurityManager(); if (sm == null) return factory.createClass(); else return AccessController.doPrivileged(new ClassCreator(factory)); }
protected Class<?> getProxyClass(ProxyFactory factory) { SecurityManager sm = System.getSecurityManager(); if (sm == null) return factory.createClass(); else return AccessController.doPrivileged(new ClassCreator(factory)); }
protected static Class<?> getProxyClass(ProxyFactory factory) { SecurityManager sm = System.getSecurityManager(); if (sm == null) return factory.createClass(); else return AccessController.doPrivileged(new ClassCreator(factory)); }
@Override protected Object createObjectFromArgsConstructor(final Class<?>[] parameterTypes, final Object[] parameters) { try { final Class proxyClass = proxyFactory.createClass(); final Constructor declaredConstructor = proxyClass.getDeclaredConstructor(parameterTypes); declaredConstructor.setAccessible(true); return declaredConstructor.newInstance(parameters); } catch (final ReflectiveOperationException e) { throw new ObjectInstantiationException(clazz, "Could not create object from args constructor", e); } }
public static <T> Class<T> createProxyClass(Class<T> proxyClass) { ProxyFactory proxyFactory = new ProxyFactory(); if (proxyClass != null) { proxyFactory.setSuperclass(proxyClass); } proxyFactory.setInterfaces(new Class<?>[]{LifecycleMixin.class, TargetInstanceProxy.class}); Class<T> clazz = proxyFactory.createClass(); return clazz; }
private static <T> Outline<T> buildOutline(Class<T> classToOutline, boolean camelCased) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setSuperclass(classToOutline); Class<? extends T> outlineClass = proxyFactory.createClass(); return new OutlineImpl<>(objenesis.newInstance(outlineClass), camelCased); }
private Class<?> proxyClass(Class<?> mockedType, Class<?>... ancilliaryTypes) { final ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setFilter(new MethodFilter() { @Override public boolean isHandled(final Method m) { // ignore finalize() and als bridge methods return !m.getName().equals("finalize") || m.isBridge(); } }); proxyFactory.setSuperclass(mockedType); proxyFactory.setInterfaces(ancilliaryTypes); return proxyFactory.createClass(); }