Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

However when I try to compile the same class from within eclipse on windows xp with jdk 1.6, I get the following error

Too many parameters, parameter BLAH is exceeding the limit of 255 words eligible for method parameters

BLAH is the the 256th parameter.

Is there a way to overcome this problem? Changing the auto generated class is not an option as I would need to change it every compile or change the generator. Both options are not acceptable because we can already get this class on linux.

PS: For those who are interested, the java class is generated from an IDL file using JacORB. Unfortunately the number of parameters in the class can not be reduced as it defines an interfaces between our software and other systems.

like image 745
hhafez Avatar asked Aug 20 '09 22:08

hhafez


People also ask

How do you fix a compilation error in Java?

If the brackets don't all match up, the result is a compile time error. The fix to this compile error is to add a leading round bracket after the println to make the error go away: int x = 10; System.

How do I fix too big for Java?

So, refactoring the erring method into several smaller methods will fix the problem for us. In the case of array initializations, we can either split the arrays or load from a file. We can also use static initializers. Even when we're using code generators, we can still refactor the code.

What is a compilation error in Java?

Compile-time errors occur when there are syntactical issues present in application code, for example, missing semicolons or parentheses, misspelled keywords or usage of undeclared variables. These syntax errors are detected by the Java compiler at compile-time and an error message is displayed on the screen.

Which case a compiler error?

Compilation error refers to a state when a compiler fails to compile a piece of computer program source code, either due to errors in the code, or, more unusually, due to errors in the compiler itself. A compilation error message often helps programmers debugging the source code.


3 Answers

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.

like image 171
Jon Skeet Avatar answered Oct 03 '22 23:10

Jon Skeet


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.
like image 24
Abhishek khandelwal Avatar answered Oct 04 '22 00:10

Abhishek khandelwal


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.

like image 30
Zed Avatar answered Oct 04 '22 00:10

Zed