001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019// LegacyInjector.java
020//
021
022/*
023 * ImageJ software for multidimensional image processing and analysis.
024 * 
025 * Copyright (c) 2010, ImageJDev.org.
026 * All rights reserved.
027 * 
028 * Redistribution and use in source and binary forms, with or without
029 * modification, are permitted provided that the following conditions are met:
030 * Redistributions of source code must retain the above copyright
031 * notice, this list of conditions and the following disclaimer.
032 * Redistributions in binary form must reproduce the above copyright
033 * notice, this list of conditions and the following disclaimer in the
034 * documentation and/or other materials provided with the distribution.
035 * Neither the names of the ImageJDev.org developers nor the
036 * names of its contributors may be used to endorse or promote products
037 * derived from this software without specific prior written permission.
038 * 
039 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
040 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
041 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
042 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
043 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
044 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
045 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
046 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
047 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
048 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
049 * POSSIBILITY OF SUCH DAMAGE.
050 */
051
052package icy.imagej;
053
054import icy.system.ClassPatcher;
055import icy.system.SystemUtil;
056
057/**
058 * Overrides class behavior of ImageJ classes using bytecode manipulation. This
059 * class uses the {@link ClassPatcher} (which uses Javassist) to inject method
060 * hooks, which are implemented in the {@link icy.imagej.patches} package.
061 * 
062 * @author Curtis Rueden
063 * @author Stephane Dallongeville
064 */
065public class ImageJPatcher
066{
067    private static final String PATCH_PKG = "icy.imagej.patches";
068    private static final String PATCH_SUFFIX = "Methods";
069
070    /** Overrides class behavior of ImageJ classes by injecting method hooks. */
071    public static void applyPatches()
072    {
073        final ClassPatcher hacker = new ClassPatcher(PATCH_PKG, PATCH_SUFFIX);
074
075        // override behavior of ij.IJ
076        hacker.insertAfterMethod("ij.IJ", "public static void showProgress(double progress)");
077        hacker.insertAfterMethod("ij.IJ", "public static void showProgress(int currentIndex, int finalIndex)");
078        hacker.insertAfterMethod("ij.IJ", "public static void showStatus(java.lang.String s)");
079        hacker.loadClass("ij.IJ");
080
081        // override behavior of ij.ImageJ
082        hacker.insertAfterMethod("ij.ImageJ", "public void showStatus(java.lang.String s)");
083        hacker.replaceMethod("ij.ImageJ", "public void configureProxy()");
084        hacker.loadClass("ij.ImageJ");
085
086        // override behavior of ij.Menus
087        hacker.insertAfterMethod("ij.Menus",
088                "public void installUserPlugin(java.lang.String className, boolean force)");
089        hacker.insertAfterMethod("ij.Menus", "public static void updateMenus()");
090        hacker.insertAfterMethod("ij.Menus",
091                "public static synchronized void updateWindowMenuItem(java.lang.String oldLabel, java.lang.String newLabel)");
092        hacker.insertAfterMethod("ij.Menus",
093                "public static synchronized void addOpenRecentItem(java.lang.String path)");
094        hacker.insertAfterMethod("ij.Menus",
095                "public static int installPlugin(java.lang.String plugin, char menuCode, java.lang.String command, java.lang.String shortcut, ij.ImageJ ij, int result)");
096        hacker.loadClass("ij.Menus");
097
098        // override behavior of ij.ImagePlus
099        // hacker.insertAfterMethod("ij.ImagePlus", "public void updateAndDraw()");
100        // hacker.insertAfterMethod("ij.ImagePlus", "public void repaintWindow()");
101        // hacker.insertAfterMethod("ij.ImagePlus",
102        // "public void show(java.lang.String statusMessage)");
103        // hacker.insertAfterMethod("ij.ImagePlus", "public void hide()");
104        // hacker.insertAfterMethod("ij.ImagePlus", "public void close()");
105        // hacker.loadClass("ij.ImagePlus");
106
107        // override behavior of ij.gui.ImageWindow
108        // hacker.insertMethod("ij.gui.ImageWindow", "public void setVisible(boolean vis)");
109        // hacker.insertMethod("ij.gui.ImageWindow", "public void show()");
110        // hacker.insertBeforeMethod("ij.gui.ImageWindow", "public void close()");
111        hacker.insertAfterMethod("ij.gui.ImageWindow", "public void windowActivated(java.awt.event.WindowEvent e)");
112        hacker.insertAfterMethod("ij.gui.ImageWindow", "public void windowClosed(java.awt.event.WindowEvent e)");
113        hacker.loadClass("ij.gui.ImageWindow");
114
115        // override behavior of MacAdapter
116        if (SystemUtil.isMac())
117        {
118            hacker.replaceMethod("MacAdapter", "public void run(java.lang.String arg)");
119            hacker.replaceMethod("MacAdapter", "public voi  d handleAbout(com.apple.eawt.ApplicationEvent e)");
120            hacker.replaceMethod("MacAdapter", "public void handleOpenApplication(com.apple.eawt.ApplicationEvent e)");
121            hacker.replaceMethod("MacAdapter", "public void handleOpenFile(com.apple.eawt.ApplicationEvent e)");
122            hacker.replaceMethod("MacAdapter", "public void handlePreferences(com.apple.eawt.ApplicationEvent e)");
123            hacker.replaceMethod("MacAdapter", "public void handlePrintFile(com.apple.eawt.ApplicationEvent e)");
124            hacker.replaceMethod("MacAdapter",
125                    "public void handleReOpenApplication(com.apple.eawt.ApplicationEvent e)");
126            hacker.replaceMethod("MacAdapter", "public void handleQuit(com.apple.eawt.ApplicationEvent e)");
127            hacker.loadClass("MacAdapter");
128        }
129    }
130}