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/01 11:30:24 UTC
svn commit: r809927 - in
/myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util:
Cast.java ClassUtils.java Null.java
Author: werpu
Date: Tue Sep 1 09:30:24 2009
New Revision: 809927
URL: http://svn.apache.org/viewvc?rev=809927&view=rev
Log:
https://issues.apache.org/jira/browse/EXTSCRIPT-3
moving over the example introspection code to add
the dynamic helpers for the java layer
Added:
myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/Cast.java
myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/Null.java
Modified:
myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/ClassUtils.java
Added: myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/Cast.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/Cast.java?rev=809927&view=auto
==============================================================================
--- myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/Cast.java (added)
+++ myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/Cast.java Tue Sep 1 09:30:24 2009
@@ -0,0 +1,42 @@
+/*
+ * 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;
+
+/**
+ * Simple casting representation for introspection
+ * calls
+ */
+public class Cast {
+
+ Class clazz;
+ Object value;
+
+ public Cast(Class clazz, Object value) {
+ this.clazz = clazz;
+ this.value = value;
+ }
+
+ public Class getClazz() {
+ return clazz;
+ }
+
+ public Object getValue() {
+ return value;
+ }
+}
Modified: myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/ClassUtils.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/ClassUtils.java?rev=809927&r1=809926&r2=809927&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/ClassUtils.java (original)
+++ myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/ClassUtils.java Tue Sep 1 09:30:24 2009
@@ -18,10 +18,12 @@
*/
package org.apache.myfaces.scripting.core.util;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+
/**
* @author werpu
- * helper class to bypass a groovy related bug
- *
+ * helper class to bypass a groovy related bug
*/
public class ClassUtils {
@@ -33,4 +35,99 @@
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++) {
+
+ if (varargs[cnt] instanceof Cast) {
+ classes[cnt] = ((Cast) varargs[cnt]).getClazz();
+ varargs[cnt] = ((Cast) varargs[cnt]).getValue();
+ } else {
+ classes[cnt] = varargs[cnt].getClass();
+ }
+ }
+
+ try {
+ Method m = obj.getClass().getMethod(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);
+ }
+ }
+
+ /**
+ * executes a function on a target object
+ *
+ * @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
+ * @return the result object for the function(method) call
+ * @throws RuntimeException an unmanaged runtime exception in case of an introspection error
+ */
+ public static Object executeFunction(Object obj, String methodName, Object... varargs) {
+ Class[] classes = new Class[varargs.length];
+ for (int cnt = 0; cnt < varargs.length; cnt++) {
+
+ if (varargs[cnt] instanceof Cast) {
+ classes[cnt] = ((Cast) varargs[cnt]).getClazz();
+ varargs[cnt] = ((Cast) varargs[cnt]).getValue();
+ } else {
+ classes[cnt] = varargs[cnt].getClass();
+ }
+ }
+
+ try {
+ Method m = obj.getClass().getMethod(methodName, classes);
+ return 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);
+ }
+
+ }
+
+
+ /**
+ * convenience method which makes the code a little bit more readable
+ * use it in conjunction with static imports
+ *
+ * @param clazz the cast target for the method call
+ * @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) {
+ return new Cast(clazz, value);
+ }
+
+ /**
+ * convenience method which makes the code a little bit more readable
+ * use it in conjunction with static imports
+ *
+ * @param clazz the cast target for the method call
+ * @return a null value Cast object of the parameters
+ */
+ public static Null nullCast(Class clazz) {
+ return new Null(clazz);
+ }
+
}
Added: myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/Null.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/Null.java?rev=809927&view=auto
==============================================================================
--- myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/Null.java (added)
+++ myfaces/extensions/scripting/trunk/core/src/main/java/org/apache/myfaces/scripting/core/util/Null.java Tue Sep 1 09:30:24 2009
@@ -0,0 +1,31 @@
+/*
+ * 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
+ * Null representation for easier introspection calls
+ */
+public class Null extends Cast {
+
+ public Null(Class clazz) {
+ super(clazz, null);
+ }
+
+}