我試圖通過對(duì)已通過其 JNDI 引用查找的 EJB 進(jìn)行反射來調(diào)用方法。它需要三個(gè)參數(shù):一個(gè) EndUser 對(duì)象(自定義對(duì)象)、一個(gè) Set(自定義類)和一個(gè)布爾值。第一個(gè)對(duì)象導(dǎo)致調(diào)用失敗并顯示“無法調(diào)用方法:java.lang.IllegalArgumentException:參數(shù)類型不匹配”。只要第一個(gè)參數(shù)不為空,就會(huì)發(fā)生這種情況。將其設(shè)置為 null 會(huì)使錯(cuò)誤消失。實(shí)際調(diào)用: public Relation createRelation(final Relation relation, final HashSet<Contact> contacts) { final EndUser user = new EndUser(); Object[] args = new Object[]{user, contacts, false}; try { return (Relation) EjbUtils.invoke("registerEndUser", REGISTRATION_SERVICE_JNDI, args); } catch (final Throwable throwable) { LOGGER.error("Could not invoke method", throwable); return null; }}EjbUtils 方法: public static Object invoke(final String methodName, final String ejbName, final Object... args) throws Throwable { final String jndiName = getEjbJndi(ejbName); final Object remoteObject = lookup(jndiName); final Method[] methods = remoteObject.getClass().getMethods(); for (final Method method : methods) { if (methodName.equals(method.getName()) && args.length == method.getParameterCount()) { try { return method.invoke(remoteObject, args); } catch (IllegalAccessException e) { final String message = String.format("Could not invoke method %s on %s: %s", methodName, ejbName, e.getMessage()); LOGGER.error(message); throw new IllegalStateException(message, e); } catch (InvocationTargetException e) { throw e.getCause(); } } } throw new IllegalArgumentException("Method not found"); }我試圖調(diào)用的方法: public Relation registerEndUser(final EndUser relation, final Set<Contact> contacts, boolean sendMail) throws RegistrationServiceException, RegistrationServiceWarning { return registerRelation(relation, contacts, sendMail); }沒有拋出異常并且調(diào)用了該方法,這應(yīng)該表明參數(shù)的類型正確。調(diào)試時(shí)我可以看到找到了正確的方法,并且所需的參數(shù)類型與我提供的相同。關(guān)于如何找出實(shí)際問題的任何想法?
1 回答

紫衣仙女
TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊
我已經(jīng)能夠找到原因。我使用遠(yuǎn)程查找和反射的原因是我在另一個(gè) EAR 中調(diào)用 EJB。結(jié)果,EndUser類已由不同的類加載器創(chuàng)建,因此未被識(shí)別為所需類的實(shí)例。
添加回答
舉報(bào)
0/150
提交
取消