22
22
public class ReflectionFactory { // XXX lease, pool
23
public static final String OLD_AJC = "bridge.tools.impl.OldAjc";
24
public static final String ECLIPSE = "org.aspectj.ajdt.ajc.AjdtCommand";
26
private static final Object[] NONE = new Object[0];
29
* Produce a compiler as an ICommand.
30
* @param cname the fully-qualified class name of the command
31
* to create by reflection (assuming a public no-argument constructor).
32
* @return ICommand compiler or null
34
public static ICommand makeCommand(String cname,
35
IMessageHandler errorSink) {
36
return (ICommand) make(ICommand.class, cname, NONE, errorSink);
40
* Make an object of type c by reflectively loading the class
41
* cname and creating an instance using args (if any),
42
* signalling errors (if any) to any errorSink.
44
private static Object make(Class c, String cname, Object[] args,
45
IMessageHandler errorSink) {
46
final boolean makeErrors = (null != errorSink);
49
final Class cfn = Class.forName(cname);
52
result = cfn.newInstance();
54
Class[] types = getTypes(args);
55
Constructor constructor = cfn.getConstructor(types);
56
if (null != constructor) {
57
result = constructor.newInstance(args);
60
error = "no constructor for " + c + " using "
61
+ Arrays.asList(types);
66
if (!c.isAssignableFrom(result.getClass())) {
68
error = "expecting type " + c + " got " + result.getClass();
74
IMessage mssg = new Message(error, IMessage.FAIL, null, null);
75
errorSink.handleMessage(mssg);
77
} catch (Throwable t) {
79
String mssg = "ReflectionFactory unable to load "
80
+ cname + " as " + c.getName();
81
IMessage m = new Message(mssg, IMessage.FAIL, t, null);
82
errorSink.handleMessage(m);
89
* @return Class[] with types of args or matching null elements
91
private static Class[] getTypes(Object[] args) {
92
if ((null == args) || (0 < args.length)) {
95
Class[] result = new Class[args.length];
96
for (int i = 0; i < result.length; i++) {
97
if (null != args[i]) {
98
result[i] = args[i].getClass();
105
private ReflectionFactory(){}
23
public static final String OLD_AJC = "bridge.tools.impl.OldAjc";
24
public static final String ECLIPSE = "org.aspectj.ajdt.ajc.AjdtCommand";
26
private static final Object[] NONE = new Object[0];
29
* Produce a compiler as an ICommand.
31
* @param cname the fully-qualified class name of the command to create by reflection (assuming a public no-argument
33
* @return ICommand compiler or null
35
public static ICommand makeCommand(String cname, IMessageHandler errorSink) {
36
return (ICommand) make(ICommand.class, cname, NONE, errorSink);
40
* Make an object of type c by reflectively loading the class cname and creating an instance using args (if any), signalling
41
* errors (if any) to any errorSink.
43
private static Object make(Class<?> c, String cname, Object[] args, IMessageHandler errorSink) {
44
final boolean makeErrors = (null != errorSink);
47
final Class<?> cfn = Class.forName(cname);
50
result = cfn.newInstance();
52
Class<?>[] types = getTypes(args);
53
Constructor<?> constructor = cfn.getConstructor(types);
54
if (null != constructor) {
55
result = constructor.newInstance(args);
58
error = "no constructor for " + c + " using " + Arrays.asList(types);
63
if (!c.isAssignableFrom(result.getClass())) {
65
error = "expecting type " + c + " got " + result.getClass();
71
IMessage mssg = new Message(error, IMessage.FAIL, null, null);
72
errorSink.handleMessage(mssg);
74
} catch (Throwable t) {
76
String mssg = "ReflectionFactory unable to load " + cname + " as " + c.getName();
77
IMessage m = new Message(mssg, IMessage.FAIL, t, null);
78
errorSink.handleMessage(m);
85
* @return Class[] with types of args or matching null elements
87
private static Class<?>[] getTypes(Object[] args) {
88
if ((null == args) || (0 < args.length)) {
91
Class<?>[] result = new Class[args.length];
92
for (int i = 0; i < result.length; i++) {
93
if (null != args[i]) {
94
result[i] = args[i].getClass();
101
private ReflectionFactory() {