001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.plugins; 003 004import java.util.List; 005 006import org.openstreetmap.josm.gui.MapFrame; 007import org.openstreetmap.josm.gui.download.DownloadSelection; 008import org.openstreetmap.josm.gui.preferences.PreferenceSetting; 009import org.openstreetmap.josm.tools.Logging; 010import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler; 011 012/** 013 * Helper class for the JOSM system to communicate with the plugin. 014 * 015 * This class should be of no interest for sole plugin writer. 016 * 017 * @author Immanuel.Scholz 018 */ 019public class PluginProxy extends Plugin { 020 021 /** 022 * The plugin. 023 */ 024 private final Object plugin; 025 private final PluginClassLoader classLoader; 026 027 /** 028 * Constructs a new {@code PluginProxy}. 029 * @param plugin the plugin 030 * @param info the associated plugin info 031 * @param classLoader the class loader for the plugin 032 * @since 12322 033 */ 034 public PluginProxy(Object plugin, PluginInformation info, PluginClassLoader classLoader) { 035 super(info); 036 this.plugin = plugin; 037 this.classLoader = classLoader; 038 } 039 040 /** 041 * Get the plugin object. 042 * @return the plugin object 043 * @since 12322 044 */ 045 public Object getPlugin() { 046 return plugin; 047 } 048 049 /** 050 * Get the class loader for the plugin. 051 * @return the plugin class loader 052 * @since 12322 053 */ 054 public PluginClassLoader getClassLoader() { 055 return classLoader; 056 } 057 058 private void handlePluginException(Throwable e) { 059 PluginHandler.pluginLoadingExceptions.put(getPluginInformation().name, e); 060 BugReportExceptionHandler.handleException(new PluginException(this, getPluginInformation().name, e)); 061 } 062 063 @Override 064 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 065 try { 066 plugin.getClass().getMethod("mapFrameInitialized", MapFrame.class, MapFrame.class).invoke(plugin, oldFrame, newFrame); 067 } catch (NoSuchMethodException e) { 068 Logging.trace(e); 069 Logging.debug("Plugin "+plugin+" does not define mapFrameInitialized"); 070 } catch (ReflectiveOperationException | IllegalArgumentException | NoClassDefFoundError e) { 071 handlePluginException(e); 072 } 073 } 074 075 @Override 076 public PreferenceSetting getPreferenceSetting() { 077 try { 078 return (PreferenceSetting) plugin.getClass().getMethod("getPreferenceSetting").invoke(plugin); 079 } catch (NoSuchMethodException e) { 080 Logging.trace(e); 081 Logging.debug("Plugin "+plugin+" does not define getPreferenceSetting"); 082 return null; 083 } catch (ReflectiveOperationException | IllegalArgumentException | NoClassDefFoundError e) { 084 handlePluginException(e); 085 } 086 return null; 087 } 088 089 @Override 090 public void addDownloadSelection(List<DownloadSelection> list) { 091 try { 092 plugin.getClass().getMethod("addDownloadSelection", List.class).invoke(plugin, list); 093 } catch (NoSuchMethodException e) { 094 Logging.trace(e); 095 Logging.debug("Plugin "+plugin+" does not define addDownloadSelection"); 096 } catch (ReflectiveOperationException | IllegalArgumentException | NoClassDefFoundError e) { 097 handlePluginException(e); 098 } 099 } 100}