Ignore:
Timestamp:
2010-01-12T18:27:40+01:00 (14 years ago)
Author:
Gubaer
Message:

fixed an ugly hack in the plugin bootstrap procedure

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/plugins/PluginInformation.java

    r2817 r2830  
    88import java.io.IOException;
    99import java.io.InputStream;
     10import java.lang.reflect.Constructor;
     11import java.lang.reflect.InvocationTargetException;
     12import java.lang.reflect.Method;
    1013import java.net.MalformedURLException;
    1114import java.net.URL;
     
    4851
    4952    public final Map<String, String> attr = new TreeMap<String, String>();
    50 
    51     /**
    52      * Used in the Plugin constructor to make the information of the plugin
    53      * that is currently initializing available.
    54      *
    55      * If you think this is hacky, you are probably right. But it is
    56      * convinient anyway ;-)
    57      */
    58     static PluginInformation currentPluginInitialization = null;
    5953
    6054    /**
     
    193187    /**
    194188     * Load and instantiate the plugin
     189     *
     190     * @param the plugin class
     191     * @return the instantiated and initialized plugin
    195192     */
    196193    public PluginProxy load(Class<?> klass) throws PluginException{
    197194        try {
    198             currentPluginInitialization = this;
    199             return new PluginProxy(klass.newInstance(), this);
     195            try {
     196                Constructor<?> c = klass.getDeclaredConstructor(PluginInformation.class);
     197                Object plugin = c.newInstance(this);
     198                return new PluginProxy(plugin, this);
     199            } catch(NoSuchMethodException e) {
     200                // do nothing - try again with the noarg constructor for legacy support
     201            }
     202            // FIXME: This is legacy support. It is necessary because of a former ugly hack in the
     203            // plugin bootstrap procedure. Plugins should be migrated to the new required
     204            // constructor with PluginInformation as argument, new plugins should only use this
     205            // constructor. The following is legacy support and should be removed by Q2/2010.
     206            // Note that this is not fool proof because it isn't
     207            // completely equivalent with the former hack: plugins derived from the Plugin
     208            // class can't access their local "info" object any more from within the noarg-
     209            // constructor. It is only set *after* constructor invocation.
     210            //
     211            Constructor<?> c = klass.getConstructor();
     212            Object plugin = c.newInstance();
     213            if (plugin instanceof Plugin) {
     214                Method m = klass.getMethod("setPluginInformation", PluginInformation.class);
     215                m.invoke(plugin, this);
     216            }
     217            return new PluginProxy(plugin, this);
     218        } catch(NoSuchMethodException e) {
     219            throw new PluginException(name, e);
    200220        } catch(IllegalAccessException e) {
    201221            throw new PluginException(name, e);
    202222        } catch (InstantiationException e) {
    203223            throw new PluginException(name, e);
     224        } catch(InvocationTargetException e) {
     225            throw new PluginException(name, e);
    204226        }
    205227    }
     
    207229    /**
    208230     * Load the class of the plugin
     231     *
     232     * @param classLoader the class loader to use
     233     * @return the loaded class
    209234     */
    210235    public Class<?> loadClass(ClassLoader classLoader) throws PluginException {
     
    215240            return realClass;
    216241        } catch (ClassNotFoundException e) {
     242            throw new PluginException(name, e);
     243        } catch(ClassCastException e) {
    217244            throw new PluginException(name, e);
    218245        }
Note: See TracChangeset for help on using the changeset viewer.