1 /*******************************************************************************
2 * This program and the accompanying materials
3 * are made available under the terms of the Eclipse Public License v1.0
4 * which accompanies this distribution, and is available at
5 * http://www.eclipse.org/legal/epl-v10.html.
7 * This file is a derivative of code released by the University of
8 * California under the terms listed below.
10 * Refinement Analysis Tools is Copyright ©2007 The Regents of the
11 * University of California (Regents). Provided that this notice and
12 * the following two paragraphs are included in any distribution of
13 * Refinement Analysis Tools or its derivative work, Regents agrees
14 * not to assert any of Regents' copyright rights in Refinement
15 * Analysis Tools against recipient for recipients reproduction,
16 * preparation of derivative works, public display, public
17 * performance, distribution or sublicensing of Refinement Analysis
18 * Tools and derivative works, in source code and object code form.
19 * This agreement not to assert does not confer, by implication,
20 * estoppel, or otherwise any license or rights in any intellectual
21 * property of Regents, including, but not limited to, any patents
22 * of Regents or Regents employees.
24 * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT,
25 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
26 * INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE
27 * AND ITS DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
32 * FOR A PARTICULAR PURPOSE AND FURTHER DISCLAIMS ANY STATUTORY
33 * WARRANTY OF NON-INFRINGEMENT. THE SOFTWARE AND ACCOMPANYING
34 * DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
35 * IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
36 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
38 package edu
.berkeley
.cs
.bodik
.svelte
.plugin
;
40 import java
.io
.FileNotFoundException
;
41 import java
.io
.PrintWriter
;
42 import java
.util
.Collection
;
43 import java
.util
.HashSet
;
46 import org
.eclipse
.core
.resources
.IFile
;
47 import org
.eclipse
.core
.resources
.IMarker
;
48 import org
.eclipse
.core
.resources
.IProject
;
49 import org
.eclipse
.core
.resources
.IResource
;
50 import org
.eclipse
.core
.resources
.IWorkspaceRunnable
;
51 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
52 import org
.eclipse
.core
.runtime
.CoreException
;
53 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
54 import org
.eclipse
.core
.runtime
.IStatus
;
55 import org
.eclipse
.core
.runtime
.Status
;
56 import org
.eclipse
.core
.runtime
.jobs
.Job
;
57 import org
.eclipse
.jdt
.core
.JavaModelException
;
58 import org
.eclipse
.jface
.text
.ITextSelection
;
59 import org
.eclipse
.ui
.IEditorPart
;
60 import org
.eclipse
.ui
.IFileEditorInput
;
62 import com
.ibm
.wala
.ipa
.slicer
.Statement
;
64 import edu
.berkeley
.cs
.bodik
.svelte
.Slice
;
65 import edu
.berkeley
.cs
.bodik
.svelte
.SourcePosition
;
66 import edu
.berkeley
.cs
.bodik
.svelte
.Slicing
.SliceType
;
67 import edu
.berkeley
.cs
.bodik
.svelte
.slicers
.AbstractSliceEnvironment
;
69 public class SlicingJob
extends Job
{
71 private IEditorPart editor
;
72 private SliceType sliceType
;
73 private ITextSelection selection
;
76 public SlicingJob(SliceType sliceType
, int depth
, IEditorPart editor
, ITextSelection selection
) {
78 this.sliceType
= sliceType
;
80 this.file
= ((IFileEditorInput
) editor
.getEditorInput()).getFile();
81 this.selection
= selection
;
87 * Draws the markers in the editor corresponding to the given slice
91 public class MarkerDrawerRunnable
implements IWorkspaceRunnable
{
93 public String markerType
= "edu.berkeley.cs.bodik.svelte.plugin.sliceMarker";
95 public MarkerDrawerRunnable(Slice slice
) {
99 public void run(IProgressMonitor monitor
) {
100 IProject proj
= file
.getProject();
104 proj
.deleteMarkers(markerType
, true, IResource
.DEPTH_INFINITE
);
105 } catch (CoreException e
) {
109 Set
<String
> markersAdded
= new HashSet
<String
>(slice
.getStatements().size());
111 // TODO: lots of resource change signals could cause slow down. temporarily remove
112 // listener? or some other way?
113 for (Statement ss
: slice
) {
115 SourcePosition s
= slice
.getEnvironment().statementToPosition(ss
);
117 // skip if have a null source position
121 // watch for duplicate lines; only for Shrike (no column info)
122 // may want to do for CAst, too -- use minimum & maximum column #s on that line.
123 if (s
.offsetStart
<= 0) {
124 String tohash
= s
.lineStart
+ " " + s
.filename
;
125 if (markersAdded
.contains(tohash
))
127 markersAdded
.add(tohash
);
130 IFile f
= EclipseUtils
.findIFileForFilename(s
.filename
);
135 IMarker marker
= f
.createMarker(markerType
);
136 marker
.setAttribute(IMarker
.SEVERITY
, 0);
137 marker
.setAttribute(IMarker
.PRIORITY
, IMarker
.PRIORITY_HIGH
);
140 marker
.setAttribute(IMarker
.LINE_NUMBER
, s
.lineStart
);
141 if (s
.offsetStart
> 0) {
142 marker
.setAttribute(IMarker
.CHAR_START
, s
.offsetStart
);
143 marker
.setAttribute(IMarker
.CHAR_END
, s
.offsetEnd
);
146 String descr
= ss
.toString().replaceFirst("[^:]*:", "");
147 if ((selection
.getStartLine() + 1) == s
.lineStart
) {
148 marker
.setAttribute(IMarker
.MESSAGE
, "Seed: " + descr
);
150 marker
.setAttribute(IMarker
.MESSAGE
, "Slice: " + descr
);
153 System
.err
.println("couldn't find filename " + s
.filename
);
155 } catch (JavaModelException e
) {
157 } catch (CoreException e
) {
165 protected IStatus
run(IProgressMonitor monitor
) {
167 return realrun(monitor
);
168 } catch (RuntimeException e
) {
169 System
.err
.println("CAUGHT RUNTIMEEXCEPTION IN SLICINGJOB:\n");
175 protected IStatus
realrun(IProgressMonitor monitor
) {
177 AbstractSliceEnvironment env
= SliceEnvironmentCache
.getInstance().getSliceEnvironment(this.file
);
179 return new Status(IStatus
.WARNING
, SveltePlugin
.PLUGIN_ID
, 0, "Unsupported filetype!",
183 SourcePosition position
= SourcePosition
.makeSourcePosition(editor
, selection
);
184 Collection
<Statement
> seed
= env
.generateSeed(position
,editor
);
185 if ( seed
== null || seed
.size() == 0 || (seed
.size() == 1 && seed
.toArray()[0] == null) ) {
186 return new Status(IStatus
.WARNING
, SveltePlugin
.PLUGIN_ID
, 0, "No seed", null);
188 Slice slice
= env
.getSlice(sliceType
, depth
, seed
);
191 return new Status(IStatus
.WARNING
, SveltePlugin
.PLUGIN_ID
, 0, "NULL Slice/empty slice",
196 // draw markers for the slice
197 ResourcesPlugin
.getWorkspace().run(new MarkerDrawerRunnable(slice
), null);
198 } catch (CoreException e1
) {
199 e1
.printStackTrace();
202 return new Status(IStatus
.OK
, SveltePlugin
.PLUGIN_ID
, 0, "Finished", null);