1 /*******************************************************************************
2 * Copyright (c) 2007, 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.fix;
13 import java.util.ArrayList;
16 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jdt.core.compiler.IProblem;
19 import org.eclipse.jdt.core.dom.CompilationUnit;
21 import org.eclipse.jdt.ui.cleanup.CleanUpContext;
22 import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
23 import org.eclipse.jdt.ui.text.java.IProblemLocation;
25 import org.eclipse.jdt.internal.ui.text.correction.ProblemLocation;
28 public abstract class AbstractMultiFix extends AbstractCleanUp implements IMultiFix {
30 protected AbstractMultiFix() {
33 protected AbstractMultiFix(Map<String, String> settings) {
38 public final ICleanUpFix createFix(CleanUpContext context) throws CoreException {
39 CompilationUnit unit= context.getAST();
43 if (context instanceof MultiFixContext) {
44 return createFix(unit, ((MultiFixContext)context).getProblemLocations());
46 return createFix(unit);
50 protected abstract ICleanUpFix createFix(CompilationUnit unit) throws CoreException;
52 protected abstract ICleanUpFix createFix(CompilationUnit unit, IProblemLocation[] problems) throws CoreException;
57 public int computeNumberOfFixes(CompilationUnit compilationUnit) {
62 * Utility method to: count number of problems in <code>problems</code> with <code>problemId</code>
63 * @param problems the set of problems
64 * @param problemId the problem id to look for
65 * @return number of problems with problem id
67 protected static int getNumberOfProblems(IProblem[] problems, int problemId) {
69 for (int i= 0; i < problems.length; i++) {
70 if (problems[i].getID() == problemId)
77 * Convert set of IProblems to IProblemLocations
78 * @param problems the problems to convert
79 * @return the converted set
81 protected static IProblemLocation[] convertProblems(IProblem[] problems) {
82 IProblemLocation[] result= new IProblemLocation[problems.length];
84 for (int i= 0; i < problems.length; i++) {
85 result[i]= new ProblemLocation(problems[i]);
92 * Returns unique problem locations. All locations in result
93 * have an id element <code>problemIds</code>.
95 * @param problems the problems to filter
96 * @param problemIds the ids of the resulting problem locations
97 * @return problem locations
99 protected static IProblemLocation[] filter(IProblemLocation[] problems, int[] problemIds) {
100 ArrayList<IProblemLocation> result= new ArrayList<IProblemLocation>();
102 for (int i= 0; i < problems.length; i++) {
103 IProblemLocation problem= problems[i];
104 if (contains(problemIds, problem.getProblemId()) && !contains(result, problem)) {
109 return result.toArray(new IProblemLocation[result.size()]);
112 private static boolean contains(ArrayList<IProblemLocation> problems, IProblemLocation problem) {
113 for (int i= 0; i < problems.size(); i++) {
114 IProblemLocation existing= problems.get(i);
115 if (existing.getProblemId() == problem.getProblemId() && existing.getOffset() == problem.getOffset() && existing.getLength() == problem.getLength()) {
123 private static boolean contains(int[] ids, int id) {
124 for (int i= 0; i < ids.length; i++) {