Example usage for java.lang NoSuchMethodException NoSuchMethodException

List of usage examples for java.lang NoSuchMethodException NoSuchMethodException

Introduction

In this page you can find the example usage for java.lang NoSuchMethodException NoSuchMethodException.

Prototype

public NoSuchMethodException(String s) 

Source Link

Document

Constructs a NoSuchMethodException with a detail message.

Usage

From source file:PropertyUtils.java

/**
 * Set specified property value/*from  w  w w  .jav  a2  s  . com*/
 */
public static void setProperty(Object bean, String property, Object value)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    PropertyDescriptor descriptor = getPropertyDescriptor(bean.getClass(), property);
    if (descriptor == null)
        throw new NoSuchMethodException("Cannot find property " + bean.getClass().getName() + "." + property);
    Method method = descriptor.getWriteMethod();
    if (method == null)
        throw new NoSuchMethodException("Cannot find setter for " + bean.getClass().getName() + "." + property);
    method.invoke(bean, new Object[] { value });
}

From source file:gobblin.util.reflection.GobblinConstructorUtils.java

/**
 * Returns a new instance of the <code>cls</code> based on a set of arguments. The method will search for a
 * constructor accepting the first k arguments in <code>args</code> for every k from args.length to 0, and will
 * invoke the first constructor found.//from  ww w.j  ava  2 s .  c om
 *
 * For example, {@link #invokeLongestConstructor}(cls, myString, myInt) will first attempt to create an object with
 * of class <code>cls</code> with constructor <init>(String, int), if it fails it will attempt <init>(String), and
 * finally <init>().
 *
 * @param cls the class to instantiate.
 * @param args the arguments to use for instantiation.
 * @throws ReflectiveOperationException
 */
public static <T> T invokeLongestConstructor(Class<T> cls, Object... args) throws ReflectiveOperationException {

    Class<?>[] parameterTypes = new Class[args.length];
    for (int i = 0; i < args.length; i++) {
        parameterTypes[i] = args[i].getClass();
    }

    for (int i = args.length; i >= 0; i--) {
        if (ConstructorUtils.getMatchingAccessibleConstructor(cls,
                Arrays.copyOfRange(parameterTypes, 0, i)) != null) {
            log.info(String.format("Found accessible constructor for class %s with parameter types %s.", cls,
                    Arrays.toString(Arrays.copyOfRange(parameterTypes, 0, i))));
            return ConstructorUtils.invokeConstructor(cls, Arrays.copyOfRange(args, 0, i));
        }
    }
    throw new NoSuchMethodException(
            String.format("No accessible constructor for class %s with parameters a subset of %s.", cls,
                    Arrays.toString(parameterTypes)));
}

From source file:org.apache.gobblin.util.reflection.GobblinConstructorUtils.java

/**
 * Returns a new instance of the <code>cls</code> based on a set of arguments. The method will search for a
 * constructor accepting the first k arguments in <code>args</code> for every k from args.length to 0, and will
 * invoke the first constructor found./*from  w w w  .j  a v a  2  s . c o  m*/
 *
 * For example, {@link #invokeLongestConstructor}(cls, myString, myInt) will first attempt to create an object with
 * of class <code>cls</code> with constructor <init>(String, int), if it fails it will attempt <init>(String), and
 * finally <init>().
 *
 * @param cls the class to instantiate.
 * @param args the arguments to use for instantiation.
 * @throws ReflectiveOperationException
 */
public static <T> T invokeLongestConstructor(Class<T> cls, Object... args) throws ReflectiveOperationException {

    Class<?>[] parameterTypes = new Class[args.length];
    for (int i = 0; i < args.length; i++) {
        parameterTypes[i] = args[i].getClass();
    }

    for (int i = args.length; i >= 0; i--) {
        if (ConstructorUtils.getMatchingAccessibleConstructor(cls,
                Arrays.copyOfRange(parameterTypes, 0, i)) != null) {
            log.debug(String.format("Found accessible constructor for class %s with parameter types %s.", cls,
                    Arrays.toString(Arrays.copyOfRange(parameterTypes, 0, i))));
            return ConstructorUtils.invokeConstructor(cls, Arrays.copyOfRange(args, 0, i));
        }
    }
    throw new NoSuchMethodException(
            String.format("No accessible constructor for class %s with parameters a subset of %s.", cls,
                    Arrays.toString(parameterTypes)));
}

From source file:net.neevek.android.lib.paginize.util.AnnotationUtils.java

private static void setListenersForView(View view, Class[] listenerTypes, Object listener)
        throws InvocationTargetException, IllegalAccessException, NoSuchMethodException,
        InstantiationException {//from w w w . j a v a 2  s  . c  o  m

    for (int j = 0; j < listenerTypes.length; ++j) {
        Class listenerClass = listenerTypes[j];

        if (!listenerClass.isAssignableFrom(listener.getClass())) {
            throw new NotImplementedInterfaceException("When injecting listener for view 0x"
                    + Integer.toHexString(view.getId()) + ", we find that " + listener.getClass().getName()
                    + " does not implement " + listenerClass.getName());
        }

        String methodName = sSetListenerMethodMap.get(listenerClass);
        if (methodName == null) {
            methodName = listenerClass.getSimpleName();

            // for interfaces from android.support.v4.**, Class.getSimpleName() may return names that contain the dollar sign
            // I have no idea whether this is a bug, the following workaround fixes the problem
            int index = methodName.lastIndexOf('$');
            if (index != -1) {
                methodName = methodName.substring(index + 1);
            }
            methodName = "set" + methodName;

            sSetListenerMethodMap.put(listenerClass, methodName);
        }

        try {
            Method method = view.getClass().getMethod(methodName, listenerClass);
            method.invoke(view, listener);
        } catch (NoSuchMethodException e) {
            throw new NoSuchMethodException("No such method: " + listenerClass.getSimpleName() + "."
                    + methodName
                    + ", you have to manually add the set-listener method to sSetListenerMethodMap to support injecting listener for view 0x"
                    + Integer.toHexString(view.getId()));
        }
    }
}

From source file:org.jkcsoft.java.util.Beans.java

public static void set(Object bean, String propName, Object value) throws Exception {
    PropertyDescriptor pd = getPropertyDescriptor(bean, propName);

    if (pd == null) {
        throw new NoSuchFieldException("Unknown property: " + propName);
    }//from   w w w .  java2  s .c om

    Method setter = pd.getWriteMethod();
    if (setter == null) {
        throw new NoSuchMethodException("No write method for: " + propName);
    }

    setter.invoke(bean, new Object[] { value });
}

From source file:org.apache.olingo.ext.proxy.commons.StructuredComposableInvokerInvocationHandler.java

@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    if (method.getName().startsWith("get")) {
        return structuredHandler.invoke(proxy, method, args);
    } else if (method.getName().startsWith("set")) {
        return structuredHandler.invoke(proxy, method, args);
    } else if ("filter".equals(method.getName()) || "orderBy".equals(method.getName())
            || "top".equals(method.getName()) || "skip".equals(method.getName())
            || "expand".equals(method.getName()) || "select".equals(method.getName())) {

        return super.invoke(proxy, method, args);
    } else if ("operations".equals(method.getName()) && ArrayUtils.isEmpty(args)) {
        return super.invoke(proxy, method, args);
    } else if (isSelfMethod(method)) {
        return invokeSelfMethod(method, args);
    } else {//from  ww w .j av  a 2 s .c om
        throw new NoSuchMethodException(method.getName());
    }
}

From source file:org.apache.olingo.ext.proxy.commons.PrimitiveCollectionInvocationHandler.java

@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    if ("filter".equals(method.getName()) || "top".equals(method.getName()) || "skip".equals(method.getName())
            || "execute".equals(method.getName()) || "executeAsync".equals(method.getName())) {

        invokeSelfMethod(method, args);/*  w w  w .  j  a  v a  2s .  c  o m*/
        return proxy;
    } else if (isSelfMethod(method)) {
        return invokeSelfMethod(method, args);
    } else if ("operations".equals(method.getName()) && ArrayUtils.isEmpty(args)) {
        final Class<?> returnType = method.getReturnType();

        return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
                new Class<?>[] { returnType }, OperationInvocationHandler.getInstance(this));
    } else {
        throw new NoSuchMethodException(method.getName());
    }
}

From source file:org.nebularis.defproxy.introspection.MethodInvokerTemplate.java

/**
 * @inheritDoc/*ww w.  jav  a2  s  .c o m*/
 */
@Override
public final Object handleInvocation(final Object delegate, final Object... params) throws Throwable {
    try {
        final Method method = getMethodBySignature(delegate.getClass(), getMethodSignature());
        if (method == null) {
            throw new NoSuchMethodException(String.format("Method %s was not found.", sig.getName()));
        }
        final CallSite site = beforeInvocation(delegate, method, params);
        final Object returnValue = site.dispatch();
        if (converter != null) {
            //noinspection unchecked
            return afterInvocation(converter.convert(returnValue), site);
        }
        return afterInvocation(returnValue, site);
    } catch (Throwable e) {
        return getExceptionHandlingPolicy().handleException(e);
    }
}

From source file:com.bstek.dorado.view.service.AbstractRemoteServiceProcessor.java

protected Object invokeRemoteService(Writer writer, DoradoContext context, String serviceName, Object parameter,
        String[] requiredParameterNames, Type[] requiredParameterTypes, Object[] requiredParameters)
        throws Exception {
    ExposedServiceDefintion exposedService = exposedServiceManager.getService(serviceName);
    if (exposedService == null) {
        throw new IllegalArgumentException("Unknown ExposedService [" + serviceName + "].");
    }//www  .  ja v a2 s. c  o  m
    Object serviceBean = BeanFactoryUtils.getBean(exposedService.getBean());
    String methodName = exposedService.getMethod();
    Method[] methods = MethodAutoMatchingUtils.getMethodsByName(serviceBean.getClass(), methodName);
    if (methods.length == 0) {
        throw new NoSuchMethodException(
                "Method [" + methodName + "] not found in [" + exposedService.getBean() + "].");
    }

    Object returnValue = null;
    context.setAttribute(SERVICE_NAME_ATTRIBUTE, exposedService.getName());

    boolean methodInvoked = false;
    MethodAutoMatchingException[] exceptions = new MethodAutoMatchingException[4];
    int i = 0;
    try {
        try {
            returnValue = invokeByParameterName(serviceBean, methods, parameter, requiredParameterNames,
                    requiredParameters, false);
            methodInvoked = true;
        } catch (MoreThanOneMethodsMatchsException e) {
            throw e;
        } catch (MethodAutoMatchingException e) {
            exceptions[i++] = e;
        } catch (AbortException e) {
            // do nothing
        }

        if (!methodInvoked) {
            try {
                returnValue = invokeByParameterName(serviceBean, methods, parameter, requiredParameterNames,
                        requiredParameters, true);
                methodInvoked = true;
            } catch (MoreThanOneMethodsMatchsException e) {
                throw e;
            } catch (MethodAutoMatchingException e) {
                exceptions[i++] = e;
            } catch (AbortException e) {
                // do nothing
            }
        }

        if (!methodInvoked) {
            try {
                returnValue = invokeByParameterType(serviceBean, methods, parameter, requiredParameterTypes,
                        requiredParameters, false);
                methodInvoked = true;
            } catch (MoreThanOneMethodsMatchsException e) {
                throw e;
            } catch (MethodAutoMatchingException e) {
                exceptions[i++] = e;
            } catch (AbortException e) {
                // do nothing
            }
        }

        if (!methodInvoked) {
            try {
                returnValue = invokeByParameterType(serviceBean, methods, parameter, requiredParameterTypes,
                        requiredParameters, true);
                methodInvoked = true;
            } catch (MoreThanOneMethodsMatchsException e) {
                throw e;
            } catch (MethodAutoMatchingException e) {
                exceptions[i++] = e;
            } catch (AbortException e) {
                // do nothing
            }
        }
    } catch (MethodAutoMatchingException e) {
        exceptions[i++] = e;
    }

    if (methodInvoked) {
        return returnValue;
    } else {
        for (MethodAutoMatchingException e : exceptions) {
            if (e == null) {
                break;
            }
            logger.error(e.getMessage());
        }
        throw new IllegalArgumentException(resourceManager.getString("dorado.common/noMatchingMethodError",
                serviceBean.getClass().getName(), methodName));
    }
}

From source file:org.apache.olingo.ext.proxy.commons.ComplexCollectionInvocationHandler.java

@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    if ("filter".equals(method.getName()) || "orderBy".equals(method.getName())
            || "top".equals(method.getName()) || "skip".equals(method.getName())
            || "expand".equals(method.getName()) || "select".equals(method.getName())
            || "nextPage".equals(method.getName()) || "execute".equals(method.getName())) {

        invokeSelfMethod(method, args);/*w  w  w.  j  av  a2  s.co m*/
        return proxy;
    } else if (isSelfMethod(method)) {
        return invokeSelfMethod(method, args);
    } else if ("operations".equals(method.getName()) && ArrayUtils.isEmpty(args)) {
        final Class<?> returnType = method.getReturnType();

        return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
                new Class<?>[] { returnType }, OperationInvocationHandler.getInstance(this));
    } else {
        throw new NoSuchMethodException(method.getName());
    }
}