Author: manik.surtani(a)jboss.com
Date: 2008-05-09 13:06:20 -0400 (Fri, 09 May 2008)
New Revision: 5821
Removed:
core/trunk/src/main/java/org/jboss/cache/util/reflect/CachedMethod.java
Modified:
core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java
core/trunk/src/main/java/org/jboss/cache/util/reflect/ReflectionUtil.java
Log:
Removed unnecessary class
Modified: core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java 2008-05-09
12:32:19 UTC (rev 5820)
+++ core/trunk/src/main/java/org/jboss/cache/factories/ComponentRegistry.java 2008-05-09
17:06:20 UTC (rev 5821)
@@ -16,7 +16,6 @@
import org.jboss.cache.factories.annotations.Start;
import org.jboss.cache.factories.annotations.Stop;
import org.jboss.cache.util.BeanUtils;
-import org.jboss.cache.util.reflect.CachedMethod;
import org.jboss.cache.util.reflect.ReflectionUtil;
import javax.management.MBeanServerFactory;
@@ -25,7 +24,6 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -72,9 +70,6 @@
// component and method containers
final Map<String, Component> componentLookup = new HashMap<String,
Component>();
- final List<PrioritizedMethod> startMethods = new
ArrayList<PrioritizedMethod>();
- final List<PrioritizedMethod> stopMethods = new
ArrayList<PrioritizedMethod>();
- final List<PrioritizedMethod> destroyMethods = new
ArrayList<PrioritizedMethod>();
CacheStatus state = CacheStatus.INSTANTIATED;
@@ -88,7 +83,6 @@
*/
private boolean invokedFromShutdownHook;
-
/**
* Creates an instance of the component registry. The configuration passed in is
automatically registered.
*
@@ -132,10 +126,12 @@
{
try
{
- List<CachedMethod> methods =
ReflectionUtil.getAllCachedMethods(target.getClass(), Inject.class);
+ // don't use the reflection cache for wireDependencies calls since these are
not managed by the ComponentRegistry
+ // and may be invoked at any time, even after the cache starts.
+ List<Method> methods = ReflectionUtil.getAllMethods(target.getClass(),
Inject.class);
// search for anything we need to inject
- for (CachedMethod method : methods) invokeInjectionMethod(target, method);
+ for (Method method : methods) invokeInjectionMethod(target, method);
}
catch (Exception e)
{
@@ -208,6 +204,7 @@
{
if (trace) log.trace("Replacing old component " + old + " with
new instance " + component);
old.instance = component;
+ old.methodsScanned = false;
c = old;
if (state == CacheStatus.STARTED) populateLifecycleMethods();
@@ -234,13 +231,13 @@
protected void addComponentDependencies(Component c)
{
Class type = c.instance.getClass();
- List<CachedMethod> methods = ReflectionUtil.getAllCachedMethods(type,
Inject.class);
+ List<Method> methods = ReflectionUtil.getAllMethods(type, Inject.class);
c.injectionMethods.clear();
- for (CachedMethod m : methods) c.injectionMethods.add(m);
+ c.injectionMethods.addAll(methods);
}
@SuppressWarnings("unchecked")
- protected void invokeInjectionMethod(Object o, CachedMethod m)
+ protected void invokeInjectionMethod(Object o, Method m)
{
Class[] dependencies = m.getParameterTypes();
Object[] params = new Object[dependencies.length];
@@ -250,7 +247,7 @@
params[i] = getOrCreateComponent(dependencies[i]);
}
- ReflectionUtil.invokeAccessibly(o, m.getMethod(), params);
+ ReflectionUtil.invokeAccessibly(o, m, params);
}
/**
@@ -478,54 +475,46 @@
*/
private void populateLifecycleMethods()
{
- // cache a list of the start annotated methods
- startMethods.clear();
for (Component c : componentLookup.values())
{
- List<Method> methods = ReflectionUtil.getAllMethods(c.instance.getClass(),
Start.class);
- for (Method m : methods)
+ if (!c.methodsScanned)
{
- PrioritizedMethod em = new PrioritizedMethod();
- em.component = c;
- em.method = m;
- em.priority = m.getAnnotation(Start.class).priority();
- startMethods.add(em);
- }
- }
+ c.methodsScanned = true;
+ c.startMethods.clear();
+ c.stopMethods.clear();
+ c.destroyMethods.clear();
- // cache a list of the stop annotated methods
- stopMethods.clear();
- for (Component c : componentLookup.values())
- {
- List<Method> methods = ReflectionUtil.getAllMethods(c.instance.getClass(),
Stop.class);
- for (Method m : methods)
- {
- PrioritizedMethod em = new PrioritizedMethod();
- em.component = c;
- em.method = m;
- em.priority = m.getAnnotation(Stop.class).priority();
- stopMethods.add(em);
- }
- }
+ List<Method> methods =
ReflectionUtil.getAllMethods(c.instance.getClass(), Start.class);
+ for (Method m : methods)
+ {
+ PrioritizedMethod em = new PrioritizedMethod();
+ em.component = c;
+ em.method = m;
+ em.priority = m.getAnnotation(Start.class).priority();
+ c.startMethods.add(em);
+ }
- // cache a list of the destroy annotated methods
- destroyMethods.clear();
- for (Component c : componentLookup.values())
- {
- List<Method> methods = ReflectionUtil.getAllMethods(c.instance.getClass(),
Destroy.class);
- for (Method m : methods)
- {
- PrioritizedMethod em = new PrioritizedMethod();
- em.component = c;
- em.method = m;
- em.priority = m.getAnnotation(Destroy.class).priority();
- destroyMethods.add(em);
+ methods = ReflectionUtil.getAllMethods(c.instance.getClass(), Stop.class);
+ for (Method m : methods)
+ {
+ PrioritizedMethod em = new PrioritizedMethod();
+ em.component = c;
+ em.method = m;
+ em.priority = m.getAnnotation(Stop.class).priority();
+ c.startMethods.add(em);
+ }
+
+ methods = ReflectionUtil.getAllMethods(c.instance.getClass(),
Destroy.class);
+ for (Method m : methods)
+ {
+ PrioritizedMethod em = new PrioritizedMethod();
+ em.component = c;
+ em.method = m;
+ em.priority = m.getAnnotation(Destroy.class).priority();
+ c.startMethods.add(em);
+ }
}
}
-
- Collections.sort(startMethods);
- Collections.sort(stopMethods);
- Collections.sort(destroyMethods);
}
/**
@@ -722,6 +711,12 @@
// first cache all start, stop and destroy methods.
populateLifecycleMethods();
+ List<PrioritizedMethod> startMethods = new
ArrayList<PrioritizedMethod>(componentLookup.size());
+ for (Component c : componentLookup.values()) startMethods.addAll(c.startMethods);
+
+ // sort the start methods by priority
+ Collections.sort(startMethods);
+
// fire all START methods according to priority
for (PrioritizedMethod em : startMethods) em.invoke();
@@ -776,6 +771,11 @@
// if this is called from a source other than the shutdown hook, deregister the
shutdown hook.
if (!invokedFromShutdownHook && shutdownHook != null)
Runtime.getRuntime().removeShutdownHook(shutdownHook);
+ List<PrioritizedMethod> stopMethods = new
ArrayList<PrioritizedMethod>(componentLookup.size());
+ for (Component c : componentLookup.values()) stopMethods.addAll(c.stopMethods);
+
+ Collections.sort(stopMethods);
+
// fire all STOP methods according to priority
for (PrioritizedMethod em : stopMethods) em.invoke();
@@ -792,6 +792,11 @@
resetNonVolatile();
+ List<PrioritizedMethod> destroyMethods = new
ArrayList<PrioritizedMethod>(componentLookup.size());
+ for (Component c : componentLookup.values())
destroyMethods.addAll(c.destroyMethods);
+
+ Collections.sort(destroyMethods);
+
// fire all DESTROY methods according to priority
for (PrioritizedMethod em : destroyMethods) em.invoke();
@@ -878,10 +883,14 @@
* The name of the component
*/
String name;
+ boolean methodsScanned;
/**
* List of injection methods used to inject dependencies into the component
*/
- List<CachedMethod> injectionMethods = new LinkedList<CachedMethod>();
+ List<Method> injectionMethods = new ArrayList<Method>(2);
+ List<PrioritizedMethod> startMethods = new
ArrayList<PrioritizedMethod>(2);
+ List<PrioritizedMethod> stopMethods = new
ArrayList<PrioritizedMethod>(2);
+ List<PrioritizedMethod> destroyMethods = new
ArrayList<PrioritizedMethod>(2);
/**
* If true, then this component is not flushed before starting the
ComponentRegistry.
*/
@@ -902,7 +911,7 @@
*/
public void injectDependencies()
{
- for (CachedMethod m : injectionMethods) invokeInjectionMethod(instance, m);
+ for (Method m : injectionMethods) invokeInjectionMethod(instance, m);
}
}
Deleted: core/trunk/src/main/java/org/jboss/cache/util/reflect/CachedMethod.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/reflect/CachedMethod.java 2008-05-09
12:32:19 UTC (rev 5820)
+++ core/trunk/src/main/java/org/jboss/cache/util/reflect/CachedMethod.java 2008-05-09
17:06:20 UTC (rev 5821)
@@ -1,39 +0,0 @@
-package org.jboss.cache.util.reflect;
-
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Method;
-
-/**
- * A cached Method object, so that calls to getParameterTypes, getAnnotations, etc are
cached.
- *
- * @author Manik Surtani (<a
href="mailto:manik@jboss.org">manik@jboss.org</a>)
- * @since 2.1.0
- */
-public class CachedMethod
-{
- Method method;
- Class[] parameterTypes;
- Annotation[][] parameterAnnotations;
-
- public CachedMethod(Method method)
- {
- this.method = method;
- this.parameterTypes = method.getParameterTypes();
- this.parameterAnnotations = method.getParameterAnnotations();
- }
-
- public Method getMethod()
- {
- return method;
- }
-
- public Class[] getParameterTypes()
- {
- return parameterTypes;
- }
-
- public Annotation[][] getParameterAnnotations()
- {
- return parameterAnnotations;
- }
-}
Modified: core/trunk/src/main/java/org/jboss/cache/util/reflect/ReflectionUtil.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/reflect/ReflectionUtil.java 2008-05-09
12:32:19 UTC (rev 5820)
+++ core/trunk/src/main/java/org/jboss/cache/util/reflect/ReflectionUtil.java 2008-05-09
17:06:20 UTC (rev 5821)
@@ -38,21 +38,6 @@
}
/**
- * Returns a set of Methods that contain the given method annotation. This includes
all public, protected, package and private
- * methods, as well as those of superclasses. Note that this does *not* include
overridden methods.
- *
- * @param c class to inspect
- * @param annotationType the type of annotation to look for
- * @return List of Method objects that require injection.
- */
- public static List<CachedMethod> getAllCachedMethods(Class c, Class<? extends
Annotation> annotationType)
- {
- List<CachedMethod> annotated = new LinkedList<CachedMethod>();
- inspectRecursivelyCached(c, annotated, annotationType);
- return annotated;
- }
-
- /**
* Inspects a class and it's superclasses (all the way to {@link Object} for
method instances that contain a given annotation.
* This even identifies private, package and protected methods, not just public ones.
*
@@ -76,29 +61,6 @@
}
/**
- * Inspects a class and it's superclasses (all the way to {@link Object} for
method instances that contain a given annotation.
- * This even identifies private, package and protected methods, not just public ones.
- *
- * @param c
- * @param s
- * @param annotationType
- */
- private static void inspectRecursivelyCached(Class c, List<CachedMethod> s,
Class<? extends Annotation> annotationType)
- {
- // Superclass first
- if (!c.equals(Object.class)) inspectRecursivelyCached(c.getSuperclass(), s,
annotationType);
-
- for (Method m : c.getDeclaredMethods())
- {
- // don't bother if this method has already been overridden by a subclass
- if (!alreadyFoundCached(m, s) && m.isAnnotationPresent(annotationType))
- {
- s.add(new CachedMethod(m));
- }
- }
- }
-
- /**
* Tests whether a method has already been found, i.e., overridden.
*
* @param m method to inspect
@@ -116,24 +78,6 @@
return false;
}
- /**
- * Tests whether a method has already been found, i.e., overridden.
- *
- * @param m method to inspect
- * @param s collection of methods found
- * @return true a method with the same signature already exists.
- */
- private static boolean alreadyFoundCached(Method m, Collection<CachedMethod> s)
- {
- for (CachedMethod found : s)
- {
- if (m.getName().equals(found.getMethod().getName()) &&
- Arrays.equals(m.getParameterTypes(), found.getParameterTypes()))
- return true;
- }
- return false;
- }
-
public static void setValue(Object instance, String fieldName, Object value)
{
try