List of usage examples for java.lang NoSuchMethodException NoSuchMethodException
public NoSuchMethodException(String s)
NoSuchMethodException
with a detail message. From source file:Main.java
/** * find the Appropriate method/*w w w . j a va2 s . co m*/ * @throws NoSuchMethodException if not find */ public static Method getAppropriateMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) throws NoSuchMethodException { try { return clazz.getMethod(methodName, paramTypes); } catch (NoSuchMethodException e) { final List<Method> ms = getMethods(clazz, methodName); final int size = ms.size(); switch (size) { case 0: throw new NoSuchMethodException( "can't find the method , methodName = " + methodName + " ,classname = " + clazz.getName()); case 1: return ms.get(0); default: throw new NoSuchMethodException("You should not have multi methods with the same name of " + methodName + "in class " + clazz.getName() + "( that means don't burden method )!"); } } }
From source file:Main.java
/** * search the method and return the defined method. * it will {@link Class#getMethod(String, Class[])}, if exception occurs, * it will search for all methods, and find the most fit method. *///w w w . j a va 2 s. com public static Method searchMethod(Class<?> currentClass, String name, Class<?>[] parameterTypes, boolean boxed) throws NoSuchMethodException { if (currentClass == null) { throw new NoSuchMethodException("class == null"); } try { return currentClass.getMethod(name, parameterTypes); } catch (NoSuchMethodException e) { Method likeMethod = null; for (Method method : currentClass.getMethods()) { if (method.getName().equals(name) && parameterTypes.length == method.getParameterTypes().length && Modifier.isPublic(method.getModifiers())) { if (parameterTypes.length > 0) { Class<?>[] types = method.getParameterTypes(); boolean eq = true; boolean like = true; for (int i = 0; i < parameterTypes.length; i++) { Class<?> type = types[i]; Class<?> parameterType = parameterTypes[i]; if (type != null && parameterType != null && !type.equals(parameterType)) { eq = false; if (boxed) { type = getBoxedClass(type); parameterType = getBoxedClass(parameterType); } if (!type.isAssignableFrom(parameterType)) { eq = false; like = false; break; } } } if (!eq) { if (like && (likeMethod == null || likeMethod.getParameterTypes()[0] .isAssignableFrom(method.getParameterTypes()[0]))) { likeMethod = method; } continue; } } return method; } } if (likeMethod != null) { return likeMethod; } throw e; } }
From source file:Main.java
/** * Locates a given method anywhere in the class inheritance hierarchy. * * @param instance an object to search the method into. * @param name method name// w w w .j a v a 2s . c o m * @param parameterTypes method parameter types * @return a method object * @throws NoSuchMethodException if the method cannot be located */ static Method findMethod(Object instance, String name, Class<?>... parameterTypes) throws NoSuchMethodException { for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) { try { Method method = clazz.getDeclaredMethod(name, parameterTypes); if (!method.isAccessible()) { method.setAccessible(true); } return method; } catch (NoSuchMethodException e) { // ignore and search next } } throw new NoSuchMethodException("Method " + name + " with parameters " + Arrays.asList(parameterTypes) + " not found in " + instance.getClass()); }
From source file:IntrospectionUtil.java
public static Method findMethod(Class clazz, String methodName, Class[] args, boolean checkInheritance, boolean strictArgs) throws NoSuchMethodException { if (clazz == null) throw new NoSuchMethodException("No class"); if (methodName == null || methodName.trim().equals("")) throw new NoSuchMethodException("No method name"); Method method = null;/*from w ww . ja v a 2 s . c om*/ Method[] methods = clazz.getDeclaredMethods(); for (int i = 0; i < methods.length && method == null; i++) { if (methods[i].getName().equals(methodName) && checkParams(methods[i].getParameterTypes(), (args == null ? new Class[] {} : args), strictArgs)) { method = methods[i]; } } if (method != null) { return method; } else if (checkInheritance) return findInheritedMethod(clazz.getPackage(), clazz.getSuperclass(), methodName, args, strictArgs); else throw new NoSuchMethodException("No such method " + methodName + " on class " + clazz.getName()); }
From source file:com.rockagen.commons.util.SysUtil.java
/** * *//from www. j a v a 2 s .co m * * @param url url * @throws Exception exception if occur */ public static void browse(String url) throws Exception { if (OS_NAME.startsWith("Mac OS")) { Method openURL = ClassUtil.getDeclaredMethod(Class.forName("com.apple.eio.FileManager"), true, "openURL", String.class); openURL.invoke(null, url); } else if (OS_NAME.startsWith("Windows")) { Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); } else { // assume Unix or Linux String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; String browser = null; for (int count = 0; count < browsers.length && browser == null; count++) if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0) browser = browsers[count]; if (browser == null) throw new NoSuchMethodException("Could not find web browser"); else Runtime.getRuntime().exec(new String[] { browser, url }); } }
From source file:JarClassLoader.java
public void invokeClass(String name, String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException { Class c = loadClass(name);/*www . ja va2s . c o m*/ Method m = c.getMethod("main", new Class[] { args.getClass() }); m.setAccessible(true); int mods = m.getModifiers(); if (m.getReturnType() != void.class || !Modifier.isStatic(mods) || !Modifier.isPublic(mods)) { throw new NoSuchMethodException("main"); } try { m.invoke(null, new Object[] { args }); } catch (IllegalAccessException e) { } }
From source file:org.gradle.internal.reflect.JavaMethod.java
private static Method findMethod(Class origTarget, Class target, String name, boolean allowStatic, Class<?>[] paramTypes) { for (Method method : target.getDeclaredMethods()) { if (!allowStatic && Modifier.isStatic(method.getModifiers())) { continue; }//from www .j a v a 2 s. c o m if (method.getName().equals(name) && Arrays.equals(method.getParameterTypes(), paramTypes)) { return method; } } Class<?> parent = target.getSuperclass(); if (parent == null) { throw new NoSuchMethodException(String.format("Could not find method %s(%s) on %s.", name, StringUtils.join(paramTypes, ", "), origTarget.getSimpleName())); } else { return findMethod(origTarget, parent, name, allowStatic, paramTypes); } }
From source file:Main.java
public static <T extends Object, Result> Result callMethodCast(T receiver, Class<Result> resultClass, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { if (receiver == null || methodName == null) { return null; }/*from w ww . j a v a2s . com*/ Class<?> cls = receiver.getClass(); Method toInvoke = null; do { Method[] methods = cls.getDeclaredMethods(); methodLoop: for (Method method : methods) { if (!methodName.equals(method.getName())) { continue; } Class<?>[] paramTypes = method.getParameterTypes(); if (args == null && paramTypes == null) { toInvoke = method; break; } else if (args == null || paramTypes == null || paramTypes.length != args.length) { continue; } for (int i = 0; i < args.length; ++i) { if (!paramTypes[i].isAssignableFrom(args[i].getClass())) { continue methodLoop; } } toInvoke = method; } } while (toInvoke == null && (cls = cls.getSuperclass()) != null); Result t; if (toInvoke != null) { boolean accessible = toInvoke.isAccessible(); toInvoke.setAccessible(true); if (resultClass != null) { t = resultClass.cast(toInvoke.invoke(receiver, args)); } else { toInvoke.invoke(receiver, args); t = null; } toInvoke.setAccessible(accessible); return t; } else { throw new NoSuchMethodException("Method " + methodName + " not found"); } }
From source file:nl.talsmasoftware.enumerables.support.json.jackson2.Compatibility.java
@SuppressWarnings("unchecked") private static <T> T call(Object target, String method) throws NoSuchMethodException { try {// ww w . ja va 2 s . c o m return (T) method(target.getClass(), method).invoke(target); } catch (IllegalAccessException iae) { NoSuchMethodException nsme = new NoSuchMethodException( String.format("Not allowed to call method \"%s\": %s", method, iae.getMessage())); nsme.initCause(iae); throw nsme; } catch (InvocationTargetException ite) { Throwable cause = ite.getCause(); if (cause == null) cause = ite; // shouldn't happen! throw cause instanceof RuntimeException ? (RuntimeException) cause : new RuntimeException(cause.getMessage(), cause); } }
From source file:com.mycompany.selenium.factory.Page.java
public void takeAction(String action, Object... param) throws Throwable { action = action.replaceAll(" ", "_"); try {//from w w w .j a v a2s . c om MethodUtils.invokeMethod(this, action, param); } catch (NoSuchMethodException e) { StringBuilder sb = new StringBuilder(); sb.append("There is no \"").append(action).append("\" action ").append("in ").append(this.getTitle()) .append(" page object").append("\n"); sb.append("Possible actions are:").append("\n"); Class tClass = this.getClass(); Method[] methods = tClass.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { sb.append("\t\"").append(this.getTitle()).append("\"->\"").append(methods[i].getName()) .append("\" with ").append(methods[i].getGenericParameterTypes().length) .append(" input parameters").append("\n"); } throw new NoSuchMethodException(sb.toString()); } catch (InvocationTargetException ex) { throw ex.getCause(); } }