View Javadoc
1   package com.atlassian.plugin.module;
2   
3   import com.atlassian.plugin.ModuleDescriptor;
4   import com.atlassian.plugin.PluginParseException;
5   
6   import java.lang.reflect.Constructor;
7   
8   /**
9    * Legacy module factory that provides module classes for descriptors that aren't using an injected ModuleFactory
10   *
11   * @since 2.5.0
12   */
13  public class LegacyModuleFactory implements ModuleFactory {
14  
15      public <T> T createModule(String name, ModuleDescriptor<T> moduleDescriptor) throws PluginParseException {
16          throw new UnsupportedOperationException(" create Module not supported by LegacyModuleFactory. Use PrefixDelegatingModuleFactory instead.");
17      }
18  
19      public <T> Class<T> getModuleClass(String name, ModuleDescriptor<T> moduleDescriptor) throws ModuleClassNotFoundException {
20  
21          try {
22              // First try and load the class, to make sure the class exists
23              @SuppressWarnings("unchecked")
24              final Class<T> loadedClass = moduleDescriptor.getPlugin().loadClass(name, null); // TODO: null means context classloader?
25  
26              // Then instantiate the class, so we can see if there are any dependencies that aren't satisfied
27              try {
28                  final Constructor<T> noargConstructor = loadedClass.getConstructor();
29                  if (noargConstructor != null) {
30                      noargConstructor.newInstance();
31                  }
32              } catch (final NoSuchMethodException e) {
33                  // If there is no "noarg" constructor then don't do the check
34              }
35              return loadedClass;
36          } catch (final ClassNotFoundException e) {
37              throw new PluginParseException("Could not load class: " + name, e);
38          } catch (final NoClassDefFoundError e) {
39              throw new PluginParseException("Error retrieving dependency of class: " + name + ". Missing class: " + e.getMessage(), e);
40          } catch (final UnsupportedClassVersionError e) {
41              throw new PluginParseException("Class version is incompatible with current JVM: " + name, e);
42          } catch (final Throwable t) {
43              throw new PluginParseException(t);
44          }
45      }
46  }