Author: martinkoci
Date: Tue Apr  3 19:37:56 2012
New Revision: 1309109

URL: http://svn.apache.org/viewvc?rev=1309109&view=rev
Log:
OWB-647 [PERF] Avoid unnecessary StringBuilder instances - improve 
checkNullInstance, checkScopeType, ... methods

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/util/
    
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/util/WebBeansUtilTest.java
Modified:
    
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ProducerFieldBean.java
    
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ProducerMethodBean.java
    
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java

Modified: 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ProducerFieldBean.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ProducerFieldBean.java?rev=1309109&r1=1309108&r2=1309109&view=diff
==============================================================================
--- 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ProducerFieldBean.java
 (original)
+++ 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ProducerFieldBean.java
 Tue Apr  3 19:37:56 2012
@@ -161,11 +161,11 @@ public class ProducerFieldBean<T> extend
      */
     protected void checkNullInstance(Object instance)
     {
-        String errorMessage = "WebBeans producer field : " + 
producerField.getName() +
-                              " return type in the component implementation 
class : " +
-                ownerComponent.getReturnType().getName() +
+        String errorMessage = "WebBeans producer field : %s" +
+                              " return type in the component implementation 
class : %s" +
                               " scope must be @Dependent to create null 
instance";
-        WebBeansUtil.checkNullInstance(instance, getScope(), errorMessage);
+        WebBeansUtil.checkNullInstance(instance, getScope(), errorMessage, 
producerField.getName(),
+                ownerComponent.getReturnType().getName());
     }
 
     /**
@@ -173,12 +173,13 @@ public class ProducerFieldBean<T> extend
      */
     protected void checkScopeType()
     {
-        String errorMessage = "WebBeans producer method : " + 
producerField.getName() +
-                              " return type in the component implementation 
class : " +
-                ownerComponent.getReturnType().getName() + " with passivating 
scope @" +
-                getScope().getName() + " must be Serializable";
+        String errorMessage = "WebBeans producer method : %s" + 
+                              " return type in the component implementation 
class : %s" +
+                              " with passivating scope @%s" +
+                              " must be Serializable";
         
getWebBeansContext().getWebBeansUtil().checkSerializableScopeType(getScope(),
-                isSerializable(), errorMessage);
+                isSerializable(), errorMessage, producerField.getName(), 
+                ownerComponent.getReturnType().getName(), 
getScope().getName());
     }
     
     @Override

Modified: 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ProducerMethodBean.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ProducerMethodBean.java?rev=1309109&r1=1309108&r2=1309109&view=diff
==============================================================================
--- 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ProducerMethodBean.java
 (original)
+++ 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ProducerMethodBean.java
 Tue Apr  3 19:37:56 2012
@@ -369,11 +369,11 @@ public class ProducerMethodBean<T> exten
      */
     protected void checkNullInstance(Object instance)
     {
-        String errorMessage = "WebBeans producer method : " + 
creatorMethod.getName() +
-                              " return type in the component implementation 
class : " +
-                ownerComponent.getReturnType().getName() +
+        String errorMessage = "WebBeans producer method : %s" +
+                              " return type in the component implementation 
class : %s" +
                               " scope type must be @Dependent to create null 
instance";
-        WebBeansUtil.checkNullInstance(instance, getScope(), errorMessage);
+        WebBeansUtil.checkNullInstance(instance, getScope(), errorMessage, 
creatorMethod.getName(),
+                ownerComponent.getReturnType().getName());
     }
 
     /**
@@ -381,12 +381,13 @@ public class ProducerMethodBean<T> exten
      */
     protected void checkScopeType()
     {
-        String errorMessage = "WebBeans producer method : " + 
creatorMethod.getName() +
-                              " return type in the component implementation 
class : " +
-                ownerComponent.getReturnType().getName() + " with passivating 
scope @" +
-                getScope().getName() + " must be Serializable";
+        String errorMessage = "WebBeans producer method : %s" +
+                              " return type in the component implementation 
class : %s" +
+                              " with passivating scope @%s" +
+                              " must be Serializable";
         
getWebBeansContext().getWebBeansUtil().checkSerializableScopeType(getScope(),
-                isSerializable(), errorMessage);
+                isSerializable(), errorMessage, creatorMethod.getName(), 
ownerComponent.getReturnType().getName(),
+                getScope().getName());
 
     }
     

Modified: 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java?rev=1309109&r1=1309108&r2=1309109&view=diff
==============================================================================
--- 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
 (original)
+++ 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
 Tue Apr  3 19:37:56 2012
@@ -40,6 +40,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
+
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.decorator.Decorator;
@@ -159,6 +160,8 @@ import org.apache.webbeans.portable.even
 import org.apache.webbeans.portable.events.generics.GProcessSessionBean;
 import org.apache.webbeans.spi.plugins.OpenWebBeansEjbPlugin;
 import org.apache.webbeans.spi.plugins.OpenWebBeansPlugin;
+
+
 import static 
org.apache.webbeans.util.InjectionExceptionUtils.throwUnproxyableResolutionException;
 
 /**
@@ -270,13 +273,16 @@ public final class WebBeansUtil
             throw new IllegalArgumentException("Bean must be Producer Field or 
Method Bean instance : " + bean);
         }
 
-        String message = "Producer Field/Method Bean with name : " + 
member.getName()
-                         + " in bean class : " + 
member.getDeclaringClass().getName();
+        String messageTemplate = "Producer Field/Method Bean with name : %s" + 
+                         " in bean class : %s"; 
 
-        if(checkGenericForProducers(type, message))
+        String memberName = member.getName();
+        String declaringClassName = member.getDeclaringClass().getName();
+        if(checkGenericForProducers(type, messageTemplate, memberName, 
declaringClassName))
         {
             if(!bean.getScope().equals(Dependent.class))
             {
+                String message = format(messageTemplate, memberName, 
declaringClassName);
                 throw new WebBeansConfigurationException(message + " scope 
must bee @Dependent");
             }
         }
@@ -285,16 +291,17 @@ public final class WebBeansUtil
     /**
      * Check generic types for producer method and fields.
      * @param type generic return type
-     * @param message error message
+     * @param messageTemplate error message
      * @return true if parametrized type argument is TypeVariable
      */
     //Helper method
-    private static boolean checkGenericForProducers(Type type, String message)
+    private static boolean checkGenericForProducers(Type type, String 
messageTemplate, Object... errorMessageArgs)
     {
         boolean result = false;
 
         if(type instanceof TypeVariable)
         {
+            String message = format(messageTemplate, errorMessageArgs);
             throw new WebBeansConfigurationException(message + " return type 
can not be type variable");
         }
 
@@ -304,6 +311,7 @@ public final class WebBeansUtil
 
             if(actualTypes.length == 0)
             {
+                String message = format(messageTemplate, errorMessageArgs);
                 throw new WebBeansConfigurationException(message +
                         " return type must define actual type arguments or 
type variable");
             }
@@ -312,6 +320,7 @@ public final class WebBeansUtil
             {
                 if(ClassUtil.isWildCardType(actualType))
                 {
+                    String message = format(messageTemplate, errorMessageArgs);
                     throw new WebBeansConfigurationException(message +
                             " return type can not define wildcard actual type 
argument");
                 }
@@ -2052,27 +2061,29 @@ public final class WebBeansUtil
      */
     private static Map<Class<? extends Annotation>, Boolean> 
isScopeTypeNormalCache =
             new ConcurrentHashMap<Class<? extends Annotation>, Boolean>();
-
-
-    public static void checkNullInstance(Object instance,Class<?> scopeType, 
String errorMessage)
+    
+    public static void checkNullInstance(Object instance, Class<? > scopeType, 
String errorMessage, 
+            Object... errorMessageArgs)
     {
         if (instance == null)
         {
             if (!scopeType.equals(Dependent.class))
             {
-                throw new IllegalProductException(errorMessage);
+                String message = format(errorMessage, errorMessageArgs);
+                throw new IllegalProductException(message);
             }
         }
-
     }
 
-    public void checkSerializableScopeType(Class<? extends Annotation> 
scopeType, boolean isSerializable, String errorMessage)
+    public void checkSerializableScopeType(Class<? extends Annotation> 
scopeType, boolean isSerializable, String errorMessage,
+            Object... errorMessageArgs)
     {
         if (webBeansContext.getBeanManagerImpl().isPassivatingScope(scopeType))
         {
             if (!isSerializable)
             {
-                throw new IllegalProductException(errorMessage);
+                String message = format(errorMessage, errorMessageArgs);
+                throw new IllegalProductException(message);
             }
         }
     }
@@ -3122,4 +3133,59 @@ public final class WebBeansUtil
 
         }
     }
+
+    // Note: following code for method 'format' is taken from google guava - 
apache 2.0 licenced library
+    // com.google.common.base.Preconditions.format(String, Object...)
+    /**
+     * Substitutes each {@code %s} in {@code template} with an argument. These
+     * are matched by position - the first {@code %s} gets {@code args[0]}, 
etc.
+     * If there are more arguments than placeholders, the unmatched arguments 
will
+     * be appended to the end of the formatted message in square braces.
+     *
+     * @param template a non-null string containing 0 or more {@code %s}
+     *     placeholders.
+     * @param args the arguments to be substituted into the message
+     *     template. Arguments are converted to strings using
+     *     {@link String#valueOf(Object)}. Arguments can be null.
+     */
+    private static String format(String template,
+            Object... args)
+    {
+        template = String.valueOf(template); // null -> "null"
+
+        // start substituting the arguments into the '%s' placeholders
+        StringBuilder builder = new StringBuilder(
+                template.length() + 16 * args.length);
+        int templateStart = 0;
+        int i = 0;
+        while (i < args.length)
+        {
+            int placeholderStart = template.indexOf("%s", templateStart);
+            if (placeholderStart == -1)
+            {
+                break;
+            }
+            builder.append(template.substring(templateStart, 
placeholderStart));
+            builder.append(args[i++]);
+            templateStart = placeholderStart + 2;
+        }
+        builder.append(template.substring(templateStart));
+
+        // if we run out of placeholders, append the extra args in square 
braces
+        if (i < args.length)
+        {
+            builder.append(" [");
+            builder.append(args[i++]);
+            while (i < args.length)
+            {
+                builder.append(", ");
+                builder.append(args[i++]);
+            }
+            builder.append(']');
+        }
+
+        return builder.toString();
+    }
+
+
 }

Added: 
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/util/WebBeansUtilTest.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/util/WebBeansUtilTest.java?rev=1309109&view=auto
==============================================================================
--- 
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/util/WebBeansUtilTest.java
 (added)
+++ 
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/util/WebBeansUtilTest.java
 Tue Apr  3 19:37:56 2012
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.util;
+
+import static org.junit.Assert.assertEquals;
+
+import javax.enterprise.context.SessionScoped;
+import javax.enterprise.inject.IllegalProductException;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+
+
+public class WebBeansUtilTest
+{
+
+    @Test
+    public void testCheckNullInstance()
+    {
+        String errorMessage = "WebBeans producer method : %s" +
+                " return type in the component implementation class : %s" +
+                " scope type must be @Dependent to create null instance";
+        
+        try
+        {
+            WebBeansUtil.checkNullInstance(null, SessionScoped.class, 
errorMessage, "aMethodName",
+                    String.class);
+        } catch (IllegalProductException e)
+        {
+            String message = e.getMessage();
+            assertEquals("WebBeans producer method : aMethodName" +
+                " return type in the component implementation class : class 
java.lang.String" +
+                " scope type must be @Dependent to create null instance", 
message);
+            return;
+        }
+        
+        Assert.fail();
+    }
+
+}


Reply via email to