5

I have three generic interfaces (with an inverses relationship between two of them) and want to process them in a recursive method:

public interface User<R extends Role<R,U>, U extends User<R,U>>
{
  public R getRole();
  public void setRole(R role);
}

public interface Role<R extends Role<R,U>,U extends User<R,U>>
{
  public List<R> getRoles();
  public void setRoles(List<R> roles);

  public List<U> getUser() ;
  public void setUser(List<U> user);
}

Now I want to do some processing with a recursion in my Worker class:

public <R extends Role<R,U>,U extends User<R,U>> void recursion(List<R> roles)
{
  for(R role : roles)
  {
    recursion(role.getRoles());
  }
}

I get this error and I'm didn't figured out why this does not work or how I can solve this:

Bound mismatch: The generic method recursion(List<R>) of type Worker is not
applicable for the arguments (List<R>). The inferred type User<R,User<R,U>>
is not a valid substitute for the bounded parameter <U extends User<R,U>>

2 Answers 2

2

I modified it, without using generic wildcards ?, so it compiles.
After stripping out method declarations irrelevant to the problem:

public interface Role<R extends Role<R, U>, U extends User<R, U>> {
    public List<Role<R, U>> getRoles(); // Change here to return type
}

public interface User<R extends Role<R, U>, U extends User<R, U>> { // No change
}

// Change to method parameter type
public static <R extends Role<R, U>, U extends User<R, U>> void recursion(List<Role<R, U>> roles) {
    for (Role<R, U> role : roles) { // Change to element type
        recursion(role.getRoles());
    }
}

I hope these changes still fit your design - let me know if they don't work for you and I'll try to work around your requirements.

Phew! Tough one!

Sign up to request clarification or add additional context in comments.

1 Comment

I'm unsure about the implication of changing the interface method for Role. I assume it returns a List of the interface of Role. I'm using the interfaces for some @Entity implementations. The recursive method is used in one utility class, perhaps the wildcard proposal ? of dacwe is suitable for me.
1

As you have noticed you need to specify the type U in one of the arguments. You can do that or if you want to ignore the U type you can use ? instead:

public <R extends Role<R,?>> void recursion(List<R> roles)
{
  for(R role : roles)
  {
    recursion(role.getRoles());
  }
}

2 Comments

Thanks, this may be a solution for me problem. But normally I only use the wildcard ? if I really don't know the type. Here I'm sure it is of type U.
Then you could to pass another parameter with that "type information", for example: ... recursion(List<R> roles, Class<U> userClass) (and ignore the parameter)..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.