protected BatcherFactory createBatcherFactory(Properties properties, int batchSize) { String batcherClass = properties.getProperty(Environment.BATCH_STRATEGY); if (batcherClass==null) { return batchSize==0 ? (BatcherFactory) new NonBatchingBatcherFactory() : (BatcherFactory) new BatchingBatcherFactory(); } else { log.info("Batcher factory: " + batcherClass); try { return (BatcherFactory) ReflectHelper.classForName(batcherClass).newInstance(); } catch (Exception cnfe) { throw new HibernateException("could not instantiate BatcherFactory: " + batcherClass, cnfe); } } }
public Class getElementClass() throws MappingException { if (elementClassName==null) { org.hibernate.type.Type elementType = getElement().getType(); return isPrimitiveArray() ? ( (PrimitiveType) elementType ).getPrimitiveClass() : elementType.getReturnedClass(); } else { try { return ReflectHelper.classForName(elementClassName); } catch (ClassNotFoundException cnfe) { throw new MappingException(cnfe); } } }
private void checkCompositeIdentifier() { if ( getIdentifier() instanceof Component ) { Component id = (Component) getIdentifier(); if ( !id.isDynamic() ) { Class idClass = id.getComponentClass(); if ( idClass != null && !ReflectHelper.overridesEquals( idClass ) ) { LogFactory.getLog(RootClass.class) .warn( "composite-id class does not override equals(): " + id.getComponentClass().getName() ); } if ( !ReflectHelper.overridesHashCode( idClass ) ) { LogFactory.getLog(RootClass.class) .warn( "composite-id class does not override hashCode(): " + id.getComponentClass().getName() ); } if ( !Serializable.class.isAssignableFrom( idClass ) ) { throw new MappingException( "composite-id class must implement Serializable: " + id.getComponentClass().getName() ); } } } }
public static final TransactionManagerLookup getTransactionManagerLookup(Properties props) throws HibernateException { String tmLookupClass = props.getProperty(Environment.TRANSACTION_MANAGER_STRATEGY); if (tmLookupClass==null) { log.info("No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)"); return null; } else { log.info("instantiating TransactionManagerLookup: " + tmLookupClass); try { TransactionManagerLookup lookup = (TransactionManagerLookup) ReflectHelper.classForName(tmLookupClass).newInstance(); log.info("instantiated TransactionManagerLookup"); return lookup; } catch (Exception e) { log.error("Could not instantiate TransactionManagerLookup", e); throw new HibernateException("Could not instantiate TransactionManagerLookup '" + tmLookupClass + "'"); } } }
private static BasicSetter getSetterOrNull(Class theClass, String propertyName) { if (theClass==Object.class || theClass==null) return null; Method method = setterMethod(theClass, propertyName); if (method!=null) { if ( !ReflectHelper.isPublic(theClass, method) ) method.setAccessible(true); return new BasicSetter(theClass, method, propertyName); } else { BasicSetter setter = getSetterOrNull( theClass.getSuperclass(), propertyName ); if (setter==null) { Class[] interfaces = theClass.getInterfaces(); for ( int i=0; setter==null && i<interfaces.length; i++ ) { setter=getSetterOrNull( interfaces[i], propertyName ); } } return setter; } }
private static BasicGetter getGetterOrNull(Class theClass, String propertyName) { if (theClass==Object.class || theClass==null) return null; Method method = getterMethod(theClass, propertyName); if (method!=null) { if ( !ReflectHelper.isPublic(theClass, method) ) method.setAccessible(true); return new BasicGetter(theClass, method, propertyName); } else { BasicGetter getter = getGetterOrNull( theClass.getSuperclass(), propertyName ); if (getter==null) { Class[] interfaces = theClass.getInterfaces(); for ( int i=0; getter==null && i<interfaces.length; i++ ) { getter=getGetterOrNull( interfaces[i], propertyName ); } } return getter; } }
private Configuration getConfiguration() throws Exception { Configuration cfg = new Configuration(); if (namingStrategy!=null) { cfg.setNamingStrategy( (NamingStrategy) ReflectHelper.classForName(namingStrategy).newInstance() ); } if (configurationFile != null) { cfg.configure( configurationFile ); } String[] files = getFiles(); for (int i = 0; i < files.length; i++) { String filename = files[i]; if ( filename.endsWith(".jar") ) { cfg.addJar( new File(filename) ); } else { cfg.addFile(filename); } } return cfg; }
private Configuration getConfiguration() throws Exception { Configuration cfg = new Configuration(); if (namingStrategy!=null) { cfg.setNamingStrategy( (NamingStrategy) ReflectHelper.classForName(namingStrategy).newInstance() ); } if (configurationFile!=null) { cfg.configure( configurationFile ); } String[] files = getFiles(); for (int i = 0; i < files.length; i++) { String filename = files[i]; if ( filename.endsWith(".jar") ) { cfg.addJar( new File(filename) ); } else { cfg.addFile(filename); } } return cfg; }
public PojoInstantiator(Component component, ReflectionOptimizer.InstantiationOptimizer optimizer) { this.mappedClass = component.getComponentClass(); this.optimizer = optimizer; this.proxyInterface = null; this.embeddedIdentifier = false; try { constructor = ReflectHelper.getDefaultConstructor(mappedClass); } catch ( PropertyNotFoundException pnfe ) { log.info( "no default (no-argument) constructor for class: " + mappedClass.getName() + " (class must be instantiated by Interceptor)" ); constructor = null; } }
public PojoInstantiator(PersistentClass persistentClass, ReflectionOptimizer.InstantiationOptimizer optimizer) { this.mappedClass = persistentClass.getMappedClass(); this.proxyInterface = persistentClass.getProxyInterface(); this.embeddedIdentifier = persistentClass.hasEmbeddedIdentifier(); this.optimizer = optimizer; try { constructor = ReflectHelper.getDefaultConstructor( mappedClass ); } catch ( PropertyNotFoundException pnfe ) { log.info( "no default (no-argument) constructor for class: " + mappedClass.getName() + " (class must be instantiated by Interceptor)" ); constructor = null; } }
public Object instantiate() { if ( ReflectHelper.isAbstractClass(mappedClass) ) { throw new InstantiationException( "Cannot instantiate abstract class or interface: ", mappedClass ); } else if ( optimizer != null ) { return optimizer.newInstance(); } else if ( constructor == null ) { throw new InstantiationException( "No default constructor for entity: ", mappedClass ); } else { try { return constructor.newInstance( null ); } catch ( Exception e ) { throw new InstantiationException( "Could not instantiate entity: ", mappedClass, e ); } } }
public Query setProperties(Object bean) throws HibernateException { Class clazz = bean.getClass(); String[] params = getNamedParameters(); for (int i = 0; i < params.length; i++) { String namedParam = params[i]; try { Getter getter = ReflectHelper.getGetter( clazz, namedParam ); Class retType = getter.getReturnType(); final Object object = getter.get( bean ); if ( Collection.class.isAssignableFrom( retType ) ) { setParameterList( namedParam, ( Collection ) object ); } else if ( retType.isArray() ) { setParameterList( namedParam, ( Object[] ) object ); } else { setParameter( namedParam, object, determineType( namedParam, retType ) ); } } catch (PropertyNotFoundException pnfe) { // ignore } } return this; }
public void lookupConstant(DotNode node) throws SemanticException { String text = ASTUtil.getPathText( node ); Queryable persister = walker.getSessionFactoryHelper().findQueryableUsingImports( text ); if ( persister != null ) { // the name of an entity class final String discrim = persister.getDiscriminatorSQLValue(); node.setDataType( persister.getDiscriminatorType() ); if ( InFragment.NULL.equals(discrim) || InFragment.NOT_NULL.equals(discrim) ) { throw new InvalidPathException( "subclass test not allowed for null or not null discriminator: '" + text + "'" ); } else { setSQLValue( node, text, discrim ); //the class discriminator value } } else { Object value = ReflectHelper.getConstantValue( text ); if ( value == null ) { throw new InvalidPathException( "Invalid path: '" + text + "'" ); } else { setConstantValue( node, text, value ); } } }
public static CollectionType customCollection( String typeName, Properties typeParameters, String role, String propertyRef, boolean embedded) { Class typeClass; try { typeClass = ReflectHelper.classForName( typeName ); } catch ( ClassNotFoundException cnfe ) { throw new MappingException( "user collection type class not found: " + typeName, cnfe ); } CustomCollectionType result = new CustomCollectionType( typeClass, role, propertyRef, embedded ); if ( typeParameters != null ) { TypeFactory.injectParameters( result.getUserType(), typeParameters ); } return result; }
public boolean isInstance(Object object) { String resolvedEntityName = null; if ( Proxy.isProxyClass( object.getClass() ) ) { InvocationHandler handler = Proxy.getInvocationHandler( object ); if ( DataProxyHandler.class.isAssignableFrom( handler.getClass() ) ) { DataProxyHandler myHandler = ( DataProxyHandler ) handler; resolvedEntityName = myHandler.getEntityName(); } } try { return ReflectHelper.classForName( entityName ).isInstance( object ); } catch( Throwable t ) { throw new HibernateException( "could not get handle to entity-name as interface : " + t ); } // return entityName.equals( resolvedEntityName ); }
/** * @param theClass Target class in which the value is to be set * @param propertyName Target property name * @return returns Setter class instance */ @SuppressWarnings("PMD.AccessorClassGeneration") private static CollectionPropertySetter getSetterOrNull(Class theClass, String propertyName) { if (theClass == Object.class || theClass == null) { return null; } Method method = setterMethod(theClass, propertyName); if (method != null) { if (!ReflectHelper.isPublic(theClass, method)) { method.setAccessible(true); } return new CollectionPropertySetter(theClass, method, propertyName); } else { CollectionPropertySetter setter = getSetterOrNull(theClass.getSuperclass(), propertyName); if (setter == null) { Class[] interfaces = theClass.getInterfaces(); for (int i = 0; setter == null && i < interfaces.length; i++) { setter = getSetterOrNull(interfaces[i], propertyName); } } return setter; } }
/** * Returns a dialect instance given the name of the class to use. * * @param dialectName The name of the dialect class. * * @return The dialect instance. */ public static Dialect buildDialect(String dialectName) { try { return ( Dialect ) ReflectHelper.classForName( dialectName ).newInstance(); } catch ( ClassNotFoundException cnfe ) { throw new HibernateException( "Dialect class not found: " + dialectName ); } catch ( Exception e ) { throw new HibernateException( "Could not instantiate dialect class", e ); } }
public void setListeners(String type, String[] listenerClasses) { Object[] listeners = (Object[]) Array.newInstance( eventListeners.getListenerClassFor(type), listenerClasses.length ); for ( int i = 0; i < listeners.length ; i++ ) { try { listeners[i] = ReflectHelper.classForName( listenerClasses[i] ).newInstance(); } catch (Exception e) { throw new MappingException( "Unable to instantiate specified event (" + type + ") listener class: " + listenerClasses[i], e ); } } setListeners( type, listeners ); }
protected QueryCacheFactory createQueryCacheFactory(Properties properties) { String queryCacheFactoryClassName = PropertiesHelper.getString( Environment.QUERY_CACHE_FACTORY, properties, "org.hibernate.cache.StandardQueryCacheFactory" ); log.info("Query cache factory: " + queryCacheFactoryClassName); try { return (QueryCacheFactory) ReflectHelper.classForName(queryCacheFactoryClassName).newInstance(); } catch (Exception cnfe) { throw new HibernateException("could not instantiate QueryCacheFactory: " + queryCacheFactoryClassName, cnfe); } }
protected CacheProvider createCacheProvider(Properties properties) { String cacheClassName = PropertiesHelper.getString( Environment.CACHE_PROVIDER, properties, DEF_CACHE_PROVIDER ); log.info("Cache provider: " + cacheClassName); try { return (CacheProvider) ReflectHelper.classForName(cacheClassName).newInstance(); } catch (Exception cnfe) { throw new HibernateException("could not instantiate CacheProvider: " + cacheClassName, cnfe); } }
protected QueryTranslatorFactory createQueryTranslatorFactory(Properties properties) { String className = PropertiesHelper.getString( Environment.QUERY_TRANSLATOR, properties, "org.hibernate.hql.ast.ASTQueryTranslatorFactory" ); log.info("Query translator: " + className); try { return (QueryTranslatorFactory) ReflectHelper.classForName(className).newInstance(); } catch (Exception cnfe) { throw new HibernateException("could not instantiate QueryTranslatorFactory: " + className, cnfe); } }
public Comparator getComparator() { if ( comparator == null && comparatorClassName != null ) { try { setComparator( (Comparator) ReflectHelper.classForName( comparatorClassName ).newInstance() ); } catch ( Exception e ) { throw new MappingException( "Could not instantiate comparator class [" + comparatorClassName + "] for collection " + getRole() ); } } return comparator; }
public Class getComponentClass() throws MappingException { try { return ReflectHelper.classForName(componentClassName); } catch (ClassNotFoundException cnfe) { throw new MappingException("component class not found: " + componentClassName, cnfe); } }
public Class getMappedClass() throws MappingException { if (className==null) return null; try { return ReflectHelper.classForName(className); } catch (ClassNotFoundException cnfe) { throw new MappingException("entity class not found: " + className, cnfe); } }
public Class getProxyInterface() { if (proxyInterfaceName==null) return null; try { return ReflectHelper.classForName(proxyInterfaceName); } catch (ClassNotFoundException cnfe) { throw new MappingException("proxy class not found: " + proxyInterfaceName, cnfe); } }
public void setTypeUsingReflection(String className, String propertyName) throws MappingException { if (typeName==null) { if (className==null) { throw new MappingException("you must specify types for a dynamic entity: " + propertyName); } typeName = ReflectHelper.reflectedPropertyClass(className, propertyName).getName(); } }
private static PropertyAccessor resolveCustomAccessor(String accessorName) { Class accessorClass; try { accessorClass = ReflectHelper.classForName(accessorName); } catch (ClassNotFoundException cnfe) { throw new MappingException("could not find PropertyAccessor class: " + accessorName, cnfe); } try { return (PropertyAccessor) accessorClass.newInstance(); } catch (Exception e) { throw new MappingException("could not instantiate PropertyAccessor class: " + accessorName, e); } }
private static Field getField(Class clazz, String name) throws PropertyNotFoundException { if ( clazz==null || clazz==Object.class ) { throw new PropertyNotFoundException("field not found: " + name); } Field field; try { field = clazz.getDeclaredField(name); } catch (NoSuchFieldException nsfe) { field = getField( clazz, clazz.getSuperclass(), name ); } if ( !ReflectHelper.isPublic(clazz, field) ) field.setAccessible(true); return field; }
private static Field getField(Class root, Class clazz, String name) throws PropertyNotFoundException { if ( clazz==null || clazz==Object.class ) { throw new PropertyNotFoundException("field [" + name + "] not found on " + root.getName()); } Field field; try { field = clazz.getDeclaredField(name); } catch (NoSuchFieldException nsfe) { field = getField( root, clazz.getSuperclass(), name ); } if ( !ReflectHelper.isPublic(clazz, field) ) field.setAccessible(true); return field; }
public static void main(String[] args) { try { Configuration cfg = new Configuration(); String propFile = null; for ( int i = 0; i < args.length; i++ ) { if ( args[i].startsWith( "--" ) ) { if ( args[i].startsWith( "--properties=" ) ) { propFile = args[i].substring( 13 ); } else if ( args[i].startsWith( "--config=" ) ) { cfg.configure( args[i].substring( 9 ) ); } else if ( args[i].startsWith( "--naming=" ) ) { cfg.setNamingStrategy( ( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) ).newInstance() ); } } else { cfg.addFile( args[i] ); } } if ( propFile != null ) { Properties props = new Properties(); props.putAll( cfg.getProperties() ); props.load( new FileInputStream( propFile ) ); cfg.setProperties( props ); } new SchemaValidator( cfg ).validate(); } catch ( Exception e ) { log.error( "Error running schema update", e ); e.printStackTrace(); } }
private static EntityTuplizer buildEntityTuplizer(String className, PersistentClass pc, EntityMetamodel em) { try { Class implClass = ReflectHelper.classForName( className ); return ( EntityTuplizer ) implClass.getConstructor( ENTITY_TUP_CTOR_SIG ).newInstance( new Object[] { em, pc } ); } catch( Throwable t ) { throw new HibernateException( "Could not build tuplizer [" + className + "]", t ); } }
private ComponentTuplizer buildComponentTuplizer(String tuplizerImpl, Component component) { try { Class implClass = ReflectHelper.classForName( tuplizerImpl ); return ( ComponentTuplizer ) implClass.getConstructor( COMPONENT_TUP_CTOR_SIG ).newInstance( new Object[] { component } ); } catch( Throwable t ) { throw new HibernateException( "Could not build tuplizer [" + tuplizerImpl + "]", t ); } }
protected Instantiator buildInstantiator(Component component) { if ( component.isEmbedded() && ReflectHelper.isAbstractClass( component.getComponentClass() ) ) { return new ProxiedInstantiator( component ); } if ( optimizer == null ) { return new PojoInstantiator( component, null ); } else { return new PojoInstantiator( component, optimizer.getInstantiationOptimizer() ); } }
private static Constructor getConstructor(PersistentClass persistentClass) { if ( persistentClass == null || !persistentClass.hasPojoRepresentation() ) { return null; } try { return ReflectHelper.getDefaultConstructor( persistentClass.getMappedClass() ); } catch( Throwable t ) { return null; } }
protected BasicLazyInitializer( String entityName, Class persistentClass, Serializable id, Method getIdentifierMethod, Method setIdentifierMethod, AbstractComponentType componentIdType, SessionImplementor session) { super(entityName, id, session); this.persistentClass = persistentClass; this.getIdentifierMethod = getIdentifierMethod; this.setIdentifierMethod = setIdentifierMethod; this.componentIdType = componentIdType; overridesEquals = ReflectHelper.overridesEquals(persistentClass); }
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { if ( constructed ) { Object result = invoke( method, args, proxy ); if ( result == INVOKE_IMPLEMENTATION ) { Object target = getImplementation(); try { final Object returnValue; if ( ReflectHelper.isPublic( persistentClass, method ) ) { if ( ! method.getDeclaringClass().isInstance( target ) ) { throw new ClassCastException( target.getClass().getName() ); } returnValue = method.invoke( target, args ); } else { if ( !method.isAccessible() ) { method.setAccessible( true ); } returnValue = method.invoke( target, args ); } return returnValue == target ? proxy : returnValue; } catch ( InvocationTargetException ite ) { throw ite.getTargetException(); } } else { return result; } } else { // while constructor is running if ( method.getName().equals( "getHibernateLazyInitializer" ) ) { return this; } else { throw new LazyInitializationException( "unexpected case hit, method=" + method.getName() ); } } }
public static Optimizer buildOptimizer(String type, Class returnClass, int incrementSize) { String optimizerClassName; if ( NONE.equals( type ) ) { optimizerClassName = NoopOptimizer.class.getName(); } else if ( HILO.equals( type ) ) { optimizerClassName = HiLoOptimizer.class.getName(); } else if ( POOL.equals( type ) ) { optimizerClassName = PooledOptimizer.class.getName(); } else { optimizerClassName = type; } try { Class optimizerClass = ReflectHelper.classForName( optimizerClassName ); Constructor ctor = optimizerClass.getConstructor( CTOR_SIG ); return ( Optimizer ) ctor.newInstance( new Object[] { returnClass, new Integer( incrementSize ) } ); } catch( Throwable ignore ) { // intentionally empty } // the default... return new NoopOptimizer( returnClass, incrementSize ); }
public static Class getIdentifierGeneratorClass(String strategy, Dialect dialect) { Class clazz = (Class) GENERATORS.get(strategy); if ( "native".equals(strategy) ) clazz = dialect.getNativeIdentifierGeneratorClass(); try { if (clazz==null) clazz = ReflectHelper.classForName(strategy); } catch (ClassNotFoundException e) { throw new MappingException("could not interpret id generator strategy: " + strategy); } return clazz; }