Java Compile error: Parameter x is exceeding the limit of 255 words

前端 未结 3 1186
粉色の甜心
粉色の甜心 2021-01-18 15:32

I have a constructor (for an auto generated class) that has 255 paremeters. Using ant on linux with javac 1.6.0_02. The class compiles fine and everything is good.

H

相关标签:
3条回答
  • 2021-01-18 15:53

    Well you're breaking the VM specification, section 4.10:

    The number of method parameters is limited to 255 by the definition of a method descriptor (§4.3.3), where the limit includes one unit for this in the case of instance or interface method invocations. Note that a method descriptor is defined in terms of a notion of method parameter length in which a parameter of type long or double contributes two units to the length, so parameters of these types further reduce the limit.

    I suggest you overcome the obstacles in changing the generated code... I'm pretty surprised it works on Linux, to be honest. I wouldn't be entirely surprised if the bytecode it generated was strictly invalid, and it just happens to be working for you at the moment.

    While it's always tempting to try to find ways of keeping with the existing code and ignoring the limits, I think in this case you should turn your attention to reducing the number of parameters immediately.

    0 讨论(0)
  • 2021-01-18 16:09

    There are 2 options to resolve this problem:

    1. Generate your class with no or some constructor arguments and then set rest parameters via setters. Like using builder pattern.
    2. Make simpler classes each holding 3 or 4 variables and set those simpler objects in 1 master class.
    0 讨论(0)
  • 2021-01-18 16:12

    Well, the class file spec. says:

    A method descriptor is valid only if it represents method parameters with a total length of 255 or less, where that length includes the contribution for this in the case of instance or interface method invocations. The total length is calculated by summing the contributions of the individual parameters, where a parameter of type long or double contributes two units to the length and a parameter of any other type contributes one unit.

    0 讨论(0)
提交回复
热议问题