[jboss-cvs] JBossAS SVN: r95393 - projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Oct 22 08:07:40 EDT 2009
Author: alesj
Date: 2009-10-22 08:07:39 -0400 (Thu, 22 Oct 2009)
New Revision: 95393
Modified:
projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistReflectionFactory.java
Log:
Reduce duplicated code.
Modified: projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistReflectionFactory.java
===================================================================
--- projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistReflectionFactory.java 2009-10-22 11:53:52 UTC (rev 95392)
+++ projects/jboss-reflect/trunk/src/main/java/org/jboss/reflect/plugins/javassist/JavassistReflectionFactory.java 2009-10-22 12:07:39 UTC (rev 95393)
@@ -30,11 +30,10 @@
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtField;
+import javassist.CtMember;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.Modifier;
-import javassist.NotFoundException;
-
import org.jboss.util.JBossStringBuilder;
import org.jboss.util.UnreachableStatementException;
@@ -47,6 +46,7 @@
* TODO JBREFLECT-3 proper classpool with pruning
* TODO JBREFLECT-6 non compiler based implementation
* @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
* @version $Revision$
*/
public class JavassistReflectionFactory
@@ -66,8 +66,70 @@
{
this.check = check;
}
-
+
/**
+ * Get class pool.
+ *
+ * @param member the member
+ * @return member's class pool
+ * @throws Throwable for anby error
+ */
+ protected ClassPool getClassPool(CtMember member) throws Throwable
+ {
+ if (member == null)
+ throw new IllegalArgumentException("Null member");
+
+ CtClass ctClass = member.getDeclaringClass();
+ return ctClass.getClassPool();
+ }
+
+ /**
+ * Apply super class.
+ *
+ * @param result the current ct class
+ * @param pool the class pool
+ */
+ protected void applySuperClass(CtClass result, ClassPool pool)
+ {
+ // TODO JBREFLECT-2 figure out how to do this on all JDKs
+ try
+ {
+ CtClass magic = pool.get("sun.reflect.MagicAccessorImpl");
+ result.setSuperclass(magic);
+ }
+ catch (Exception ignored)
+ {
+ }
+ }
+
+ /**
+ * Create instance.
+ *
+ * @param result the current ct class
+ * @param expectedType expected type
+ * @return new instance
+ * @throws Throwable for any error
+ */
+ protected <T> T createInstance(final CtClass result, final Class<T> expectedType) throws Throwable
+ {
+ try
+ {
+ return AccessController.doPrivileged(new PrivilegedExceptionAction<T>()
+ {
+ public T run() throws Exception
+ {
+ Class<?> clazz = result.toClass();
+ return expectedType.cast(clazz.newInstance());
+ }
+ });
+ }
+ catch (PrivilegedActionException e)
+ {
+ throw e.getCause();
+ }
+ }
+
+ /**
* Creates a javassist method.
*
* This method generates an implementation of {@code JavassistMethod}, loads
@@ -79,18 +141,9 @@
*/
public JavassistMethod createMethod(CtMethod ctMethod) throws Throwable
{
- //TODO shouldn't we provide a not null class loader?
- ClassPool pool = JavassistTypeInfoFactoryImpl.getPoolFactory().getPoolForLoader(null);
+ ClassPool pool = getClassPool(ctMethod);
final CtClass result = pool.makeClass(JavassistMethod.class.getName() + counter.incrementAndGet());
- /* TODO JBREFLECT-2 figure out how to do this on all JDKs
- try
- {
- CtClass magic = pool.get("sun.reflect.MagicAccessorImpl");
- result.setSuperclass(magic);
- }
- catch (NotFoundException ignored)
- {
- }*/
+ applySuperClass(result, pool);
result.addInterface(pool.get(JavassistMethod.class.getName()));
CtConstructor constructor = new CtConstructor(null, result);
@@ -235,21 +288,7 @@
}
// Create it
- try
- {
- return AccessController.doPrivileged(new PrivilegedExceptionAction<JavassistMethod>()
- {
- public JavassistMethod run() throws Exception
- {
- Class<?> clazz = result.toClass();
- return (JavassistMethod) clazz.newInstance();
- }
- });
- }
- catch (PrivilegedActionException e)
- {
- throw e.getCause();
- }
+ return createInstance(result, JavassistMethod.class);
}
/**
@@ -261,18 +300,9 @@
*/
public JavassistConstructor createConstructor(CtConstructor ctConstructor) throws Throwable
{
- //TODO: Should we provide a non-null class loader?
- ClassPool pool = JavassistTypeInfoFactoryImpl.getPoolFactory().getPoolForLoader(null);
+ ClassPool pool = getClassPool(ctConstructor);
final CtClass result = pool.makeClass(JavassistConstructor.class.getName() + counter.incrementAndGet());
- try
- {
- // TODO JBREFLECT-2
- CtClass magic = pool.get("sun.reflect.MagicAccessorImpl");
- result.setSuperclass(magic);
- }
- catch (NotFoundException ignored)
- {
- }
+ applySuperClass(result, pool);
result.addInterface(pool.get(JavassistConstructor.class.getName()));
CtConstructor constructor = new CtConstructor(null, result);
@@ -362,21 +392,7 @@
}
// Create it
- try
- {
- return AccessController.doPrivileged(new PrivilegedExceptionAction<JavassistConstructor>()
- {
- public JavassistConstructor run() throws Exception
- {
- Class<?> clazz = result.toClass();
- return (JavassistConstructor) clazz.newInstance();
- }
- });
- }
- catch (PrivilegedActionException e)
- {
- throw e.getCause();
- }
+ return createInstance(result, JavassistConstructor.class);
}
/**
@@ -388,18 +404,9 @@
*/
public JavassistField createField(CtField ctField) throws Throwable
{
- // TODO should we provide a non-null class loader?
- ClassPool pool = JavassistTypeInfoFactoryImpl.getPoolFactory().getPoolForLoader(null);
+ ClassPool pool = getClassPool(ctField);
final CtClass result = pool.makeClass(JavassistField.class.getName() + counter.incrementAndGet());
- // TODO JBREFLECT-2
- try
- {
- CtClass magic = pool.get("sun.reflect.MagicAccessorImpl");
- result.setSuperclass(magic);
- }
- catch (NotFoundException ignored)
- {
- }
+ applySuperClass(result, pool);
result.addInterface(pool.get(JavassistField.class.getName()));
CtConstructor constructor = new CtConstructor(null, result);
@@ -565,21 +572,7 @@
}
// Create it
- try
- {
- return AccessController.doPrivileged(new PrivilegedExceptionAction<JavassistField>()
- {
- public JavassistField run() throws Exception
- {
- Class<?> clazz = result.toClass();
- return (JavassistField) clazz.newInstance();
- }
- });
- }
- catch (PrivilegedActionException e)
- {
- throw e.getCause();
- }
+ return createInstance(result, JavassistField.class);
}
/**
More information about the jboss-cvs-commits
mailing list