[jboss-cvs] JBossCache/src/org/jboss/cache/pojo/impl ...
Jason Thomas Greene
jgreene at jboss.com
Fri Jul 27 14:54:22 EDT 2007
User: jgreene
Date: 07/07/27 14:54:21
Modified: src/org/jboss/cache/pojo/impl AdvisedPojoHandler.java
CachedType.java PojoCacheDelegate.java
Log:
Fix JBCACHE-1150
Remove dead and duplicate code
Simplify annotation handling
Revision Changes Path
1.10 +4 -27 JBossCache/src/org/jboss/cache/pojo/impl/AdvisedPojoHandler.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: AdvisedPojoHandler.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/pojo/impl/AdvisedPojoHandler.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- AdvisedPojoHandler.java 18 Jul 2007 15:21:02 -0000 1.9
+++ AdvisedPojoHandler.java 27 Jul 2007 18:54:21 -0000 1.10
@@ -32,7 +32,7 @@
*
* @author Ben Wang
* Date: Aug 4, 2005
- * @version $Id: AdvisedPojoHandler.java,v 1.9 2007/07/18 15:21:02 jgreene Exp $
+ * @version $Id: AdvisedPojoHandler.java,v 1.10 2007/07/27 18:54:21 jgreene Exp $
*/
class AdvisedPojoHandler
{
@@ -92,7 +92,6 @@
// we will do it recursively.
// Map of sub-objects that are non-primitive
Map subPojoMap = new HashMap();
- boolean hasFieldAnnotation = hasAnnotation(obj.getClass(), ((Advised) obj)._getAdvisor(), type);
for (Iterator i = type.getFields().iterator(); i.hasNext();)
{
@@ -106,25 +105,9 @@
{
throw new CacheException("field access failed", e);
}
- CachedType fieldType = pCache_.getCachedType(field.getType());
- // check for non-replicatable types
- if (CachedType.isPrimitiveNonReplicatable(field))
- {
- continue;
- }
-
- if (hasFieldAnnotation)
- {
- if (CachedType.hasTransientAnnotation(field, ((Advised) obj)._getAdvisor()))
- {
- continue;
- }
- }
// we simply treat field that has @Serializable as a primitive type.
- if (fieldType.isImmediate() ||
- (hasFieldAnnotation &&
- CachedType.hasSerializableAnnotation(field, ((Advised) obj)._getAdvisor())))
+ if (CachedType.isSimpleAttribute(field))
{
// switched using batch update
map.put(field.getName(), value);
@@ -179,9 +162,9 @@
for (Iterator i = type.getFields().iterator(); i.hasNext();)
{
Field field = (Field) (((FieldPersistentReference) i.next())).get();
- CachedType fieldType = pCache_.getCachedType(field.getType());
Object value = null;
- if (!fieldType.isImmediate())
+
+ if (! CachedType.isSimpleAttribute(field))
{
value = pCache_.detach(fqn, field.getName());
@@ -216,10 +199,4 @@
return null; // Not really null though.
}
-
- private static boolean hasAnnotation(Class clazz, Advisor advisor, CachedType type)
- {
- return CachedType.hasAnnotation(clazz, advisor, type);
- }
-
}
1.5 +7 -78 JBossCache/src/org/jboss/cache/pojo/impl/CachedType.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CachedType.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/pojo/impl/CachedType.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- CachedType.java 18 Jul 2007 17:36:31 -0000 1.4
+++ CachedType.java 27 Jul 2007 18:54:21 -0000 1.5
@@ -54,9 +54,6 @@
private WeakReference<Class> type;
private boolean immutable;
private boolean immediate;
- // This map caches the class that contains no annotation.
- private static Map CachedClassWithNoAnnotation_ = new WeakHashMap();
- private static Map CachedClassWithAnnotation_ = new WeakHashMap();
// Java fields . Will use special FieldPersistentReference to prevent classloader leakage.
private List fields = new ArrayList();
@@ -211,7 +208,7 @@
for (int i = 0; i < classFields.length; i++)
{
Field f = classFields[i];
- if (isPrimitiveNonReplicatable(f)) continue;
+ if (isNonReplicatable(f)) continue;
f.setAccessible(true);
@@ -222,88 +219,20 @@
}
}
- /**
- * We check whether this class has any field annotation declaration. We assume that there is only
- * one such declaring class per vm and it is static.
- */
- public static boolean hasAnnotation(Class clazz, Advisor advisor, CachedType type)
- {
- // It is ok that we don't synchronize it here.
- if (CachedClassWithNoAnnotation_.get(clazz) != null)
- {
- return false;
- }
- else if (CachedClassWithAnnotation_.get(clazz) != null)
- {
- return true;
- }
-
- for (Iterator i = type.getFields().iterator(); i.hasNext();)
- {
- Field field = (Field) (((FieldPersistentReference) i.next())).get();
- // check for non-replicatable types
- if (CachedType.hasFieldAnnotation(field, advisor))
- {
- synchronized (CachedClassWithAnnotation_)
- {
- CachedClassWithAnnotation_.put(clazz, clazz.getName());
- }
- return true;
- }
- }
-
- // This obj class doesn't have field annotation. It is ok that multiple threads
- // put it repeatedly actually.
- synchronized (CachedClassWithNoAnnotation_)
- {
- CachedClassWithNoAnnotation_.put(clazz, clazz.getName());
- }
- return false;
- }
-
- public static boolean isPrimitiveNonReplicatable(Field f)
+ public static boolean isNonReplicatable(Field field)
{
- int mods = f.getModifiers();
+ int mods = field.getModifiers();
/**
* The following modifiers are ignored in the cache, i.e., they will not be stored in the cache.
* Whenever, user trying to access these fields, it will be accessed from the in-memory version.
*/
- return Modifier.isStatic(mods) || Modifier.isTransient(mods) || Modifier.isFinal(mods);
- }
-
- private static boolean hasFieldAnnotation(Field field, Advisor advisor)
- {
- return hasTransientAnnotation(field, advisor) || hasSerializableAnnotation(field, advisor);
+ return Modifier.isStatic(mods) || Modifier.isTransient(mods) || Modifier.isFinal(mods) ||
+ field.isAnnotationPresent(org.jboss.cache.pojo.annotation.Transient.class);
}
- public static boolean hasTransientAnnotation(Field field, Advisor advisor)
+ public static boolean isSimpleAttribute(Field field)
{
- Object obj = advisor.resolveAnnotation(field, org.jboss.cache.pojo.annotation.Transient.class);
- if (obj != null)
- {
- return true;
- }
- return false;
- }
-
- public static boolean hasSerializableAnnotation(Field field, Advisor advisor)
- {
- Object obj = advisor.resolveAnnotation(field, org.jboss.cache.pojo.annotation.Serializable.class);
- if (obj != null)
- {
- return true;
- }
- return false;
- }
-
- public static boolean hasSerializableAnnotation(FieldInvocation invocation)
- {
- Object obj = invocation.resolveAnnotation(org.jboss.cache.pojo.annotation.Serializable.class);
- if (obj != null)
- {
- return true;
- }
- return false;
+ return isImmediate(field.getType()) || field.isAnnotationPresent(org.jboss.cache.pojo.annotation.Serializable.class);
}
/*
1.11 +0 -150 JBossCache/src/org/jboss/cache/pojo/impl/PojoCacheDelegate.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: PojoCacheDelegate.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/pojo/impl/PojoCacheDelegate.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- PojoCacheDelegate.java 28 Jun 2007 00:56:06 -0000 1.10
+++ PojoCacheDelegate.java 27 Jul 2007 18:54:21 -0000 1.11
@@ -409,27 +409,6 @@
}
- /**
- * Based on the pojo to perform a bulk remove recursively if there is no object graph
- * relationship for performance optimization.
- */
- private boolean bulkRemove(Fqn fqn, Object obj) throws CacheException
- {
- // Check for cross-reference. If there is, we can't do bulk remove
- // map contains (pojo, cacheinterceptor) pair that needs to rollback the the removal.
- Map undoMap = new HashMap();
- if (pojoGraphMultipleReferenced(obj, undoMap))
- {
- undoInterceptorDetach(undoMap);
- return false;
- }
- else
- {
- cache.removeNode(fqn);// interceptor has been removed so it is safe to do bulk remove now.
- }
- return true;
- }
-
private void detachInterceptor(InstanceAdvisor advisor, Interceptor interceptor,
boolean detachOnly, Map undoMap)
{
@@ -471,135 +450,6 @@
}
}
- /**
- * Check recursively if the pojo and its graph is multiple referenced. If it is, we can't
- * do a bulk remove.
- */
- private boolean pojoGraphMultipleReferenced(Object obj, Map undoMap) throws CacheException
- {
- return true;
- }
-
- private boolean XpojoGraphMultipleReferenced(Object obj, Map undoMap) throws CacheException
- {
- // store object in cache
- if (obj instanceof Advised)
- {
- CachedType type = pojoCache.getCachedType(obj.getClass());
- // add interceptor
- InstanceAdvisor advisor = ((Advised) obj)._getInstanceAdvisor();
- if (advisor == null)
- {
- throw new PojoCacheException("pojoGraphMultipleReferenced(): InstanceAdvisor is null for: " + obj);
- }
-
- BaseInterceptor interceptor = (BaseInterceptor) AopUtil.findCacheInterceptor(advisor);
- // just in case
- if (interceptor == null)
- {
- return false;
- }
- PojoInstance pojoInstance = interceptor.getAopInstance();
- // Check if there is cross referenced.
- if (pojoInstance.getRefCount() != 0) return true;// I have been referenced
- if (pojoInstance.getInternalFqn() != null) return true;// I am referencing others
-
- boolean hasFieldAnnotation = hasAnnotation(obj.getClass(), ((Advised) obj)._getAdvisor(), type);
- // Check the fields
- for (Iterator i = type.getFields().iterator(); i.hasNext();)
- {
- Field field = (Field) (((FieldPersistentReference) i.next())).get();
- Object value = null;
- try
- {
- value = field.get(obj);
- }
- catch (IllegalAccessException e)
- {
- throw new CacheException("field access failed", e);
- }
-
- CachedType fieldType = pojoCache.getCachedType(field.getType());
-
- // we simply treat field that has @Serializable as a primitive type.
- if (fieldType.isImmediate() ||
- (hasFieldAnnotation &&
- CachedType.hasSerializableAnnotation(field, ((Advised) obj)._getAdvisor())))
- {
- continue;
- }
-
- // check for non-replicatable types
- if (CachedType.isPrimitiveNonReplicatable(field))
- {
- continue;
- }
-
- if (!hasFieldAnnotation)
- {
- if (CachedType.hasTransientAnnotation(field, ((Advised) obj)._getAdvisor()))
- {
- continue;
- }
- }
-
- // Need to do a getObject just in case this is a failover removeObject.
- if (value == null)
- {
- value = getObject(new Fqn(interceptor.getFqn(), field.getName()), null);
- }
-
- if (value == null) continue;// this is no brainer.
-
- if (pojoGraphMultipleReferenced(value, undoMap)) return true;
- }
- boolean detachOnly = false;
- detachInterceptor(advisor, interceptor, detachOnly, undoMap);
- }
- else if (obj instanceof Map || obj instanceof List || obj instanceof Set)
- {
- // TODO Is this really necessary?
- if (!(obj instanceof ClassProxy)) return false;
-
- InstanceAdvisor advisor = ((ClassProxy) obj)._getInstanceAdvisor();
- BaseInterceptor interceptor = (BaseInterceptor) AopUtil.findCollectionInterceptor(advisor);
- PojoInstance pojoInstance = interceptor.getAopInstance();
- if (pojoInstance == null) return false;// safeguard
- // Check if there is cross referenced.
- if (pojoInstance.getRefCount() != 0) return true;// I have been referenced
- if (pojoInstance.getInternalFqn() != null) return true;// I am referencing others
- // iterate thru the keys
- if (obj instanceof Map)
- {
- for (Iterator it = ((Map) obj).keySet().iterator(); it.hasNext();)
- {
- Object subObj = ((Map) obj).get(it.next());
- if (pojoGraphMultipleReferenced(subObj, undoMap)) return true;
- }
- }
- else if (obj instanceof List || obj instanceof Set)
- {
- for (Iterator it = ((Collection) obj).iterator(); it.hasNext();)
- {
- Object subObj = it.next();
- if (pojoGraphMultipleReferenced(subObj, undoMap)) return true;
- }
- }
- // Don't remove now.
- boolean removeFromCache = false;
- ((AbstractCollectionInterceptor) interceptor).detach(removeFromCache);// detach the interceptor. This will trigger a copy and remove.
- boolean detachOnly = true;
- detachInterceptor(advisor, interceptor, detachOnly, undoMap);
- }
-
- return false;
- }
-
- private static boolean hasAnnotation(Class clazz, Advisor advisor, CachedType type)
- {
- return CachedType.hasAnnotation(clazz, advisor, type);
- }
-
private void findChildObjects(Fqn fqn, Map map) throws CacheException
{
// We need to traverse then
More information about the jboss-cvs-commits
mailing list