jsr107 / jsr107spec

JSR107 Cache Specification
Apache License 2.0
413 stars 164 forks source link

Classloading: Usage of TCCL in Factory.ClassFactory #349

Open cruftex opened 8 years ago

cruftex commented 8 years ago

Current code is:

    public T create() {
      try {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Class<?> clazz = loader.loadClass(className);
        return (T) clazz.newInstance();
    . . .

When used with a class, the classloader is stripped away and the class is actually loaded again:

    public ClassFactory(Class<T> clazz) {
      this.className = clazz.getName();
    }

The behavior is not obvious and not described in the Java Doc.

Furthermore, the Spec says on page 20:

A CacheManager provides: [ ... ] The ClassLoader that caches will use, should they require it, for resolving and loading application classes.

My thoughts:

To fit this together, it could mean that Caches need to set the TCCL to the user provided classloader, before calling the factory. Maybe this is happening already.

I think there is a TODO here to ensure consistent semantics across implementations.

smillidge commented 8 years ago

the whole of classloading needs specifying in detail for EE integration. This is particularly acute for Entry processors and events.

brianoliver commented 8 years ago

Completely agree. This is why for Coherence we invented the concept of a Coherence/Grid Archive (gar) that could encapsulate these things.

Perhaps Java 9 Modularity will help here... ;)