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:com.strider.datadefender.DatabaseAnonymizer.java

/**
 * Calls the anonymization function for the given Column, and returns its
 * anonymized value./* w ww. j  a  va 2s  .  c  o  m*/
 * 
 * @param dbConn
 * @param row
 * @param column
 * @return
 * @throws NoSuchMethodException
 * @throws SecurityException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @throws InvocationTargetException 
 */
private Object callAnonymizingFunctionWithParameters(final Connection dbConn, final ResultSet row,
        final Column column, final String vendor) throws SQLException, NoSuchMethodException, SecurityException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    final String function = column.getFunction();
    if (function == null || function.equals("")) {
        log.warn("Function is not defined for column [" + column + "]. Moving to the next column.");
        return "";
    }

    try {
        final String className = Utils.getClassName(function);
        final String methodName = Utils.getMethodName(function);
        final Class<?> clazz = Class.forName(className);

        final CoreFunctions instance = (CoreFunctions) Class.forName(className).newInstance();
        instance.setDatabaseConnection(dbConn);
        instance.setVendor(vendor);

        final List<Parameter> parms = column.getParameters();
        final Map<String, Object> paramValues = new HashMap<>(parms.size());
        final String columnValue = row.getString(column.getName());

        for (final Parameter param : parms) {
            if (param.getValue().equals("@@value@@")) {
                paramValues.put(param.getName(), columnValue);
            } else if (param.getValue().equals("@@row@@") && param.getType().equals("java.sql.ResultSet")) {
                paramValues.put(param.getName(), row);
            } else {
                paramValues.put(param.getName(), param.getTypeValue());
            }
        }

        final List<Object> fnArguments = new ArrayList<>(parms.size());
        final Method[] methods = clazz.getMethods();
        Method selectedMethod = null;

        methodLoop: for (final Method m : methods) {
            if (m.getName().equals(methodName) && m.getReturnType() == String.class) {

                log.debug("  Found method: " + m.getName());
                log.debug("  Match w/: " + paramValues);

                final java.lang.reflect.Parameter[] mParams = m.getParameters();
                fnArguments.clear();
                for (final java.lang.reflect.Parameter par : mParams) {
                    //log.debug("    Name present? " + par.isNamePresent());
                    // Note: requires -parameter compiler flag
                    log.debug("    Real param: " + par.getName());
                    if (!paramValues.containsKey(par.getName())) {
                        continue methodLoop;
                    }

                    final Object value = paramValues.get(par.getName());
                    Class<?> fnParamType = par.getType();
                    final Class<?> confParamType = (value == null) ? fnParamType : value.getClass();

                    if (fnParamType.isPrimitive() && value == null) {
                        continue methodLoop;
                    }
                    if (ClassUtils.isPrimitiveWrapper(confParamType)) {
                        if (!ClassUtils.isPrimitiveOrWrapper(fnParamType)) {
                            continue methodLoop;
                        }
                        fnParamType = ClassUtils.primitiveToWrapper(fnParamType);
                    }
                    if (!fnParamType.equals(confParamType)) {
                        continue methodLoop;
                    }
                    fnArguments.add(value);
                }

                // actual parameters check less than xml defined parameters size, because values could be auto-assigned (like 'values' and 'row' params)
                if (fnArguments.size() != mParams.length || fnArguments.size() < paramValues.size()) {
                    continue;
                }

                selectedMethod = m;
                break;
            }
        }

        if (selectedMethod == null) {
            final StringBuilder s = new StringBuilder("Anonymization method: ");
            s.append(methodName).append(" with parameters matching (");
            String comma = "";
            for (final Parameter p : parms) {
                s.append(comma).append(p.getType()).append(' ').append(p.getName());
                comma = ", ";
            }
            s.append(") was not found in class ").append(className);
            throw new NoSuchMethodException(s.toString());
        }

        log.debug("Anonymizing function: " + methodName + " with parameters: "
                + Arrays.toString(fnArguments.toArray()));
        final Object anonymizedValue = selectedMethod.invoke(instance, fnArguments.toArray());
        if (anonymizedValue == null) {
            return null;
        }
        return anonymizedValue.toString();

    } catch (InstantiationException | ClassNotFoundException ex) {
        log.error(ex.toString());
    }

    return "";
}

From source file:javadz.beanutils.MethodUtils.java

/**
 * <p>Invoke a method whose parameter types match exactly the parameter
 * types given.</p>/*  ww  w . jav  a  2 s.co m*/
 *
 * <p>This uses reflection to invoke the method obtained from a call to
 * <code>getAccessibleMethod()</code>.</p>
 *
 * @param object invoke method on this object
 * @param methodName get method with this name
 * @param args use these arguments - treat null as empty array
 * @param parameterTypes match these parameters - treat null as empty array
 * @return The value returned by the invoked method
 *
 * @throws NoSuchMethodException if there is no such accessible method
 * @throws InvocationTargetException wraps an exception thrown by the
 *  method invoked
 * @throws IllegalAccessException if the requested method is not accessible
 *  via reflection
 */
public static Object invokeExactMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

    if (args == null) {
        args = EMPTY_OBJECT_ARRAY;
    }

    if (parameterTypes == null) {
        parameterTypes = EMPTY_CLASS_PARAMETERS;
    }

    Method method = getAccessibleMethod(object.getClass(), methodName, parameterTypes);
    if (method == null) {
        throw new NoSuchMethodException(
                "No such accessible method: " + methodName + "() on object: " + object.getClass().getName());
    }
    return method.invoke(object, args);

}

From source file:net.lightbody.bmp.proxy.jetty.xml.XmlConfiguration.java

private void set(Object obj, XmlParser.Node node) throws ClassNotFoundException, NoSuchMethodException,
        InvocationTargetException, IllegalAccessException {
    String attr = node.getAttribute("name");
    String name = "set" + attr.substring(0, 1).toUpperCase() + attr.substring(1);
    Object value = value(obj, node);
    Object[] arg = { value };/*from   w ww .ja va 2  s. c  o m*/

    Class oClass = nodeClass(node);
    if (oClass != null)
        obj = null;
    else
        oClass = obj.getClass();

    Class[] vClass = { Object.class };
    if (value != null)
        vClass[0] = value.getClass();

    if (log.isDebugEnabled())
        log.debug(obj + "." + name + "(" + vClass[0] + " " + value + ")");

    // Try for trivial match
    try {
        Method set = oClass.getMethod(name, vClass);
        set.invoke(obj, arg);
        return;
    } catch (IllegalArgumentException e) {
        LogSupport.ignore(log, e);
    } catch (IllegalAccessException e) {
        LogSupport.ignore(log, e);
    } catch (NoSuchMethodException e) {
        LogSupport.ignore(log, e);
    }

    // Try for native match
    try {
        Field type = vClass[0].getField("TYPE");
        vClass[0] = (Class) type.get(null);
        Method set = oClass.getMethod(name, vClass);
        set.invoke(obj, arg);
        return;
    } catch (NoSuchFieldException e) {
        LogSupport.ignore(log, e);
    } catch (IllegalArgumentException e) {
        LogSupport.ignore(log, e);
    } catch (IllegalAccessException e) {
        LogSupport.ignore(log, e);
    } catch (NoSuchMethodException e) {
        LogSupport.ignore(log, e);
    }

    // Try a field
    try {
        Field field = oClass.getField(attr);
        if (Modifier.isPublic(field.getModifiers())) {
            field.set(obj, value);
            return;
        }
    } catch (NoSuchFieldException e) {
        LogSupport.ignore(log, e);
    }

    // Search for a match by trying all the set methods
    Method[] sets = oClass.getMethods();
    Method set = null;
    for (int s = 0; sets != null && s < sets.length; s++) {
        if (name.equals(sets[s].getName()) && sets[s].getParameterTypes().length == 1) {
            // lets try it
            try {
                set = sets[s];
                sets[s].invoke(obj, arg);
                return;
            } catch (IllegalArgumentException e) {
                LogSupport.ignore(log, e);
            } catch (IllegalAccessException e) {
                LogSupport.ignore(log, e);
            }
        }
    }

    // Try converting the arg to the last set found.
    if (set != null) {
        try {
            Class sClass = set.getParameterTypes()[0];
            if (sClass.isPrimitive()) {
                for (int t = 0; t < __primitives.length; t++) {
                    if (sClass.equals(__primitives[t])) {
                        sClass = __primitiveHolders[t];
                        break;
                    }
                }
            }
            Constructor cons = sClass.getConstructor(vClass);
            arg[0] = cons.newInstance(arg);
            set.invoke(obj, arg);
            return;
        } catch (NoSuchMethodException e) {
            LogSupport.ignore(log, e);
        } catch (IllegalAccessException e) {
            LogSupport.ignore(log, e);
        } catch (InstantiationException e) {
            LogSupport.ignore(log, e);
        }
    }

    // No Joy
    throw new NoSuchMethodException(oClass + "." + name + "(" + vClass[0] + ")");
}

From source file:it.openutils.mgnlaws.magnolia.init.ClasspathProviderImpl.java

private Method getMethod(Class theclass, String methodName) throws NoSuchMethodException {
    Method[] declaredMethods = theclass.getDeclaredMethods();

    for (Method method : declaredMethods) {
        if (method.getName().equals(methodName)) {
            return method;
        }/* w  w w .  j a  va 2  s .  co  m*/
    }

    throw new NoSuchMethodException(theclass.getName() + "." + methodName + "()");
}

From source file:com.ottogroup.bi.streaming.operator.json.aggregate.WindowedJsonContentAggregator.java

/**
 * Aggregates a new value by combining it with an existing value under a given method
 * @param newValue/*from www.  j  a  v  a  2  s  .co m*/
 * @param existingValue
 * @param type
 * @param method
 * @return
 * @throws NoSuchMethodException
 * @throws Exception
 */
@SuppressWarnings("unchecked")
protected Serializable aggregateValue(final Serializable newValue, final Serializable existingValue,
        final JsonContentType type, final ContentAggregator method) throws NoSuchMethodException, Exception {

    JsonContentAggregateFunction<Serializable> function = this.contentAggregatorFunctions.get(type);
    if (function == null)
        throw new NoSuchMethodException(
                "Requested aggregation method '" + method + "' not found for type '" + type + "'");

    if (method == null)
        throw new NoSuchMethodException(
                "Requested aggregation method 'null' not found for type '" + type + "'");

    switch (method) {
    case AVG: {
        return function.average((MutablePair<Serializable, Integer>) existingValue, newValue);
    }
    case MIN: {
        return function.min(existingValue, newValue);
    }
    case MAX: {
        return function.max(existingValue, newValue);
    }
    case SUM: {
        return function.sum(existingValue, newValue);
    }
    default: { // COUNT
        return function.count((Integer) existingValue);
    }
    }
}

From source file:net.mojodna.sprout.SproutAutoLoaderPlugIn.java

/**
 * Finds the method in the target class which corresponds to a registered
 * pathname./*from  w  w  w.  j  a  v a2  s  .c om*/
 * 
 * @param name Action portion of pathname.
 * @param clazz Target class.
 * @return Corresponding method.
 * @throws NoSuchMethodException when corresponding method cannot be found.
 */
private Method findMethod(final String name, final Class clazz) throws NoSuchMethodException {
    final Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        String methodName = methods[i].getName();
        if (methodName.equals("publick"))
            methodName = "public";
        if (methodName.equalsIgnoreCase(name.replaceAll("_([a-z])", "$1")))
            return methods[i];
    }
    throw new NoSuchMethodException(name);
}

From source file:org.jdto.util.MethodUtils.java

/**
 * <p>Invoke a named static method whose parameter type matches the object
 * type.</p>/*w w w  .j  a  v a  2s.  c o  m*/
 *
 * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 *
 * <p>This method supports calls to methods taking primitive parameters via
 * passing in wrapping classes. So, for example, a
 * <code>Boolean</code> class would match a
 * <code>boolean</code> primitive.</p>
 *
 *
 * @param cls invoke static method on this class
 * @param methodName get method with this name
 * @param args use these arguments - treat null as empty array
 * @param parameterTypes match these parameters - treat null as empty array
 * @return The value returned by the invoked method
 *
 * @throws NoSuchMethodException if there is no such accessible method
 * @throws InvocationTargetException wraps an exception thrown by the method
 * invoked
 * @throws IllegalAccessException if the requested method is not accessible
 * via reflection
 */
public static Object invokeStaticMethod(Class cls, String methodName, Object[] args, Class[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    if (parameterTypes == null) {
        parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    Method method = getMatchingAccessibleMethod(cls, methodName, parameterTypes);
    if (method == null) {
        throw new NoSuchMethodException(
                "No such accessible method: " + methodName + "() on class: " + cls.getName());
    }
    return method.invoke(null, args);
}

From source file:com.tmind.framework.pub.utils.MethodUtils.java

/**
 * <p>Invoke a method whose parameter types match exactly the parameter
 * types given.</p>//from w w  w.j av  a 2  s .  com
 *
 * <p>This uses reflection to invoke the method obtained from a call to
 * {@link #getAccessibleMethod}.</p>
 *
 * @param object invoke method on this object
 * @param methodName get method with this name
 * @param args use these arguments - treat null as empty array
 * @param parameterTypes match these parameters - treat null as empty array
 *
 * @throws NoSuchMethodException if there is no such accessible method
 * @throws InvocationTargetException wraps an exception thrown by the
 *  method invoked
 * @throws IllegalAccessException if the requested method is not accessible
 *  via reflection
 */
public static Object invokeExactMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

    if (args == null) {
        args = emptyObjectArray;
    }

    if (parameterTypes == null) {
        parameterTypes = emptyClassArray;
    }

    Method method = getAccessibleMethod(object.getClass(), methodName, parameterTypes);
    if (method == null)
        throw new NoSuchMethodException(
                "No such accessible method: " + methodName + "() on object: " + object.getClass().getName());
    return method.invoke(object, args);

}

From source file:javadz.beanutils.MethodUtils.java

/**
 * <p>Invoke a static method whose parameter types match exactly the parameter
 * types given.</p>/* w  w w  .ja v a  2s  . com*/
 *
 * <p>This uses reflection to invoke the method obtained from a call to
 * {@link #getAccessibleMethod(Class, String, Class[])}.</p>
 *
 * @param objectClass invoke static method on this class
 * @param methodName get method with this name
 * @param args use these arguments - treat null as empty array
 * @param parameterTypes match these parameters - treat null as empty array
 * @return The value returned by the invoked method
 *
 * @throws NoSuchMethodException if there is no such accessible method
 * @throws InvocationTargetException wraps an exception thrown by the
 *  method invoked
 * @throws IllegalAccessException if the requested method is not accessible
 *  via reflection
 * @since 1.8.0
 */
public static Object invokeExactStaticMethod(Class objectClass, String methodName, Object[] args,
        Class[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

    if (args == null) {
        args = EMPTY_OBJECT_ARRAY;
    }

    if (parameterTypes == null) {
        parameterTypes = EMPTY_CLASS_PARAMETERS;
    }

    Method method = getAccessibleMethod(objectClass, methodName, parameterTypes);
    if (method == null) {
        throw new NoSuchMethodException(
                "No such accessible method: " + methodName + "() on class: " + objectClass.getName());
    }
    return method.invoke(null, args);

}

From source file:eu.qualityontime.commons.MethodUtils.java

/**
 * <p>/*from   w w  w.ja v a2  s  .  com*/
 * Invoke a method whose parameter types match exactly the parameter types
 * given.
 * </p>
 *
 * <p>
 * This uses reflection to invoke the method obtained from a call to
 * <code>getAccessibleMethod()</code>.
 * </p>
 *
 * @param object
 *            invoke method on this object
 * @param methodName
 *            get method with this name
 * @param args
 *            use these arguments - treat null as empty array (passing null
 *            will result in calling the parameterless method with name
 *            {@code methodName}).
 * @param parameterTypes
 *            match these parameters - treat null as empty array
 * @return The value returned by the invoked method
 *
 * @throws NoSuchMethodException
 *             if there is no such accessible method
 * @throws InvocationTargetException
 *             wraps an exception thrown by the method invoked
 * @throws IllegalAccessException
 *             if the requested method is not accessible via reflection
 */
public static Object invokeExactMethod(final Object object, final String methodName, Object[] args,
        Class<?>[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

    if (args == null) {
        args = EMPTY_OBJECT_ARRAY;
    }

    if (parameterTypes == null) {
        parameterTypes = EMPTY_CLASS_PARAMETERS;
    }

    final Method method = getAccessibleMethod(object.getClass(), methodName, parameterTypes);
    if (method == null) {
        throw new NoSuchMethodException(
                "No such accessible method: " + methodName + "() on object: " + object.getClass().getName());
    }
    return method.invoke(object, args);
}