【问题标题】:Unable to call inherited method on Java object from coldfusion无法从 Coldfusion 调用 Java 对象上的继承方法
【发布时间】:2013-06-26 22:53:11
【问题描述】:

我正在使用 twitter4j 库,但其中一个类有问题。在我的代码中,我得到了一个从函数返回的RequestToken 实例。我可以将变量转储到屏幕上,然后看到它实际上是正确的类。它有 2 个自己的公共方法,我可以毫无问题地使用,但它有 6 个从 OAuthToken 继承的公共方法,我不能使用。当我尝试访问其中任何一个时,Coldfusion 会引发错误:

Either there are no methods with the specified method name and argument types, or 
the getTokenSecret method is overloaded with argument types that ColdFusion cannot
decipher reliably. ColdFusion found 0 methods that matched the provided arguments. 
If this is a Java object and you verified that the method exists, you may need to 
use the javacast function to reduce ambiguity.

部分相关代码:

<cfset twitterFactory = createObject("java", "twitter4j.TwitterFactory").init(config)>
<cfset twitter = twitterFactory.getInstance()>
<cfset RequestToken = twitter.getOAuthRequestToken()>
<cfset TokenSecret = RequestToken.getTokenSecret()>

转储 RequestToken 我可以在转储中看到 java 类名,并显示方法。

我需要使用的两个方法是getToken() 和getTokenSecret()。也不接受任何参数,因此没有什么可以使用 javacast。

使用coldfusion8,以及最新的twitter4j release 3.0.3

【问题讨论】:

    标签: coldfusion twitter4j


    【解决方案1】:

    在我看来一切都是正确的:类和方法都是public可能是一个错误。 CF 使用reflection 来识别和调用方法。它通常是正确的,但并非总是如此。我已经看到在旧版本中发生过一次或两次。通常使用继承的方法。

    如果这是问题所在,您可以自己使用反射作为解决方法:

    <cfscript>
        emptyArray = [];
        method = RequestToken.getClass().getMethod("getTokenSecret", emptyArray);
        result = method.invoke( RequestToken, emptyArray);
    </cfscript>
    

    【讨论】:

    • 非常感谢您的工作!仍然对它一开始没有用感到困惑/恼火,但至少我现在可以继续前进。
    • 从我所见,原始代码应该可以正常工作。这就是为什么我怀疑一个错误。您可能想检查错误数据库,只是为了查看是否在以后的版本中报告和/或修复了任何类似的内容。无论如何,很高兴解决方法有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多