Changeset 267 in josm for src/org/openstreetmap/josm/plugins


Ignore:
Timestamp:
2007-06-30T22:01:02+02:00 (18 years ago)
Author:
imi
Message:
  • added and fixed some tests
Location:
src/org/openstreetmap/josm/plugins
Files:
1 added
4 edited

Legend:

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

    r237 r267  
    4040public abstract class Plugin {
    4141
    42         private String name;
     42        String name;
    4343
    4444        public Plugin() {
    45                 URL[] urls = ((URLClassLoader)getClass().getClassLoader()).getURLs();
    46                 name = urls[urls.length-1].toString();
    47                 if (name.toLowerCase().endsWith(".jar")) {
    48                         int lastSlash = name.lastIndexOf('/');
    49                         name = name.substring(lastSlash+1, name.length()-4);
    50                 }
     45                try {
     46                URL[] urls = ((URLClassLoader)getClass().getClassLoader()).getURLs();
     47                name = urls[urls.length-1].toString();
     48                if (name.toLowerCase().endsWith(".jar")) {
     49                        int lastSlash = name.lastIndexOf('/');
     50                        name = name.substring(lastSlash+1, name.length()-4);
     51                }
     52        } catch (RuntimeException e) {
     53                name = "unknown";
     54        }
    5155    }
    5256
    5357        /**
    5458         * @return The name of this plugin. This is the name of the .jar file.
     59         * @deprecated Plugins have to know their name by themself.
    5560         */
    56         public final String getName() {
     61        @Deprecated public final String getName() {
    5762                return name;
    5863        }
    5964        /**
    6065         * @return The directory for the plugin to store all kind of stuff.
     66         * @deprecated Use <code>Main.pref.getPreferencesDir()+"plugins/"+name+"/";</code> instead.
    6167         */
    62         public final String getPluginDir() {
     68        @Deprecated public final String getPluginDir() {
    6369                return Main.pref.getPreferencesDir()+"plugins/"+name+"/";
    6470        }
  • src/org/openstreetmap/josm/plugins/PluginException.java

    r149 r267  
    1111 */
    1212public class PluginException extends RuntimeException {
    13         private final PluginProxy plugin;
    14         private final String name;
     13        public final PluginProxy plugin;
     14        public final String name;
    1515
    1616        public PluginException(PluginProxy plugin, String name, Throwable cause) {
     
    1919                this.name = name;
    2020    }
    21        
    22         public PluginProxy getPlugin() {
    23                 return plugin;
    24         }
    25         public String getName() {
    26                 return name;
    27         }
    2821}
  • src/org/openstreetmap/josm/plugins/PluginInformation.java

    r250 r267  
    66import java.io.InputStream;
    77import java.net.URL;
    8 import java.net.URLClassLoader;
    98import java.util.ArrayList;
    109import java.util.Collection;
     
    3332        public final String author;
    3433        public final int stage;
    35         protected final List<URL> libraries = new ArrayList<URL>();
    36         protected ClassLoader classLoader;
     34        public final List<URL> libraries = new ArrayList<URL>();
    3735
    3836        public final Map<String, String> attr = new TreeMap<String, String>();
     
    8280                                        s = entry.toString();
    8381                                        entry = new StringBuilder();
    84                                         if (!s.startsWith("/") && !s.startsWith("\\") && !s.matches("^.\\:"))
     82                                        if (!s.startsWith("/") && !s.startsWith("\\") && !s.matches("^.\\:") && file != null)
    8583                                                s = file.getParent() + File.separator + s;
    8684                                        libraries.add(new URL(getURLString(s)));
     
    111109         * Load the class of the plugin
    112110         */
    113         public Class<?> loadClass() {
     111        public Class<?> loadClass(ClassLoader classLoader) {
    114112                try {
    115                         Class<?> realClass = Class.forName(className, true, getClassLoader());
     113                        Class<?> realClass = Class.forName(className, true, classLoader);
    116114                        return realClass;
    117115                } catch (Exception e) {
     
    126124        }
    127125
    128         /**
    129          * Returns all libraries the plugin needs (the plugin's jar file itself and all jars declared
    130          * in the manifest's Class-Path section).
    131          * @return the libraries a list of URLs to the libraries.
    132          */
    133         public List<URL> getLibraries() {
    134                 return libraries;
    135         }
    136 
    137         /**
    138      * @return the classLoader
    139      */
    140     public ClassLoader getClassLoader() {
    141                 // if I have no classloader set, create one for me and my libraries:
    142                 if(classLoader == null) {
    143                         URL[] urls = new URL[libraries.size()];
    144                         urls = libraries.toArray(urls);
    145                         classLoader = URLClassLoader.newInstance(urls, getClass().getClassLoader());
    146                 }
    147         return classLoader;
    148     }
    149 
    150         /**
    151      * @param classLoader the classLoader to set
    152      */
    153     public void setClassLoader(ClassLoader classLoader) {
    154         this.classLoader = classLoader;
    155     }
    156        
    157126        /**
    158127         * Try to find a plugin after some criterias. Extract the plugin-information
  • src/org/openstreetmap/josm/plugins/PluginProxy.java

    r238 r267  
    2424                this.plugin = plugin;
    2525                this.info = info;
     26
     27                // setting name of the plugin by reflection
     28                if (plugin instanceof Plugin) {
     29                        try {
     30                        Plugin.class.getDeclaredField("name").set(plugin, info.name);
     31                } catch (Exception e) {
     32                        if (e instanceof RuntimeException)
     33                                throw (RuntimeException)e;
     34                        throw new RuntimeException(e);
     35                }
     36                }
    2637    }
    2738
Note: See TracChangeset for help on using the changeset viewer.