001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.util.reflect;
022:
023: import java.lang.reflect.InvocationTargetException;
024: import java.lang.reflect.Method;
025: import java.sql.SQLException;
026: import java.util.HashSet;
027: import java.util.LinkedList;
028: import java.util.List;
029: import java.util.Set;
030: import java.util.regex.Pattern;
031:
032: import net.sf.hajdbc.util.SQLExceptionFactory;
033:
034: /**
035: * @author Paul Ferraro
036: */
037: public final class Methods {
038: /**
039: * Helper method for <code>Method.invoke(Object, Object...)</code> that performs the necessary exception handling.
040: * @param method a method to invoke
041: * @param object the object on which to invoke the given method
042: * @param parameters the method parameters
043: * @return the return value of the method invocation
044: * @throws SQLException the target exception of the method invocation
045: * @throws IllegalArgumentException if the the underlying method is inaccessible
046: */
047: public static Object invoke(Method method, Object object,
048: Object... parameters) throws SQLException {
049: try {
050: return method.invoke(object, parameters);
051: } catch (IllegalAccessException e) {
052: throw new IllegalArgumentException(e);
053: } catch (InvocationTargetException e) {
054: throw SQLExceptionFactory.createSQLException(e
055: .getTargetException());
056: }
057: }
058:
059: /**
060: * Returns a set of methods for the specified class whose names match the specified regular expression patterns.
061: * @param sourceClass the class from which to find methods
062: * @param patterns regular expression patterns
063: * @return a set of methods
064: */
065: public static Set<Method> findMethods(Class<?> sourceClass,
066: String... patterns) {
067: List<Method> list = new LinkedList<Method>();
068:
069: Method[] methods = sourceClass.getMethods();
070:
071: for (String regex : patterns) {
072: Pattern pattern = Pattern.compile(regex);
073:
074: for (Method method : methods) {
075: if (pattern.matcher(method.getName()).matches()) {
076: list.add(method);
077: }
078: }
079: }
080:
081: return new HashSet<Method>(list);
082: }
083:
084: /**
085: * Helper method for {@link Class#getMethod(String, Class...)} where method is known to exist.
086: * @param sourceClass the class from which to find methods
087: * @param name the method name
088: * @param types the parameter types
089: * @return the method with the specified name and parameter types
090: * @throws IllegalArgumentException if no such method exists
091: */
092: public static Method getMethod(Class<?> sourceClass, String name,
093: Class<?>... types) {
094: try {
095: return sourceClass.getMethod(name, types);
096: } catch (NoSuchMethodException e) {
097: throw new IllegalArgumentException(e);
098: }
099: }
100:
101: /**
102: * Helper method for {@link Class#getMethod(String, Class...)} that returns null if the method does not exist.
103: * @param sourceClass the class from which to find methods
104: * @param name the method name
105: * @param types the parameter types
106: * @return the method with the specified name and parameter types, or null if the method does not exist
107: */
108: public static Method findMethod(Class<?> sourceClass, String name,
109: Class<?>... types) {
110: try {
111: return sourceClass.getMethod(name, types);
112: } catch (NoSuchMethodException e) {
113: return null;
114: }
115: }
116:
117: /**
118: * Helper method for {@link Class#getMethod(String, Class...)} that returns null if the class or method does not exist.
119: * @param className the name of the class containing the method
120: * @param name the method name
121: * @param types the parameter types
122: * @return the method with the specified name and parameter types, or null if the class or method does not exist
123: */
124: public static Method findMethod(String className, String name,
125: Class<?>... types) {
126: try {
127: return findMethod(Class.forName(className), name, types);
128: } catch (ClassNotFoundException e) {
129: return null;
130: }
131: }
132:
133: private Methods() {
134: // Hide constructor
135: }
136: }
|