alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Hibernate example source code file (InterceptFieldClassFileTransformerTest.java)

This example Hibernate source code file (InterceptFieldClassFileTransformerTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Hibernate tags/keywords

arraylist, arraylist, class, exception, instrumentedclassloader, instrumentedclassloader, interceptfieldclassfiletransformertest, interceptfieldclassfiletransformertest, list, method, nosuchmethodexception, nosuchmethodexception, reflection, testcase, util

The Hibernate InterceptFieldClassFileTransformerTest.java source code

//$Id$
package org.hibernate.ejb.test.instrument;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;

/**
 * @author Emmanuel Bernard
 * @author Hardy Ferentschik
 */
public class InterceptFieldClassFileTransformerTest extends TestCase {
	/**
	 * Tests that class file enhancement works.
	 * 
	 * @throws Exception in case the test fails.
	 */
	public void testEnhancement() throws Exception {
		List<String> entities = new ArrayList();
		entities.add( "org.hibernate.ejb.test.instrument.Simple" );

		// sanity check that the class is unmodified and does not contain getFieldHandler()
		try {
			org.hibernate.ejb.test.instrument.Simple.class.getDeclaredMethod( "getFieldHandler" );
			fail();
		} catch ( NoSuchMethodException nsme ) {
			// success
		}

		// use custom class loader which enhances the class
		InstrumentedClassLoader cl = new InstrumentedClassLoader( Thread.currentThread().getContextClassLoader() );
		cl.setEntities( entities );
		Class clazz = cl.loadClass( entities.get( 0 ) );
		
		// javassist is our default byte code enhancer. Enhancing will eg add the method getFieldHandler()
		// see org.hibernate.bytecode.internal.javassist.FieldTransformer
		Method method = clazz.getDeclaredMethod( "getFieldHandler" );
		assertNotNull( method );
	}
}

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate InterceptFieldClassFileTransformerTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.