Example usage for java.lang.reflect Method invoke

List of usage examples for java.lang.reflect Method invoke

Introduction

In this page you can find the example usage for java.lang.reflect Method invoke.

Prototype

@CallerSensitive
@ForceInline 
@HotSpotIntrinsicCandidate
public Object invoke(Object obj, Object... args)
        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException 

Source Link

Document

Invokes the underlying method represented by this Method object, on the specified object with the specified parameters.

Usage

From source file:dyco4j.instrumentation.internals.CLI.java

private static void addEntryToClassPath(final URLClassLoader urlClassLoader, final Method method,
        final String s) {
    final File _f = new File(s);
    try {/*from  ww w.  java  2s  .  co  m*/
        final URL _u = _f.toURI().toURL();
        method.invoke(urlClassLoader, _u);
        LOGGER.info(MessageFormat.format("Adding {0} to classpath", s));
    } catch (MalformedURLException | IllegalAccessException | InvocationTargetException _e) {
        throw new RuntimeException(_e);
    }
}

From source file:jp.go.nict.langrid.client.soap.io.SoapResponseParser.java

private static <T> T nodeToType(XPathWorkspace w, Node node, Class<T> clazz, Converter converter)
        throws InstantiationException, IllegalAccessException, IllegalArgumentException,
        InvocationTargetException, ConversionException, DOMException, ParseException {
    if (clazz.isPrimitive()) {
        return converter.convert(resolveHref(w, node).getTextContent(), clazz);
    } else if (clazz.equals(String.class)) {
        return clazz.cast(resolveHref(w, node).getTextContent());
    } else if (clazz.equals(byte[].class)) {
        try {//from w w w.  j  av  a2s  . c  o  m
            return clazz.cast(Base64.decodeBase64(resolveHref(w, node).getTextContent().getBytes("ISO8859-1")));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    } else if (clazz.equals(Calendar.class)) {
        Date date = fmt.get().parse(resolveHref(w, node).getTextContent());
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return clazz.cast(c);
    } else if (clazz.isArray()) {
        Class<?> ct = clazz.getComponentType();
        List<Object> elements = new ArrayList<Object>();
        node = resolveHref(w, node);
        for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (!(child instanceof Element))
                continue;
            elements.add(nodeToType(w, child, ct, converter));
        }
        return clazz.cast(elements.toArray((Object[]) Array.newInstance(ct, elements.size())));
    } else {
        T instance = clazz.newInstance();
        node = resolveHref(w, node);
        for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (!(child instanceof Element))
                continue;
            String nn = child.getLocalName();
            Method setter = ClassUtil.findSetter(clazz, nn);
            setter.invoke(instance,
                    nodeToType(w, resolveHref(w, child), setter.getParameterTypes()[0], converter));
        }
        return instance;
    }
}

From source file:Main.java

/**
 * Sets the thread policy.// w w w.  jav  a2  s.  c  o  m
 *
 * @param strictMode
 *            the strict mode to work with
 * @throws ClassNotFoundException
 *             on problem
 * @throws NoSuchMethodException
 *             on problem
 * @throws InstantiationException
 *             on problem
 * @throws IllegalAccessException
 *             on problem
 * @throws InvocationTargetException
 *             on problem
 */
private static void setThreadPolicy(final Class<?> strictMode) throws ClassNotFoundException,
        NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {

    Class<?> threadPolicyType = Class.forName("android.os.StrictMode$ThreadPolicy");
    Method setThreadPolicy = strictMode.getMethod("setThreadPolicy", threadPolicyType);
    Class<?> threadPolicyBuilder = Class.forName("android.os.StrictMode$ThreadPolicy$Builder");

    Object policy = buildPolicy(threadPolicyBuilder);
    setThreadPolicy.invoke(strictMode, policy);
}

From source file:com.wavemaker.tools.project.LauncherHelper.java

/**
 * Invoke a method. It's expected that the ClassLoader cl will be the same as the context classloader, or else the
 * Spring init will likely fail (or otherwise be bad).
 *///from w ww. j  a  v a  2 s .  c  om
public static Object invoke(ClassLoader cl, String fqClass, String methodName, Class<?>[] argTypes,
        Object[] args, boolean isStatic)
        throws ClassNotFoundException, SecurityException, NoSuchMethodException, InstantiationException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    if (ResourceManager.getInstance() == null) {
        SpringUtils.initSpringConfig();
    }

    Class<?> klass = cl.loadClass(fqClass);
    Method m = klass.getMethod(methodName, argTypes);

    Object o;
    if (isStatic) {
        o = null;
    } else {
        o = klass.newInstance();
    }

    return m.invoke(o, args);
}