下面列出了怎么用org.eclipse.ui.IMarkerResolution的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* {@inheritDoc}
*/
@Override
public IMarkerResolution[] getResolutions(IMarker marker) {
Collection<ICheckstyleMarkerResolution> fixes = new ArrayList<>();
// get all fixes that apply to this marker instance
String moduleName = marker.getAttribute(CheckstyleMarker.MODULE_NAME, null);
RuleMetadata metadata = MetadataFactory.getRuleMetadata(moduleName);
List<ICheckstyleMarkerResolution> potentialFixes = getInstantiatedQuickfixes(metadata);
for (ICheckstyleMarkerResolution fix : potentialFixes) {
if (fix.canFix(marker)) {
fixes.add(fix);
}
}
return fixes.toArray(new ICheckstyleMarkerResolution[fixes.size()]);
}
@Override
protected IMarkerResolution[] getAdaptedResolutions(List<IssueResolution> resolutions) {
// choose valid resolutions
final List<IssueResolution> validResolutions = new ArrayList<>(resolutions.size());
if (isMultiApplyAttempt()) {
// only those that support multi-apply are valid
for (IssueResolution currResolution : resolutions) {
if (supportsMultiApply(currResolution))
validResolutions.add(currResolution);
}
if (validResolutions.size() < resolutions.size())
showError_MultiApplyNotSupported();
} else {
// all are valid
validResolutions.addAll(resolutions);
}
// perform wrapping
IMarkerResolution[] result = new IMarkerResolution[validResolutions.size()];
for (int i = 0; i < validResolutions.size(); i++)
result[i] = new MultiResolutionAdapter(validResolutions.get(i));
return result;
}
@Override
public IMarkerResolution[] getResolutions(IMarker marker) {
ArrayList<IMarkerResolution> resolutions = new ArrayList<>();
try {
if ("com.google.cloud.tools.eclipse.appengine.validation.applicationMarker"
.equals(marker.getType())) {
resolutions.add(new ApplicationQuickFix());
} else if ("com.google.cloud.tools.eclipse.appengine.validation.versionMarker"
.equals(marker.getType())) {
resolutions.add(new VersionQuickFix());
} else if ("com.google.cloud.tools.eclipse.appengine.validation.runtimeMarker"
.equals(marker.getType())) {
resolutions.add(new UpgradeRuntimeQuickFix());
}
} catch (CoreException ex) {
logger.log(Level.SEVERE, ex.getMessage());
}
return resolutions.toArray(new IMarkerResolution[0]);
}
protected void applyQuickfixOnMultipleMarkers(IMarker[] markers) {
IMarker primaryMarker = markers[0];
IMarkerResolution[] resolutions = markerResolutionGenerator.getResolutions(primaryMarker);
Assert.assertEquals(1, resolutions.length);
assertTrue(resolutions[0] instanceof WorkbenchMarkerResolution);
WorkbenchMarkerResolution resolution = (WorkbenchMarkerResolution) resolutions[0];
List<IMarker> others = Lists.newArrayList(resolution.findOtherMarkers(markers));
assertFalse(others.contains(primaryMarker));
assertEquals(markers.length - 1, others.size());
others.add(primaryMarker);
long seed = new Random().nextLong();
// System out is intended so that the seed used can be recovered on failures
System.out.println(seed);
Collections.shuffle(others, new Random(seed));
resolution.run(others.toArray(new IMarker[others.size()]), new NullProgressMonitor());
}
/**
* Generate resolutions for the given error marker.
* Marker type must be SpellChecker.SPELLING_ERROR_MARKER_TYPE.
*
* @param marker marker for the error
* @return an array of resolutions for the given marker
* or null if an error occurs or the marker is of wrong type
*/
public IMarkerResolution[] getResolutions(IMarker marker) {
try {
if (!SpellChecker.SPELLING_ERROR_MARKER_TYPE.equals(marker.getType())) {
return null;
}
} catch (CoreException e) {
return null;
}
String[] proposals = SpellChecker.getProposals(marker);
if (proposals == null || proposals.length == 0) {
return null;
}
IDocument doc = getProviderDocument();
IMarkerResolution[] res = new IMarkerResolution[proposals.length];
for (int i = 0; i < res.length; i++) {
res[i] = new SpellingMarkerResolution(proposals[i], doc);
}
return res;
}
public IMarkerResolution[] createBugResolutions(String bugType, IMarker marker) {
Assert.isNotNull(bugType);
Assert.isNotNull(marker);
List<QuickFixContribution> classes = quickFixes.get(bugType);
if (classes == null) {
return new IMarkerResolution[0];
}
Set<BugResolution> fixes = instantiateBugResolutions(classes);
for (Iterator<BugResolution> iterator = fixes.iterator(); iterator.hasNext();) {
BugResolution fix = iterator.next();
if (fix.isApplicable(marker)) {
fix.setMarker(marker);
} else {
iterator.remove();
}
}
return fixes.toArray(new IMarkerResolution[fixes.size()]);
}
private void applyMultiResolutionToAllMarkers(IMarker[] markers) {
IMarkerResolution[] resolutions = getResolutionGenerator().getResolutions(markers[0]);
if (resolutions[0] instanceof WorkbenchMarkerResolution) {
//this represents one of the bugs a user would click on in the problems menu
WorkbenchMarkerResolution resolutionFromProblemsMenu = ((WorkbenchMarkerResolution) resolutions[0]);
//in theory, we should have filtered all the bugs of the passed in type
//So, findOtherMarkers should return them all
assertEquals(markers.length - 1, resolutionFromProblemsMenu.findOtherMarkers(markers).length);
resolutionFromProblemsMenu.run(markers, null);
} else {
fail("Should have been a WorkBenchMarkerResolution: " + resolutions[0]);
}
}
public IMarkerResolution[] getResolutions(IMarker marker) {
List conflictResolutions = new ArrayList();
try {
if (marker.getAttribute("textConflict") != null && marker.getAttribute("textConflict").toString().equals("true")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
conflictResolutions.add(new EditConflictsResolution());
conflictResolutions.add(new AcceptMineResolution());
conflictResolutions.add(new AcceptTheirsResolution());
}
} catch (Exception e) {
SVNUIPlugin.log(e.getMessage());
}
conflictResolutions.add(new MarkAsResolvedResolution());
IMarkerResolution[] resolutionArray = new IMarkerResolution[conflictResolutions.size()];
conflictResolutions.toArray(resolutionArray);
return resolutionArray;
}
@Override
public IMarkerResolution[] getResolutions(IMarker marker) {
CodewindApplication app = getApplication(marker);
if (app == null) {
return null;
}
String quickFixId = marker.getAttribute(CodewindEclipseApplication.QUICK_FIX_ID, (String)null);
String quickFixDescription = marker.getAttribute(CodewindEclipseApplication.QUICK_FIX_DESCRIPTION, (String)null);
IMarkerResolution resolution = new CodewindMarkerResolution(app, quickFixId, quickFixDescription);
return new IMarkerResolution[] { resolution };
}
protected void runQuickFix(IResource resource) throws Exception {
IMarker[] markers = getMarkers(resource);
assertTrue("There should be at least one marker for " + resource.getName() + ": " + markers.length, markers.length > 0);
IMarkerResolution[] resolutions = IDE.getMarkerHelpRegistry().getResolutions(markers[0]);
assertTrue("Did not get any marker resolutions.", resolutions.length > 0);
resolutions[0].run(markers[0]);
TestUtil.waitForJobs(10, 1);
}
@Override
public IMarkerResolution[] getResolutions(IMarker marker) {
if (existsDirtyEditorFor(marker)) {
showError_UnsavedChanges();
return new IMarkerResolution[0];
}
if (marker.getResource() instanceof IProject) {
// This happens with IssueCodes.NODE_MODULES_OUT_OF_SYNC
Issue issue = getIssueUtil().createIssue(marker);
Iterable<IssueResolution> result = resolutionProvider.getResolutions(issue);
return getAdaptedResolutions(Lists.newArrayList(result));
}
return super.getResolutions(marker);
}
@Test
public void testGetResolutions_versionElement() throws CoreException {
AppEngineWebMarkerResolutionGenerator resolution = new AppEngineWebMarkerResolutionGenerator();
IMarker marker = Mockito.mock(IMarker.class);
Mockito.when(marker.getType())
.thenReturn("com.google.cloud.tools.eclipse.appengine.validation.versionMarker");
IMarkerResolution[] resolutions = resolution.getResolutions(marker);
assertEquals(1, resolutions.length);
assertEquals(VersionQuickFix.class, resolutions[0].getClass());
}
@Test
public void testGetResolutions_applicationElement() throws CoreException {
AppEngineWebMarkerResolutionGenerator resolution = new AppEngineWebMarkerResolutionGenerator();
IMarker marker = Mockito.mock(IMarker.class);
Mockito.when(marker.getType())
.thenReturn("com.google.cloud.tools.eclipse.appengine.validation.applicationMarker");
IMarkerResolution[] resolutions = resolution.getResolutions(marker);
assertEquals(1, resolutions.length);
assertEquals(ApplicationQuickFix.class, resolutions[0].getClass());
}
@Test
public void testGetResolutions() {
ServletMarkerResolutionGenerator resolution = new ServletMarkerResolutionGenerator();
IMarker marker = Mockito.mock(IMarker.class);
IMarkerResolution[] resolutions = resolution.getResolutions(marker);
assertEquals(1, resolutions.length);
assertNotNull(resolutions[0]);
}
@Override
public IMarkerResolution[] getResolutions(IMarker marker) {
IMarkerResolution[] markerResolutions = new IMarkerResolution[1];
IMarkerResolution fix = new ToServlet25QuickFix();
markerResolutions[0] = fix;
return markerResolutions;
}
private void assertAndApplyAllResolutions(XtextEditor xtextEditor, String issueCode, int issueDataCount, int issueCount,
String resolutionLabel) throws CoreException {
InternalBuilderTest.setAutoBuild(true);
if (xtextEditor.isDirty()) {
xtextEditor.doSave(new NullProgressMonitor());
}
InternalBuilderTest.fullBuild();
IXtextDocument document = xtextEditor.getDocument();
validateInEditor(document);
List<Issue> issues = getIssues(document);
assertFalse("Document has no issues, but should.", issues.isEmpty());
issues.iterator().forEachRemaining((issue) -> {
assertEquals(issueCode, issue.getCode());
assertNotNull(issue.getData());
assertEquals(issueDataCount, issue.getData().length);
});
IResource resource = xtextEditor.getResource();
IMarker[] problems = resource.findMarkers(MarkerTypes.FAST_VALIDATION, true, IResource.DEPTH_INFINITE);
assertEquals("Resource should have " + issueCount + " error marker.", issueCount, problems.length);
validateInEditor(document);
MarkerResolutionGenerator instance = injector.getInstance(MarkerResolutionGenerator.class);
List<IMarkerResolution> resolutions = Lists.newArrayList(instance.getResolutions(problems[0]));
assertEquals(1, resolutions.size());
IMarkerResolution resolution = resolutions.iterator().next();
assertTrue(resolution instanceof WorkbenchMarkerResolution);
WorkbenchMarkerResolution workbenchResolution = (WorkbenchMarkerResolution) resolution;
IMarker primaryMarker = problems[0];
List<IMarker> others = Lists.newArrayList(workbenchResolution.findOtherMarkers(problems));
assertFalse(others.contains(primaryMarker));
assertEquals(problems.length - 1, others.size());
others.add(primaryMarker);
workbenchResolution.run(others.toArray(new IMarker[others.size()]), new NullProgressMonitor());
if (xtextEditor.isDirty()) {
xtextEditor.doSave(null);
}
InternalBuilderTest.cleanBuild();
problems = resource.findMarkers(MarkerTypes.FAST_VALIDATION, true, IResource.DEPTH_INFINITE);
assertEquals("Resource should have no error marker.", 0, problems.length);
}
@Override
public IMarkerResolution[] getResolutions(IMarker marker) {
final IMarkerResolution[] emptyResult = new IMarkerResolution[0];
try {
if (!marker.isSubtypeOf(MarkerTypes.ANY_VALIDATION))
return emptyResult;
} catch (CoreException e) {
return emptyResult;
}
if (!languageResourceHelper.isLanguageResource(marker.getResource())) {
return emptyResult;
}
XtextEditor editor = findEditor(marker.getResource());
if (editor != null) {
IAnnotationModel annotationModel = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
if (annotationModel != null && !isMarkerStillValid(marker, annotationModel))
return emptyResult;
}
Issue issue = getIssueUtil().createIssue(marker);
if (issue == null)
return emptyResult;
List<IssueResolution> resolutions = getResolutionProvider().getResolutions(issue);
List<IMarkerResolution> result = Lists.newArrayList();
List<IssueResolution> remaining = Lists.newArrayList();
for (IssueResolution resolution : resolutions) {
if (resolution.getModification() instanceof IBatchableModification) {
result.add(adapterFactory.create(marker, resolution));
} else if (resolution.getModification() instanceof ITextualMultiModification) {
result.add(textualMultiModificationAdapterFactory.create(marker, resolution));
} else {
remaining.add(resolution);
}
}
result.addAll(Lists.newArrayList(getAdaptedResolutions(remaining)));
return result.toArray(new IMarkerResolution[result.size()]);
}
protected IMarkerResolution[] getAdaptedResolutions(List<IssueResolution> resolutions) {
IMarkerResolution[] result = new IMarkerResolution[resolutions.size()];
for(int i=0; i<resolutions.size(); i++)
result[i] = new ResolutionAdapter(resolutions.get(i));
return result;
}
public void getMarkerResolutions(IMarker marker){
SootAttributeResolutionGenerator sarg = new SootAttributeResolutionGenerator();
if (sarg.hasResolutions(marker)){
IMarkerResolution [] res = sarg.getResolutions(marker);
for (int i = 0; i < res.length; i++){
//System.out.println("res: "+res[i].getLabel());
}
}
}
/**
* Converts the given marker resolutions to completion proposals.
*
* @param resolutions marker resolutions
* @param marker marker that holds the given resolutions
* @return completion proposals for the given marker
*/
private static ICompletionProposal[] convertAll(IMarkerResolution[] resolutions, IMarker marker) {
ICompletionProposal[] array = new ICompletionProposal[resolutions.length];
for (int i = 0; i < resolutions.length; i++) {
SpellingMarkerResolution smr = (SpellingMarkerResolution) resolutions[i];
array[i] = new SpellingCompletionProposal(smr.getSolution(), marker);
}
return array;
}
@Override
public IMarkerResolution[] getResolutions(IMarker marker) {
String type = MarkerUtil.getBugPatternString(marker);
if (type == null) {
return null;
}
BugResolutionAssociations resolutions = getBugResolutions();
if (resolutions == null) {
return new IMarkerResolution[0];
}
return resolutions.createBugResolutions(type, marker);
}
protected void doTestQuickfixResolution(String classFileName, Class<? extends IMarkerResolution> resolutionClass, String... expectedPatterns)
throws CoreException, IOException {
QuickFixTestPackager packager = new QuickFixTestPackager();
packager.addBugPatterns(expectedPatterns);
doTestQuickfixResolution(classFileName, resolutionClass, packager.asList());
}
protected void doTestQuickfixResolution(String classFileName, Class<? extends IMarkerResolution> resolutionClass,
List<QuickFixTestPackage> packages) throws CoreException, IOException {
// Run FindBugs on the input class
work(createFindBugsWorker(), getInputResource(classFileName));
// Assert the expected markers are present
IMarker[] markers = getInputFileMarkers(classFileName);
assertEquals("Too many or too few markers", packages.size(), markers.length);
sortMarkers(markers);
assertPresentBugPatterns(packages, markers);
assertPresentLabels(packages, markers);
assertPresentLineNumbers(packages, markers);
// Assert all markers have resolution
assertAllMarkersHaveResolutions(markers);
// Apply resolution to each marker
if (resolutionClass != null) {
applySpecificResolutionForAllMarkers(markers, resolutionClass);
} else {
applySingleResolutionForAllMarkers(markers);
}
// Assert output file
assertEqualFiles(getExpectedOutputFile(classFileName), getInputCompilationUnit(classFileName));
assertEquals(0, getInputFileMarkers(classFileName).length);
}
private void applySingleResolutionForAllMarkers(IMarker[] markers) {
for (int i = 0; i < markers.length; i++) {
IMarkerResolution[] resolutions = getResolutionGenerator().getResolutions(markers[i]);
assertEquals(1, resolutions.length);
resolutions[0].run(markers[i]);
}
}
private void applySpecificResolutionForAllMarkers(IMarker[] markers, Class<? extends IMarkerResolution> resolutionClass) {
for (int i = 0; i < markers.length; i++) {
IMarkerResolution[] resolutions = getResolutionGenerator().getResolutions(markers[i]);
for (int j = 0; j < resolutions.length; j++) {
if (resolutionClass.isInstance(resolutions[j])) {
resolutions[j].run(markers[i]);
return;
}
}
}
Assert.fail("No resolution of class " + resolutionClass);
}
/**
* {@inheritDoc}
* Note : this method is largely copied from the superclass, all we need to override is the call to
* getAdaptedResolutions to provider the marker.
*/
@Override
public IMarkerResolution[] getResolutions(final IMarker marker) {
final IMarkerResolution[] emptyResult = new IMarkerResolution[0];
try {
if (!marker.isSubtypeOf(MarkerTypes.ANY_VALIDATION)) {
return emptyResult;
}
} catch (CoreException e) {
return emptyResult;
}
if (!languageResourceHelper.isLanguageResource(marker.getResource())) {
return emptyResult;
}
final XtextEditor editor = getEditor(marker.getResource());
if (editor != null) {
final IAnnotationModel annotationModel = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
if (annotationModel != null && !isMarkerStillValid(marker, annotationModel)) {
return emptyResult;
}
}
final Iterable<IssueResolution> resolutions = getResolutionProvider().getResolutions(getIssueUtil().createIssue(marker));
if (editor != null && editor.isEditorInputReadOnly()) {
editor.close(false);
}
return getAdaptedResolutions(resolutions, marker);
}
/**
* If we have a Core resolution execute in current thread, otherwise run in UI thread.
*
* @param markerResolution
* the marker resolution to apply
*/
protected void applyResolution(final IMarkerResolution markerResolution) {
final IssueResolution issueResolution = ((WorkbenchResolutionAdapter) markerResolution).getResolution();
if (issueResolution instanceof IssueResolutionWrapper) {
issueResolution.apply();
} else {
PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
@Override
public void run() {
issueResolution.apply();
}
});
}
}
public IMarkerResolution[] getResolutions(IMarker marker) {
return new IMarkerResolution[] {
new SuppressProblemInLineResolution(),
new SuppressProblemResolution(),
new SuppressFileResolution(),
new ReportBugResolution(),
new CheckDescriptionResolution()
};
}
public IMarkerResolution[] getResolutions(IMarker marker) {
if (!hasResolutions(marker)) {
return NO_RESOLUTIONS;
}
ICompilationUnit cu = getCompilationUnit(marker);
if (cu != null) {
IEditorInput input = new FileEditorInput(
(IFile) cu.getPrimary().getResource());
if (input != null) {
int offset = marker.getAttribute(IMarker.CHAR_START, -1);
int length = marker.getAttribute(IMarker.CHAR_END, -1) - offset;
int problemId = marker.getAttribute(IJavaModelMarker.ID, -1);
boolean isError = (marker.getAttribute(IMarker.SEVERITY, -1) == IMarker.SEVERITY_ERROR);
String[] arguments = CorrectionEngine.getProblemArguments(marker);
IProblemLocation location = new ProblemLocation(offset, length,
problemId, arguments, isError, null);
IInvocationContext context = new AssistContext(cu, offset, length);
IJavaCompletionProposal[] proposals = new IJavaCompletionProposal[0];
try {
proposals = getCorrections(context, new IProblemLocation[] {location});
} catch (CoreException e) {
CorePluginLog.logError(e);
}
int nProposals = proposals.length;
IMarkerResolution[] resolutions = new IMarkerResolution[nProposals];
for (int i = 0; i < nProposals; i++) {
resolutions[i] = new QuickFixCompletionProposalWrapper(cu, offset,
length, proposals[i]);
}
return resolutions;
}
}
return NO_RESOLUTIONS;
}
@Override
public IMarkerResolution[] getResolutions(IMarker marker) {
if (isMissingObjectType(marker)) {
return new IMarkerResolution[] { new FixMissingObjectType() };
}
return new IMarkerResolution[0];
}