Changeset 2830 in josm for trunk/src/org/openstreetmap/josm/plugins/PluginInformation.java
- Timestamp:
- 2010-01-12T18:27:40+01:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/plugins/PluginInformation.java
r2817 r2830 8 8 import java.io.IOException; 9 9 import java.io.InputStream; 10 import java.lang.reflect.Constructor; 11 import java.lang.reflect.InvocationTargetException; 12 import java.lang.reflect.Method; 10 13 import java.net.MalformedURLException; 11 14 import java.net.URL; … … 48 51 49 52 public final Map<String, String> attr = new TreeMap<String, String>(); 50 51 /**52 * Used in the Plugin constructor to make the information of the plugin53 * that is currently initializing available.54 *55 * If you think this is hacky, you are probably right. But it is56 * convinient anyway ;-)57 */58 static PluginInformation currentPluginInitialization = null;59 53 60 54 /** … … 193 187 /** 194 188 * Load and instantiate the plugin 189 * 190 * @param the plugin class 191 * @return the instantiated and initialized plugin 195 192 */ 196 193 public PluginProxy load(Class<?> klass) throws PluginException{ 197 194 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); 200 220 } catch(IllegalAccessException e) { 201 221 throw new PluginException(name, e); 202 222 } catch (InstantiationException e) { 203 223 throw new PluginException(name, e); 224 } catch(InvocationTargetException e) { 225 throw new PluginException(name, e); 204 226 } 205 227 } … … 207 229 /** 208 230 * Load the class of the plugin 231 * 232 * @param classLoader the class loader to use 233 * @return the loaded class 209 234 */ 210 235 public Class<?> loadClass(ClassLoader classLoader) throws PluginException { … … 215 240 return realClass; 216 241 } catch (ClassNotFoundException e) { 242 throw new PluginException(name, e); 243 } catch(ClassCastException e) { 217 244 throw new PluginException(name, e); 218 245 }
Note:
See TracChangeset
for help on using the changeset viewer.