List of usage examples for java.lang NoSuchMethodException NoSuchMethodException
public NoSuchMethodException(String s)
NoSuchMethodException
with a detail message. From source file:eu.qualityontime.commons.MethodUtils.java
/** * <p>// www.jav a2 s . com * Invoke a named method whose parameter type matches the object type. * </p> * * <p> * The behaviour of this method is less deterministic than * {@link #invokeExactMethod(Object object,String methodName,Object [] args,Class[] parameterTypes)}. * It loops through all methods with names that match and then executes the * first it finds with compatible parameters. * </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 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 invokeMethod(final Object object, final String methodName, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { if (parameterTypes == null) { parameterTypes = EMPTY_CLASS_PARAMETERS; } if (args == null) { args = EMPTY_OBJECT_ARRAY; } final Method method = getMatchingAccessibleMethod(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:org.apache.sling.commons.proxy.impl.SlingInvocationHandler.java
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { final InvokedTO to = InvokedTOFactory.newInstance(proxy, method, args); if (to.isGetter()) { if (Annotations.methodHasAnnotation(method, SlingReference.class)) { return (this.handleGetReference((BaseInvokedTO) to)); } else if (Annotations.methodHasAnnotation(method, SlingChildren.class)) { return (this.handleGetChildren((InvokedChildrenTO) to)); } else {// w w w .jav a 2 s. c o m return (this.handleGetProperty((InvokedPropertyTO) to)); } } else if (to.isType(MethodType.JavaBeanSet)) { throw new UnsupportedOperationException("Setter methods not yet implemented."); } else if (to.isType(MethodType.ToString)) { return new GetMethodToStringImpl().toString(proxy); } else if (to.isType(MethodType.HashCode)) { return new JDPHashCodeImpl().hashCode(proxy); } else if (to.isType(MethodType.Equals)) { if ((args == null) || (args.length != 1)) { final String msg = "Method 'equals' requires exactly 1 argument."; throw new IllegalArgumentException(msg); } return new JDPEqualsImpl().equals(proxy, args[0]); } else if (to.isType(MethodType.BackingResource)) { return this.r; } throw new NoSuchMethodException("Method " + method.getName() + " DNE"); }
From source file:org.mule.util.ClassUtils.java
public static <T> T instanciateClass(Class<? extends T> clazz, Object... constructorArgs) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Class<?>[] args;// ww w . j a va 2s . com if (constructorArgs != null) { args = new Class[constructorArgs.length]; for (int i = 0; i < constructorArgs.length; i++) { if (constructorArgs[i] == null) { args[i] = null; } else { args[i] = constructorArgs[i].getClass(); } } } else { args = new Class[0]; } // try the arguments as given //Constructor ctor = clazz.getConstructor(args); Constructor<?> ctor = getConstructor(clazz, args); if (ctor == null) { // try again but adapt value classes to primitives ctor = getConstructor(clazz, wrappersToPrimitives(args)); } if (ctor == null) { StringBuffer argsString = new StringBuffer(100); for (Class<?> arg : args) { argsString.append(arg.getName()).append(", "); } throw new NoSuchMethodException( "could not find constructor on class: " + clazz + ", with matching arg params: " + argsString); } return (T) ctor.newInstance(constructorArgs); }
From source file:com.tmind.framework.pub.utils.MethodUtils.java
/** * <p>Invoke a named method whose parameter type matches the object type.</p> * * <p>The behaviour of this method is less deterministic * than {@link//from w w w.j a v a 2s. co m * #invokeExactMethod(Object object,String methodName,Object [] args,Class[] parameterTypes)}. * It loops through all methods with names that match * and then executes the first it finds with compatable parameters.</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 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 invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { if (parameterTypes == null) { parameterTypes = emptyClassArray; } if (args == null) { args = emptyObjectArray; } Method method = getMatchingAccessibleMethod(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:org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.java
private Method findMethod(Class<?> cls, String baseName) throws NoSuchMethodException { Method[] publicMethods = cls.getMethods(); String capitalized = StringUtils.capitalize(baseName); for (Method m : publicMethods) { if (m.getParameterTypes().length == 0) { String methodName = m.getName(); if (baseName.equals(methodName) || ("get" + capitalized).equals(methodName) || ("is" + capitalized).equals(methodName)) { // this method is good, check whether allowed if (isMethodAllowed(m)) { return m; }// ww w.j a v a2s . c o m // method would match but is not allwed, abort break; } } } throw new NoSuchMethodException(baseName); }
From source file:org.xwiki.filter.xml.internal.serializer.DefaultXMLSerializer.java
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = null;//from w ww.j a v a 2 s. com if (method.getName().startsWith("begin")) { beginEvent(method.getName(), args); } else if (method.getName().startsWith("end")) { endEvent(); } else if (method.getName().startsWith("on")) { onEvent(method.getName(), args); } else { throw new NoSuchMethodException(method.toGenericString()); } return result; }
From source file:com.netspective.sparx.form.action.ActionWrapper.java
public void locateExecutor() throws NoSuchMethodException { final Method execMethod = getMethod(executeMethodName, Writer.class) == null ? getMethod(executeMethodName, (Class[]) null) : getMethod(executeMethodName, Writer.class); if (execMethod == null) throw new NoSuchMethodException("Unable to find " + executeMethodName + "() or " + executeMethodName + "(Writer) method in " + actionSchema.getBean()); if (execMethod.getParameterTypes().length > 0) { if (execMethod.getReturnType() == String.class) { actionExecutor = new ActionExecutor() { public String execute(Writer writer, Object instance) throws Exception { return (String) execMethod.invoke(instance, new Object[] { writer }); }//from w w w . jav a 2 s . c om }; } else { actionExecutor = new ActionExecutor() { public String execute(Writer writer, Object instance) throws Exception { execMethod.invoke(instance, new Object[] { writer }); return null; } }; } } else { if (execMethod.getReturnType() == String.class) { actionExecutor = new ActionExecutor() { public String execute(Writer writer, Object instance) throws Exception { return (String) execMethod.invoke(instance, null); } }; } else { actionExecutor = new ActionExecutor() { public String execute(Writer writer, Object instance) throws Exception { execMethod.invoke(instance, null); return null; } }; } } }
From source file:com.github.aliteralmind.codelet.TagletOfTypeProcessor.java
/** <p>Invoke the customizer and get its return value, as required by the taglet-processor--For {@code {@.codelet.and.out}} taglets only.</p> /*from w w w . j a va 2 s .c om*/ <p>This {@linkplain com.github.aliteralmind.codelet.simplesig.SimpleMethodSignature#getMethodFromParamValueList(List) obtains} and then {@linkplain java.lang.reflect.Method#invoke(Object, Object...) invokes} the customizer signature, and returns its return value, which must be a {@code CustomizationInstructions} of the type required by the processor.</p> * @param customizer_sig May not be {@code null}, and the function in represents must conform to the requirements. * @exception IllegalArgumentException If <br/> <code><i>[the-instructions-returned-by-the-customizer]</i>.{@link CustomizationInstructions#getNeededAlterArrayType() getNeededAlterArrayType}()</code> <br/>is different than <code>needed_defaultAlterType</code>. See <br/> <code>{@link CustomizationInstructions#CustomizationInstructions(CodeletInstance, CodeletType)}</code> */ public CustomizationInstructions<T> getInstructionsFromSig(SimpleMethodSignature customizer_sig, CodeletType needed_defaultAlterType) throws ClassNotFoundException, NoSuchMethodException, SecurityException { List<Object> paramValueList = null; try { //Must add the non-extra parameters to the object list. Since they're //not "simple" types (primitives or strings), they can't added to the string-signature. paramValueList = customizer_sig.getParamValueObjectList(); paramValueList.add(0, getInstance()); paramValueList.add(1, needed_defaultAlterType); } catch (RuntimeException rx) { throw CrashIfObject.nullOrReturnCause(customizer_sig, "customizer_sig", null, rx); } Method customizer = null; try { customizer = customizer_sig.getMethodFromParamValueList(paramValueList); } catch (NoSuchMethodException nsmx) { throw new NoSuchMethodException("Customizer function not found (is its containing class compiled?):" + LINE_SEP + " - class.function: " + customizer_sig.getStringSig_ret_class_func_params(false, true, true, true) + LINE_SEP + " - Taglet: " + getInstance() + LINE_SEP + " - Original error: <<" + nsmx + ">>"); } boolean doDebug = isDebugOn(getInstance(), "zzTagletOfTypeProcessor.getCustomizerSigFromString"); if (doDebug) { debugln(" Customizer method obtained..."); } customizer.setAccessible(true); if (doDebug) { debugln(" ...made accessible..."); } /* //The below call to sstatic() makes this redundant if(!Modifier.isStatic(customizer.getModifiers())) { throw new NoSuchMethodException("Method found, but not static: " + customizer_sig); } */ Object o = new InvokeMethodWithRtx(customizer).sstatic().parameters(paramValueList.toArray()) .invokeGetReturnValueWXtraInfo("Attempting to execute the *static* Codelet customizer."); if (doDebug) { debugln(" ...invoked..."); } try { @SuppressWarnings("unchecked") CustomizationInstructions<T> instructions = (CustomizationInstructions<T>) o; if (doDebug) { debugln(" ...return-value cast properly to CustomizationInstructions<T> (done)"); } if (instructions.getNeededAlterArrayType() != needed_defaultAlterType) { throw new IllegalArgumentException( "The returned CustomizationInstructions' getNeededAlterArrayType() (" + instructions.getNeededAlterArrayType() + ") is different than needed_defaultAlterType (" + needed_defaultAlterType + ")."); } return instructions; } catch (ClassCastException ccx) { throw new IllegalStateException( "Customizer invoked properly, but its return value cannot be cast to a C (CustomizationInstructions): return-type=" + o.getClass().getName() + ", value=[" + o + "]"); } }
From source file:com.google.code.siren4j.util.ReflectionUtils.java
private static Method _findMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes) throws NoSuchMethodException { Method method = null;// w w w .j av a 2s . com try { method = clazz.getMethod(methodName, parameterTypes); } catch (NoSuchMethodException e) { try { method = clazz.getDeclaredMethod(methodName, parameterTypes); } catch (NoSuchMethodException ignore) { } } if (method == null) { Class<?> superClazz = clazz.getSuperclass(); if (superClazz != null) { method = _findMethod(superClazz, methodName, parameterTypes); } if (method == null) { throw new NoSuchMethodException("Method: " + methodName); } else { return method; } } else { return method; } }
From source file:org.kawanfw.sql.servlet.DatabaseMetaDataExecutor.java
/** * //from w ww. j a va2s . c om * Calls a remote metadata method from the PC <br> * * @throws IOException * all network, etc. errors * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException * @throws NoSuchMethodException * @throws SecurityException * @throws InvocationTargetException * @throws IllegalArgumentException */ private void callMetaDataFunction(HttpServletRequest request, OutputStream out, Connection connection) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { // The method name String methodName = request.getParameter(Parameter.METHOD_NAME); // methodName = HtmlConverter.fromHtml(methodName); // The parms name String paramsTypes = request.getParameter(Parameter.PARAMS_TYPES); String paramsValues = request.getParameter(Parameter.PARAMS_VALUES); // Make sure all values are not null and trimed methodName = this.getTrimValue(methodName); paramsTypes = this.getTrimValue(paramsTypes); paramsValues = this.getTrimValue(paramsValues); debug("actionInvokeRemoteMethod:methodName : " + methodName); // paramsTypes = HtmlConverter.fromHtml(paramsTypes); // paramsValues = HtmlConverter.fromHtml(paramsValues); List<String> listParamsTypes = ListOfStringTransport.fromJson(paramsTypes); List<String> listParamsValues = ListOfStringTransport.fromJson(paramsValues); debug("actionInvokeRemoteMethod:listParamsTypes : " + listParamsTypes); debug("actionInvokeRemoteMethod:listParamsValues : " + listParamsValues); DatabaseMetaData databaseMetaData = connection.getMetaData(); // Trap DatabaseMetaData.getTables() & DatabaseMetaData.getUDTs() // that have special array String[] or int[] parameters if (methodName.equals("getTables") || methodName.equals("getUDTs") || methodName.equals("getPrimaryKeys")) { DatabaseMetaDataSpecial databaseMetaDataSpecial = new DatabaseMetaDataSpecial(databaseMetaData, methodName, listParamsValues); ResultSet rs = databaseMetaDataSpecial.execute(); dumpResultSetOnServletOutStream(rs); return; } @SuppressWarnings("rawtypes") Class[] argTypes = new Class[listParamsTypes.size()]; Object[] values = new Object[listParamsValues.size()]; for (int i = 0; i < listParamsTypes.size(); i++) { String value = listParamsValues.get(i); String javaType = listParamsTypes.get(i); JavaValueBuilder javaValueBuilder = new JavaValueBuilder(javaType, value); argTypes[i] = javaValueBuilder.getClassOfValue(); values[i] = javaValueBuilder.getValue(); // Trap NULL values if (values[i].equals("NULL")) { values[i] = null; } debug("argTypes[i]: " + argTypes[i]); debug("values[i] : " + values[i]); } Class<?> c = Class.forName("java.sql.DatabaseMetaData"); Object theObject = databaseMetaData; // Invoke the method Method main = null; Object resultObj = null; // Get the Drvier Info String database = ""; String productVersion = ""; String DriverName = ""; String DriverVersion = ""; String driverInfo = Tag.PRODUCT; // try { // database = databaseMetaData.getDatabaseProductName(); // productVersion = databaseMetaData.getDatabaseProductVersion(); // DriverName = databaseMetaData.getDriverName(); // DriverVersion= databaseMetaData.getDriverVersion(); // driverInfo += database + " " + productVersion + " " + DriverName + // " " + DriverVersion; // } catch (Exception e1) { // ServerLogger.getLogger().log(Level.WARNING, Tag.PRODUCT + // "Impossible to get User Driver info."); // } database = databaseMetaData.getDatabaseProductName(); productVersion = databaseMetaData.getDatabaseProductVersion(); DriverName = databaseMetaData.getDriverName(); DriverVersion = databaseMetaData.getDriverVersion(); driverInfo += database + " " + productVersion + " " + DriverName + " " + DriverVersion; String methodParams = getMethodParams(values); try { main = c.getDeclaredMethod(methodName, argTypes); } catch (SecurityException e) { throw new SecurityException(driverInfo + " - Security - Impossible to get declared DatabaseMetaData." + methodName + "(" + methodParams + ")"); } catch (NoSuchMethodException e) { throw new NoSuchMethodException( driverInfo + " - No Such Method - Impossible get declared DatabaseMetaData." + methodName + "(" + methodParams + ")"); } try { resultObj = main.invoke(theObject, values); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( driverInfo + " - Impossible to call DatabaseMetaData." + methodName + "(" + methodParams + ")"); } catch (IllegalAccessException e) { throw new IllegalAccessException(driverInfo + " - Impossible to access DatabaseMetaData method." + methodName + "(" + methodParams + ")"); } catch (InvocationTargetException e) { throw new InvocationTargetException(e, driverInfo + " - Impossible to invoke DatabaseMetaData method." + methodName + "(" + methodParams + ")"); } if (resultObj instanceof ResultSet) { ResultSet rs = (ResultSet) resultObj; dumpResultSetOnServletOutStream(rs); } else { // All other formats are handled in String String result = null; if (resultObj != null) result = resultObj.toString(); debug("actionInvokeRemoteMethod:result: " + result); result = HtmlConverter.toHtml(result); //out.println(TransferStatus.SEND_OK); //out.println(result); ServerSqlManager.writeLine(out, TransferStatus.SEND_OK); ServerSqlManager.writeLine(out, result); } }