Author: sebb
Date: Tue Apr  3 10:10:29 2007
New Revision: 525211

URL: http://svn.apache.org/viewvc?view=rev&rev=525211
Log:
New utility class

Added:
    
jakarta/jmeter/branches/rel-2-2/src/jorphan/org/apache/jorphan/reflect/ClassTools.java
   (with props)

Added: 
jakarta/jmeter/branches/rel-2-2/src/jorphan/org/apache/jorphan/reflect/ClassTools.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/branches/rel-2-2/src/jorphan/org/apache/jorphan/reflect/ClassTools.java?view=auto&rev=525211
==============================================================================
--- 
jakarta/jmeter/branches/rel-2-2/src/jorphan/org/apache/jorphan/reflect/ClassTools.java
 (added)
+++ 
jakarta/jmeter/branches/rel-2-2/src/jorphan/org/apache/jorphan/reflect/ClassTools.java
 Tue Apr  3 10:10:29 2007
@@ -0,0 +1,106 @@
+/*
+ * 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.jorphan.reflect;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.apache.commons.lang.ClassUtils;
+import org.apache.jorphan.util.JMeterException;
+
+/**
+ * Utility methods for handling dynamic access to classes. 
+ */
+public class ClassTools {
+
+
+       /**
+        * Call no-args constructor for a class.
+        * 
+        * @param className
+        * @return
+        * @throws JMeterException
+        */
+       public static Object construct(String className) throws JMeterException 
{
+               Object instance = null;
+               try {
+                       instance = ClassUtils.getClass(className).newInstance();
+               } catch (ClassNotFoundException e) {
+                       throw new JMeterException(e);
+               } catch (InstantiationException e) {
+                       throw new JMeterException(e);
+               } catch (IllegalAccessException e) {
+                       throw new JMeterException(e);
+               }
+               return instance;
+       }
+
+       /**
+        * Call a class constructor
+        * @param className
+        * @param parameter (integer)
+        * @return
+        * @throws JMeterException
+        */
+       public static Object construct(String className, int parameter) throws 
JMeterException 
+       {
+               Object instance = null;
+               try {
+                       Class clazz = ClassUtils.getClass(className);
+                       clazz.getConstructor(new Class [] {Integer.TYPE});
+                       instance = ClassUtils.getClass(className).newInstance();
+               } catch (ClassNotFoundException e) {
+                       throw new JMeterException(e);
+               } catch (InstantiationException e) {
+                       throw new JMeterException(e);
+               } catch (IllegalAccessException e) {
+                       throw new JMeterException(e);
+               } catch (SecurityException e) {
+                       throw new JMeterException(e);
+               } catch (NoSuchMethodException e) {
+                       throw new JMeterException(e);
+               }
+               return instance;
+       }
+
+       /**
+        * Invoke a public method on a class instance
+        * 
+        * @param instance
+        * @param methodName
+        * @throws SecurityException
+        * @throws IllegalArgumentException
+        * @throws JMeterException
+        */
+       public static void invoke(Object instance, String methodName) 
+       throws SecurityException, IllegalArgumentException, JMeterException 
+       {
+               Method m;
+               try {
+                       m = ClassUtils.getPublicMethod(instance.getClass(), 
methodName, new Class [] {});
+                       m.invoke(instance, null);
+               } catch (NoSuchMethodException e) {
+                       throw new JMeterException(e);
+               } catch (IllegalAccessException e) {
+                       throw new JMeterException(e);
+               } catch (InvocationTargetException e) {
+                       throw new JMeterException(e);
+               }
+       }
+}

Propchange: 
jakarta/jmeter/branches/rel-2-2/src/jorphan/org/apache/jorphan/reflect/ClassTools.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/jmeter/branches/rel-2-2/src/jorphan/org/apache/jorphan/reflect/ClassTools.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to