~ubuntu-branches/ubuntu/wily/aspectj/wily-proposed

« back to all changes in this revision

Viewing changes to org.aspectj/modules/bridge/src/org/aspectj/bridge/ReflectionFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-03-15 23:54:31 UTC
  • mfrom: (1.2.5 upstream)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20110315235431-iq2gxbsx08kpwuiw
* New upstream release.
* Updated Standards-Version to 3.9.1 (no changes needed).
* Fix local Javadoc links:
  - d/patches/07_javadoc_links.diff: Use locally installed
   javadoc packages and hyperlink with them.
  - d/control: Add B-D on default-java-doc and libasm3-java-doc.
* d/control: Drop B-D on itself (our new bootstrap infrastructure doesn't need
  that anymore).
* Split packages into :
  - aspectj: only contains CLI tools.
  - libaspectj-java: JAR librairies for /usr/share/java.
  - libaspectj-java-doc: 4 API's Javadoc.
  - aspectj-doc: Programming Guides and SDK Documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 * 
21
21
 */
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";
25
 
   
26
 
    private static final Object[] NONE = new Object[0];
27
 
    
28
 
    /**
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
33
 
     */
34
 
    public static ICommand makeCommand(String cname, 
35
 
                                         IMessageHandler errorSink) {
36
 
        return (ICommand) make(ICommand.class, cname, NONE, errorSink);                                          
37
 
    }
38
 
    
39
 
    /**
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.
43
 
     */
44
 
    private static Object make(Class c, String cname, Object[] args,
45
 
        IMessageHandler errorSink) {
46
 
        final boolean makeErrors = (null != errorSink);
47
 
        Object result = null;
48
 
        try {
49
 
            final Class cfn = Class.forName(cname);
50
 
            String error = null;
51
 
            if (args == NONE) {
52
 
                result = cfn.newInstance();
53
 
            } else {
54
 
                Class[] types = getTypes(args);
55
 
                Constructor constructor = cfn.getConstructor(types);
56
 
                if (null != constructor) {
57
 
                    result = constructor.newInstance(args);
58
 
                } else {
59
 
                    if (makeErrors) {
60
 
                        error = "no constructor for " + c + " using " 
61
 
                        + Arrays.asList(types);
62
 
                    }
63
 
                }
64
 
            }
65
 
            if (null != result) {
66
 
                if (!c.isAssignableFrom(result.getClass())) {
67
 
                    if (makeErrors) {
68
 
                        error = "expecting type " + c + " got " + result.getClass();
69
 
                    }
70
 
                    result = null;
71
 
                }
72
 
            }
73
 
            if (null != error) {
74
 
                IMessage mssg = new Message(error, IMessage.FAIL, null, null);
75
 
                errorSink.handleMessage(mssg);
76
 
            } 
77
 
        } catch (Throwable t) {
78
 
            if (makeErrors) {
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);
83
 
            }
84
 
        }
85
 
        return result;
86
 
    }
87
 
 
88
 
    /**
89
 
     * @return Class[] with types of args or matching null elements
90
 
     */
91
 
    private static Class[] getTypes(Object[] args) {
92
 
        if ((null == args) || (0 < args.length)) {
93
 
            return new Class[0];
94
 
        } else {
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();
99
 
                }
100
 
            }
101
 
            return result;
102
 
        }
103
 
    }
104
 
    
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";
 
25
 
 
26
        private static final Object[] NONE = new Object[0];
 
27
 
 
28
        /**
 
29
         * Produce a compiler as an ICommand.
 
30
         * 
 
31
         * @param cname the fully-qualified class name of the command to create by reflection (assuming a public no-argument
 
32
         *        constructor).
 
33
         * @return ICommand compiler or null
 
34
         */
 
35
        public static ICommand makeCommand(String cname, IMessageHandler errorSink) {
 
36
                return (ICommand) make(ICommand.class, cname, NONE, errorSink);
 
37
        }
 
38
 
 
39
        /**
 
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.
 
42
         */
 
43
        private static Object make(Class<?> c, String cname, Object[] args, IMessageHandler errorSink) {
 
44
                final boolean makeErrors = (null != errorSink);
 
45
                Object result = null;
 
46
                try {
 
47
                        final Class<?> cfn = Class.forName(cname);
 
48
                        String error = null;
 
49
                        if (args == NONE) {
 
50
                                result = cfn.newInstance();
 
51
                        } else {
 
52
                                Class<?>[] types = getTypes(args);
 
53
                                Constructor<?> constructor = cfn.getConstructor(types);
 
54
                                if (null != constructor) {
 
55
                                        result = constructor.newInstance(args);
 
56
                                } else {
 
57
                                        if (makeErrors) {
 
58
                                                error = "no constructor for " + c + " using " + Arrays.asList(types);
 
59
                                        }
 
60
                                }
 
61
                        }
 
62
                        if (null != result) {
 
63
                                if (!c.isAssignableFrom(result.getClass())) {
 
64
                                        if (makeErrors) {
 
65
                                                error = "expecting type " + c + " got " + result.getClass();
 
66
                                        }
 
67
                                        result = null;
 
68
                                }
 
69
                        }
 
70
                        if (null != error) {
 
71
                                IMessage mssg = new Message(error, IMessage.FAIL, null, null);
 
72
                                errorSink.handleMessage(mssg);
 
73
                        }
 
74
                } catch (Throwable t) {
 
75
                        if (makeErrors) {
 
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);
 
79
                        }
 
80
                }
 
81
                return result;
 
82
        }
 
83
 
 
84
        /**
 
85
         * @return Class[] with types of args or matching null elements
 
86
         */
 
87
        private static Class<?>[] getTypes(Object[] args) {
 
88
                if ((null == args) || (0 < args.length)) {
 
89
                        return new Class[0];
 
90
                } else {
 
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();
 
95
                                }
 
96
                        }
 
97
                        return result;
 
98
                }
 
99
        }
 
100
 
 
101
        private ReflectionFactory() {
 
102
        }
106
103
}