Java Code Examples for org.hibernate.util.ReflectHelper

The following examples show how to use org.hibernate.util.ReflectHelper. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
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);
		}
	}
}
 
Example 2
Source Project: cacheonix-core   Source File: Array.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
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);
		}
	}
}
 
Example 3
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() );
			}
		}
	}
}
 
Example 4
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 + "'");
			}
		}
	}
 
Example 5
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;
		}

	}
 
Example 6
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;
		}
	}
 
Example 7
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;
}
 
Example 8
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;
}
 
Example 9
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;
}
 
Example 10
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;
	}
}
 
Example 11
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;
	}
}
 
Example 12
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 );
		}
	}
}
 
Example 13
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;
}
 
Example 14
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 );
		}
	}
}
 
Example 15
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;
}
 
Example 16
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 );
	}
 
Example 17
private static void bindAuxiliaryDatabaseObject(Element auxDbObjectNode, Mappings mappings) {
	AuxiliaryDatabaseObject auxDbObject = null;
	Element definitionNode = auxDbObjectNode.element( "definition" );
	if ( definitionNode != null ) {
		try {
			auxDbObject = ( AuxiliaryDatabaseObject ) ReflectHelper
					.classForName( definitionNode.attributeValue( "class" ) )
					.newInstance();
		}
		catch( ClassNotFoundException e ) {
			throw new MappingException(
					"could not locate custom database object class [" +
					definitionNode.attributeValue( "class" ) + "]"
				);
		}
		catch( Throwable t ) {
			throw new MappingException(
					"could not instantiate custom database object class [" +
					definitionNode.attributeValue( "class" ) + "]"
				);
		}
	}
	else {
		auxDbObject = new SimpleAuxiliaryDatabaseObject(
				auxDbObjectNode.elementTextTrim( "create" ),
				auxDbObjectNode.elementTextTrim( "drop" )
			);
	}

	Iterator dialectScopings = auxDbObjectNode.elementIterator( "dialect-scope" );
	while ( dialectScopings.hasNext() ) {
		Element dialectScoping = ( Element ) dialectScopings.next();
		auxDbObject.addDialectScope( dialectScoping.attributeValue( "name" ) );
	}

	mappings.addAuxiliaryDatabaseObject( auxDbObject );
}
 
Example 18
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 );
}
 
Example 19
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);
	}
}
 
Example 20
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);
	}
}
 
Example 21
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);
	}
}
 
Example 22
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;
}
 
Example 23
public Class getComponentClass() throws MappingException {
	try {
		return ReflectHelper.classForName(componentClassName);
	}
	catch (ClassNotFoundException cnfe) {
		throw new MappingException("component class not found: " + componentClassName, cnfe);
	}
}
 
Example 24
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);
	}
}
 
Example 25
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);
	}
}
 
Example 26
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();
	}
}
 
Example 27
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);
	}
}
 
Example 28
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;
}
 
Example 29
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;
}
 
Example 30
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();
	}
}