Example usage for org.apache.commons.lang.reflect MethodUtils invokeExactMethod

List of usage examples for org.apache.commons.lang.reflect MethodUtils invokeExactMethod

Introduction

In this page you can find the example usage for org.apache.commons.lang.reflect MethodUtils invokeExactMethod.

Prototype

public static Object invokeExactMethod(Object object, String methodName, Object[] args)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException 

Source Link

Document

Invoke a method whose parameter types match exactly the object types.

This uses reflection to invoke the method obtained from a call to getAccessibleMethod().

Usage

From source file:com.bstek.dorado.view.config.definition.ViewConfigDefinition.java

protected void injectResourceString(ViewConfig viewConfig, String key, String resourceString) throws Exception {
    Object object = viewConfig;/*from ww  w  . j  ava  2 s. c  om*/
    ResourceInjection resourceInjection = object.getClass().getAnnotation(ResourceInjection.class);

    String[] sections = StringUtils.split(key, ".");
    int len = sections.length;
    for (int i = 0; i < len; i++) {
        String section = sections[i];
        boolean isObject = section.charAt(0) == '#';
        if (isObject) {
            section = section.substring(1);
        }

        if (i == 0 && section.equals("view")) {
            object = viewConfig.getView();
        } else if (isObject) {
            if (object == viewConfig) {
                object = viewConfig.getDataType(section);
                if (object == null && viewConfig.getView() != null) {
                    object = viewConfig.getView().getViewElement(section);
                }
            } else {
                if (resourceInjection == null) {
                    throwInvalidResourceKey(key);
                }
                String methodName = resourceInjection.subObjectMethod();
                if (StringUtils.isEmpty(methodName)) {
                    throwInvalidResourceKey(key);
                }
                object = MethodUtils.invokeExactMethod(object, methodName, new String[] { section });
            }
            if (object == null) {
                break;
            }
            resourceInjection = object.getClass().getAnnotation(ResourceInjection.class);

            if (i == len - 1) {
                String[] defaultProperties;
                if (resourceInjection == null) {
                    defaultProperties = DEFAULT_PROPERTIES;
                } else {
                    defaultProperties = resourceInjection.defaultProperty();
                }

                boolean found = false;
                for (String property : defaultProperties) {
                    if (PropertyUtils.isWriteable(object, property)) {
                        if (PropertyUtils.getSimpleProperty(object, property) == null) {
                            PropertyUtils.setSimpleProperty(object, property, resourceString);
                        }
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    throwInvalidResourceKey(key);
                }
            }
        } else {
            if (i == len - 1) {
                if (PropertyUtils.getSimpleProperty(object, section) == null) {
                    PropertyUtils.setSimpleProperty(object, section, resourceString);
                }
            } else {
                object = PropertyUtils.getSimpleProperty(object, section);
            }
        }
    }
}

From source file:com.bstek.dorado.data.config.definition.DataTypeDefinition.java

protected void injectResourceString(EntityDataType dataType, String key, String resourceString)
        throws Exception {
    Object object = dataType;//from w  w  w . jav  a2  s . c  o  m
    ResourceInjection resourceInjection = object.getClass().getAnnotation(ResourceInjection.class);

    String[] sections = StringUtils.split(key, ".");
    int len = sections.length;
    for (int i = 0; i < len; i++) {
        String section = sections[i];
        boolean isObject = section.charAt(0) == '#';

        if (isObject) {
            section = section.substring(1);

            if (resourceInjection == null) {
                throwInvalidResourceKey(key);
            }
            String methodName = resourceInjection.subObjectMethod();
            if (StringUtils.isEmpty(methodName)) {
                throwInvalidResourceKey(key);
            }
            object = MethodUtils.invokeExactMethod(object, methodName, new String[] { section });
            if (object == null) {
                break;
            }
            resourceInjection = object.getClass().getAnnotation(ResourceInjection.class);

            if (i == len - 1) {
                String[] defaultProperties;
                if (resourceInjection == null) {
                    defaultProperties = DEFAULT_PROPERTIES;
                } else {
                    defaultProperties = resourceInjection.defaultProperty();
                }

                boolean found = false;
                for (String property : defaultProperties) {
                    if (PropertyUtils.isWriteable(object, property)) {
                        // if (PropertyUtils.getSimpleProperty(object,
                        // property) == null) {
                        PropertyUtils.setSimpleProperty(object, property, resourceString);
                        // }
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    throwInvalidResourceKey(key);
                }
            }
        } else {
            if (i == len - 1) {
                if (PropertyUtils.getSimpleProperty(object, section) == null) {
                    PropertyUtils.setSimpleProperty(object, section, resourceString);
                }
            } else {
                object = PropertyUtils.getSimpleProperty(object, section);
            }
        }
    }
}

From source file:org.dkpro.lab.Util.java

public static void createSymbolicLink(File aSource, File aTarget) throws IOException {
    if (aTarget.exists()) {
        throw new FileExistsException(aTarget);
    }/*from w w w  .ja va2  s  . c  om*/

    File parentDir = aTarget.getAbsoluteFile().getParentFile();
    if (parentDir != null && !parentDir.exists()) {
        FileUtils.forceMkdir(parentDir);
    }

    // Try Java 7 methods
    try {
        Object fromPath = MethodUtils.invokeExactMethod(aSource, "toPath", new Object[0]);
        Object toPath = MethodUtils.invokeExactMethod(aTarget, "toPath", new Object[0]);
        Object options = Array.newInstance(Class.forName("java.nio.file.attribute.FileAttribute"), 0);
        MethodInvoker inv = new MethodInvoker();
        inv.setStaticMethod("java.nio.file.Files.createSymbolicLink");
        inv.setArguments(new Object[] { toPath, fromPath, options });
        inv.prepare();
        inv.invoke();
        return;
    } catch (ClassNotFoundException e) {
        // Ignore
    } catch (NoSuchMethodException e) {
        // Ignore
    } catch (IllegalAccessException e) {
        // Ignore
    } catch (InvocationTargetException e) {
        if ("java.nio.file.FileAlreadyExistsException".equals(e.getTargetException().getClass().getName())) {
            throw new FileExistsException(aTarget);
        }
    }

    // If the Java 7 stuff is not available, fall back to Runtime.exec
    String[] cmdline = { "ln", "-s", aSource.getAbsolutePath(), aTarget.getAbsolutePath() };
    Execute exe = new Execute();
    exe.setVMLauncher(false);
    exe.setCommandline(cmdline);
    exe.execute();
    if (exe.isFailure()) {
        throw new IOException("Unable to create symlink from [" + aSource + "] to [" + aTarget + "]");
    }
}

From source file:org.eclipse.mylyn.commons.sdk.util.CommonTestUtil.java

private static File getFileFromClassLoaderBeforeLuna(String filename, ClassLoader classLoader)
        throws Exception {
    Object classpathManager = MethodUtils.invokeExactMethod(classLoader, "getClasspathManager", null);
    Object baseData = MethodUtils.invokeExactMethod(classpathManager, "getBaseData", null);
    Bundle bundle = (Bundle) MethodUtils.invokeExactMethod(baseData, "getBundle", null);
    URL localURL = FileLocator.toFileURL(bundle.getEntry(filename));
    return new File(localURL.getFile());
}

From source file:org.eclipse.mylyn.commons.sdk.util.CommonTestUtil.java

private static File getFileFromClassLoader4Luna(String filename, ClassLoader classLoader) throws Exception {
    Object classpathManager = MethodUtils.invokeExactMethod(classLoader, "getClasspathManager", null);
    Object generation = MethodUtils.invokeExactMethod(classpathManager, "getGeneration", null);
    Object bundleFile = MethodUtils.invokeExactMethod(generation, "getBundleFile", null);
    File file = (File) MethodUtils.invokeExactMethod(bundleFile, "getFile", new Object[] { filename, true },
            new Class[] { String.class, boolean.class });
    return file;/*from w w w. ja  va2  s  .  co m*/
}

From source file:org.jahia.osgi.FrameworkService.java

/**
 * Initiate synchronous or asynchronous delivery of an OSGi event using {@link EventAdmin} service. If synchronous delivery is
 * requested, this method does not return to the caller until delivery of the event is completed.
 *
 * @param topic the topic of the event/* w  w w  .j a  va  2 s.  c  om*/
 * @param properties the event's properties (may be {@code null}). A property whose key is not of type {@code String} will be ignored.
 * @param asynchronous if <code>true</code> an event is delivered asynchronously; in case of <code>false</code> this method does not
 *            return to the caller until delivery of the event is completed
 */
public static void sendEvent(String topic, Map<String, ?> properties, boolean asynchronous) {
    BundleContext context = FrameworkService.getBundleContext();
    ServiceReference<?> ref = context.getServiceReference(EventAdmin.class.getName());
    if (ref != null) {
        Object service = context.getService(ref);
        try {
            // have to use the class loader of the EventAdmin service
            ClassLoader classLoader = service.getClass().getClassLoader();

            Object evt = classLoader.loadClass("org.osgi.service.event.Event")
                    .getConstructor(String.class, Map.class).newInstance(topic, properties);

            logger.info("Sending {} event with the properties {} to the topic {}...",
                    new Object[] { asynchronous ? "asynchronous" : "synchronous", properties, topic });

            MethodUtils.invokeExactMethod(service, asynchronous ? "postEvent" : "sendEvent", evt);

            logger.info("Event sent to the topic {}", topic);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException
                | ClassNotFoundException | InstantiationException | IllegalArgumentException
                | SecurityException e) {
            throw new IllegalArgumentException(e);
        }
    }
}