org.hibernate.util.ReflectHelper Java Examples

The following examples show how to use org.hibernate.util.ReflectHelper. 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
Source File: TransactionManagerLookupFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #2
Source File: PojoInstantiator.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #3
Source File: PojoInstantiator.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #4
Source File: PojoInstantiator.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #5
Source File: SchemaValidatorTask.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #6
Source File: SchemaUpdateTask.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #7
Source File: SchemaExportTask.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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
Source File: BasicPropertyAccessor.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #9
Source File: BasicPropertyAccessor.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #10
Source File: AbstractQueryImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #11
Source File: RootClass.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #12
Source File: LiteralProcessor.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #13
Source File: MyEntityInstantiator.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #14
Source File: TypeFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #15
Source File: SettingsFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 #16
Source File: Array.java    From cacheonix-core with 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 #17
Source File: ClassType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Object get(ResultSet rs, String name) throws HibernateException, SQLException {
	String str = (String) Hibernate.STRING.get(rs, name);
	if (str == null) {
		return null;
	}
	else {
		try {
			return ReflectHelper.classForName(str);
		}
		catch (ClassNotFoundException cnfe) {
			throw new HibernateException("Class not found: " + str);
		}
	}
}
 
Example #18
Source File: SessionFactoryImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String getImportedClassName(String className) {
	String result = (String) imports.get(className);
	if (result==null) {
		try {
			ReflectHelper.classForName(className);
			return className;
		}
		catch (ClassNotFoundException cnfe) {
			return null;
		}
	}
	else {
		return result;
	}
}
 
Example #19
Source File: ComponentEntityModeToTuplizerMapping.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 );
	}
}
 
Example #20
Source File: EntityType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Class determineAssociatedEntityClass() {
	try {
		return ReflectHelper.classForName( getAssociatedEntityName() );
	}
	catch ( ClassNotFoundException cnfe ) {
		return java.util.Map.class;
	}
}
 
Example #21
Source File: PojoComponentTuplizer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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() );
	}
}
 
Example #22
Source File: PropertyFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static Constructor getConstructor(PersistentClass persistentClass) {
	if ( persistentClass == null || !persistentClass.hasPojoRepresentation() ) {
		return null;
	}

	try {
		return ReflectHelper.getDefaultConstructor( persistentClass.getMappedClass() );
	}
	catch( Throwable t ) {
		return null;
	}
}
 
Example #23
Source File: ClassType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Object fromStringValue(String xml) throws HibernateException {
	try {
		return ReflectHelper.classForName(xml);
	}
	catch (ClassNotFoundException cnfe) {
		throw new HibernateException("could not parse xml", cnfe);
	}
}
 
Example #24
Source File: BasicLazyInitializer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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);
}
 
Example #25
Source File: CGLIBLazyInitializer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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() );
		}
	}
}
 
Example #26
Source File: Dialect.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static Dialect instantiateDialect(String dialectName) throws HibernateException {
	if ( dialectName == null ) {
		throw new HibernateException( "The dialect was not set. Set the property hibernate.dialect." );
	}
	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 );
	}
}
 
Example #27
Source File: OptimizerFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 );
}
 
Example #28
Source File: SessionFactoryImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private CurrentSessionContext buildCurrentSessionContext() {
	String impl = properties.getProperty( Environment.CURRENT_SESSION_CONTEXT_CLASS );
	// for backward-compatability
	if ( impl == null && transactionManager != null ) {
		impl = "jta";
	}

	if ( impl == null ) {
		return null;
	}
	else if ( "jta".equals( impl ) ) {
		if ( settings.getTransactionFactory().areCallbacksLocalToHibernateTransactions() ) {
			log.warn( "JTASessionContext being used with JDBCTransactionFactory; auto-flush will not operate correctly with getCurrentSession()" );
		}
		return new JTASessionContext( this );
	}
	else if ( "thread".equals( impl ) ) {
		return new ThreadLocalSessionContext( this );
	}
	else if ( "managed".equals( impl ) ) {
		return new ManagedSessionContext( this );
	}
	else {
		try {
			Class implClass = ReflectHelper.classForName( impl );
			return ( CurrentSessionContext ) implClass
					.getConstructor( new Class[] { SessionFactoryImplementor.class } )
					.newInstance( new Object[] { this } );
		}
		catch( Throwable t ) {
			log.error( "Unable to construct current session context [" + impl + "]", t );
			return null;
		}
	}
}
 
Example #29
Source File: IdentifierGeneratorFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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;
}
 
Example #30
Source File: DialectFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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 );
	}
}