001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal;
022:
023: import java.lang.reflect.*;
024:
025: /**
026: * @exclude
027: *
028: * Use the methods in this class for system classes only, since they
029: * are not ClassLoader or Reflector-aware.
030: *
031: * TODO: this class should go to foundation.reflect, along with ReflectException and ReflectPlatform
032: */
033: public class Reflection4 {
034:
035: public static Object invoke(Object obj, String methodName)
036: throws ReflectException {
037: return invoke(obj.getClass().getName(), methodName, null, null,
038: obj);
039: }
040:
041: public static Object invoke(Object obj, String methodName,
042: Object[] params) throws ReflectException {
043: Class[] paramClasses = new Class[params.length];
044: for (int i = 0; i < params.length; i++) {
045: paramClasses[i] = params[i].getClass();
046: }
047: return invoke(obj.getClass().getName(), methodName,
048: paramClasses, params, obj);
049: }
050:
051: public static Object invoke(Object obj, String methodName,
052: Class[] paramClasses, Object[] params)
053: throws ReflectException {
054: return invoke(obj.getClass().getName(), methodName,
055: paramClasses, params, obj);
056: }
057:
058: public static Object invoke(Class clazz, String methodName,
059: Class[] paramClasses, Object[] params)
060: throws ReflectException {
061: return invoke(clazz.getName(), methodName, paramClasses,
062: params, null);
063: }
064:
065: public static Object invoke(String className, String methodName,
066: Class[] paramClasses, Object[] params, Object onObject)
067: throws ReflectException {
068: Method method = getMethod(className, methodName, paramClasses);
069: return invoke(params, onObject, method);
070: }
071:
072: public static Object invoke(Object[] params, Object onObject,
073: Method method) throws ReflectException {
074: if (method == null) {
075: return null;
076: }
077: try {
078: return method.invoke(onObject, params);
079: } catch (InvocationTargetException e) {
080: throw new ReflectException(e.getTargetException());
081: } catch (IllegalArgumentException e) {
082: throw new ReflectException(e);
083: } catch (IllegalAccessException e) {
084: throw new ReflectException(e);
085: }
086: }
087:
088: /**
089: * calling this method "method" will break C# conversion with the old converter
090: */
091: public static Method getMethod(String className, String methodName,
092: Class[] paramClasses) {
093: Class clazz = ReflectPlatform.forName(className);
094: if (clazz == null) {
095: return null;
096: }
097: try {
098: return clazz.getMethod(methodName, paramClasses);
099: } catch (Exception e) {
100: // This is a catch all since the reflection call is
101: // intended for cases where the method is not present.
102: }
103: return null;
104: }
105:
106: }
|