-
Notifications
You must be signed in to change notification settings - Fork 82
/
AbstractMultiFix.java
130 lines (106 loc) · 3.83 KB
/
AbstractMultiFix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*******************************************************************************
* Copyright (c) 2007, 2011 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.ui.fix;
import java.util.ArrayList;
import java.util.Map;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.ui.cleanup.CleanUpContext;
import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
import org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore;
import org.eclipse.jdt.internal.ui.text.correction.ProblemLocation;
public abstract class AbstractMultiFix extends AbstractCleanUp implements IMultiFix {
protected AbstractMultiFix() {
}
protected AbstractMultiFix(Map<String, String> settings) {
super(settings);
}
@Override
public final ICleanUpFix createFix(CleanUpContext context) throws CoreException {
CompilationUnit unit= context.getAST();
if (unit == null)
return null;
if (context instanceof MultiFixContext) {
return createFix(unit, ((MultiFixContext)context).getProblemLocations());
} else {
return createFix(unit);
}
}
protected abstract ICleanUpFix createFix(CompilationUnit unit) throws CoreException;
protected abstract ICleanUpFix createFix(CompilationUnit unit, IProblemLocationCore[] problems) throws CoreException;
@Override
public int computeNumberOfFixes(CompilationUnit compilationUnit) {
return -1;
}
/**
* Utility method to: count number of problems in <code>problems</code> with <code>problemId</code>
* @param problems the set of problems
* @param problemId the problem id to look for
* @return number of problems with problem id
*/
protected static int getNumberOfProblems(IProblem[] problems, int problemId) {
int result= 0;
for (IProblem problem : problems) {
if (problem.getID() == problemId) {
result++;
}
}
return result;
}
/**
* Convert set of IProblems to IProblemLocationCore
* @param problems the problems to convert
* @return the converted set
*/
protected static IProblemLocationCore[] convertProblems(IProblem[] problems) {
IProblemLocationCore[] result= new IProblemLocationCore[problems.length];
for (int i= 0; i < problems.length; i++) {
result[i]= new ProblemLocation(problems[i]);
}
return result;
}
/**
* Returns unique problem locations. All locations in result
* have an id element <code>problemIds</code>.
*
* @param problems the problems to filter
* @param problemIds the ids of the resulting problem locations
* @return problem locations
*/
protected static IProblemLocationCore[] filter(IProblemLocationCore[] problems, int[] problemIds) {
ArrayList<IProblemLocationCore> result= new ArrayList<>();
for (IProblemLocationCore problem : problems) {
if (contains(problemIds, problem.getProblemId()) && !contains(result, problem)) {
result.add(problem);
}
}
return result.toArray(new IProblemLocationCore[result.size()]);
}
private static boolean contains(ArrayList<IProblemLocationCore> problems, IProblemLocationCore problem) {
for (IProblemLocationCore existing : problems) {
if (existing.getProblemId() == problem.getProblemId() && existing.getOffset() == problem.getOffset() && existing.getLength() == problem.getLength()) {
return true;
}
}
return false;
}
private static boolean contains(int[] ids, int id) {
for (int id2 : ids) {
if (id2 == id)
return true;
}
return false;
}
}