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  
16      public <T> T createModule(String name, ModuleDescriptor<T> moduleDescriptor) throws PluginParseException
17      {
18          throw new UnsupportedOperationException(" create Module not supported by LegacyModuleFactory. Use PrefixDelegatingModuleFactory instead.");
19      }
20  
21      public <T> Class<T> getModuleClass(String name, ModuleDescriptor<T> moduleDescriptor) throws ModuleClassNotFoundException {
22  
23          try
24          {
25              // First try and load the class, to make sure the class exists
26              @SuppressWarnings ("unchecked")
27              final Class<T> loadedClass = (Class<T>) moduleDescriptor.getPlugin().loadClass(name, null); // TODO: null means context classloader?
28  
29              // Then instantiate the class, so we can see if there are any dependencies that aren't satisfied
30              try
31              {
32                  final Constructor<T> noargConstructor = loadedClass.getConstructor(new Class[] { });
33                  if (noargConstructor != null)
34                  {
35                      loadedClass.newInstance();
36                  }
37              }
38              catch (final NoSuchMethodException e)
39              {
40                  // If there is no "noarg" constructor then don't do the check
41              }
42              return loadedClass;
43          }
44          catch (final ClassNotFoundException e)
45          {
46              throw new PluginParseException("Could not load class: " + name, e);
47          }
48          catch (final NoClassDefFoundError e)
49          {
50              throw new PluginParseException("Error retrieving dependency of class: " + name + ". Missing class: " + e.getMessage(), e);
51          }
52          catch (final UnsupportedClassVersionError e)
53          {
54              throw new PluginParseException("Class version is incompatible with current JVM: " + name, e);
55          }
56          catch (final Throwable t)
57          {
58              throw new PluginParseException(t);
59          }
60      }
61  }