← Back Home

Java: Recursive Type Declaration

  1. til

Just playing around jackson library, and found interesting interface, VisibilityChecker.

This class use recursive type declaration, to support builder/fluent pattern.

Here the snippet:

package com.fasterxml.jackson.databind.introspect;

**
 * Interface for object used for determine which property elements
 * (methods, fields, constructors) can be auto-detected, with respect
 * to their visibility modifiers.
 *<p>
 * Note on type declaration: funky recursive type is necessary to
 * support builder/fluent pattern.
 */
public interface VisibilityChecker<T extends VisibilityChecker<T>>
{
  ...
}

By using this interface, we can create 2 types of class

  1. Class that implement VisibilityChecker with it's class as type.
public class Leaf implements VisibilityChecker<Leaf> 
{
  ...
}
  1. Class that implement VisibilityChecker with other class as type, the other class should implement VisibilityChecker with it's class as type
public class Node implements VisibilityChecker<Leaf> 
{
  ...
}

But, we can't create class that implement VisibilityChecker with other class as type, the other class that implement VisibilityCheker with another class as type.

// this one not working
public class Root implements VisibilityChecker<Node>
{
  ...
}
comments powered by Disqus