001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jorphan.reflect;
020:
021: import java.lang.reflect.InvocationTargetException;
022: import java.lang.reflect.Method;
023:
024: import org.apache.commons.lang.ClassUtils;
025: import org.apache.jorphan.util.JMeterException;
026:
027: /**
028: * Utility methods for handling dynamic access to classes.
029: */
030: public class ClassTools {
031:
032: /**
033: * Call no-args constructor for a class.
034: *
035: * @param className
036: * @return an instance of the class
037: * @throws JMeterException if class cannot be created
038: */
039: public static Object construct(String className)
040: throws JMeterException {
041: Object instance = null;
042: try {
043: instance = ClassUtils.getClass(className).newInstance();
044: } catch (ClassNotFoundException e) {
045: throw new JMeterException(e);
046: } catch (InstantiationException e) {
047: throw new JMeterException(e);
048: } catch (IllegalAccessException e) {
049: throw new JMeterException(e);
050: }
051: return instance;
052: }
053:
054: /**
055: * Call a class constructor with an integer parameter
056: * @param className
057: * @param parameter (integer)
058: * @return an instance of the class
059: * @throws JMeterException if class cannot be created
060: */
061: public static Object construct(String className, int parameter)
062: throws JMeterException {
063: Object instance = null;
064: try {
065: Class clazz = ClassUtils.getClass(className);
066: clazz.getConstructor(new Class[] { Integer.TYPE });
067: instance = ClassUtils.getClass(className).newInstance();
068: } catch (ClassNotFoundException e) {
069: throw new JMeterException(e);
070: } catch (InstantiationException e) {
071: throw new JMeterException(e);
072: } catch (IllegalAccessException e) {
073: throw new JMeterException(e);
074: } catch (SecurityException e) {
075: throw new JMeterException(e);
076: } catch (NoSuchMethodException e) {
077: throw new JMeterException(e);
078: }
079: return instance;
080: }
081:
082: /**
083: * Invoke a public method on a class instance
084: *
085: * @param instance
086: * @param methodName
087: * @throws SecurityException
088: * @throws IllegalArgumentException
089: * @throws JMeterException
090: */
091: public static void invoke(Object instance, String methodName)
092: throws SecurityException, IllegalArgumentException,
093: JMeterException {
094: Method m;
095: try {
096: m = ClassUtils.getPublicMethod(instance.getClass(),
097: methodName, new Class[] {});
098: m.invoke(instance, null);
099: } catch (NoSuchMethodException e) {
100: throw new JMeterException(e);
101: } catch (IllegalAccessException e) {
102: throw new JMeterException(e);
103: } catch (InvocationTargetException e) {
104: throw new JMeterException(e);
105: }
106: }
107: }
|