PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jdt.ui.source_3.7.1.r371_v20110824-0800/org/eclipse/jdt/internal/ui/text/correction/CorrectionMarkerResolutionGenerator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 418 lines | 305 code | 63 blank | 50 comment | 77 complexity | 896881ae9cb032f38317e050926c6d0a MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2011 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.eclipse.jdt.internal.ui.text.correction;
  12. import java.util.ArrayList;
  13. import java.util.Collections;
  14. import java.util.Hashtable;
  15. import java.util.Iterator;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.Map.Entry;
  19. import org.eclipse.swt.graphics.Image;
  20. import org.eclipse.core.runtime.CoreException;
  21. import org.eclipse.core.runtime.IProgressMonitor;
  22. import org.eclipse.core.runtime.NullProgressMonitor;
  23. import org.eclipse.core.resources.IFile;
  24. import org.eclipse.core.resources.IMarker;
  25. import org.eclipse.core.resources.IResource;
  26. import org.eclipse.jface.text.IDocument;
  27. import org.eclipse.jface.text.Position;
  28. import org.eclipse.jface.text.source.Annotation;
  29. import org.eclipse.jface.text.source.IAnnotationModel;
  30. import org.eclipse.ui.IEditorInput;
  31. import org.eclipse.ui.IEditorPart;
  32. import org.eclipse.ui.IMarkerResolution;
  33. import org.eclipse.ui.IMarkerResolutionGenerator2;
  34. import org.eclipse.ui.views.markers.WorkbenchMarkerResolution;
  35. import org.eclipse.ui.texteditor.ITextEditor;
  36. import org.eclipse.ui.texteditor.MarkerUtilities;
  37. import org.eclipse.jdt.core.CorrectionEngine;
  38. import org.eclipse.jdt.core.ICompilationUnit;
  39. import org.eclipse.jdt.core.IJavaElement;
  40. import org.eclipse.jdt.core.IJavaModelMarker;
  41. import org.eclipse.jdt.core.JavaCore;
  42. import org.eclipse.jdt.core.compiler.IProblem;
  43. import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring.MultiFixTarget;
  44. import org.eclipse.jdt.ui.JavaUI;
  45. import org.eclipse.jdt.ui.cleanup.ICleanUp;
  46. import org.eclipse.jdt.ui.text.java.CompletionProposalComparator;
  47. import org.eclipse.jdt.ui.text.java.IInvocationContext;
  48. import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
  49. import org.eclipse.jdt.ui.text.java.IProblemLocation;
  50. import org.eclipse.jdt.internal.ui.JavaPlugin;
  51. import org.eclipse.jdt.internal.ui.fix.IMultiFix;
  52. import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
  53. import org.eclipse.jdt.internal.ui.javaeditor.JavaMarkerAnnotation;
  54. import org.eclipse.jdt.internal.ui.text.correction.proposals.FixCorrectionProposal;
  55. public class CorrectionMarkerResolutionGenerator implements IMarkerResolutionGenerator2 {
  56. public static class CorrectionMarkerResolution extends WorkbenchMarkerResolution {
  57. private static final IMarker[] NO_MARKERS= new IMarker[0];
  58. private ICompilationUnit fCompilationUnit;
  59. private int fOffset;
  60. private int fLength;
  61. private IJavaCompletionProposal fProposal;
  62. private final IMarker fMarker;
  63. /**
  64. * Constructor for CorrectionMarkerResolution.
  65. * @param cu the compilation unit
  66. * @param offset the offset
  67. * @param length the length
  68. * @param proposal the proposal for the given marker
  69. * @param marker the marker to fix
  70. */
  71. public CorrectionMarkerResolution(ICompilationUnit cu, int offset, int length, IJavaCompletionProposal proposal, IMarker marker) {
  72. fCompilationUnit= cu;
  73. fOffset= offset;
  74. fLength= length;
  75. fProposal= proposal;
  76. fMarker= marker;
  77. }
  78. /* (non-Javadoc)
  79. * @see IMarkerResolution#getLabel()
  80. */
  81. public String getLabel() {
  82. return fProposal.getDisplayString();
  83. }
  84. /* (non-Javadoc)
  85. * @see IMarkerResolution#run(IMarker)
  86. */
  87. public void run(IMarker marker) {
  88. try {
  89. IEditorPart part= EditorUtility.isOpenInEditor(fCompilationUnit);
  90. if (part == null) {
  91. part= JavaUI.openInEditor(fCompilationUnit, true, false);
  92. if (part instanceof ITextEditor) {
  93. ((ITextEditor) part).selectAndReveal(fOffset, fLength);
  94. }
  95. }
  96. if (part != null) {
  97. IEditorInput input= part.getEditorInput();
  98. IDocument doc= JavaPlugin.getDefault().getCompilationUnitDocumentProvider().getDocument(input);
  99. fProposal.apply(doc);
  100. }
  101. } catch (CoreException e) {
  102. JavaPlugin.log(e);
  103. }
  104. }
  105. /**
  106. * {@inheritDoc}
  107. */
  108. @Override
  109. public void run(IMarker[] markers, IProgressMonitor monitor) {
  110. if (markers.length == 1) {
  111. run(markers[0]);
  112. return;
  113. }
  114. if (!(fProposal instanceof FixCorrectionProposal))
  115. return;
  116. if (monitor == null)
  117. monitor= new NullProgressMonitor();
  118. try {
  119. MultiFixTarget[] problems= getCleanUpTargets(markers);
  120. ((FixCorrectionProposal)fProposal).resolve(problems, monitor);
  121. IEditorPart part= EditorUtility.isOpenInEditor(fCompilationUnit);
  122. if (part instanceof ITextEditor) {
  123. ((ITextEditor) part).selectAndReveal(fOffset, fLength);
  124. part.setFocus();
  125. }
  126. } catch (CoreException e) {
  127. JavaPlugin.log(e);
  128. } finally {
  129. monitor.done();
  130. }
  131. }
  132. private MultiFixTarget[] getCleanUpTargets(IMarker[] markers) {
  133. Hashtable<ICompilationUnit, List<IProblemLocation>> problemLocations= new Hashtable<ICompilationUnit, List<IProblemLocation>>();
  134. for (int i= 0; i < markers.length; i++) {
  135. IMarker marker= markers[i];
  136. ICompilationUnit cu= getCompilationUnit(marker);
  137. if (cu != null) {
  138. IEditorInput input= EditorUtility.getEditorInput(cu);
  139. IProblemLocation location= findProblemLocation(input, marker);
  140. if (location != null) {
  141. List<IProblemLocation> l= problemLocations.get(cu.getPrimary());
  142. if (l == null) {
  143. l= new ArrayList<IProblemLocation>();
  144. problemLocations.put(cu.getPrimary(), l);
  145. }
  146. l.add(location);
  147. }
  148. }
  149. }
  150. MultiFixTarget[] result= new MultiFixTarget[problemLocations.size()];
  151. int i= 0;
  152. for (Iterator<Map.Entry<ICompilationUnit, List<IProblemLocation>>> iterator= problemLocations.entrySet().iterator(); iterator.hasNext();) {
  153. Map.Entry<ICompilationUnit, List<IProblemLocation>> entry= iterator.next();
  154. ICompilationUnit cu= entry.getKey();
  155. List<IProblemLocation> locations= entry.getValue();
  156. result[i]= new MultiFixTarget(cu, locations.toArray(new IProblemLocation[locations.size()]));
  157. i++;
  158. }
  159. return result;
  160. }
  161. /* (non-Javadoc)
  162. * @see org.eclipse.ui.IMarkerResolution2#getDescription()
  163. */
  164. public String getDescription() {
  165. return fProposal.getAdditionalProposalInfo();
  166. }
  167. /* (non-Javadoc)
  168. * @see org.eclipse.ui.IMarkerResolution2#getImage()
  169. */
  170. public Image getImage() {
  171. return fProposal.getImage();
  172. }
  173. /**
  174. * {@inheritDoc}
  175. */
  176. @Override
  177. public IMarker[] findOtherMarkers(IMarker[] markers) {
  178. if (!(fProposal instanceof FixCorrectionProposal))
  179. return NO_MARKERS;
  180. FixCorrectionProposal fix= (FixCorrectionProposal)fProposal;
  181. final ICleanUp cleanUp= fix.getCleanUp();
  182. if (!(cleanUp instanceof IMultiFix))
  183. return NO_MARKERS;
  184. IMultiFix multiFix= (IMultiFix) cleanUp;
  185. final Hashtable<IFile, List<IMarker>> fileMarkerTable= getMarkersForFiles(markers);
  186. if (fileMarkerTable.isEmpty())
  187. return NO_MARKERS;
  188. final List<IMarker> result= new ArrayList<IMarker>();
  189. for (Iterator<Entry<IFile, List<IMarker>>> iterator= fileMarkerTable.entrySet().iterator(); iterator.hasNext();) {
  190. Map.Entry<IFile, List<IMarker>> entry= iterator.next();
  191. IFile file= entry.getKey();
  192. List<IMarker> fileMarkers= entry.getValue();
  193. IJavaElement element= JavaCore.create(file);
  194. if (element instanceof ICompilationUnit) {
  195. ICompilationUnit unit= (ICompilationUnit) element;
  196. for (int i= 0, size= fileMarkers.size(); i < size; i++) {
  197. IMarker marker= fileMarkers.get(i);
  198. IProblemLocation problem= createFromMarker(marker, unit);
  199. if (problem != null && multiFix.canFix(unit, problem)) {
  200. result.add(marker);
  201. }
  202. }
  203. }
  204. }
  205. if (result.size() == 0)
  206. return NO_MARKERS;
  207. return result.toArray(new IMarker[result.size()]);
  208. }
  209. /**
  210. * Returns the markers with the same type as fMarker.getType for each IFile.
  211. * @param markers the markers
  212. * @return mapping files to markers
  213. */
  214. private Hashtable<IFile, List<IMarker>> getMarkersForFiles(IMarker[] markers) {
  215. final Hashtable<IFile, List<IMarker>> result= new Hashtable<IFile, List<IMarker>>();
  216. String markerType;
  217. try {
  218. markerType= fMarker.getType();
  219. } catch (CoreException e1) {
  220. JavaPlugin.log(e1);
  221. return result;
  222. }
  223. for (int i= 0; i < markers.length; i++) {
  224. IMarker marker= markers[i];
  225. if (!marker.equals(fMarker)) {
  226. String currMarkerType= null;
  227. try {
  228. currMarkerType= marker.getType();
  229. } catch (CoreException e1) {
  230. JavaPlugin.log(e1);
  231. }
  232. if (currMarkerType != null && currMarkerType.equals(markerType)) {
  233. IResource res= marker.getResource();
  234. if (res instanceof IFile && res.isAccessible()) {
  235. List<IMarker> markerList= result.get(res);
  236. if (markerList == null) {
  237. markerList= new ArrayList<IMarker>();
  238. result.put((IFile) res, markerList);
  239. }
  240. markerList.add(marker);
  241. }
  242. }
  243. }
  244. }
  245. return result;
  246. }
  247. }
  248. private static final IMarkerResolution[] NO_RESOLUTIONS= new IMarkerResolution[0];
  249. /**
  250. * Constructor for CorrectionMarkerResolutionGenerator.
  251. */
  252. public CorrectionMarkerResolutionGenerator() {
  253. super();
  254. }
  255. /* (non-Javadoc)
  256. * @see org.eclipse.ui.IMarkerResolutionGenerator2#hasResolutions(org.eclipse.core.resources.IMarker)
  257. */
  258. public boolean hasResolutions(IMarker marker) {
  259. return internalHasResolutions(marker);
  260. }
  261. /* (non-Javadoc)
  262. * @see IMarkerResolutionGenerator#getResolutions(IMarker)
  263. */
  264. public IMarkerResolution[] getResolutions(IMarker marker) {
  265. return internalGetResolutions(marker);
  266. }
  267. private static boolean internalHasResolutions(IMarker marker) {
  268. int id= marker.getAttribute(IJavaModelMarker.ID, -1);
  269. ICompilationUnit cu= getCompilationUnit(marker);
  270. return cu != null && JavaCorrectionProcessor.hasCorrections(cu, id, MarkerUtilities.getMarkerType(marker));
  271. }
  272. private static IMarkerResolution[] internalGetResolutions(IMarker marker) {
  273. if (!internalHasResolutions(marker)) {
  274. return NO_RESOLUTIONS;
  275. }
  276. ICompilationUnit cu= getCompilationUnit(marker);
  277. if (cu != null) {
  278. IEditorInput input= EditorUtility.getEditorInput(cu);
  279. if (input != null) {
  280. IProblemLocation location= findProblemLocation(input, marker);
  281. if (location != null) {
  282. IInvocationContext context= new AssistContext(cu, location.getOffset(), location.getLength());
  283. if (!hasProblem (context.getASTRoot().getProblems(), location))
  284. return NO_RESOLUTIONS;
  285. ArrayList<IJavaCompletionProposal> proposals= new ArrayList<IJavaCompletionProposal>();
  286. JavaCorrectionProcessor.collectCorrections(context, new IProblemLocation[] { location }, proposals);
  287. Collections.sort(proposals, new CompletionProposalComparator());
  288. int nProposals= proposals.size();
  289. IMarkerResolution[] resolutions= new IMarkerResolution[nProposals];
  290. for (int i= 0; i < nProposals; i++) {
  291. resolutions[i]= new CorrectionMarkerResolution(context.getCompilationUnit(), location.getOffset(), location.getLength(), proposals.get(i), marker);
  292. }
  293. return resolutions;
  294. }
  295. }
  296. }
  297. return NO_RESOLUTIONS;
  298. }
  299. private static boolean hasProblem(IProblem[] problems, IProblemLocation location) {
  300. for (int i= 0; i < problems.length; i++) {
  301. IProblem problem= problems[i];
  302. if (problem.getID() == location.getProblemId() && problem.getSourceStart() == location.getOffset())
  303. return true;
  304. }
  305. return false;
  306. }
  307. private static ICompilationUnit getCompilationUnit(IMarker marker) {
  308. IResource res= marker.getResource();
  309. if (res instanceof IFile && res.isAccessible()) {
  310. IJavaElement element= JavaCore.create((IFile) res);
  311. if (element instanceof ICompilationUnit)
  312. return (ICompilationUnit) element;
  313. }
  314. return null;
  315. }
  316. private static IProblemLocation findProblemLocation(IEditorInput input, IMarker marker) {
  317. IAnnotationModel model= JavaPlugin.getDefault().getCompilationUnitDocumentProvider().getAnnotationModel(input);
  318. if (model != null) { // open in editor
  319. Iterator<Annotation> iter= model.getAnnotationIterator();
  320. while (iter.hasNext()) {
  321. Annotation curr= iter.next();
  322. if (curr instanceof JavaMarkerAnnotation) {
  323. JavaMarkerAnnotation annot= (JavaMarkerAnnotation) curr;
  324. if (marker.equals(annot.getMarker())) {
  325. Position pos= model.getPosition(annot);
  326. if (pos != null) {
  327. return new ProblemLocation(pos.getOffset(), pos.getLength(), annot);
  328. }
  329. }
  330. }
  331. }
  332. } else { // not open in editor
  333. ICompilationUnit cu= getCompilationUnit(marker);
  334. return createFromMarker(marker, cu);
  335. }
  336. return null;
  337. }
  338. private static IProblemLocation createFromMarker(IMarker marker, ICompilationUnit cu) {
  339. try {
  340. int id= marker.getAttribute(IJavaModelMarker.ID, -1);
  341. int start= marker.getAttribute(IMarker.CHAR_START, -1);
  342. int end= marker.getAttribute(IMarker.CHAR_END, -1);
  343. int severity= marker.getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
  344. String[] arguments= CorrectionEngine.getProblemArguments(marker);
  345. String markerType= marker.getType();
  346. if (cu != null && id != -1 && start != -1 && end != -1 && arguments != null) {
  347. boolean isError= (severity == IMarker.SEVERITY_ERROR);
  348. return new ProblemLocation(start, end - start, id, arguments, isError, markerType);
  349. }
  350. } catch (CoreException e) {
  351. JavaPlugin.log(e);
  352. }
  353. return null;
  354. }
  355. }