SWING开发释放资源工具类

1.最近在做swing开发,数据量很多,然后碰到swing内存溢出的问题,故参照CSDN上别人的思路写啦一个工具类。
package com.sf.module.common.util;

import java.awt.Component;
import java.awt.Container;
import java.awt.Window;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;
import java.awt.event.ContainerListener;
import java.awt.event.FocusListener;
import java.awt.event.HierarchyBoundsListener;
import java.awt.event.HierarchyListener;
import java.awt.event.InputMethodListener;
import java.awt.event.ItemListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.beans.PropertyChangeListener;
import java.beans.VetoableChangeListener;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.EventListener;

import javax.swing.AbstractButton;
import javax.swing.ActionMap;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;
import javax.swing.event.AncestorListener;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeListener;
import javax.swing.text.JTextComponent;


/**
* <p>类型名称: UIReleaseUtil </p>
* <p>类型描述: 释放资源的工具类。</p>
* <p>add by sgh 461245 on 2014-01-07</p>
*/
public class UIReleaseUtil
{
    private UIReleaseUtil()
    {  
    }

/**
     * 释放资源
     * @param cmp
     */
    public static void freeSwingObject(Component cmp)
    {
        freeSwingObjectImpl(cmp);
        freeObject(cmp);
    }
   
    /**
     * 使一个对话框关闭时内存可以被释放
     * @param dlg
     */
    public static void createFreeableDlg(JDialog dlg)
    {
        dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        dlg.addWindowListener(new WindowCloseAdapter());
    }


    private static void freeSwingObjectImpl(Component cmp)
    {
        if (cmp == null)
        {
            return;
        }
        freeComponent(cmp);
        freeContainer(cmp);
        freeJComponent(cmp);
        freeButton(cmp);
        freeText(cmp);
        freeWindow(cmp);
    }

    private static void freeComponent(Component cmp)
    {
        //注销并释放监听器资源
        EventListener[] listeners = cmp.getComponentListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            cmp.removeComponentListener((ComponentListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = cmp.getFocusListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            cmp.removeFocusListener((FocusListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = cmp.getHierarchyListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            cmp.removeHierarchyListener((HierarchyListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = cmp.getHierarchyBoundsListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            cmp.removeHierarchyBoundsListener((HierarchyBoundsListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = cmp.getInputMethodListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            cmp.removeInputMethodListener((InputMethodListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = cmp.getKeyListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            cmp.removeKeyListener((KeyListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = cmp.getMouseListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            cmp.removeMouseListener((MouseListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = cmp.getMouseMotionListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            cmp.removeMouseMotionListener((MouseMotionListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = cmp.getMouseWheelListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            cmp.removeMouseWheelListener((MouseWheelListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = cmp.getPropertyChangeListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            cmp.removePropertyChangeListener((PropertyChangeListener) listeners[i]);
        }
        closeUIFreeable(listeners);
    }

    private static void freeContainer(Component cmp)
    {
        if (!(cmp instanceof Container))
        {
            return;
        }
        Container container = (Container) cmp;
        Component[] cmps = container.getComponents();
        for (int i = 0; i < cmps.length; i++)
        {
            freeSwingObjectImpl(cmps[i]);
        }
        container.removeAll();
    }

    private static void freeJComponent(Component cmp)
    {
        if (!(cmp instanceof JComponent))
        {
            return;
        }

        JComponent jcmp = (JComponent) cmp;

        //注销并释放监听器资源
        EventListener[] listeners = jcmp.getAncestorListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            jcmp.removeAncestorListener((AncestorListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = jcmp.getContainerListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            jcmp.removeContainerListener((ContainerListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = jcmp.getVetoableChangeListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            jcmp.removeVetoableChangeListener((VetoableChangeListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        //释放keystroke
        KeyStroke[] keystrokes = jcmp.getRegisteredKeyStrokes();
        for (int i = 0; i < keystrokes.length; i++)
        {
            jcmp.unregisterKeyboardAction(keystrokes[i]);
        }
        closeUIFreeable(keystrokes);

        //其他
        ActionMap actionMap = jcmp.getActionMap();
        if (actionMap != null)
        {
            actionMap.clear();
        }
        jcmp.setActionMap(null);
    }

    private static void freeWindow(Component cmp)
    {
        if (!(cmp instanceof Window))
        {
            return;
        }
        Window window = (Window) cmp;
        //注销并释放监听器资源
        EventListener[] listeners = window.getWindowFocusListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            window.removeWindowFocusListener((WindowFocusListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = window.getWindowListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            window.removeWindowListener((WindowListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = window.getWindowStateListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            window.removeWindowStateListener((WindowStateListener) listeners[i]);
        }
        closeUIFreeable(listeners);
        window.dispose();
    }

    private static void freeButton(Component cmp)
    {
        if (!(cmp instanceof AbstractButton))
        {
            return;
        }

        AbstractButton btn = (AbstractButton) cmp;
        //注销并释放监听器资源
        EventListener[] listeners = btn.getActionListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            btn.removeActionListener((ActionListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = btn.getChangeListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            btn.removeChangeListener((ChangeListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        listeners = btn.getItemListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            btn.removeItemListener((ItemListener) listeners[i]);
        }
        closeUIFreeable(listeners);

        closeUIFreeable(btn.getAction());
        btn.setAction(null);
    }

    private static void freeText(Component cmp)
    {
        if (!(cmp instanceof JTextComponent))
        {
            return;
        }

        JTextComponent text = (JTextComponent) cmp;
        EventListener[] listeners = text.getCaretListeners();
        for (int i = 0; i < listeners.length; i++)
        {
            text.removeCaretListener((CaretListener) listeners[i]);
        }
        closeUIFreeable(listeners);
    }

    static void freeObject(final Object obj)
    {
        Field[] fields = obj.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++)
        {
            if (Modifier.isFinal(fields[i].getModifiers()))
            {
                continue;
            }
            if (Modifier.isStatic(fields[i].getModifiers()))
            {
                continue;
            }
            //基本类型或者String不释放
            if (fields[i].getType().isPrimitive() || fields[i].getType().getName().equals("java.lang.String"))
            {
                continue;
            }
            try
            {
                fields[i].setAccessible(true);
                fields[i].set(obj, null);
            }
            catch (Exception ignore)
            {
                //dMsg.warn(ignore);
            }
        }
    }

    private static void closeUIFreeable(Object[] freeables)
    {
        for (int i = 0; i < freeables.length; i++)
        {
            closeUIFreeable(freeables[i]);
        }
    }

    private static void closeUIFreeable(Object freeable)
    {
       /* if (freeable instanceof UIFreeable)
        {
            ((UIFreeable) (freeable)).freeResource();
        }*/
    freeable = null;
    }
   
   // private static DebugPrn dMsg = new DebugPrn(GuiUtil.class.getName());
}

/**
* <p>文件名称: WindowCloseAdapter.java </p>
* <p>文件描述: 窗口关闭器。
* <p>用于关闭对话框,并回收其资源。</p>
*/
class WindowCloseAdapter extends WindowAdapter

    /**
     * 默认构造方法。
     */
    public WindowCloseAdapter()
    {
        super();
    }
   
    /**
     * 带是否释放资源参数的构造方法。
     * @param dispose boolean
     */
    public WindowCloseAdapter(boolean dispose)
    {
        super();
        this.dispose = dispose;
    }

    /**
     * @see java.awt.event.WindowListener#windowClosing(WindowEvent)
     * @param e
     */
    public void windowClosing(WindowEvent e)
    {
        Window source = e.getWindow();      
        if (source != null)
        {
            if(!dispose)
            {
                source.hide();
                return;
            }
           
           // if (source instanceof UIFreeable)
           // {
               // ((UIFreeable) source).freeResource();
           // }
            source.dispose();
            UIReleaseUtil.freeSwingObject(source);
            //GuiUtil.freeSwingObject(source);
        }
    }
   
    //关闭的时候是否释放资源
    private boolean dispose = true;
}



你可能感兴趣的:(swing)