
How Many Type Parameters Can a Java Method Have? - pplonski86
http://justinblank.com/experiments/howmanytypeparameterscanajavamethodhave.html
======
leibnitz27
One little thing - the string that's running out of space here is the
signature of the interface, not the signature of the method. You could use
that fact together with method scoped type parameters to get a few more, if it
weren't for the fact the descriptor would blow up too soon already.

Since it's not mentioned in the article - the _signature_ of a method is the
description containing generic information. The _descriptor_ is the erased
signature, which, because A strips to Ljava/lang/Object; will likely be a fair
bit longer.

An aside - you can (mostly) still get away with corrupting signatures, if you
want to obfuscate code.

(eg
[https://github.com/ItzSomebody/StopDecompilingMyJava/blob/ma...](https://github.com/ItzSomebody/StopDecompilingMyJava/blob/master/decompiler-
tool-bugs/Entry-005/Entry.md) )

(yes, I used to blow up on one of these in
[https://www.benf.org/other/cfr](https://www.benf.org/other/cfr) \- (my java
decompiler), but it's all good now).

~~~
hyperpape
Thanks for that. I confess I got a bit lazy about checking _which_ string was
at issue--I'll try to do an update when I get home, if I can make time.

------
cobbzilla
A fun short article, but if you’re too lazy to read, the summary answer is
“way more than you will ever need.”

~~~
RobertDeNiro
Or if you need that much, your design is severely broken

~~~
blattimwind
[https://github.com/groovy/groovy-
core/blob/master/src/main/o...](https://github.com/groovy/groovy-
core/blob/master/src/main/org/codehaus/groovy/runtime/ArrayUtil.java)

~~~
cobbzilla
Wow -- that is some ugly code. Why couldn't they use a single varargs method
declaration? Does this code need to support older Java versions that lack
varargs support?

~~~
lokedhs
It may be a performance hack. Varargs results in an array being allocated.
I've seen this trick in the past, but they usually stopped at a handful of
arguments and let the rest be handled by varargs.

~~~
usrusr
Groovy is chock-full of absurd little microoptimizations that will forever
fail to make even the tiniest dent in the general overhead relative to java.
For several years there was even an "assume equality of hashes match" shortcut
hidden in certain set operation code paths.

~~~
zmmmmm
When compiled statically groovy has almost zero overhead relative to Java. I
appreciate how much effort they have made to keep the overhead low and reduce
the bloat as much as they can.

------
smcl
I tried this with the F# mono REPL - it eventually failed after creating a let
binding with 1328 params: [http://blog.mclemon.io/f-number-polymorphic-
parameter-overfl...](http://blog.mclemon.io/f-number-polymorphic-parameter-
overflow)

~~~
agumonkey
one can store a whole Forth OS in a java signature

------
twoodfin
I think it's pretty cool that the result emerges, as the author points out,
not from the implementation details of any particular JVM, but from well-
defined parts of the Java specs intersecting in a fully determined way.

------
deathwarmedover
I wonder if it would be feasible to scan, say, GitHub or Apache projects to
find the highest parameter count in the wild?

~~~
erdeszt
Do you mean type parameters or regular parameters? If it's the former(and
Scala is allowed) then I think Rho's Result type is a pretty good bet with
it's ~60 type arguments:
[https://github.com/http4s/rho/blob/989db04e6dd828518770a42fd...](https://github.com/http4s/rho/blob/989db04e6dd828518770a42fdbb17f4d4d7d3643/core/src/main/scala/org/http4s/rho/Result.scala#L9)

~~~
nkkollaw
Ugh, why would they do that?

~~~
lmm
Looks like the type tells you which HTTP status codes are possible from a
given route, so that e.g. a route that can return only 200 or 403 is a
different type from a route that could return 302. Which makes a lot of sense,
because certain transformations / routing directives that you might want to do
only make sense for particular HTTP status codes.

------
LoSboccacc
this reminds me of that one table with more than 255 columns which hibernate
converted into a class that could not be compiled since the constructor had
too many parameters. we split it up using the component mapping to realize
some of these column as if they were composition.

the early '00 were a lawless wasteland.

~~~
LanceH
If you were using Hibernate in 2000 and not caught up in the EJB hype, you
were ahead of the game.

~~~
LoSboccacc
maybe because I was fresh out of academia then but neither xml defined
behavior nor the ejb looked like something that really removed work, moreover
I've always been biased against things I can't put a break-point and debug in
the event of having issues (which is also why I skipped on angular, for
example).

------
sebazzz
I imagine that the limit on the .NET CLR is even lower because there needs to
be runtime support for type parameters all the way down to the JIT.

~~~
reifiedgent
I modified the python script to generate C# and it compiled with 131072 type
parameters. It did take 9 minutes though so I didn't continue after that.

~~~
int_19h
In theory, the maximum encodable compressed unsigned integer, as used by
GenParamCount in MSIL, is 0x1FFFFFFF.

------
Aweorih
> Nonetheless, if I was king, I'd consider explicitly banning any class or
> method from having more than 255 type parameters.

As he says there is probably no use case where you need so many and the
additional overhead of time to implement this is (in my eyes) a bit wasted.
But he can be actually the king by contributing to the openJDK project.

------
tauwauwau
Going to leave it here

Inside the Java Virtual Machine by Bill Venners

[https://www.artima.com/insidejvm/ed2/](https://www.artima.com/insidejvm/ed2/)

~~~
s_k_
Is it still relevant? If you've read it, what's your review in one sentence
(or not just in one if you wish :D)?

~~~
tauwauwau
I read it a few years ago, when I wanted to know how many methods can there be
in a Java class. (count: 2 bytes, but compiler fails to compile such a class a
long before that).

Many of the concepts such as structure of class files should still be relevant
(fact that all classes start with hex code CAFEBABE etc).

Garbage collection, security should be more or less relevant.

------
chii
i wonder if it's possible to generate type parameters such that computation is
done with it at compile time?

~~~
OskarS
Theoretically, sure:
[https://arxiv.org/pdf/1605.05274.pdf](https://arxiv.org/pdf/1605.05274.pdf)

~~~
JadeNB
I think it's a good idea to link to abstracts
[https://arxiv.org/abs/1605.05274](https://arxiv.org/abs/1605.05274) (as ygra
does
([https://news.ycombinator.com/item?id=19777896](https://news.ycombinator.com/item?id=19777896)
)) rather than directly to PDFs.

------
jnty
I wonder how many other articles could do with a conclusion entitled "None of
This Matters"?!

~~~
jmkni
Tempted to make that my email signature.

------
g105b
The article will make Uncle Bob shudder.

~~~
michaelcampbell
He can assuage himself with more tests.

