1 package org.apache.rat.mp.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.plugin.MojoFailureException;
24
25 public final class ConfigurationHelper {
26
27 private ConfigurationHelper() {
28
29 }
30
31 public static <T> T newInstance(final Class<T> clazz, final String className)
32 throws MojoExecutionException, MojoFailureException {
33 try {
34 final ClassLoader cl = Thread.currentThread()
35 .getContextClassLoader();
36 @SuppressWarnings("unchecked")
37 final T o = (T) cl.loadClass(className).newInstance();
38
39 if (!clazz.isAssignableFrom(o.getClass())) {
40 throw new MojoFailureException("The class "
41 + o.getClass().getName() + " does not implement "
42 + clazz.getName());
43 }
44 return o;
45 } catch (final InstantiationException e) {
46 throw new MojoExecutionException("Failed to instantiate class "
47 + className + ": " + e.getMessage(), e);
48 } catch (final ClassCastException e) {
49 throw new MojoExecutionException("The class " + className
50 + " is not implementing " + clazz.getName() + ": "
51 + e.getMessage(), e);
52 } catch (final IllegalAccessException e) {
53 throw new MojoExecutionException("Illegal access to class "
54 + className + ": " + e.getMessage(), e);
55 } catch (final ClassNotFoundException e) {
56 throw new MojoExecutionException("Class " + className
57 + " not found: " + e.getMessage(), e);
58 }
59 }
60
61 }