Decrease scope of variables. #1538

This commit is contained in:
Michal Kordas 2015-08-02 16:38:00 +02:00 committed by Roman Ivanov
parent d49eaaf2e4
commit 2880edd655
12 changed files with 20 additions and 33 deletions

View File

@ -67,7 +67,6 @@ public final class Main {
boolean cliViolations = false;
// provide proper exit code based on results.
final int exitWithCliViolation = -1;
final int exitWithCheckstyleException = -2;
int exitStatus = 0;
try {
@ -109,6 +108,7 @@ public final class Main {
printUsage();
}
catch (CheckstyleException e) {
final int exitWithCheckstyleException = -2;
exitStatus = exitWithCheckstyleException;
errorCounter = 1;
System.out.println(e.getMessage());

View File

@ -100,11 +100,11 @@ final class PropertyCacheFile {
* @throws IOException when there is a problems with file read
*/
void load() throws IOException {
FileInputStream inStream = null;
// get the current config so if the file isn't found
// the first time the hash will be added to output file
final String currentConfigHash = getConfigHashCode(config);
if (new File(fileName).exists()) {
FileInputStream inStream = null;
try {
inStream = new FileInputStream(fileName);
details.load(inStream);

View File

@ -122,17 +122,16 @@ public final class FileText extends AbstractList<String> {
}
catch (final UnsupportedCharsetException ex) {
final String message = "Unsupported charset: " + charsetName;
final UnsupportedEncodingException ex2;
ex2 = new UnsupportedEncodingException(message);
final UnsupportedEncodingException ex2 = new UnsupportedEncodingException(message);
ex2.initCause(ex);
throw ex2;
}
final char[] chars = new char[READ_BUFFER_SIZE];
final StringBuilder buf = new StringBuilder();
final FileInputStream stream = new FileInputStream(file);
final Reader reader = new InputStreamReader(stream, decoder);
try {
final char[] chars = new char[READ_BUFFER_SIZE];
while (true) {
final int len = reader.read(chars);
if (len < 0) {

View File

@ -371,7 +371,6 @@ public final class LocalizedMessage
// The below is a copy of the default implementation.
final String bundleName = toBundleName(aBaseName, aLocale);
final String resourceName = toResourceName(bundleName, "properties");
ResourceBundle bundle = null;
InputStream stream = null;
if (aReload) {
final URL url = aLoader.getResource(resourceName);
@ -386,6 +385,7 @@ public final class LocalizedMessage
else {
stream = aLoader.getResourceAsStream(resourceName);
}
ResourceBundle bundle = null;
if (stream != null) {
final Reader streamReader = new InputStreamReader(stream, "UTF-8");
try {

View File

@ -244,7 +244,6 @@ public class AvoidEscapedUnicodeCharactersCheck
* @return true if trail comment is present after ast token.
*/
private boolean hasTrailComment(DetailAST ast) {
boolean result = false;
final DetailAST variableDef = getVariableDef(ast);
DetailAST semi;
@ -260,6 +259,7 @@ public class AvoidEscapedUnicodeCharactersCheck
semi = getSemi(ast);
}
boolean result = false;
if (semi != null) {
final int lineNo = semi.getLineNo();
final String currentLine = getLine(lineNo - 1);

View File

@ -79,9 +79,8 @@ public class NewlineAtEndOfFileCheck
@Override
protected void processFiltered(File file, List<String> lines) {
// Cannot use lines as the line separators have been removed!
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file, "r");
final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
boolean threw = true;
try {
if (!endsWithNewline(randomAccessFile)) {

View File

@ -71,12 +71,12 @@ public class CovariantEqualsCheck extends Check {
@Override
public void visitToken(DetailAST ast) {
equalsMethods.clear();
boolean hasEqualsObject = false;
// examine method definitions for equals methods
final DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);
if (objBlock != null) {
DetailAST child = objBlock.getFirstChild();
boolean hasEqualsObject = false;
while (child != null) {
if (child.getType() == TokenTypes.METHOD_DEF
&& CheckUtils.isEqualsMethod(child)) {

View File

@ -154,13 +154,11 @@ public class RedundantImportCheck
* @return whether from the package
*/
private static boolean fromPackage(String importName, String pkg) {
boolean retVal = false;
// imports from unnamed package are not allowed:
// http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.5
// So '.' must be present in member name and we are not checking for it
final int index = importName.lastIndexOf('.');
final String front = importName.substring(0, index);
retVal = front.equals(pkg);
return retVal;
return front.equals(pkg);
}
}

View File

@ -143,12 +143,10 @@ public class HandlerFactory {
return createMethodCallHandler(indentCheck, ast, parent);
}
AbstractExpressionHandler expHandler = null;
final Constructor<?> handlerCtor =
typeHandlers.get(ast.getType());
expHandler = (AbstractExpressionHandler) Utils.invokeConstructor(
handlerCtor, indentCheck, ast, parent);
return expHandler;
return (AbstractExpressionHandler) Utils.invokeConstructor(
handlerCtor, indentCheck, ast, parent);
}
/**

View File

@ -351,8 +351,7 @@ public class JavadocStyleCheck
final Deque<HtmlTag> htmlStack = new ArrayDeque<>();
final String[] text = comment.getText();
TagParser parser = null;
parser = new TagParser(text, lineno);
final TagParser parser = new TagParser(text, lineno);
while (parser.hasNextTag()) {
final HtmlTag tag = parser.nextTag();

View File

@ -186,17 +186,14 @@ public class RegexpCheck extends AbstractFormatCheck {
/** recursive method that finds the matches. */
private void findMatch() {
int startLine;
boolean foundMatch;
boolean ignore = false;
foundMatch = matcher.find();
final boolean foundMatch = matcher.find();
if (foundMatch) {
final FileText text = getFileContents().getText();
final LineColumn start = text.lineColumn(matcher.start());
startLine = start.getLine();
final int startLine = start.getLine();
ignore = isIgnore(startLine, text, start);
final boolean ignore = isIgnore(startLine, text, start);
if (!ignore) {
matchCount++;
@ -234,10 +231,6 @@ public class RegexpCheck extends AbstractFormatCheck {
* @return true is that need to be ignored
*/
private boolean isIgnore(int startLine, FileText text, LineColumn start) {
int startColumn;
boolean ignore = false;
int endLine;
int endColumn;
final LineColumn end;
if (matcher.end() == 0) {
end = text.lineColumn(0);
@ -245,9 +238,10 @@ public class RegexpCheck extends AbstractFormatCheck {
else {
end = text.lineColumn(matcher.end() - 1);
}
startColumn = start.getColumn();
endLine = end.getLine();
endColumn = end.getColumn();
final int startColumn = start.getColumn();
final int endLine = end.getLine();
final int endColumn = end.getColumn();
boolean ignore = false;
if (ignoreComments) {
final FileContents theFileContents = getFileContents();
ignore = theFileContents.hasIntersectionWithComment(startLine,

View File

@ -122,9 +122,9 @@ public class SuppressWithNearbyCommentFilter
else {
tagMessageRegexp = null;
}
int influence = 0;
format = expandFrocomment(
text, filter.influenceFormat, filter.commentRegexp);
int influence = 0;
try {
if (Utils.startsWithChar(format, '+')) {
format = format.substring(1);