1

I have a setup like this:

public final class RequestContext<T extends Cache> {
     T roleSpecificCache;

     public static final class Spec implements Supplier<RequestContext> {
          private Spec() {

          }

          T roleSpecificCache; // << Getting error here
     }

     private RequestContext(Spec b) {
        this.roleSpecificCache = b.roleSpecificCache; // << I want to do this
     }
   }

However on the line T roleSpecificCache, I get the following error :

RequestContext.this cannot be referenced from a static context

I understand the reason why I'm getting this error (i.e. there's no direct link between the two classes), but don't know how to fix it. I want to to be able to do what I'm doing at the end.

Also, I cannot make Spec non-static (out of my hands).

Hearen
  • 7,420
  • 4
  • 53
  • 63
Ahmad
  • 12,886
  • 30
  • 93
  • 146

1 Answers1

2

You would need to parameterize Spec:

public final class RequestContext<T extends Cache> {
     T roleSpecificCache;

     public static final class Spec<T extends Cache>
     implements Supplier<RequestContext<T>> {
          private Spec() {
          }

          T roleSpecificCache;
     }

    private RequestContext(Spec<T> b) {
        this.roleSpecificCache = b.roleSpecificCache;
    }
}

It kind of seems like it should have been that way to begin with, because of the raw type argument to Supplier, which should be avoided.

Radiodef
  • 37,180
  • 14
  • 90
  • 125