DEV Community

Nancy Deschenes
Nancy Deschenes

Posted on

Today, I learned that Tuple != Tuple2

Once upon a time, I wrote a service class that generates pairs of users. I thought to myself, "Ohh! I know this! I need to return a list of tuples!"

List<Tuple<User, User>> getUserMatches() {
  List<Tuple<User, User>> returnVal = []
  while (moreUsers()) {
    User a = getUserA()
    User b = getUserB() 
    returnVal.add(new Tuple[a, b])
Enter fullscreen mode Exit fullscreen mode

Test the code. The code runs. All is good. Move on.

Later, a co-worker asks, "why aren't the tests running?" What? no, my tests run, I'm sure! On closer inspection, not only do the tests not pass, they fail to compile. I shake my head. I can't imagine... What's worse, IntelliJ shows absolutely no compilation problems. Without a compilation error, it's hard to fix the problem. What I get instead is an exception:

    at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.validateConstructorArguments(
    at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.<init>(
    at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.make(
    at sun.reflect.generics.factory.CoreReflectionFactory.makeParameterizedType(
    at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(

Enter fullscreen mode Exit fullscreen mode

Some googling leads me this blog post. Ah, okay, I need to find out where I managed to parametrize a class incorrectly.

The exception only occurs when compiling the tests, so that where I look. I try to get the system to only compile some of the tests, to figure out which is causing the problem. Finally find that the test for my service above is responsible. I still have no idea what's wrong with it, mind you. All I know is that when I try to compile it, it fails with the dreaded java.lang.reflect.MalformedParameterizedTypeException.

I comment out half the file. The problem still happens. I comment out another quarter. Another eighth. Until all I have left is

class UserMergeServiceSpec extends Specification {
Enter fullscreen mode Exit fullscreen mode

And it still crashes the compiler. I comment out the annotation, and it compiles! I put the whole file back in, and as long as the annotation is commented out, it works.

It was positively mystifying.

I tried to approach it from the other end. I set a breakpoint where the MalformedParameterizedTypeException was thrown, and walked up the stack, and looked at local values, and... there's my Tuple. I look at the definition:

public class Tuple extends AbstractList {
  // ...
Enter fullscreen mode Exit fullscreen mode

Hm. That's not parametrized. How do you parametrize a Tuple, then?

That's when I found this blog post that cleared everything up.

Tuple is an immutable list of items, and is not parametrizable. Tuple2 is a pair of two things, and the class of those two things can be parametrized.

I don't know why this was a problem only when running the tests; the code compiles and runs just fine on its own, and the "bad code" is in the Service class, not in the test, but the test must have tickled something... At least it's fixed!

Top comments (1)

david_ojeda profile image
David Ojeda

Nice post, thanks!

Glad to see some Grails, Groovy and Spock around here 🖖🏼