[weld-commits] Weld SVN: r5448 - in core/trunk/impl/src/main/java/org/jboss/weld: logging and 1 other directories.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Tue Jan 12 11:55:06 EST 2010


Author: pete.muir at jboss.org
Date: 2010-01-12 11:55:05 -0500 (Tue, 12 Jan 2010)
New Revision: 5448

Modified:
   core/trunk/impl/src/main/java/org/jboss/weld/bean/AbstractProducerBean.java
   core/trunk/impl/src/main/java/org/jboss/weld/logging/WeldMessageConveyor.java
   core/trunk/impl/src/main/java/org/jboss/weld/resources/ClassTransformer.java
Log:
WELD-262

Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/AbstractProducerBean.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/AbstractProducerBean.java	2010-01-12 14:06:01 UTC (rev 5447)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/AbstractProducerBean.java	2010-01-12 16:55:05 UTC (rev 5448)
@@ -42,7 +42,7 @@
 import java.lang.reflect.WildcardType;
 import java.util.HashSet;
 import java.util.Set;
-import java.util.concurrent.Callable;
+import java.util.concurrent.ConcurrentMap;
 
 import javax.enterprise.context.Dependent;
 import javax.enterprise.context.NormalScope;
@@ -63,10 +63,12 @@
 import org.jboss.weld.manager.DummyInjectionPoint;
 import org.jboss.weld.metadata.cache.MetaAnnotationStore;
 import org.jboss.weld.util.Beans;
-import org.jboss.weld.util.collections.ConcurrentCache;
 import org.jboss.weld.util.reflection.Reflections;
 import org.slf4j.cal10n.LocLogger;
 
+import com.google.common.base.Function;
+import com.google.common.collect.MapMaker;
+
 /**
  * The implicit producer bean
  * 
@@ -79,6 +81,17 @@
  */
 public abstract class AbstractProducerBean<X, T, S extends Member> extends AbstractReceiverBean<X, T, S>
 {
+   
+   private static final Function<Class<?>, Boolean> SERIALIZABLE_CHECK = new Function<Class<?>, Boolean>()
+   {
+
+      public Boolean apply(Class<?> from)
+      {
+         return Reflections.isSerializable(from);
+      }
+      
+   };
+   
    // Logger for messages
    private static final LocLogger log = loggerFactory().getLogger(BEAN);
 
@@ -90,7 +103,7 @@
    private boolean passivationCapableDependency;
 
    // Serialization cache for produced types at runtime
-   private ConcurrentCache<Class<?>, Boolean> serializationCheckCache;
+   private ConcurrentMap<Class<?>, Boolean> serializationCheckCache;
 
    /**
     * Constructor
@@ -101,7 +114,7 @@
    public AbstractProducerBean(String idSuffix, AbstractClassBean<X> declaringBean, BeanManagerImpl beanManager)
    {
       super(idSuffix, declaringBean, beanManager);
-      serializationCheckCache = new ConcurrentCache<Class<?>, Boolean>();
+      serializationCheckCache = new MapMaker().makeComputingMap(SERIALIZABLE_CHECK);
    }
 
    @Override
@@ -277,15 +290,7 @@
 
    protected boolean isTypeSerializable(final Class<?> clazz)
    {
-      return serializationCheckCache.putIfAbsent(clazz, new Callable<Boolean>()
-      {
-
-         public Boolean call() throws Exception
-         {
-            return Reflections.isSerializable(clazz);
-         }
-
-      });
+      return serializationCheckCache.get(clazz);
    }
 
    @Override

Modified: core/trunk/impl/src/main/java/org/jboss/weld/logging/WeldMessageConveyor.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/logging/WeldMessageConveyor.java	2010-01-12 14:06:01 UTC (rev 5447)
+++ core/trunk/impl/src/main/java/org/jboss/weld/logging/WeldMessageConveyor.java	2010-01-12 16:55:05 UTC (rev 5448)
@@ -2,27 +2,58 @@
 
 import java.lang.reflect.Field;
 import java.util.Locale;
-import java.util.concurrent.Callable;
+import java.util.concurrent.ConcurrentMap;
 
-import org.jboss.weld.util.collections.ConcurrentCache;
 import org.jboss.weld.util.reflection.SecureReflections;
 
 import ch.qos.cal10n.MessageConveyor;
 import ch.qos.cal10n.MessageConveyorException;
 
+import com.google.common.base.Function;
+import com.google.common.collect.MapMaker;
+
 public class WeldMessageConveyor extends MessageConveyor
 {
    
+   private static class ComputeMessagePrefix implements Function<Enum<?>, String>
+   {
+      
+      private final String subsystem;
+
+      private ComputeMessagePrefix(String subsystem)
+      {
+         this.subsystem = subsystem;
+      }
+
+      public String apply(Enum<?> from)
+      {
+         Field field;
+         try
+         {
+            field = SecureReflections.getField(from.getClass(), from.name());
+         }
+         catch (NoSuchFieldException e)
+         {
+            throw new IllegalArgumentException("Cannot reflect on key to obtain @MessageId. Key: " + from + "; Key Type: " + from.getClass());
+         }
+         if (!field.isAnnotationPresent(MessageId.class))
+         {
+            throw new IllegalArgumentException("@MessageId must be present. Key: " + from + "; Key Type: " + from.getClass());
+         }
+         String messageId = field.getAnnotation(MessageId.class).value();
+         return new StringBuilder().append(subsystem).append(SEPARATOR).append(messageId).append(" ").toString();
+      }
+      
+   }
+   
    private static final String SEPARATOR = "-";
-   private final String subsystem;
    
-   private final ConcurrentCache<Enum<?>, String> messagePrefixCache;
+   private final ConcurrentMap<Enum<?>, String> messagePrefixCache;
    
    public WeldMessageConveyor(Locale locale, String subsystem)
    {
       super(locale);
-      this.subsystem = subsystem;
-      this.messagePrefixCache = new ConcurrentCache<Enum<?>, String>();
+      this.messagePrefixCache = new MapMaker().makeComputingMap(new ComputeMessagePrefix(subsystem));
    }
    
    @Override
@@ -33,21 +64,7 @@
    
    private <E extends Enum<?>> String getMessagePrefix(final E key)
    {
-      return messagePrefixCache.putIfAbsent(key, new Callable<String>()
-      {
-         
-         public String call() throws Exception
-         {
-            Field field = SecureReflections.getField(key.getClass(), key.name());
-            if (!field.isAnnotationPresent(MessageId.class))
-            {
-               throw new IllegalArgumentException("@MessageId must be present. Key: " + key + "; Key Type: " + key.getClass());
-            }
-            String messageId = field.getAnnotation(MessageId.class).value();
-            return new StringBuilder().append(subsystem).append(SEPARATOR).append(messageId).append(" ").toString();
-         }
-         
-      });
+      return messagePrefixCache.get(key);
    }
 
 }

Modified: core/trunk/impl/src/main/java/org/jboss/weld/resources/ClassTransformer.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/resources/ClassTransformer.java	2010-01-12 14:06:01 UTC (rev 5447)
+++ core/trunk/impl/src/main/java/org/jboss/weld/resources/ClassTransformer.java	2010-01-12 16:55:05 UTC (rev 5448)
@@ -18,7 +18,7 @@
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
-import java.util.concurrent.Callable;
+import java.util.concurrent.ConcurrentMap;
 
 import javax.enterprise.inject.spi.AnnotatedType;
 
@@ -28,14 +28,116 @@
 import org.jboss.weld.introspector.jlr.WeldAnnotationImpl;
 import org.jboss.weld.introspector.jlr.WeldClassImpl;
 import org.jboss.weld.metadata.TypeStore;
-import org.jboss.weld.util.collections.ConcurrentCache;
 
+import com.google.common.base.Function;
+import com.google.common.collect.MapMaker;
+
 public class ClassTransformer implements Service
 {
+   
+   private static class TransformTypeToWeldClass implements Function<TypeHolder<?>, WeldClass<?>>
+   {
+      
+      private final ClassTransformer classTransformer;
 
-   private final ConcurrentCache<Type, WeldClass<?>> classes;
-   private final ConcurrentCache<AnnotatedType<?>, WeldClass<?>> annotatedTypes;
-   private final ConcurrentCache<Class<?>, WeldAnnotation<?>> annotations;
+      private TransformTypeToWeldClass(ClassTransformer classTransformer)
+      {
+         this.classTransformer = classTransformer;
+      }
+
+      public WeldClass<?> apply(TypeHolder<?> from)
+      {
+         return WeldClassImpl.of(from.getRawType(), from.getBaseType(), classTransformer);
+      }
+      
+   }
+   
+   private static class TransformClassToWeldAnnotation implements Function<Class<? extends Annotation>, WeldAnnotation<?>>
+   {
+      
+      private final ClassTransformer classTransformer;
+
+      private TransformClassToWeldAnnotation(ClassTransformer classTransformer)
+      {
+         this.classTransformer = classTransformer;
+      }
+
+      public WeldAnnotation<?> apply(Class<? extends Annotation> from)
+      {
+         return WeldAnnotationImpl.of(from, classTransformer);
+      }
+      
+   }
+   
+   private static class TransformAnnotatedTypeToWeldClass implements Function<AnnotatedType<?>, WeldClass<?>>
+   {
+      
+      private final ClassTransformer classTransformer;
+
+      private TransformAnnotatedTypeToWeldClass(ClassTransformer classTransformer)
+      {
+         super();
+         this.classTransformer = classTransformer;
+      }
+
+      public WeldClass<?> apply(AnnotatedType<?> from)
+      {
+         return WeldClassImpl.of(from, classTransformer);
+      }
+      
+   }
+   
+   private static final class TypeHolder<T>
+   {
+      private final Class<T> rawType;
+      private final Type baseType;
+      
+      private TypeHolder(Class<T> rawType, Type baseType)
+      {
+         this.rawType = rawType;
+         this.baseType = baseType;
+      }
+      
+      public Type getBaseType()
+      {
+         return baseType;
+      }
+      
+      public Class<T> getRawType()
+      {
+         return rawType;
+      }
+      
+      @Override
+      public boolean equals(Object obj)
+      {
+         if (obj instanceof TypeHolder<?>)
+         {
+            TypeHolder<?> that = (TypeHolder<?>) obj;
+            return this.getBaseType().equals(that.getBaseType());
+         }
+         else
+         {
+            return false;
+         }
+      }
+      
+      @Override
+      public int hashCode()
+      {
+         return getBaseType().hashCode();
+      }
+      
+      @Override
+      public String toString()
+      {
+         return getBaseType().toString();
+      }
+   }
+
+   private final ConcurrentMap<TypeHolder<?>, WeldClass<?>> classes;
+   private final ConcurrentMap<AnnotatedType<?>, WeldClass<?>> annotatedTypes;
+   private final ConcurrentMap<Class<? extends Annotation>, WeldAnnotation<?>> annotations;
    private final TypeStore typeStore;
 
    /**
@@ -43,62 +145,35 @@
     */
    public ClassTransformer(TypeStore typeStore)
    {
-      classes = new ConcurrentCache<Type, WeldClass<?>>();
-      this.annotatedTypes = new ConcurrentCache<AnnotatedType<?>, WeldClass<?>>();
-      annotations = new ConcurrentCache<Class<?>, WeldAnnotation<?>>();
+      MapMaker maker = new MapMaker();
+      this.classes = maker.makeComputingMap(new TransformTypeToWeldClass(this));
+      this.annotatedTypes = maker.makeComputingMap(new TransformAnnotatedTypeToWeldClass(this));
+      this.annotations = maker.makeComputingMap(new TransformClassToWeldAnnotation(this));
       this.typeStore = typeStore;
    }
 
+   @SuppressWarnings("unchecked")
    public <T> WeldClass<T> loadClass(final Class<T> rawType, final Type baseType)
    {
-      return classes.putIfAbsent(baseType, new Callable<WeldClass<T>>()
-      {
-
-         public WeldClass<T> call() throws Exception
-         {
-            return WeldClassImpl.of(rawType, baseType, ClassTransformer.this);
-         }
-
-      });
+      return (WeldClass<T>) classes.get(new TypeHolder<T>(rawType, baseType));
    }
    
+   @SuppressWarnings("unchecked")
    public <T> WeldClass<T> loadClass(final Class<T> clazz)
    {
-      return classes.putIfAbsent(clazz, new Callable<WeldClass<T>>()
-      {
-
-         public WeldClass<T> call() throws Exception
-         {
-            return WeldClassImpl.of(clazz, ClassTransformer.this);
-         }
-
-      });
+      return (WeldClass<T>) classes.get(new TypeHolder<T>(clazz, clazz));
    }
    
+   @SuppressWarnings("unchecked")
    public <T> WeldClass<T> loadClass(final AnnotatedType<T> clazz)
    {
-      return annotatedTypes.putIfAbsent(clazz, new Callable<WeldClass<T>>()
-      {
-
-         public WeldClass<T> call() throws Exception
-         {
-            return WeldClassImpl.of(clazz, ClassTransformer.this);
-         }
-
-      });
+      return (WeldClass<T>) annotatedTypes.get(clazz);
    }
 
+   @SuppressWarnings("unchecked")
    public <T extends Annotation> WeldAnnotation<T> loadAnnotation(final Class<T> clazz)
    {
-      return annotations.putIfAbsent(clazz, new Callable<WeldAnnotation<T>>()
-      {
-         
-         public WeldAnnotation<T> call() throws Exception
-         {
-            return WeldAnnotationImpl.of(clazz, ClassTransformer.this);
-         }
-
-      });
+      return (WeldAnnotation<T>) annotations.get(clazz);
    }
    
    public TypeStore getTypeStore()



More information about the weld-commits mailing list