public ClassLoader getClassLoader() { return IJ.getClassLoader(); }
public static URL getUrl(String path) { return IJ.getClassLoader().getResource(path); } }
@Override public void run() { Thread.currentThread().setContextClassLoader(IJ.getClassLoader()); } });
public static boolean haveJava3D() { final ClassLoader loader = IJ.getClassLoader(); if (loader == null) throw new RuntimeException("IJ.getClassLoader() failed (!)"); try { final Class<?> c = loader.loadClass("ij3d.ImageWindow3D"); /* * In fact the documentation says that this should throw an * exception and not return null, but just in case: */ return c != null; } catch (final Exception e) { return false; } }
public static boolean openIJ1Editor(String title, String body) { try { Class<?> clazz = IJ.getClassLoader().loadClass("ij.plugin.frame.Editor"); Constructor<?> ctor = clazz.getConstructor(new Class[] { Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE }); Object ed = ctor.newInstance(new Object[] { 16, 60, 0, 3 }); Method method = clazz.getMethod(title.endsWith(".ijm") ? "createMacro" : "create", new Class[] { String.class, String.class }); method.invoke(ed, new Object[] { title, body }); return true; } catch (Exception e) { IJ.handleException(e); } return false; }
@Override public void initialized() { final String property = System.getProperty("ij1.patcher.initializer"); try { final ClassLoader loader = IJ.getClassLoader(); Thread.currentThread().setContextClassLoader(loader); Class<?> runClass; if (property != null) { runClass = loader.loadClass(property); } else { try { runClass = loader.loadClass("net.imagej.legacy.plugin.LegacyInitializer"); } catch (ClassNotFoundException e) { runClass = loader.loadClass("imagej.legacy.plugin.LegacyInitializer"); } } final Runnable run = (Runnable)runClass.newInstance(); run.run(); } catch (ClassNotFoundException e) { // ignore } catch (Throwable t) { t.printStackTrace(); } }
public static boolean openEditor(File file, File templateFile) { try { Class<?> clazz = IJ.getClassLoader().loadClass("fiji.scripting.TextEditor"); Constructor<?> ctor = clazz.getConstructor(new Class[] { File.class, File.class }); Frame frame = (Frame)ctor.newInstance(new Object[] { file, templateFile }); frame.setVisible(true); return true; } catch (Exception e) { IJ.handleException(e); } return false; }
private void runInitializer() { final String property = System.getProperty("ij1.patcher.initializer"); try { final ClassLoader loader = IJ.getClassLoader(); Thread.currentThread().setContextClassLoader(loader); Class<?> runClass; if (property != null) { runClass = loader.loadClass(property); } else { try { runClass = loader.loadClass("net.imagej.legacy.plugin.LegacyInitializer"); } catch (ClassNotFoundException e) { runClass = loader.loadClass("imagej.legacy.plugin.LegacyInitializer"); } } final Runnable run = (Runnable)runClass.newInstance(); run.run(); } catch (ClassNotFoundException e) { // ignore } catch (Throwable t) { t.printStackTrace(); } }
/** * Runs a plug-in with an optional argument. * * @param className the plugin class * @param arg the argument (use "" if you do not want to pass anything) */ public static void runPlugInGently(String className, String arg) { try { Class<?> clazz = IJ.getClassLoader() .loadClass(className); if (clazz != null) { PlugIn plugin = (PlugIn)clazz.newInstance(); plugin.run(arg); } } catch (NoClassDefFoundError e) { } catch (ClassNotFoundException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } }
public static boolean openFijiEditor(final File file) { try { Class<?> textEditor = ij.IJ.getClassLoader().loadClass("fiji.scripting.TextEditor"); final Constructor<?> ctor = textEditor.getConstructor(String.class); final Runnable run = new Runnable() {
public String getJarForItem(String label) { String className = (String)Menus.getCommands().get(label); int paren = className.indexOf('('); if (paren > 0) className = className.substring(0, paren); try { Class clazz = IJ.getClassLoader().loadClass(className); int dot = className.lastIndexOf('.'); String fileName = className.substring(dot + 1) + ".class"; String resource = clazz.getResource(fileName).toString(); String path = className.substring(0, dot + 1).replace('.', '/') + fileName; if (resource.startsWith("jar:") && resource.endsWith("!/" + path)) resource = resource.substring(4, resource.length() - path.length() - 2); if (resource.startsWith("file:")) resource = resource.substring(5); String ijDir = System.getProperty("ij.dir"); if (!ijDir.endsWith("/")) ijDir += "/"; if (resource.startsWith(ijDir)) resource = resource.substring(ijDir.length()); return resource; } catch (Exception e) { IJ.handleException(e); return null; } }
public void run(String arg) { if ("check".equals(arg)) { check(); return; } try { try { IJ2Updater.run(); } catch (Throwable t) { t.printStackTrace(); @SuppressWarnings("unchecked") final Class<Runnable> runnable = (Class<Runnable>) IJ.getClassLoader().loadClass(OBSOLETE_UPDATER_CLASS_NAME); runnable.newInstance().run(); } } catch (Throwable t) { t.printStackTrace(); runRemote(); } }
/** * Calls the Fiji Script Editor for text files. * * A couple of sanity checks are needed, e.g. that the script editor is in the class path * and that it agrees that the file is binary, that there is no infinite loop ponging back * and forth between the TextEditor's and the Opener's open() methods. * * @param path the path to the candidate file * @return whether we opened it in the script editor */ public static boolean maybeOpenEditor(String path) { try { Class<?> textEditor = ij.IJ.getClassLoader().loadClass("fiji.scripting.TextEditor"); if (path.indexOf("://") < 0 && !getFileExtension(path).equals("") && !((Boolean)textEditor.getMethod("isBinary", new Class[] { String.class }).invoke(null, path)).booleanValue() && !stackTraceContains("fiji.scripting.TextEditor.open(") && IJ.runPlugIn("fiji.scripting.Script_Editor", path) != null) return true; } catch (Throwable t) { t.printStackTrace(); } return false; }
public HelpButton(URL helpUrl) { //icon licence: CC 3.0, attribution required: http://p.yusukekamiyamane.com/icons/attribution/ super(new ImageIcon(IJ.getClassLoader().getResource("resources/help/images/question-button-icon.png"))); setBorder(BorderFactory.createEmptyBorder()); setBorderPainted(false); setIconTextGap(0); setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); this.url = helpUrl; addActionListener(new HelpButtonActionListener()); }
public static List<PostProcessingModule> getPostProcessingModules() { //workaround a bug when service loading does not work after refreshing menus in ImageJ boolean oldUseCaching = setUseCaching(false); List<PostProcessingModule> retval = new ArrayList<PostProcessingModule>(); try { ServiceLoader loader = ServiceLoader.load(PostProcessingModule.class, IJ.getClassLoader()); for(Iterator<PostProcessingModule> it = loader.iterator(); it.hasNext();) { //when something goes wrong while loading modules, log the error and try to continue try { retval.add(it.next()); } catch(ServiceConfigurationError e) { IJ.handleException(e); } } } catch(Throwable e) { IJ.handleException(e); } finally { setUseCaching(oldUseCaching); } if(retval.isEmpty()) { //throw exception only when no modules are succesfully loaded throw new RuntimeException("No modules of type " + PostProcessingModule.class.getSimpleName() + " loaded."); } return retval; }
public static <T extends IModule> ArrayList<T> getModules(Class<T> c) { //workaround a bug when service loading does not work after refreshing menus in ImageJ boolean oldUseCaching = setUseCaching(false); ArrayList<T> retval = new ArrayList<T>(); try { ServiceLoader loader = ServiceLoader.load(c, IJ.getClassLoader()); for(Iterator<T> it = loader.iterator(); it.hasNext();) { //when something goes wrong while loading modules, log the error and try to continue try { retval.add(it.next()); } catch(ServiceConfigurationError e) { IJ.handleException(e); } } } catch(Throwable e) { IJ.handleException(e); } finally { setUseCaching(oldUseCaching); } if(retval.isEmpty()) { //throw exception only when no modules are succesfully loaded throw new RuntimeException("No modules of type " + c.getSimpleName() + " loaded."); } return retval; }
/** Opens an image file using the Bio-Formats plugin. */ public static ImagePlus openUsingBioFormats(String path) { String className = "loci.plugins.BF"; String methodName = "openImagePlus"; try { Class c = IJ.getClassLoader().loadClass(className); if (c==null) return null; Class[] argClasses = new Class[1]; argClasses[0] = methodName.getClass(); Method m = c.getMethod(methodName, argClasses); Object[] args = new Object[1]; args[0] = path; Object obj = m.invoke(null, args); ImagePlus[] images = obj!=null?(ImagePlus[])obj:null; if (images==null || images.length==0) return null; ImagePlus imp = images[0]; if (imp.getStackSize()==3 && imp.getNChannels()==3 && imp.getBitDepth()==8) imp = imp.flatten(); return imp; } catch(Exception e) { } return null; }
private void checkForUpdate(String plugin, String currentVersion) { int slashIndex = plugin.lastIndexOf("/"); if (slashIndex==-1 || !plugin.endsWith(".jar")) return; String className = plugin.substring(slashIndex+1, plugin.length()-4); File f = new File(Prefs.getImageJDir()+"plugins"+File.separator+"jars"+File.separator+className+".jar"); if (!f.exists() || !f.canWrite()) { if (IJ.debugMode) IJ.log("checkForUpdate: jar not found ("+plugin+")"); return; } String version = null; try { Class c = IJ.getClassLoader().loadClass("Compiler"); version = "0.00a"; Method m = c.getDeclaredMethod("getVersion", new Class[0]); version = (String)m.invoke(null, new Object[0]); } catch (Exception e) {} if (version==null) { if (IJ.debugMode) IJ.log("checkForUpdate: class not found ("+className+")"); return; } if (version.compareTo(currentVersion)>=0) { if (IJ.debugMode) IJ.log("checkForUpdate: up to date ("+className+" "+version+")"); return; } boolean ok = Macro_Runner.downloadJar(plugin); if (IJ.debugMode) IJ.log("checkForUpdate: "+className+" "+version+" "+ok); }
private void check() { try { try { IJ2Updater.newCheck(); return; } catch (Throwable t) { t.printStackTrace(); final Class<?> clazz = IJ.getClassLoader().loadClass(OBSOLETE_UPTODATE_CLASS_NAME); final Method check = clazz.getMethod("check"); final Object result = check.invoke(null); if (result != null && "UPDATEABLE".equals(result.toString())) { if (IJ.showMessageWithCancel("Updates available", "There are updates available. Run the updater?")) { run(""); } } } } catch (Throwable t) { t.printStackTrace(); if (IJ.showMessageWithCancel("Updater problem", "There was a problem checking whether everything is up-to-date.\n" + "Start the updater?")) { runRemote(); } } }
/** Opens an image file using the Bio-Formats plugin. */ public static ImagePlus openUsingBioFormats(String path) { String className = "loci.plugins.BF"; String methodName = "openImagePlus"; try { Class c = IJ.getClassLoader().loadClass(className); if (c==null) return null; Class[] argClasses = new Class[1]; argClasses[0] = methodName.getClass(); Method m = c.getMethod(methodName, argClasses); Object[] args = new Object[1]; args[0] = path; Object obj = m.invoke(null, args); ImagePlus[] images = obj!=null?(ImagePlus[])obj:null; if (images==null || images.length==0) return null; ImagePlus imp = images[0]; if (imp.getStackSize()==3 && imp.getNChannels()==3 && imp.getBitDepth()==8) imp = imp.flatten(); return imp; } catch(Exception e) { } return null; }