【问题标题】:Java cfObject with method names that are CF reserved words方法名称为 CF 保留字的 Java cfObject
【发布时间】:2012-07-06 04:05:18
【问题描述】:

我一直在研究 ColdFusion 中的 Braintree 集成。 Braintree 不直接支持 CF,但它们提供了一个 Java 库,到目前为止我所做的一切都运行良好......直到现在。似乎某些对象(特别是搜索功能)具有无法从 CF 访问的方法,我怀疑这是因为它们是 CF 保留字,例如“is”和“contains”。有没有办法解决这个问题?

<cfscript>
gate = createObject( "java", "com.braintreegateway.BraintreeGateway" ).init(env,merchant.getMerchantAccountId(), merchant.getMerchantAccountPublicSecret(),merchant.getMerchantAccountPrivateSecret());
req = createObject( "java","com.braintreegateway.CustomerSearchRequest").id().is("#user.getUserId()#");
customer = gate.customer().search(req);
</cfscript>

抛出的错误:无效的 CFML 构造 ... ColdFusion 正在查看以下文本:是

【问题讨论】:

    标签: java coldfusion braintree


    【解决方案1】:

    这表示 CF 编译器中的一个错误。 CF 中没有规定不能定义称为is()this() 的方法,实际上在基本情况下调用它们也没有问题。此代码演示:

    <!--- Junk.cfc --->
    <cfcomponent>
        <cffunction name="is">
            <cfreturn true>
        </cffunction>
        <cffunction name="contains">
            <cfreturn true>
        </cffunction>
    </cfcomponent>
    
    <!--- test.cfm --->
    <cfset o = new Junk()>
    
    <cfoutput>
        #o.is()#<br />
        #o.contains()#<br />
    </cfoutput>
    

    这 - 可以预见 - 输出:

    true
    true
    

    但是,如果我们向 Junk.cfc 引入 init() 方法,我们就会遇到问题,因此:

    <cffunction name="init">
        <cfreturn this>
    </cffunction>
    

    然后相应地调整test.cfm:

    #o.init().is()#<br />
    #o.init().contains()#<br />
    

    这会导致编译器错误:

    在第 4 行第 19 列发现无效的 CFML 构造。

    ColdFusion 正在查看以下文本:

    [...]

    coldfusion.compiler.ParseException:在第 4 行第 19 列发现无效的 CFML 构造。

    at coldfusion.compiler.cfml40.generateParseException(cfml40.java:12135)
    

    [等]

    如果o.is() 没问题,那么o.init().is() 不应该没问题是没有正当理由的。

    我推荐你file a bug。我会投票给它。

    作为一种解决方法,如果您使用中间值而不是方法链,则应该没问题。

    【讨论】:

    • 哦,中间值没有区别。
    • 嗯?请向我们展示修改后的代码和确切的错误消息。干杯。
    【解决方案2】:

    您可能可以使用Java Reflection API 来调用对象的is() 方法。

    我还会致电 Adob​​e,看看他们是否会修复它或提供他们自己的解决方法。我可以理解不允许定义您自己的方法或名为“is”的变量,但尝试在这里调用它应该是安全的。

    【讨论】:

      【解决方案3】:

      这是解决此问题的方法。至少有一个修复程序可以让您启动并运行。

      试试这个代码。

      <cfscript>
          //Get our credentials here, this is a custom private function I have, so your mileage may vary
          credentials = getCredentials();
      
          //Supply the credentials for the gateway    
          gateway = createObject("java", "com.braintreegateway.BraintreeGateway" ).init(credentials.type, credentials.merchantId, credentials.publicKey, credentials.privateKey);
      
          //Setup the customer search object  
          customerSearch = createObject("java", "com.braintreegateway.CustomerSearchRequest").id();
      
          //can't chain the methods here for the contains, since it's a reserved word in cf.  lame.
          customerSearchRequest = customerSearch.contains(arguments.customerId);
      
          //Build the result here
          result = gateway.customer().search(customerSearchRequest);
      </cfscript>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-24
        • 2021-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多