簡體   English   中英

如何在Coldfusion中處理“無效的方法代碼長度”?

[英]How to handle “Invalid method Code length” in Coldfusion?

我有一堆cfc文件(運行coldfusion8 ),它包含一個cfswitch捆綁類似的功能(用戶,搜索,...)。

一些cfc文件變得太大,所以我收到一個Invalid method Code length 72645 ,我假設“你的文件太大而無法解析”。

我通常在大約2000行達到這個目標,並且認為這是......並不多。

由於我在一堆文件上遇到了這個上限,我正在考慮添加另一個功能層=從switch語句中刪除所有函數,並使用單獨的cfc per函數調用cfinvoke

題:
我的應用程序並不是那么大,所以我想知道,有沒有辦法繞過“你不能超過2000行的cfc”上限,如果沒有,對於在應用程序中調用的每個主要方法,是否可以使用單獨的CFC /組件?

謝謝!

編輯:重新:“計划”:-)
目前我的CFC結構如下:

<cfcomponent extends="controllers.main" output="false" hint="handle all user interactions">     
    <cfscript>
        VARIABLES.Instance.Validation = {   
            // all user-relate form fields including validation method to call (pass = no validation)
            id="spec_id"
          , corp="pass"
          ...
        };
    </cfscript> 

    <cffunction name="Init" access="public" returntype="any" output="false" hint="Initialize form data">
        <cfreturn true />
    </cffunction>   
    <cffunction name="Defaults" access="public" returntype="struct" output="false" hint="Assign defaults">
       <cfscript>
          // form default values assigned to instance
          var formDefaults = {
              id=""
            , comp=""
            ...
          };
       </cfscript>
       <cfreturn formDefaults />
    </cffunction>

    <cffunction name="Commit" access="remote" returntype="struct" output="false" hint="Main handler">
        <cfscript>
        // all var declarations
        var userID = "";
        var strRememberMe = "";
        var timestamp = now();
        ... 
        var defaultValues = THIS.Defaults();
        var LOCAL = {};

        structAppend(defaultValues, VARIABLES.Instance.FormData);
        LOCAL.User = defaultValues;
        LOCAL.User.timestamp = timestamp ;
        </cfscript> 

        <!--- the switch --->       
        <cfswitch expression = #LOCAL.User.submitted_form#>

            ... lot of stuff ...

        </cfswitch>

        <cfreturn LOCAL.Response />
    </cffunction>

    <!--- UTILITY FUNCTIONS --->
    <cffunction name="Validate" access="public" returntype="array" output="false" hint="validate form inputs">
        <cfscript>
        var LOCAL = {};
        var double = structNew();
        double.criteria = VARIABLES.Instance.Validation;
        double.form = VARIABLES.Instance.FormData;
        </cfscript>

        <!--- Get error name and type --->
        <cfinvoke component="form_validate" method="validate_fields" double="#double#" returnvariable="validation_errors"></cfinvoke>
        <cfset LOCAL.ErrorMessages = validation_errors />
        <cfreturn LOCAL.ErrorMessages />
    </cffunction>                                   
</cfcomponent>

現在我已經編寫了很多非結構化的東西,但是在功能性cfc中拆分,然后像這樣處理它們對我來說似乎沒有“非計划”。

如果是,那么設置它的更好的方法是什么,因為我不得不重新做它? 該交換機將有大約15個案例,這是我正在使用的所有主要cfcs的平均值。

謝謝!

我前段時間也在CF8中遇到過這個問題。 沒有通用的“2000行限制”,但JVM中的最大值是地址偏移量,以便在子例程中跳轉。 偏移量不得超過2個字節(WORD),否則您將面臨此異常。 為了避免子程序(函數)中的大地址偏移,您需要最小化大塊條件跳轉(if / else / switch)。 您可以通過使用多個子例程來完成此操作(這些調用可能需要最多4/8字節的完整寄存器)。

例如:重新設計......

function A (x, y) {
    if (...) {
        switch (...) {
            case ...:
                switch (...) {
                    ...
                }
            ...
        }
    } else {
        switch (...) {
            ...
        }
    }
}

對......這樣的...

function A (x, y) {
    if (...) {
        call B(x, y);
    } else {
        call C(x, y);
    }
}

function B (x, y) {
    switch (...) {
        case ...:
            call B1(x, y);
        ...
    }
}

function B1 (x, y) {
    switch (...) {
        ....
    }
}

function C (x, y) {
    switch (...) {
        ....
    }
}

......你明白了。 這通常還會提高可讀性和可維護性。

基本上這個錯誤正在發生,因為你在ColdFusion中編寫的函數或方法每個方法的Java限制為65535字節(大約2000行CF代碼)。

只需通過調用該函數中的較小函數來縮小該函數。

調用10,000字節的函數只需要100個字節。

Before (blows up):
    Function(){
      10,000 bytes of code
      15,000 bytes of code
      20,000 bytes of code
      30,000 bytes of code
    }

    After (success!):
    Function(){
      100 Byte call to 10,000 bytes of code
      100 Byte call to 15,000 bytes of code
      100 Byte call to 20,000 bytes of code
      100 Byte call to 30,000 bytes of code
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM