at.spardat.xma.boot.util.Reflectives Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
/*
* Created on : 04.2003
* Created by : s3595
*/
package at.spardat.xma.boot.util;
import java.lang.reflect.Method;
import at.spardat.xma.boot.logger.LogLevel;
import at.spardat.xma.boot.logger.Logger;
/**
*
* @author CGS
* @version $Id: Reflectives.java 2084 2007-11-27 14:53:31Z s3460 $
* @deprecated because not used any more
*/
public class Reflectives {
private boolean accessible;
private static Object zero[] = new Object[0];
/**
* Create a new Reflect instance.
* @deprecated
*/
public Reflectives() {}
/**
* Create a new Reflect instance.
*
* @param accessible whether to bypass access permissions
* @deprecated
*/
public Reflectives(boolean accessible) {
this.accessible = accessible;
}
/**
* Invoke a zero-parameter static method by name.
* @deprecated
*/
public Object invokeStatic(String className, String method) {
return invokeStatic(className, method, zero);
}
/**
* Invoke the static method using the specified parameters.
* @deprecated
*/
public Object invokeStatic(String className, String method, Object args[]) {
try {
Class c = Class.forName(className, true, Reflectives.class.getClassLoader());
Method m = getMethod(c, method, args);
if (m.isAccessible() != accessible)
m.setAccessible(accessible);
return m.invoke(null, args);
}
catch (Exception ex) { // eat
Logger.getLogger("bootrt.bootRuntime").log(LogLevel.WARNING,"",ex);
return null;
}
}
/**
* Invoke a zero-parameter method by name on the specified
* object.
* @deprecated
*/
public Object invoke(Object object, String method)
throws Exception {
return invoke(object, method, zero);
}
/**
* Invoke a method by name with the specified parameters.
*
* @return the result of the method
* @deprecated
*/
public Object invoke(Object object, String method, Object args[])
throws Exception {
try {
Method m = getMethod(object.getClass(), method, args);
if (m.isAccessible() != accessible)
m.setAccessible(accessible);
return m.invoke(object, args);
}
catch (Exception ex) {
ex.printStackTrace();
throw ex ;
}
}
/**
* Return the Method matching the specified name and number of
* arguments.
* @deprecated
*/
public Method getMethod(Class type, String method, Object args[])
throws Exception {
try {
for (Class c = type; c != null; c = c.getSuperclass()) {
Method methods[] = c.getMethods();
for (int i=0; i < methods.length; i++) {
if (methods[i].getName().equals(method)) {
Class parameters[] = methods[i].getParameterTypes();
if (parameters.length == args.length)
return methods[i];
}
}
}
}
catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
return null;
}
}