https://issues.apache.org/jira/browse/EXTSCRIPT-156 fixing a bean annotation not movable issue
cleaning up code

git-svn-id: https://svn.apache.org/repos/asf/myfaces/extensions/scripting/trunk@1301389 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/core/common/util/ReflectUtil.java b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/core/common/util/ReflectUtil.java
index e61febd..fb7bce7 100644
--- a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/core/common/util/ReflectUtil.java
+++ b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/core/common/util/ReflectUtil.java
@@ -18,13 +18,9 @@
  */
 package org.apache.myfaces.extensions.scripting.core.common.util;
 
-
 import org.apache.myfaces.extensions.scripting.core.api.WeavingContext;
 
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
+import java.lang.reflect.*;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.logging.Level;
@@ -35,11 +31,13 @@
  * @version $Revision$ $Date$
  */
 
-public class ReflectUtil {
+public class ReflectUtil
+{
 
     static final Logger _logger = Logger.getLogger(ReflectUtil.class.getName());
 
-    public static Object instantiate(String clazz, Object... varargs) {
+    public static Object instantiate(String clazz, Object... varargs)
+    {
         return instantiate(ClassUtils.forName(clazz), varargs);
     }
 
@@ -50,28 +48,41 @@
      * @param varargs the instantiation parameters
      * @return the instantiated object
      */
-    public static Object instantiate(Class clazz, Object... varargs) {
+    public static Object instantiate(Class clazz, Object... varargs)
+    {
         Class[] classes = new Class[varargs.length];
-        for (int cnt = 0; cnt < varargs.length; cnt++) {
+        for (int cnt = 0; cnt < varargs.length; cnt++)
+        {
 
-            if (varargs[cnt] instanceof Cast) {
+            if (varargs[cnt] instanceof Cast)
+            {
                 classes[cnt] = ((Cast) varargs[cnt]).getClazz();
                 varargs[cnt] = ((Cast) varargs[cnt]).getValue();
-            } else {
+            } else
+            {
                 classes[cnt] = varargs[cnt].getClass();
             }
         }
 
-        try {
+        try
+        {
             Constructor constr = clazz.getConstructor(classes);
             return constr.newInstance(varargs);
-        } catch (NoSuchMethodException e) {
+        }
+        catch (NoSuchMethodException e)
+        {
             throw new RuntimeException(e);
-        } catch (InvocationTargetException e) {
+        }
+        catch (InvocationTargetException e)
+        {
             throw new RuntimeException(e);
-        } catch (IllegalAccessException e) {
+        }
+        catch (IllegalAccessException e)
+        {
             throw new RuntimeException(e);
-        } catch (InstantiationException e) {
+        }
+        catch (InstantiationException e)
+        {
             throw new RuntimeException(e);
         }
     }/*this is mostly just a helper to bypass a groovy bug in a more
@@ -80,7 +91,8 @@
    * fixes that
    * */
 
-    public static Object newObject(Class clazz) throws IllegalAccessException, InstantiationException {
+    public static Object newObject(Class clazz) throws IllegalAccessException, InstantiationException
+    {
         return clazz.newInstance();
     }
 
@@ -93,18 +105,21 @@
      * @param varargs    the arguments which have to be passed to the method
      * @return the return value of the method
      */
-    public static Object executeStaticMethod(Class obj, String methodName, Object... varargs) {
+    public static Object executeStaticMethod(Class obj, String methodName, Object... varargs)
+    {
 
         Collection<Method> methods = getMethods(obj, methodName, varargs.length);
 
         Object retVal = handleStaticMethod(obj, methodName, methods, varargs);
-        if (!methodNotFound(retVal)) {
+        if (!methodNotFound(retVal))
+        {
             return retVal;
         }
 
         methods = getAllMethods(obj, methodName, varargs.length);
         retVal = handleStaticMethod(obj, methodName, methods, varargs);
-        if (!methodNotFound(retVal)) {
+        if (!methodNotFound(retVal))
+        {
             return retVal;
         }
 
@@ -112,11 +127,69 @@
 
     }
 
-    public static Collection<Method> getAllMethods(Class clazz, String methodName, int varargLength) {
+    public static void setField(Object obj, String fieldName, Object value, boolean protectedField)
+    {
+        try
+        {
+            Field f = null;
+            try
+            {
+                f = obj.getClass().getDeclaredField(fieldName);
+            }
+            catch (NoSuchFieldException e)
+            {
+                f = obj.getClass().getField(fieldName);
+            }
+
+            f.setAccessible(protectedField);
+            f.set(obj, value);
+
+        }
+        catch (NoSuchFieldException e)
+        {
+            throw new RuntimeException(e);
+        }
+        catch (IllegalAccessException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static Object getField(Object obj, String fieldName, boolean protectedField)
+    {
+        try
+        {
+            Field f = null;
+            try
+            {
+                f = obj.getClass().getDeclaredField(fieldName);
+            }
+            catch (NoSuchFieldException e)
+            {
+                f = obj.getClass().getField(fieldName);
+            }
+            f.setAccessible(protectedField);
+            return f.get(obj);
+        }
+        catch (NoSuchFieldException e)
+        {
+            throw new RuntimeException(e);
+        }
+        catch (IllegalAccessException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static Collection<Method> getAllMethods(Class clazz, String methodName, int varargLength)
+    {
         ArrayList<Method> retVal = new ArrayList<Method>(30);
-        while (clazz != null) {
-            for (Method m : clazz.getDeclaredMethods()) {
-                if (m.getParameterTypes().length == varargLength && m.getName().equals(methodName)) {
+        while (clazz != null)
+        {
+            for (Method m : clazz.getDeclaredMethods())
+            {
+                if (m.getParameterTypes().length == varargLength && m.getName().equals(methodName))
+                {
                     retVal.add(m);
                 }
             }
@@ -126,10 +199,13 @@
         return retVal;
     }
 
-    public static Collection<Method> getMethods(Class clazz, String methodName, int varargLength) {
+    public static Collection<Method> getMethods(Class clazz, String methodName, int varargLength)
+    {
         ArrayList<Method> retVal = new ArrayList<Method>(30);
-        for (Method m : clazz.getDeclaredMethods()) {
-            if (m.getParameterTypes().length == varargLength && m.getName().equals(methodName)) {
+        for (Method m : clazz.getDeclaredMethods())
+        {
+            if (m.getParameterTypes().length == varargLength && m.getName().equals(methodName))
+            {
                 retVal.add(m);
             }
         }
@@ -150,7 +226,8 @@
      *                          language execution where failures can happen but method executions
      *                          should not enforce exception handling
      */
-    public static Object executeMethod(Object obj, String methodName, Object... varargs) {
+    public static Object executeMethod(Object obj, String methodName, Object... varargs)
+    {
 
         Collection<Method> methods;
         //if we have an invocationHandler here we
@@ -158,7 +235,8 @@
         //That way we can cover more dynamic stuff
         //our reload invocation handler is treated differently here
 
-        if (obj instanceof InvocationHandler) {
+        if (obj instanceof InvocationHandler)
+        {
             InvocationHandler objToInvoke = (InvocationHandler) obj;
 
             Object realTarget = WeavingContext.getInstance().getDelegateFromProxy(objToInvoke);
@@ -167,13 +245,15 @@
             //to be accessed
             methods = getMethods(realTarget.getClass(), methodName, varargs.length);
             Object retVal = handleInvHandlerMethod(objToInvoke, methodName, methods, varargs);
-            if (!methodNotFound(retVal)) {
+            if (!methodNotFound(retVal))
+            {
                 return retVal;
             }
             //if not we try all of them until we have a match
             methods = getAllMethods(realTarget.getClass(), methodName, varargs.length);
             retVal = handleInvHandlerMethod(objToInvoke, methodName, methods, varargs);
-            if (!(methodNotFound(retVal))) {
+            if (!(methodNotFound(retVal)))
+            {
                 return retVal;
             }
 
@@ -186,14 +266,16 @@
         //to be accessed
         methods = getMethods(clazz, methodName, varargs.length);
         Object retVal = handleObjMethod(obj, methodName, methods, varargs);
-        if (!methodNotFound(retVal)) {
+        if (!methodNotFound(retVal))
+        {
             return retVal;
         }
 
         //if not we try all of them until we have a match
         methods = getAllMethods(clazz, methodName, varargs.length);
         retVal = handleObjMethod(obj, methodName, methods, varargs);
-        if (!methodNotFound(retVal)) {
+        if (!methodNotFound(retVal))
+        {
             return retVal;
         }
 
@@ -204,7 +286,8 @@
      * special marker class which is a special return value indicating
      * that not method has been found which can be executed
      */
-    static class _MethodNotFound {
+    static class _MethodNotFound
+    {
     }
 
     /**
@@ -214,7 +297,8 @@
      * @param retVal the retVal which has to be investigated
      * @return true if the retVal is instance of _MethodNotFound false otherwise
      */
-    private static boolean methodNotFound(Object retVal) {
+    private static boolean methodNotFound(Object retVal)
+    {
         return retVal instanceof _MethodNotFound;
     }
 
@@ -228,14 +312,20 @@
      * @param varargs     the list of varargs to be passed to the method
      * @return the result of the invocation, or an object of type _MethodNotFound otherwise
      */
-    static private Object handleInvHandlerMethod(InvocationHandler objToInvoke, String methodName, Collection<Method> methods, Object... varargs) {
-        for (Method m : methods) {
-            if (!m.getName().equals(methodName) || m.getParameterTypes().length != varargs.length) {
+    static private Object handleInvHandlerMethod(InvocationHandler objToInvoke, String methodName, Collection<Method> methods, Object... varargs)
+    {
+        for (Method m : methods)
+        {
+            if (!m.getName().equals(methodName) || m.getParameterTypes().length != varargs.length)
+            {
                 continue;
             }
-            try {
+            try
+            {
                 return objToInvoke.invoke(objToInvoke, m, varargs);
-            } catch (Throwable e) {
+            }
+            catch (Throwable e)
+            {
                 handleException(e);
             }
         }
@@ -251,14 +341,20 @@
      * @param varargs     the list of varargs to be passed to the method
      * @return the result of the invocation, or an object of type _MethodNotFound otherwise
      */
-    static private Object handleObjMethod(Object objToInvoke, String methodName, Collection<Method> methods, Object... varargs) {
-        for (Method m : methods) {
-            if (!m.getName().equals(methodName) || m.getParameterTypes().length != varargs.length) {
+    static private Object handleObjMethod(Object objToInvoke, String methodName, Collection<Method> methods, Object... varargs)
+    {
+        for (Method m : methods)
+        {
+            if (!m.getName().equals(methodName) || m.getParameterTypes().length != varargs.length)
+            {
                 continue;
             }
-            try {
+            try
+            {
                 return m.invoke(objToInvoke, varargs);
-            } catch (Throwable e) {
+            }
+            catch (Throwable e)
+            {
                 handleException(e);
             }
         }
@@ -274,30 +370,42 @@
      * @param varargs     the list of varargs to be passed to the method
      * @return the result of the invocation, or an object of type _MethodNotFound otherwise
      */
-    static private Object handleStaticMethod(Class objToInvoke, String methodName, Collection<Method> methods, Object... varargs) {
-        for (Method m : methods) {
-            if (!m.getName().equals(methodName) || m.getParameterTypes().length != varargs.length) {
+    static private Object handleStaticMethod(Class objToInvoke, String methodName, Collection<Method> methods, Object... varargs)
+    {
+        for (Method m : methods)
+        {
+            if (!m.getName().equals(methodName) || m.getParameterTypes().length != varargs.length)
+            {
                 continue;
             }
-            try {
+            try
+            {
                 return m.invoke(objToInvoke, varargs);
-            } catch (Throwable e) {
+            }
+            catch (Throwable e)
+            {
                 handleException(e);
             }
         }
         return new _MethodNotFound();
     }
 
-    private static void handleException(Throwable e) {
-        if (e instanceof IllegalAccessException) {
-            if (_logger.isLoggable(Level.FINEST)) {
+    private static void handleException(Throwable e)
+    {
+        if (e instanceof IllegalAccessException)
+        {
+            if (_logger.isLoggable(Level.FINEST))
+            {
                 _logger.log(Level.FINEST, "", e);
             }
-        } else if (e instanceof IllegalArgumentException) {
-            if (_logger.isLoggable(Level.FINEST)) {
+        } else if (e instanceof IllegalArgumentException)
+        {
+            if (_logger.isLoggable(Level.FINEST))
+            {
                 _logger.log(Level.FINEST, "", e);
             }
-        } else {
+        } else
+        {
             throw new RuntimeException(e);
         }
     }
@@ -313,26 +421,37 @@
      * @return the result object for the Method(method) call
      * @throws RuntimeException an unmanaged runtime exception in case of an introspection error
      */
-    public static Object fastExecuteMethod(Object obj, String methodName, Object... varargs) {
+    public static Object fastExecuteMethod(Object obj, String methodName, Object... varargs)
+    {
         Class[] classes = new Class[varargs.length];
-        for (int cnt = 0; cnt < varargs.length; cnt++) {
+        for (int cnt = 0; cnt < varargs.length; cnt++)
+        {
 
-            if (varargs[cnt] instanceof Cast) {
+            if (varargs[cnt] instanceof Cast)
+            {
                 classes[cnt] = ((Cast) varargs[cnt]).getClazz();
                 varargs[cnt] = ((Cast) varargs[cnt]).getValue();
-            } else {
+            } else
+            {
                 classes[cnt] = varargs[cnt].getClass();
             }
         }
 
-        try {
+        try
+        {
             Method m = fastGetMethod(obj, methodName, classes);
             return m.invoke(obj, varargs);
-        } catch (NoSuchMethodException e) {
+        }
+        catch (NoSuchMethodException e)
+        {
             throw new RuntimeException(e);
-        } catch (InvocationTargetException e) {
+        }
+        catch (InvocationTargetException e)
+        {
             throw new RuntimeException(e);
-        } catch (IllegalAccessException e) {
+        }
+        catch (IllegalAccessException e)
+        {
             throw new RuntimeException(e);
         }
 
@@ -351,11 +470,15 @@
      * @return the method if found
      * @throws NoSuchMethodException in case it could not be found
      */
-    public static Method fastGetMethod(Object obj, String methodName, Class[] classes) throws NoSuchMethodException {
+    public static Method fastGetMethod(Object obj, String methodName, Class[] classes) throws NoSuchMethodException
+    {
         Method m;
-        try {
+        try
+        {
             m = obj.getClass().getDeclaredMethod(methodName, classes);
-        } catch (NoSuchMethodException e) {
+        }
+        catch (NoSuchMethodException e)
+        {
             m = obj.getClass().getMethod(methodName, classes);
         }
         return m;
@@ -372,36 +495,51 @@
      * @return the result object for the Method(method) call
      * @throws RuntimeException an unmanaged runtime exception in case of an introspection error
      */
-    public static Object fastExecuteStaticMethod(Class obj, String methodName, Object... varargs) {
+    public static Object fastExecuteStaticMethod(Class obj, String methodName, Object... varargs)
+    {
         Class[] classes = new Class[varargs.length];
-        for (int cnt = 0; cnt < varargs.length; cnt++) {
+        for (int cnt = 0; cnt < varargs.length; cnt++)
+        {
 
-            if (varargs[cnt] instanceof Cast) {
+            if (varargs[cnt] instanceof Cast)
+            {
                 classes[cnt] = ((Cast) varargs[cnt]).getClazz();
                 varargs[cnt] = ((Cast) varargs[cnt]).getValue();
-            } else {
+            } else
+            {
                 classes[cnt] = varargs[cnt].getClass();
             }
         }
 
-        try {
+        try
+        {
             Method m = fastGetStaticMethod(obj, methodName, classes);
             return m.invoke(obj, varargs);
-        } catch (NoSuchMethodException e) {
+        }
+        catch (NoSuchMethodException e)
+        {
             throw new RuntimeException(e);
-        } catch (InvocationTargetException e) {
+        }
+        catch (InvocationTargetException e)
+        {
             throw new RuntimeException(e);
-        } catch (IllegalAccessException e) {
+        }
+        catch (IllegalAccessException e)
+        {
             throw new RuntimeException(e);
         }
 
     }
 
-    public static Method fastGetStaticMethod(Class obj, String methodName, Class[] classes) throws NoSuchMethodException {
+    public static Method fastGetStaticMethod(Class obj, String methodName, Class[] classes) throws NoSuchMethodException
+    {
         Method m;
-        try {
+        try
+        {
             m = obj.getDeclaredMethod(methodName, classes);
-        } catch (NoSuchMethodException e) {
+        }
+        catch (NoSuchMethodException e)
+        {
             m = obj.getMethod(methodName, classes);
         }
         return m;
@@ -415,7 +553,8 @@
      * @param value the value object to be used as param
      * @return a Cast object of the parameters
      */
-    public static Cast cast(Class clazz, Object value) {
+    public static Cast cast(Class clazz, Object value)
+    {
         return new Cast(clazz, value);
     }
 
@@ -426,7 +565,8 @@
      * @param clazz the cast target for the method call
      * @return a null value Cast object of the parameters
      */
-    public static Null nullCast(Class clazz) {
+    public static Null nullCast(Class clazz)
+    {
         return new Null(clazz);
     }
 }
diff --git a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/adapters/handlers/MyFacesBeanHandler.java b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/adapters/handlers/MyFacesBeanHandler.java
index c9136c2..0116362 100644
--- a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/adapters/handlers/MyFacesBeanHandler.java
+++ b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/adapters/handlers/MyFacesBeanHandler.java
@@ -23,7 +23,6 @@
  * @author Werner Punz (latest modification by $Author$)
  * @version $Revision$ $Date$
  *
- * TODO refactor this out
  */
 
 import org.apache.myfaces.config.RuntimeConfig;
@@ -276,6 +275,12 @@
         return WeavingContext.getInstance().isDynamic(managedBeanClass) && tainted.contains(managedBeanClass.getName());
     }
 
-
+    public  void registerManagedBean(Class clazz, String beanName) {
+        //TODO move this over from the beanimplementationListener
+    }
+    public  void removeManagedBean(String className) {
+        //TODO move this over from the beanimplementationlistener
+    }
+        
 }
 
diff --git a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/annotation/BeanImplementationListener.java b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/annotation/BeanImplementationListener.java
index 0f2d99f..ac5811e 100644
--- a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/annotation/BeanImplementationListener.java
+++ b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/annotation/BeanImplementationListener.java
@@ -52,7 +52,6 @@
 
     public void register(Class clazz, java.lang.annotation.Annotation ann) {
 
-        RuntimeConfig config = getRuntimeConfig();
 
         javax.faces.bean.ManagedBean annCasted = (javax.faces.bean.ManagedBean) ann;
 
@@ -64,6 +63,19 @@
         beanName = beanName.replaceAll("\"", "");
         //we need to reregister for every bean due to possible managed prop
         //and scope changes
+        registerManagedBean(clazz, beanName);
+
+    }
+
+    /**
+     * registers a new managed bean into runtime config
+     * @param clazz
+     * @param beanName
+     */
+    protected void registerManagedBean(Class clazz, String beanName)
+    {
+        RuntimeConfig config = getRuntimeConfig();
+
         ManagedBean mbean;
         if (!hasToReregister(beanName, clazz)) {
             mbean = (ManagedBean) _alreadyRegistered.get(beanName);
@@ -73,13 +85,14 @@
         }
 
         mbean.setBeanClass(clazz.getName());
+
+        ReflectUtil.setField(mbean, "beanClass", null, true);
         mbean.setName(beanName);
         handleManagedpropertiesCompiled(mbean, fields(clazz));
         resolveScope(clazz, mbean);
 
         _alreadyRegistered.put(beanName, mbean);
         config.addManagedBean(beanName, mbean);
-
     }
 
     private void resolveScope(Class clazz, ManagedBean mbean) {
@@ -102,7 +115,7 @@
         mbean.setScope(scope);
     }
 
-    private void handleManagedpropertiesCompiled(ManagedBean mbean, Field[] fields) {
+    protected void handleManagedpropertiesCompiled(ManagedBean mbean, Field[] fields) {
         /*since we reprocess the managed properties we can handle them here by clearing them first*/
         mbean.getManagedProperties().clear();
         for (Field field : fields) {
diff --git a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/implementations/RenderkitProxy.java b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/implementations/RenderkitProxy.java
index 494df5d..f8420f4 100644
--- a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/implementations/RenderkitProxy.java
+++ b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/dynamicDecorators/implementations/RenderkitProxy.java
@@ -21,6 +21,7 @@
 import org.apache.myfaces.extensions.scripting.core.api.Decorated;
 import org.apache.myfaces.extensions.scripting.core.api.ScriptingConst;
 import org.apache.myfaces.extensions.scripting.core.api.WeavingContext;
+import org.apache.myfaces.extensions.scripting.core.common.util.ReflectUtil;
 
 import javax.faces.context.ResponseStream;
 import javax.faces.context.ResponseWriter;
@@ -76,24 +77,19 @@
             String name = proxiedObject.getClass().getName();
             while (name.contains("ExtVal") && (name.contains("Wrapper") || name.contains("Proxy")))
             {
-                Field proxiedField = proxiedObject.getClass().getDeclaredField("wrapped");
-                proxiedField.setAccessible(true);
-                proxiedObject = (Renderer) proxiedField.get(proxiedObject);
-
+                proxiedObject = (Renderer)  ReflectUtil.getField(proxiedObject,
+                        "wrapped", true);
                 name = proxiedObject.getClass().getName();
             }
 
             // "getWrapped");
         }
-        catch (IllegalAccessException e)
+        catch (RuntimeException e)
         {
             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
         }
-        catch (NoSuchFieldException e)
-        {
-            e.printStackTrace();  //To change body of catch statement use File | Settings | File
-            // Templates.
-        }
+
+
         return proxiedObject;
     }
 
@@ -107,23 +103,16 @@
                String name = proxy.getClass().getName();
                while (name.contains("ExtVal") && (name.contains("Wrapper") || name.contains("Proxy")))
                {
-                   Field proxiedField = proxy.getClass().getDeclaredField("wrapped");
-                   proxiedField.setAccessible(true);
                    oldProxiedObject = proxy;
-                   proxy = (Renderer) proxiedField.get(proxy);
+                   proxy = (Renderer) ReflectUtil.getField(proxy,"wrapped", true); //(Renderer) proxiedField.get(proxy);
                    name = proxy.getClass().getName();
                    if(!name.contains("ExtVal") && !(name.contains("Wrapper") || name.contains("Proxy"))) {
-                       proxiedField.set(oldProxiedObject, proxy);
+                       ReflectUtil.setField(oldProxiedObject,"wrapped", proxy, true);
                        return proxy;
                    }
-
                }
            }
-           catch (IllegalAccessException e)
-           {
-               e.printStackTrace();
-           }
-           catch (NoSuchFieldException e)
+           catch (RuntimeException e)
            {
                e.printStackTrace();
            }
diff --git a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/startup/RefreshPhaseListener.java b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/startup/RefreshPhaseListener.java
index 5ab9b57..66232eb 100644
--- a/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/startup/RefreshPhaseListener.java
+++ b/extscript-core-root/extscript-core/src/main/java/org/apache/myfaces/extensions/scripting/jsf/startup/RefreshPhaseListener.java
@@ -50,8 +50,8 @@
         Map<Object, Object> params = context.getAttributes();
         if(params.containsKey("ANN_PROCESSED")) return;
         else params.put("ANN_PROCESSED", Boolean.TRUE);
-        WeavingContext.getInstance().annotationScan();
         WeavingContext.getInstance().getImplementationSPI().refreshManagedBeans();
+        WeavingContext.getInstance().annotationScan();
     }
 
     @Override
diff --git a/extscript-examples/myfaces20-example/src/main/webapp/WEB-INF/groovy/org/apache/myfaces/groovyloader/blog/BlogService.groovy b/extscript-examples/myfaces20-example/src/main/webapp/WEB-INF/groovy/org/apache/myfaces/groovyloader/blog/BlogService.groovy
index 97f1d9d..f164bd6 100644
--- a/extscript-examples/myfaces20-example/src/main/webapp/WEB-INF/groovy/org/apache/myfaces/groovyloader/blog/BlogService.groovy
+++ b/extscript-examples/myfaces20-example/src/main/webapp/WEB-INF/groovy/org/apache/myfaces/groovyloader/blog/BlogService.groovy
@@ -23,9 +23,6 @@
 import javax.faces.bean.ManagedBean
 
 
-
-@ManagedBean(name = "blogService")
-@ApplicationScoped
 public class BlogService {
   List blogEntries = new ArrayList()
 
diff --git a/extscript-examples/myfaces20-example/src/main/webapp/WEB-INF/groovy/org/apache/myfaces/groovyloader/blog/BlogService2.groovy b/extscript-examples/myfaces20-example/src/main/webapp/WEB-INF/groovy/org/apache/myfaces/groovyloader/blog/BlogService2.groovy
index f1d7920..a7d59fc 100644
--- a/extscript-examples/myfaces20-example/src/main/webapp/WEB-INF/groovy/org/apache/myfaces/groovyloader/blog/BlogService2.groovy
+++ b/extscript-examples/myfaces20-example/src/main/webapp/WEB-INF/groovy/org/apache/myfaces/groovyloader/blog/BlogService2.groovy
@@ -24,6 +24,8 @@
 
 
 
+@ManagedBean(name = "blogService")
+@ApplicationScoped
 public class BlogService2 {
   List blogEntries = new ArrayList()