You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by we...@apache.org on 2009/09/15 10:29:40 UTC
svn commit: r815215 - in /myfaces/extensions/scripting/trunk:
core/core-java6/src/main/java/org/apache/myfaces/scripting/loaders/java/jsr199/
core/core/src/main/java/org/apache/myfaces/scripting/core/util/
core/core/src/test/java/ core/core/src/test/ja...
Author: werpu
Date: Tue Sep 15 08:29:39 2009
New Revision: 815215
URL: http://svn.apache.org/viewvc?rev=815215&view=rev
Log:
reworking of the reflect utils to get the casting out, for normal usecases
Added:
myfaces/extensions/scripting/trunk/core/core/src/test/java/
myfaces/extensions/scripting/trunk/core/core/src/test/java/org/
myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/
myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/
myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/
myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/
myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/
myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/ReflectUtilTest.java (with props)
myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/TestProbe.java (with props)
Removed:
myfaces/extensions/scripting/trunk/core/core-java6/src/main/java/org/apache/myfaces/scripting/loaders/java/jsr199/ReflectCompilerFacade.java
Modified:
myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/core/util/ReflectUtil.java
myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java
Modified: myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/core/util/ReflectUtil.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/core/util/ReflectUtil.java?rev=815215&r1=815214&r2=815215&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/core/util/ReflectUtil.java (original)
+++ myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/scripting/core/util/ReflectUtil.java Tue Sep 15 08:29:39 2009
@@ -62,44 +62,94 @@
* exeption wrongly, delegating the instantiation code to java
* fixes that
* */
+
public static Object newObject(Class clazz) throws IllegalAccessException, InstantiationException {
return clazz.newInstance();
}
- /**
- * executes a method
- *
- * @param obj the target object
- * @param methodName the method name
- * @param varargs a list of objects casts or nulls defining the parameter classes and its values
- * if something occurs on introspection level an unmanaged exception is throw, just like
- * it would happen in a scripting class
- */
- public static void executeMethod(Object obj, String methodName, Object... varargs) {
- Class[] classes = new Class[varargs.length];
- for (int cnt = 0; cnt < varargs.length; cnt++) {
+ public static Object executeStaticMethod(Class obj, String methodName, Object... varargs) {
- if (varargs[cnt] instanceof Cast) {
- classes[cnt] = ((Cast) varargs[cnt]).getClazz();
- varargs[cnt] = ((Cast) varargs[cnt]).getValue();
- } else {
- classes[cnt] = varargs[cnt].getClass();
+ Method[] methods = obj.getDeclaredMethods();
+
+ for (Method m : methods) {
+
+ if (!m.getName().equals(methodName) || m.getParameterTypes().length != varargs.length) {
+ continue;
+ }
+ try {
+ return m.invoke(obj, varargs);
+ } catch (IllegalAccessException e) {
+ //expected, we do it that way for speed reasons
+ //because it is faster on most cases than looping over the param types
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
}
}
- try {
- Method m = getMethod(obj, methodName, classes);
- m.invoke(obj, varargs);
- } catch (NoSuchMethodException e) {
- throw new RuntimeException(e);
- } catch (InvocationTargetException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
+ methods = obj.getMethods();
+ for (Method m : methods) {
+ if (!m.getName().equals(methodName) || m.getParameterTypes().length != varargs.length) {
+ continue;
+ }
+ try {
+ return m.invoke(obj, varargs);
+ } catch (IllegalAccessException e) {
+ //expected, we do it that way for speed reasons
+ //because it is faster on most cases than looping over the param types
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ throw new RuntimeException("Static Method of :" + methodName + " from class " + obj.getClass().getName() + " not found");
+
+ }
+
+
+
+
+
+
+
+ public static Object executeMethod(Object obj, String methodName, Object... varargs) {
+ Class clazz = obj.getClass();
+ Method[] methods = clazz.getDeclaredMethods();
+ for (Method m : methods) {
+ if (!m.getName().equals(methodName) || m.getParameterTypes().length != varargs.length) {
+ continue;
+ }
+ try {
+ return m.invoke(obj, varargs);
+ } catch (IllegalAccessException e) {
+ //expected, we do it that way for speed reasons
+ //because it is faster on most cases than looping over the param types
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ methods = clazz.getMethods();
+ for (Method m : methods) {
+ if (!m.getName().equals(methodName) || m.getParameterTypes().length != varargs.length) {
+ continue;
+ }
+ try {
+ return m.invoke(obj, varargs);
+ } catch (IllegalAccessException e) {
+ //expected, we do it that way for speed reasons
+ //because it is faster on most cases than looping over the param types
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
+ }
}
+ throw new RuntimeException("Method of :" + methodName + " from class " + obj.getClass().getName() + " not found");
}
+
+
+
+
/**
* executes a function method on a target object
*
@@ -108,10 +158,10 @@
* @param varargs a list of objects casts or nulls defining the parameter classes and its values
* if something occurs on introspection level an unmanaged exception is throw, just like
* it would happen in a scripting class
- * @return the result object for the function(method) call
+ * @return the result object for the Method(method) call
* @throws RuntimeException an unmanaged runtime exception in case of an introspection error
*/
- public static Object executeFunction(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++) {
@@ -124,7 +174,7 @@
}
try {
- Method m = getMethod(obj, methodName, classes);
+ Method m = fastGetMethod(obj, methodName, classes);
return m.invoke(obj, varargs);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
@@ -136,7 +186,8 @@
}
- public static Method getMethod(Object obj, String methodName, Class[] classes) throws NoSuchMethodException {
+
+ public static Method fastGetMethod(Object obj, String methodName, Class[] classes) throws NoSuchMethodException {
Method m = null;
try {
m = obj.getClass().getDeclaredMethod(methodName, classes);
@@ -154,10 +205,10 @@
* @param varargs a list of objects casts or nulls defining the parameter classes and its values
* if something occurs on introspection level an unmanaged exception is throw, just like
* it would happen in a scripting class
- * @return the result object for the function(method) call
+ * @return the result object for the Method(method) call
* @throws RuntimeException an unmanaged runtime exception in case of an introspection error
*/
- public static Object executeStaticFunction(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++) {
@@ -172,7 +223,7 @@
//TODO add autocasting instead of manual casting
try {
- Method m = getStaticMethod(obj, methodName, classes);
+ Method m = fastGetStaticMethod(obj, methodName, classes);
return m.invoke(obj, varargs);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
@@ -184,7 +235,7 @@
}
- public static Method getStaticMethod(Class obj, String methodName, Class[] classes) throws NoSuchMethodException {
+ public static Method fastGetStaticMethod(Class obj, String methodName, Class[] classes) throws NoSuchMethodException {
Method m = null;
try {
m = obj.getDeclaredMethod(methodName, classes);
Added: myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/ReflectUtilTest.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/ReflectUtilTest.java?rev=815215&view=auto
==============================================================================
--- myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/ReflectUtilTest.java (added)
+++ myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/ReflectUtilTest.java Tue Sep 15 08:29:39 2009
@@ -0,0 +1,72 @@
+/*
+ * 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.myfaces.scripting.core.util;
+
+import static org.junit.Assert.*;
+import static org.apache.myfaces.scripting.core.util.ReflectUtil.*;
+
+import org.junit.Before;
+import org.junit.Test;
+
+
+/**
+ * @author Werner Punz (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+
+public class ReflectUtilTest {
+
+ TestProbe probe = new TestProbe();
+
+ @Test
+ public void executeMethod1() {
+ executeMethod(probe, "testMethod1");
+
+ try {
+ executeMethod(probe, "testMethod2");
+ fail("method2 throws an internal error");
+ } catch (RuntimeException ex) {
+ }
+
+ }
+
+ @Test
+ public void executeMethod2() {
+ try {
+ executeMethod(probe, "testMethod3");
+ } catch (RuntimeException e) {
+
+ }
+
+ try {
+ executeMethod(probe, "testMethod3", 10);
+ } catch (RuntimeException e) {
+
+ }
+
+ boolean retVal = (Boolean) executeMethod(probe, "testMethod3", "hello world");
+ assertTrue(retVal);
+ }
+
+ @Test
+ public void executeStatic() {
+ boolean retVal = (Boolean) executeStaticMethod(TestProbe.class, "testMethod4", "1", "2");
+ }
+
+}
Propchange: myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/ReflectUtilTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/ReflectUtilTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/TestProbe.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/TestProbe.java?rev=815215&view=auto
==============================================================================
--- myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/TestProbe.java (added)
+++ myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/TestProbe.java Tue Sep 15 08:29:39 2009
@@ -0,0 +1,51 @@
+/*
+ * 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.myfaces.scripting.core.util;
+
+/**
+ * @author Werner Punz (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ *
+ * testprobe for our reflectutils
+ */
+
+public class TestProbe {
+
+ public TestProbe() {
+ }
+
+ public void testMethod1() {
+
+ }
+
+ public void testMethod2() {
+ throw new NullPointerException("for test");
+ }
+
+
+ public boolean testMethod3(String param1) {
+ return true;
+ }
+
+ public static boolean testMethod4(String param1, String param2) {
+ return true;
+ }
+
+
+}
Propchange: myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/TestProbe.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/scripting/core/util/TestProbe.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified: myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java?rev=815215&r1=815214&r2=815215&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java (original)
+++ myfaces/extensions/scripting/trunk/examples/src/main/webapp/WEB-INF/java/org/apache/myfaces/javaloader/blog/JSFUtil.java Tue Sep 15 08:29:39 2009
@@ -49,8 +49,8 @@
Log log = LogFactory.getLog(JSFUtil.class);
Object facesContext = FacesContext.getCurrentInstance();
- Object elContext = executeFunction(facesContext, "getELContext");
- Object elResolver = executeFunction(elContext, "getELResolver");
+ Object elContext = executeMethod(facesContext, "getELContext");
+ Object elResolver = executeMethod(elContext, "getELResolver");
try {
@@ -65,7 +65,7 @@
*/
//we use the introspection calls here to achieve our goal that way
//we can shift the dependency resolution from compile time to runtime
- return executeFunction(elResolver, "getValue", cast(ClassUtils.classForName("javax.el.ELContext"), elContext), nullCast(Object.class), cast(Object.class, beanName));
+ return executeMethod(elResolver, "getValue", elContext, null, beanName);
// return FacesContext.getCurrentInstance().getELContext().getELResolver().getValue(FacesContext.getCurrentInstance().getELContext(), null, beanName);
} catch (ClassNotFoundException e) {