private static String setterName(Throwable t, BulkAccessor accessor) { if (t instanceof BulkAccessorException ) { return accessor.getSetters()[ ( (BulkAccessorException) t ).getIndex() ]; } else { return "?"; } }
private static String getterName(Throwable t, BulkAccessor accessor) { if (t instanceof BulkAccessorException ) { return accessor.getGetters()[ ( (BulkAccessorException) t ).getIndex() ]; } else { return "?"; } } }
/** * Constructs a FastClass * * @param type The class to optimize * * @return The fast class access to the given class */ public static FastClass create(Class type) { return new FastClass( type ); }
BulkAccessor bulkAccessor; try { fastClass = FastClass.create( clazz ); bulkAccessor = BulkAccessor.create( clazz, getterNames, setterNames, types ); if ( !clazz.isInterface() && !Modifier.isAbstract( clazz.getModifiers() ) ) { if ( fastClass == null ) { final Object instance = fastClass.newInstance(); bulkAccessor.setPropertyValues( instance, bulkAccessor.getPropertyValues( instance ) ); int index = 0; if (t instanceof BulkAccessorException) { index = ( (BulkAccessorException) t ).getIndex(); return new ReflectionOptimizerImpl( new InstantiationOptimizerAdapter( fastClass ), new AccessOptimizerAdapter( bulkAccessor, clazz ) );
BulkAccessor create() { final Method[] getters = new Method[getterNames.length]; final Method[] setters = new Method[setterNames.length]; findAccessors( targetBean, getterNames, setterNames, types, getters, setters ); final Class beanClass; try { final ClassFile classfile = make( getters, setters ); final ClassLoader loader = this.getClassLoader(); if ( writeDirectory != null ) { FactoryHelper.writeFile( classfile, writeDirectory ); } beanClass = FactoryHelper.toClass( classfile, null, loader, getDomain() ); return (BulkAccessor) this.newInstance( beanClass ); } catch ( Exception e ) { throw new BulkAccessorException( e.getMessage(), e ); } }
private ClassFile make(Method[] getters, Method[] setters) throws CannotCompileException { String className = targetBean.getName(); // set the name of bulk accessor. className = className + "_$$_bulkaccess_" + counter++; if ( className.startsWith( "java." ) ) { className = PACKAGE_NAME_PREFIX + className; } final ClassFile classfile = new ClassFile( false, className, BULKACESSOR_CLASS_NAME ); classfile.setAccessFlags( AccessFlag.PUBLIC ); addDefaultConstructor( classfile ); addGetter( classfile, getters ); addSetter( classfile, setters ); return classfile; }
/** * Invoke the default constructor * * @return The constructed instance * * @throws InvocationTargetException Indicates a problem performing invocation */ public Object newInstance() throws InvocationTargetException { return this.newInstance( this.getIndex( EMPTY_CLASS_ARRAY ), null ); }
/** * Creates a new instance of <code>BulkAccessor</code>. * The created instance provides methods for setting/getting * specified properties at once. * * @param beanClass the class of the JavaBeans accessed * through the created object. * @param getters the names of setter methods for specified properties. * @param setters the names of getter methods for specified properties. * @param types the types of specified properties. * * @return The created BulkAccessor */ public static BulkAccessor create( Class beanClass, String[] getters, String[] setters, Class[] types) { return new BulkAccessorFactory( beanClass, getters, setters, types ).create(); } }
/** * Access to invoke a method on the class that this fast class handles * * @param name The name of the method to invoke, * @param parameterTypes The method parameter types * @param obj The instance on which to invoke the method * @param args The parameter arguments * * @return The method result * * @throws InvocationTargetException Indicates a problem performing invocation */ public Object invoke( String name, Class[] parameterTypes, Object obj, Object[] args) throws InvocationTargetException { return this.invoke( this.getIndex( name, parameterTypes ), obj, args ); }
private static void findAccessors( Class clazz, String[] getterNames, String[] setterNames, Class[] types, Method[] getters, Method[] setters) { final int length = types.length; if ( setterNames.length != length || getterNames.length != length ) { throw new BulkAccessorException( "bad number of accessors" ); } final Class[] getParam = new Class[0]; final Class[] setParam = new Class[1]; for ( int i = 0; i < length; i++ ) { if ( getterNames[i] != null ) { final Method getter = findAccessor( clazz, getterNames[i], getParam, i ); if ( getter.getReturnType() != types[i] ) { throw new BulkAccessorException( "wrong return type: " + getterNames[i], i ); } getters[i] = getter; } if ( setterNames[i] != null ) { setParam[0] = types[i]; setters[i] = findAccessor( clazz, setterNames[i], setParam, i ); } } }
@Override public void setPropertyValues(Object object, Object[] values) { try { bulkAccessor.setPropertyValues( object, values ); } catch ( Throwable t ) { throw new PropertyAccessException( t, PROPERTY_SET_EXCEPTION, true, mappedClass, setterName( t, bulkAccessor ) ); } }
@Override public Object[] getPropertyValues(Object object) { try { return bulkAccessor.getPropertyValues( object ); } catch ( Throwable t ) { throw new PropertyAccessException( t, PROPERTY_GET_EXCEPTION, false, mappedClass, getterName( t, bulkAccessor ) ); } }
@Override public Object newInstance() { try { return fastClass.newInstance(); } catch ( Exception e ) { throw new InstantiationException( "Could not instantiate entity with Javassist optimizer: ", fastClass.getJavaClass(), e ); } } }
@Override public ProxyFactoryFactory getProxyFactoryFactory() { return new ProxyFactoryFactoryImpl(); }
/** * Returns the values of properties of a given bean. * * @param bean JavaBean. * * @return The property values */ public Object[] getPropertyValues(Object bean) { final Object[] values = new Object[getters.length]; getPropertyValues( bean, values ); return values; }
/** * Constructs a BasicProxyFactoryImpl * * @param superClass The abstract super class (or null if none). * @param interfaces Interfaces to be proxied (or null if none). * * @return The constructed BasicProxyFactoryImpl */ @Override public BasicProxyFactory buildBasicProxyFactory(Class superClass, Class[] interfaces) { return new BasicProxyFactoryImpl( superClass, interfaces ); }
@Override public String[] getPropertyNames() { return bulkAccessor.getGetters(); }
@Test public void testBulkAccessorDirectly() { BulkAccessor bulkAccessor = BulkAccessor.create( Bean.class, BeanReflectionHelper.getGetterNames(), BeanReflectionHelper.getSetterNames(), BeanReflectionHelper.getTypes() ); }
public Object getProxy() { try { final Proxy proxy = (Proxy) proxyClass.newInstance(); proxy.setHandler( new PassThroughHandler( proxy, proxyClass.getName() ) ); return proxy; } catch ( Throwable t ) { throw new HibernateException( "Unable to instantiated proxy instance" ); } }
/** * Invoke a parameterized constructor * * @param parameterTypes The parameter types * @param args The parameter arguments to pass along * * @return The constructed instance * * @throws InvocationTargetException Indicates a problem performing invocation */ public Object newInstance( Class[] parameterTypes, Object[] args) throws InvocationTargetException { return this.newInstance( this.getIndex( parameterTypes ), args ); }