如何处理冷聚变中的"Invalid method Code length"?



我有一堆cfc文件(运行coldfusion8),其中包含一个cfswitch捆绑了类似的功能(用户,搜索,…)。

一些cfc文件变得太大,所以我收到一个Invalid method Code length 72645,我假设它说,"你的文件太大而无法解析"..

我通常在2000行左右达到这个值,我认为这是…不多。

由于我在一堆文件上碰到了这个上限,我正在考虑添加另一个功能层=从switch语句中删除所有函数,并使用每个函数单独的cfc调用cfinvoke

问题:
我的应用程序不是那么大,所以我想知道,是否有一种方法来规避"你不能有超过2000行在一个cfc"的上限,如果没有,它是一个可行的方法,有单独的cfc/组件在一个应用程序中调用的每个主要方法?

谢谢!

EDIT: re: "planned":-)
目前我的CFCs的结构是这样的:

<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>

现在我已经写了很多非结构化的东西,但是把它们分解成函数cfcs,然后像这样处理它们,对我来说并不是很"计划外"。

如果是,有什么更好的方法来设置它,因为我必须重新做它?开关将有大约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中编写的函数或方法正在吹每个方法65535字节的Java限制(大约2000行CF代码)。

通过从该函数调用更小的函数来收缩该函数。

调用一个10000字节的函数只需要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
    }

相关内容

最新更新