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() 

Source Link

Document

Constructs a NoSuchMethodException without a detail message.

Usage

From source file:com.link_intersystems.lang.reflect.SerializableMethodTest.java

@Override
protected Method getMethod(Class<?> declaringClass, String methodName, Class<?>[] parameterTypes)
        throws NoSuchMethodException {
    throw new NoSuchMethodException();
}

From source file:fr.paris.lutece.util.method.MethodUtil.java

/**
 * Gets the primitive method./*from w  w w. ja  v  a  2s  .  co m*/
 *
 * @param <A> the generic type of the instance
 * @param strMethodName the str method name
 * @param instance the instance
 * @param clazz the clazz
 * @return the primitive method
 * @throws SecurityException the security exception
 * @throws NoSuchMethodException the no such method exception
 */
public static <A> Method getPrimitiveMethod(String strMethodName, A instance, Class<?> clazz)
        throws SecurityException, NoSuchMethodException {
    if (clazz.equals(Integer.class)) {
        return instance.getClass().getMethod(strMethodName, new Class[] { int.class });
    } else if (clazz.equals(Long.class)) {
        return instance.getClass().getMethod(strMethodName, new Class[] { long.class });
    } else if (clazz.equals(Double.class)) {
        return instance.getClass().getMethod(strMethodName, new Class[] { double.class });
    } else if (clazz.equals(Short.class)) {
        return instance.getClass().getMethod(strMethodName, new Class[] { short.class });
    } else if (clazz.equals(Byte.class)) {
        return instance.getClass().getMethod(strMethodName, new Class[] { byte.class });
    } else if (clazz.equals(Float.class)) {
        return instance.getClass().getMethod(strMethodName, new Class[] { float.class });
    } else if (clazz.equals(Character.class)) {
        return instance.getClass().getMethod(strMethodName, new Class[] { char.class });
    } else if (clazz.equals(Boolean.class)) {
        return instance.getClass().getMethod(strMethodName, new Class[] { boolean.class });
    }

    throw new NoSuchMethodException();
}

From source file:org.jaffa.util.BeanHelper.java

/** This will inspect the specified java bean, and extract an Object from the beans
 * getXxx() method, where 'xxx' is the field name passed in.
 * A null will be returned in case there is any error in invoking the getter.
 * @param bean The Java Bean./*  w w w .ja va  2 s.c o m*/
 * @param field The field.
 * @throws NoSuchMethodException if there is no getter for the input field.
 * @return the output of the getter for the field.
 */
public static Object getField(Object bean, String field) throws NoSuchMethodException {
    java.lang.reflect.Method method = null;
    try {
        if (bean instanceof DynaBean) {
            try {
                return PropertyUtils.getProperty(bean, field);
            } catch (NoSuchMethodException e) {
                // If the bean is a FlexBean instance, then the field could exist on the associated persistentObject
                if (bean instanceof FlexBean && ((FlexBean) bean).getPersistentObject() != null)
                    return PropertyUtils.getProperty(((FlexBean) bean).getPersistentObject(), field);
                else
                    throw e;
            }
        } else {
            // Get the Java Bean Info
            java.beans.BeanInfo info = java.beans.Introspector.getBeanInfo(bean.getClass());
            if (info != null) {
                // Get all the properties
                java.beans.PropertyDescriptor[] pds = info.getPropertyDescriptors();
                if (pds != null) {
                    // Loop for a matching method
                    for (PropertyDescriptor pd : pds) {
                        if (StringHelper.equalsIgnoreCaseFirstChar(pd.getName(), field)) {
                            // Match found....
                            method = pd.getReadMethod();
                            break;
                        }
                    }
                }
            }
            if (method != null) {
                return method.invoke(bean, new Object[] {});
            }

            // Finally, check the FlexBean
            if (bean instanceof IFlexFields) {
                FlexBean flexBean = ((IFlexFields) bean).getFlexBean();
                if (flexBean != null && flexBean.get(field) != null) {
                    return flexBean.get(field);
                } else {
                    throw new NoSuchMethodException();
                }
            }
        }
    } catch (NoSuchMethodException ex) {
        throw ex;
    } catch (Exception ex) {
        log.error("Introspection of Property " + field + " on Bean " + bean + " failed. Reason : "
                + ex.getMessage(), ex);
        return null;
    }

    // If we reach here, the method was not found
    throw new NoSuchMethodException("Field Name = " + field);
}

From source file:mashapeautoloader.MashapeAutoloader.java

/**
 * @param librayName//from ww w  .ja  v a  2s.  c om
 * @param methodName
 * @param arguments
 * @return JSONObject
 * @throws MalformedURLException
 * @throws IOException
 * 
 * Returns the json result of invoked method on called api
 */
public static JSONObject exec(String librayName, String methodName, String... arguments)
        throws MalformedURLException, IOException {
    // check for local api store
    if (MashapeAutoloader.apiStore == null)
        return null;

    // check for library existance
    if (!downloadLib(librayName))
        return null;

    Object libraryInstance;
    Method method;
    Object result;
    JSONObject trueResult;

    try {
        // instance .class library
        ClassLoaderExt loader = new ClassLoaderExt();

        String filePath = new File(apiStore + librayName + ".class").getAbsolutePath();
        String fileUrl = "file:" + filePath.replace("\\", "/");
        URL myUrl = new URL(fileUrl);
        URLConnection connection = myUrl.openConnection();
        InputStream input = connection.getInputStream();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        int data = input.read();
        while (data != -1) {
            buffer.write(data);
            data = input.read();
        }
        input.close();

        byte[] classData = buffer.toByteArray();
        Class<?> libraryClass = loader.defineClassCallable(librayName, classData);

        // call constructor
        try {
            Constructor<?> constructor = libraryClass.getConstructor(String.class, String.class);
            libraryInstance = constructor.newInstance(publicKey, privateKey);
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        } catch (SecurityException ex) {
            throw new RuntimeException(ex);
        } catch (InstantiationException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalArgumentException ex) {
            throw new RuntimeException(ex);
        } catch (InvocationTargetException ex) {
            throw new RuntimeException(ex);
        }

        // invoke method
        try {
            switch (arguments.length) {
            case 0:
                method = libraryClass.getMethod(methodName);
                result = method.invoke(libraryInstance);
                break;

            case 1:
                method = libraryClass.getMethod(methodName, String.class);
                result = method.invoke(libraryInstance, arguments[0]);
                break;

            case 2:
                method = libraryClass.getMethod(methodName, String.class);
                result = method.invoke(libraryInstance, arguments[0], arguments[1]);
                break;

            case 3:
                method = libraryClass.getMethod(methodName, String.class);
                result = method.invoke(libraryInstance, arguments[0], arguments[1], arguments[2]);
                break;

            case 4:
                method = libraryClass.getMethod(methodName, String.class);
                result = method.invoke(libraryInstance, arguments[0], arguments[1], arguments[2], arguments[3]);
                break;

            case 5:
                method = libraryClass.getMethod(methodName, String.class);
                result = method.invoke(libraryInstance, arguments[0], arguments[1], arguments[2], arguments[3],
                        arguments[4]);
                break;

            default:
                throw new NoSuchMethodException();
            }
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        } catch (SecurityException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalArgumentException ex) {
            throw new RuntimeException(ex);
        } catch (InvocationTargetException ex) {
            throw new RuntimeException(ex);
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    // convert result
    try {
        trueResult = (JSONObject) result;
    } catch (ClassCastException ex) {
        throw new RuntimeException(ex);
    }

    return trueResult;
}

From source file:org.talend.components.netsuite.client.model.beans.Beans.java

/**
 * Get enum accessor for given enum class.
 *
 * @param clazz enum class// w  w w  .j av a  2 s.c o m
 * @return enum accessor
 */
protected static AbstractEnumAccessor getEnumAccessorImpl(Class<? extends Enum> clazz) {
    EnumAccessor accessor = null;
    Method m;
    try {
        m = clazz.getDeclaredMethod("getEnumAccessor", new Class[0]);
        if (!m.getReturnType().equals(EnumAccessor.class)) {
            throw new NoSuchMethodException();
        }
        try {
            accessor = (EnumAccessor) m.invoke(new Object[0]);
        } catch (IllegalAccessException | InvocationTargetException e) {
            LOG.error("Failed to get optimized EnumAccessor: enum class " + clazz.getName(), e);
        }
    } catch (NoSuchMethodException e) {
    }
    if (accessor != null) {
        return new OptimizedEnumAccessor(clazz, accessor);
    } else {
        return new ReflectEnumAccessor(clazz);
    }
}

From source file:Mopex.java

/**
 * Returns a Method that has the signature specified by the calling
 * parameters./*from www .j  ava 2  s  .  c o m*/
 * 
 * @return Method
 * @param cls
 *            java.lang.Class
 * @param name
 *            String
 * @param paramTypes
 *            java.lang.Class[]
 */
//start extract getSupportedMethod
public static Method getSupportedMethod(Class cls, String name, Class[] paramTypes)
        throws NoSuchMethodException {
    if (cls == null) {
        throw new NoSuchMethodException();
    }
    try {
        return cls.getDeclaredMethod(name, paramTypes);
    } catch (NoSuchMethodException ex) {
        return getSupportedMethod(cls.getSuperclass(), name, paramTypes);
    }
}

From source file:ml.shifu.shifu.util.ClassUtils.java

public static Method getDeclaredSetter(String name, Class<?> clazz) throws NoSuchMethodException {
    Method method = getFirstMethodWithName("set" + StringUtils.capitalize(name), clazz);
    if (method == null) {
        throw new NoSuchMethodException();
    }/*from   ww  w  . jav a  2s . com*/
    return method;
}

From source file:org.red5.server.persistence.FilePersistence.java

/**
 * Load resource with given name and attaches to persistable object
 * @param name             Resource name
 * @param object           Object to attach to
 * @return                 Persistable object
 *///  ww w .j av a 2s.c o m
private IPersistable doLoad(String name, IPersistable object) {
    IPersistable result = object;
    Resource data = resources.getResource(name);
    if (data == null || !data.exists()) {
        // No such file
        return null;
    }

    FileInputStream input;
    String filename;
    try {
        File fp = data.getFile();
        if (fp.length() == 0) {
            // File is empty
            log.error("The file at " + data.getFilename() + " is empty.");
            return null;
        }

        filename = fp.getAbsolutePath();
        input = new FileInputStream(filename);
    } catch (FileNotFoundException e) {
        log.error("The file at " + data.getFilename() + " does not exist.");
        return null;
    } catch (IOException e) {
        log.error("Could not load file from " + data.getFilename() + '.', e);
        return null;
    }

    try {
        ByteBuffer buf = ByteBuffer.allocate(input.available());
        try {
            ServletUtils.copy(input, buf.asOutputStream());
            buf.flip();
            Input in = new Input(buf);
            Deserializer deserializer = new Deserializer();
            String className = (String) deserializer.deserialize(in);
            if (result == null) {
                // We need to create the object first
                try {
                    Class theClass = Class.forName(className);
                    Constructor constructor = null;
                    try {
                        // Try to create object by calling constructor with Input stream as
                        // parameter.
                        for (Class interfaceClass : in.getClass().getInterfaces()) {
                            constructor = theClass.getConstructor(new Class[] { interfaceClass });
                            if (constructor != null) {
                                break;
                            }
                        }
                        if (constructor == null) {
                            throw new NoSuchMethodException();
                        }

                        result = (IPersistable) constructor.newInstance(in);
                    } catch (NoSuchMethodException err) {
                        // No valid constructor found, use empty
                        // constructor.
                        result = (IPersistable) theClass.newInstance();
                        result.deserialize(in);
                    } catch (InvocationTargetException err) {
                        // Error while invoking found constructor, use empty
                        // constructor.
                        result = (IPersistable) theClass.newInstance();
                        result.deserialize(in);
                    }
                } catch (ClassNotFoundException cnfe) {
                    log.error("Unknown class " + className);
                    return null;
                } catch (IllegalAccessException iae) {
                    log.error("Illegal access.", iae);
                    return null;
                } catch (InstantiationException ie) {
                    log.error("Could not instantiate class " + className);
                    return null;
                }

                // Set object's properties
                result.setName(getObjectName(name));
                result.setPath(getObjectPath(name, result.getName()));
            } else {
                // Initialize existing object
                String resultClass = result.getClass().getName();
                if (!resultClass.equals(className)) {
                    log.error("The classes differ: " + resultClass + " != " + className);
                    return null;
                }

                result.deserialize(in);
            }
        } finally {
            buf.release();
            buf = null;
        }
        if (result.getStore() != this) {
            result.setStore(this);
        }
        super.save(result);
        if (log.isDebugEnabled()) {
            log.debug("Loaded persistent object " + result + " from " + filename);
        }
    } catch (IOException e) {
        log.error("Could not load file at " + filename);
        return null;
    }

    return result;
}

From source file:org.forgerock.openidm.shell.CustomCommandScope.java

/**
 * Fetch a method specified by name (gets the method with the largest number of parameters).
 * @param name of the method to fetch//from  w w  w  .  j a v a2 s. co  m
 * @return the method with the largest number of parameters by name
 * @throws NoSuchMethodException if no such method exists
 */
protected Method getLongestMethodByName(String name) throws NoSuchMethodException {
    List<Method> methods = getAllMethodsByName(name);
    if (methods.isEmpty()) {
        throw new NoSuchMethodException();
    }
    return methods.get(methods.size() - 1);
}

From source file:org.forgerock.openidm.shell.CustomCommandScope.java

/**
 * Fetch a method specified by name (gets the method with the shortest number of parameters).
 * @param name of the method to fetch/*  ww  w.j  a  v a  2 s .  c o  m*/
 * @return the method with the largest number of parameters by name
 * @throws NoSuchMethodException if no such method exists
 */
protected Method getShortestMethodByName(String name) throws NoSuchMethodException {
    List<Method> methods = getAllMethodsByName(name);
    if (methods.isEmpty()) {
        throw new NoSuchMethodException();
    }
    return methods.get(0);
}