From da28cc19fcfc124dfdfac6ed590b8fa0e7fe2d83 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Fri, 10 Feb 2012 17:34:15 -0800 Subject: [PATCH] Java 1.5-style loops, etc. --- .../ui/quickfix/CaseBreakQuickFixBreak.java | 13 ++-- .../internal/checkers/CaseBreakChecker.java | 23 +++--- .../core/model/AbstractProblemReporter.java | 6 +- .../model/CodanMarkerProblemReporter.java | 39 ++++------ .../core/model/CodanProblemMarker.java | 72 ++++--------------- .../parser/scanner/LocationCtxContainer.java | 3 +- 6 files changed, 50 insertions(+), 106 deletions(-) diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixBreak.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixBreak.java index 7de791f5e8c..d2699c4f724 100644 --- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixBreak.java +++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/CaseBreakQuickFixBreak.java @@ -6,7 +6,7 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Gil Barash - Initial implementation + * Gil Barash - Initial implementation *******************************************************************************/ package org.eclipse.cdt.codan.internal.checkers.ui.quickfix; @@ -33,7 +33,7 @@ import org.eclipse.ltk.core.refactoring.Change; public class CaseBreakQuickFixBreak extends AbstractAstRewriteQuickFix { @Override public boolean isApplicable(IMarker marker) { - int line = marker.getAttribute(IMarker.LINE_NUMBER, -1) - 1; + int line = marker.getAttribute(IMarker.LINE_NUMBER, 0) - 1; if (line < 0) return false; return true; @@ -45,17 +45,18 @@ public class CaseBreakQuickFixBreak extends AbstractAstRewriteQuickFix { } protected IASTStatement getStmtBeforeBreak(IMarker marker, IASTTranslationUnit ast) throws BadLocationException { - int line = marker.getAttribute(IMarker.LINE_NUMBER, -1) - 1; + int line = marker.getAttribute(IMarker.LINE_NUMBER, 0) - 1; if (line < 0) return null; IRegion lineInformation = getDocument().getLineInformation(line); IASTNodeSelector nodeSelector = ast.getNodeSelector(null); IASTNode containedNode = nodeSelector.findFirstContainedNode(lineInformation.getOffset(), lineInformation.getLength()); IASTNode beforeBreakNode = null; - if (containedNode != null) + if (containedNode != null) { beforeBreakNode = CxxAstUtils.getEnclosingStatement(containedNode); - else + } else { beforeBreakNode = nodeSelector.findEnclosingNode(lineInformation.getOffset(), lineInformation.getLength()); + } if (beforeBreakNode instanceof IASTCompoundStatement) { while (beforeBreakNode != null) { if (beforeBreakNode.getParent() instanceof IASTCompoundStatement @@ -79,7 +80,7 @@ public class CaseBreakQuickFixBreak extends AbstractAstRewriteQuickFix { try { IASTTranslationUnit ast = getTranslationUnitViaEditor(marker).getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS); IASTStatement beforeBreak = getStmtBeforeBreak(marker, ast); - if (beforeBreak.getParent() instanceof IASTCompoundStatement) { + if (beforeBreak != null && beforeBreak.getParent() instanceof IASTCompoundStatement) { IASTCompoundStatement enclosingStatement = (IASTCompoundStatement) beforeBreak.getParent(); IASTStatement after = getAfterStatement(beforeBreak); ASTRewrite r = ASTRewrite.create(enclosingStatement.getTranslationUnit()); diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CaseBreakChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CaseBreakChecker.java index dc39e19c277..b6b001ae71a 100644 --- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CaseBreakChecker.java +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/CaseBreakChecker.java @@ -43,12 +43,9 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke public static final String PARAM_EMPTY_CASE = "empty_case_param"; //$NON-NLS-1$ public static final String PARAM_NO_BREAK_COMMENT = "no_break_comment"; //$NON-NLS-1$ public static final String DEFAULT_NO_BREAK_COMMENT = "no break"; //$NON-NLS-1$ - private Boolean _checkLastCase; // Should we check the last case in the switch? - private Boolean _checkEmptyCase; // Should we check an empty case (a case without any statements within it) - private String _noBreakComment; // The comment suppressing this warning - - public CaseBreakChecker() { - } + private boolean fCheckLastCase; // Should we check the last case in the switch? + private boolean fCheckEmptyCase; // Should we check an empty case (a case without any statements within it) + private String fNoBreakComment; // The comment suppressing this warning /** * This visitor looks for "switch" statements and invokes "SwitchVisitor" on them. @@ -99,10 +96,10 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke // Next is case or end of switch - means this one is the last if (prevCase != null && (isCaseStatement(next) || next == null)) { // Check that current statement end with break or any other exit statement - if (!_checkEmptyCase && isCaseStatement(curr) && next != null) { + if (!fCheckEmptyCase && isCaseStatement(curr) && next != null) { continue; // Empty case and we don't care } - if (!_checkLastCase && next == null) { + if (!fCheckLastCase && next == null) { continue; // Last case and we don't care } if (!isProducedByMacroExpansion(prevCase) && isFallThroughStamement(curr)) { @@ -116,7 +113,7 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke } if (comment != null) { String str = getTrimmedComment(comment); - if (str.toLowerCase().contains(_noBreakComment.toLowerCase())) + if (str.toLowerCase().contains(fNoBreakComment.toLowerCase())) continue; } reportProblem(curr, prevCase); @@ -162,7 +159,7 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke } } - public void reportProblem(IASTStatement curr, IASTStatement prevCase) { + private void reportProblem(IASTStatement curr, IASTStatement prevCase) { reportProblem(ER_ID, getProblemLocationAtEndOfNode(curr)); } @@ -227,9 +224,9 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke @Override public void processAst(IASTTranslationUnit ast) { - _checkLastCase = (Boolean) getPreference(getProblemById(ER_ID, getFile()), PARAM_LAST_CASE); - _checkEmptyCase = (Boolean) getPreference(getProblemById(ER_ID, getFile()), PARAM_EMPTY_CASE); - _noBreakComment = (String) getPreference(getProblemById(ER_ID, getFile()), PARAM_NO_BREAK_COMMENT); + fCheckLastCase = (Boolean) getPreference(getProblemById(ER_ID, getFile()), PARAM_LAST_CASE); + fCheckEmptyCase = (Boolean) getPreference(getProblemById(ER_ID, getFile()), PARAM_EMPTY_CASE); + fNoBreakComment = (String) getPreference(getProblemById(ER_ID, getFile()), PARAM_NO_BREAK_COMMENT); SwitchFindingVisitor visitor = new SwitchFindingVisitor(); ast.accept(visitor); } diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractProblemReporter.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractProblemReporter.java index 0b842fa58fd..967b17fe1ce 100644 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractProblemReporter.java +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/core/model/AbstractProblemReporter.java @@ -6,7 +6,7 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * QNX Software Systems (Alena Laskavaia) - initial API and implementation + * QNX Software Systems (Alena Laskavaia) - initial API and implementation *******************************************************************************/ package org.eclipse.cdt.codan.core.model; @@ -16,7 +16,7 @@ import org.eclipse.core.resources.IResource; /** * Abstract implementation of a IProblemReporter - * + * * @since 2.0 */ public abstract class AbstractProblemReporter implements IProblemReporter { @@ -30,7 +30,7 @@ public abstract class AbstractProblemReporter implements IProblemReporter { IProblem problem = CheckersRegistry.getInstance().getResourceProfile(file).findProblem(id); if (problem == null) throw new IllegalArgumentException("Id is not registered:" + id); //$NON-NLS-1$ - if (problem.isEnabled() == false) + if (!problem.isEnabled()) return; // skip ICodanProblemMarker codanProblemMarker = new CodanProblemMarker(problem, loc, args); reportProblem(codanProblemMarker); diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/model/CodanMarkerProblemReporter.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/model/CodanMarkerProblemReporter.java index deda9238a8f..4a7cc5f81c1 100644 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/model/CodanMarkerProblemReporter.java +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/model/CodanMarkerProblemReporter.java @@ -6,14 +6,13 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Alena Laskavaia - initial API and implementation + * Alena Laskavaia - initial API and implementation *******************************************************************************/ package org.eclipse.cdt.codan.internal.core.model; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Iterator; import org.eclipse.cdt.codan.core.CodanCorePlugin; import org.eclipse.cdt.codan.core.CodanRuntime; @@ -36,16 +35,15 @@ import org.eclipse.core.runtime.IProgressMonitor; /** * Problem reported that created eclipse markers */ -public class CodanMarkerProblemReporter extends AbstractProblemReporter implements - IProblemReporterPersistent, IProblemReporterSessionPersistent { +public class CodanMarkerProblemReporter extends AbstractProblemReporter + implements IProblemReporterPersistent, IProblemReporterSessionPersistent { private IResource resource; private IChecker checker; private ArrayList toAdd = new ArrayList(); /** * Create instance, which can be use as factory for - * IProblemReporterSessionPersistent or - * as IProblemReporterPersistent. + * IProblemReporterSessionPersistent or as IProblemReporterPersistent. */ public CodanMarkerProblemReporter() { super(); @@ -117,9 +115,8 @@ public class CodanMarkerProblemReporter extends AbstractProblemReporter implemen @Override public void run(IProgressMonitor monitor) throws CoreException { Collection markers = findResourceMarkers(file, checker); - for (Iterator iterator = markers.iterator(); iterator.hasNext();) { - IMarker iMarker = iterator.next(); - iMarker.delete(); + for (IMarker marker : markers) { + marker.delete(); } } }, null, IWorkspace.AVOID_UPDATE, null); @@ -146,9 +143,8 @@ public class CodanMarkerProblemReporter extends AbstractProblemReporter implemen for (int i = 0; i < markers.length; i++) { IMarker m = markers[i]; String id = m.getAttribute(ICodanProblemMarker.ID, ""); //$NON-NLS-1$ - for (Iterator iterator = problems.iterator(); iterator.hasNext();) { - IProblem iProblem = iterator.next(); - if (iProblem.getId().equals(id)) { + for (IProblem problem : problems) { + if (problem.getId().equals(id)) { res.add(m); } } @@ -176,10 +172,11 @@ public class CodanMarkerProblemReporter extends AbstractProblemReporter implemen @Override public void done() { if (checker != null) { - if (toAdd.size() == 0) + if (toAdd.isEmpty()) { deleteProblems(false); - else + } else { reconcileMarkers(); + } toAdd.clear(); } } @@ -190,8 +187,7 @@ public class CodanMarkerProblemReporter extends AbstractProblemReporter implemen @Override public void run(IProgressMonitor monitor) throws CoreException { Collection markers = findResourceMarkers(resource, checker); - for (Iterator iterator = markers.iterator(); iterator.hasNext();) { - IMarker m = iterator.next(); + for (IMarker m : markers) { ICodanProblemMarker cm = similarMarker(m); if (cm == null) { m.delete(); @@ -200,8 +196,7 @@ public class CodanMarkerProblemReporter extends AbstractProblemReporter implemen toAdd.remove(cm); } } - for (Iterator iterator = toAdd.iterator(); iterator.hasNext();) { - ICodanProblemMarker cm = iterator.next(); + for (ICodanProblemMarker cm : toAdd) { cm.createMarker(); } } @@ -244,8 +239,7 @@ public class CodanMarkerProblemReporter extends AbstractProblemReporter implemen protected ICodanProblemMarker similarMarker(IMarker m) { ICodanProblemMarker mcm = CodanProblemMarker.createCodanProblemMarkerFromResourceMarker(m); ArrayList cand = new ArrayList(); - for (Iterator iterator = toAdd.iterator(); iterator.hasNext();) { - ICodanProblemMarker cm = iterator.next(); + for (ICodanProblemMarker cm : toAdd) { if (mcm.equals(cm)) return cm; if (markersAreSimilar(mcm, cm)) { @@ -276,11 +270,6 @@ public class CodanMarkerProblemReporter extends AbstractProblemReporter implemen return true; } - /* - * (non-Javadoc) - * - * @see IProblemReporterSessionPersistent#deleteProblems(boolean) - */ @Override public void deleteProblems(boolean all) { if (all) diff --git a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/model/CodanProblemMarker.java b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/model/CodanProblemMarker.java index 58d0c525ffa..8bf70a95b95 100644 --- a/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/model/CodanProblemMarker.java +++ b/codan/org.eclipse.cdt.codan.core/src/org/eclipse/cdt/codan/internal/core/model/CodanProblemMarker.java @@ -6,7 +6,7 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * QNX Software Systems (Alena Laskavaia) - initial API and implementation + * QNX Software Systems (Alena Laskavaia) - initial API and implementation *******************************************************************************/ package org.eclipse.cdt.codan.internal.core.model; @@ -31,7 +31,7 @@ import org.eclipse.core.runtime.CoreException; /** * Instance of a problem. Intermediate representation before problem become a * marker - * + * * @since 1.1 */ public class CodanProblemMarker implements ICodanProblemMarker { @@ -40,11 +40,6 @@ public class CodanProblemMarker implements ICodanProblemMarker { private IProblem problem; private Object args[]; - @Override - public Object[] getArgs() { - return args; - } - /** * @param problem * @param loc @@ -56,41 +51,26 @@ public class CodanProblemMarker implements ICodanProblemMarker { this.args = args; } - /* - * (non-Javadoc) - * - * @see org.eclipse.cdt.codan.core.model.ICodanProblemMarker#getLocation() - */ + @Override + public Object[] getArgs() { + return args; + } + @Override public IProblemLocation getLocation() { return loc; } - /* - * (non-Javadoc) - * - * @see org.eclipse.cdt.codan.core.model.ICodanProblemMarker#getProblem() - */ @Override public IProblem getProblem() { return problem; } - /* - * (non-Javadoc) - * - * @see org.eclipse.cdt.codan.core.model.ICodanProblemMarker#getResource() - */ @Override public IResource getResource() { return loc.getFile(); } - /* - * (non-Javadoc) - * - * @see org.eclipse.cdt.codan.core.model.ICodanProblemMarker#createMarker() - */ @Override public IMarker createMarker() throws CoreException { IResource file = loc.getFile(); @@ -112,11 +92,6 @@ public class CodanProblemMarker implements ICodanProblemMarker { return marker; } - /* - * (non-Javadoc) - * - * @see org.eclipse.cdt.codan.core.model.ICodanProblemMarker#createMessage() - */ @Override public String createMessage() { String messagePattern = problem.getMessagePattern(); @@ -130,10 +105,6 @@ public class CodanProblemMarker implements ICodanProblemMarker { return message; } - /** - * @param args2 - * @return - */ private static String serializeArgs(Object[] args) { if (args != null) { Properties prop = new Properties(); @@ -155,9 +126,9 @@ public class CodanProblemMarker implements ICodanProblemMarker { } /** - * Return the argument of a problem that checker passed to "reportProblem" + * Returns the argument of a problem that checker passed to "reportProblem" * method - * + * * @param marker - problem marker * @param index - index of the argument 0 based * @return problem argument at index, can be null if not set. Can throw AUBE @@ -169,9 +140,9 @@ public class CodanProblemMarker implements ICodanProblemMarker { } /** - * Return the arguments of a problem that checker passed to "reportProblem" + * Returns the arguments of a problem that checker passed to "reportProblem" * method - * + * * @param marker - problem marker * @return problem arguments, can not be null. Can be 0 sized array. */ @@ -194,8 +165,8 @@ public class CodanProblemMarker implements ICodanProblemMarker { } /** - * Return problemId from marker - * + * Returns problemId from marker + * * @param marker * @return codan problemId */ @@ -225,8 +196,8 @@ public class CodanProblemMarker implements ICodanProblemMarker { } /** - * Attempt to restore CodamProblemMaker from the resource marker - * + * Attempts to restore CodamProblemMaker from the resource marker + * * @param marker * @return new instanceof of ICodanProblemMarker or null if marker is not * codan marker @@ -239,10 +210,6 @@ public class CodanProblemMarker implements ICodanProblemMarker { return new CodanProblemMarker(problem, loc, getProblemArguments(marker)); } - /** - * @param marker - * @return - */ public static CodanProblem getProblem(IMarker marker) { String id = getProblemId(marker); if (id == null) @@ -255,10 +222,6 @@ public class CodanProblemMarker implements ICodanProblemMarker { return problem; } - /** - * @param resource - * @return - */ public static IProblemProfile getProfile(IResource resource) { IProblemProfile profile = CheckersRegistry.getInstance().getResourceProfile(resource); return profile; @@ -276,11 +239,6 @@ public class CodanProblemMarker implements ICodanProblemMarker { return loc; } - /** - * @param marker - * @param res - * @throws CoreException - */ public static void setProblemArguments(IMarker marker, String[] args) throws CoreException { String propArgs = serializeArgs(args); marker.setAttribute(PROBLEM_ARGS, propArgs); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtxContainer.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtxContainer.java index bd4e98ad844..ab7420409ee 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtxContainer.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtxContainer.java @@ -241,7 +241,7 @@ class LocationCtxContainer extends LocationCtx { if (idx < 0) { return -idx; } - return idx+1; + return idx + 1; } private int[] computeLineOffsets() { @@ -255,7 +255,6 @@ class LocationCtxContainer extends LocationCtx { int[] result= new int[offsets.size()]; for (int i = 0; i < result.length; i++) { result[i]= offsets.get(i).intValue(); - } return result; }