eplic.core.breakpoint.BreakpointManager.java Source code

Java tutorial

Introduction

Here is the source code for eplic.core.breakpoint.BreakpointManager.java

Source

/**
 * EPLIC - A Tool to Assist Locating Interested Code.
 * Copyright (C) 2013 Frank Wang <eternnoir@gmail.com>
 * 
 * This file is part of EPLIC.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package eplic.core.breakpoint;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.debug.core.model.ILineBreakpoint;

import eplic.core.eventHandler.EventCenter;

/**
 * Class ?breakpoint??breakpoint
 * ?breakpoint
 * 
 * @author FrankWang
 *
 */
public class BreakpointManager {
    private static BreakpointManager instance = null;
    private ArrayList<ILineBreakpoint> _result;
    private Map<IBreakpoint, IResource> _breakpointMap;
    private IBreakpointSetter _bpSetter;

    public static BreakpointManager getInstance() {
        if (instance == null) {
            instance = new BreakpointManager();
        }
        return instance;
    }

    public BreakpointManager() {
        _result = null;
        _breakpointMap = new HashMap<IBreakpoint, IResource>();
        _bpSetter = new JavaBreakpointSetter(); //just support java
    }

    /**
     * breakpoint set, normal setIS
     * IS?NSbreakpoint
     * 
     * @param nor
     * @param rec
     * @return result set
     */
    public ArrayList<ILineBreakpoint> diffResult(ArrayList<ILineBreakpoint> nor, ArrayList<ILineBreakpoint> rec) {
        for (ILineBreakpoint nm : nor) {
            while (rec.indexOf(nm) != -1) {
                rec.remove(nm);
            }
        }
        _result = new ArrayList<ILineBreakpoint>(rec.size());
        for (ILineBreakpoint b : rec) {
            _result.add(b);
        }
        return _result;
    }

    /**
     * get result set
     * @return
     */

    public ArrayList<ILineBreakpoint> getResult() {
        assert (_result != null);
        return _result;
    }

    /**
     * disable all breakpoint in project
     */
    public void disableAllBreakpoint() {
        IBreakpoint[] b = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints();
        for (IBreakpoint _b : b) {
            try {
                _b.setEnabled(false);
            } catch (CoreException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * remove all breakpoint 
     */
    public void removeAllBreakpoint() {
        IBreakpoint[] b = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints();
        try {
            DebugPlugin.getDefault().getBreakpointManager().removeBreakpoints(b, true);
        } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * set result set's breakpoint
     */
    public void setResult() {
        for (ILineBreakpoint b : _result) {
            try {
                b.setEnabled(true);
            } catch (CoreException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * ??
     */
    public void setAllBreakpoint() {
        // for now, just support java
        ArrayList<IResource> resourceList = FileParaviserUtils.getAllFilesInProject("java");
        for (IResource r : resourceList) {
            setBreakpointByResource(r);
        }
    }

    /**
     * iresource??
     * 
     * @param r
     */
    public void setBreakpointByResource(IResource r) {
        IFile f = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(r.getLocation());
        String strings;
        String[] lines = null;
        // get single line,to check need to set breakpoint
        try {
            InputStream s = f.getContents();
            strings = IOUtils.toString(s, "UTF-8");
            lines = strings.split("\n");
        } catch (CoreException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ArrayList<Integer> lineNumbers = checkFunctionNameLineNumber(lines);
        for (int i : lineNumbers) {
            _bpSetter.setBreakpoint(r, i);
        }

    }

    /**
     * create a map between breakpoint and resource
     * ?breakpoint
     * 
     * @param b
     * @param r
     */
    public void addBreakpointSet(IBreakpoint b, IResource r) {
        _breakpointMap.put(b, r);
    }

    /**
     * get iresource by breakpoint
     * @param b
     * @return
     */
    public IResource getResourceByBreakpoint(IBreakpoint b) {
        IResource ret = null;
        ret = _breakpointMap.get(b);
        return ret;
    }

    /**
     * get Normal set, it will ask eventcenter for normal set
     * 
     * @see EventCenter
     * @return
     */
    public ArrayList<ILineBreakpoint> getNormalSet() {
        return EventCenter.getInstance().getNorMode().getBreakPointRecorder().getBPS();
    }

    /**
     * get Interested  set, it will ask eventcenter for Interested  set
     * 
     * @see EventCenter
     * @return
     */
    public ArrayList<ILineBreakpoint> getInterestedSet() {
        return EventCenter.getInstance().getInsMode().getBreakPointRecorder().getBPS();
    }

    /**
     * reset, it will remove all breakpoints which are ECPLIC set.
     */
    public void reset() {
        this.removeAllBreakpoint();
    }

    /**
     * ???
     * 
     * @param line
     * @return
     */

    private ArrayList<Integer> checkFunctionNameLineNumber(String[] line) {
        ArrayList<Integer> ret = new ArrayList<Integer>();
        for (int i = 0; i < line.length; i++) {
            if (checkFunction(line[i])) { //function
                ret.add(i + 1);
            }
        }

        return ret;
    }

    /**
     * ???
     * @param s
     * @return
     */

    private boolean checkFunction(String s) {
        if ((s.indexOf("public") > -1) || (s.indexOf("private") > -1) || (s.indexOf("protected") > -1)) { //function
            if ((s.indexOf("(") > -1) && (s.indexOf("abstract") < 0)) {
                return true;
            }
        }
        return false;
    }

}