Apply parts of patch #1952556 fixing some FindBugs bugs. Thanks to Travis Schneeberger

This commit is contained in:
Oliver Burn 2008-07-02 03:16:04 +00:00
parent ecc5239c78
commit d2af4accdd
8 changed files with 90 additions and 45 deletions

View File

@ -270,9 +270,15 @@ public final class Main
final Properties properties = new Properties();
try {
FileInputStream fis = null;
fis = new FileInputStream(aFile);
properties.load(fis);
fis.close();
try {
fis = new FileInputStream(aFile);
properties.load(fis);
}
finally {
if (fis != null) {
fis.close();
}
}
}
catch (final IOException ex) {
System.out.println("Unable to load properties from file: "

View File

@ -24,10 +24,12 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Properties;
import java.security.MessageDigest;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.api.Utils;
@ -122,15 +124,32 @@ final class PropertyCacheFile
.debug("Unable to save cache file.", e);
}
finally {
if (out != null) {
try {
out.flush();
out.close();
}
catch (final IOException ex) {
Utils.getExceptionLogger()
.debug("Unable to close cache file.", ex);
}
this.flushAndCloseOutStream(out);
}
}
}
/**
* Flushes and closes output stream.
* @param aStream the output stream
*/
private void flushAndCloseOutStream(OutputStream aStream)
{
if (aStream != null) {
try {
aStream.flush();
}
catch (final IOException ex) {
Utils.getExceptionLogger()
.debug("Unable to flush output stream.", ex);
}
finally {
try {
aStream.close();
}
catch (final IOException ex) {
Utils.getExceptionLogger()
.debug("Unable to close output stream.", ex);
}
}
}
@ -170,10 +189,14 @@ final class PropertyCacheFile
// im-memory serialization of Configuration
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(aConfiguration);
oos.flush();
oos.close();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(aConfiguration);
}
finally {
this.flushAndCloseOutStream(oos);
}
// Instead of hexEncoding baos.toByteArray() directly we
// use a message digest here to keep the length of the

View File

@ -18,6 +18,7 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.api;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Collections;
@ -38,7 +39,7 @@ import java.util.ResourceBundle;
* @version 1.0
*/
public final class LocalizedMessage
implements Comparable<LocalizedMessage>
implements Comparable<LocalizedMessage>, Serializable
{
/** hash function multiplicand */
private static final int HASH_MULT = 29;

View File

@ -217,7 +217,7 @@ public final class Utils
; // silently ignore
}
}
return lines.toArray(new String[0]);
return lines.toArray(new String[lines.size()]);
}
/**

View File

@ -219,7 +219,8 @@ public final class IllegalTypeCheck extends AbstractFormatCheck
*/
public String[] getIllegalClassNames()
{
return mIllegalClassNames.toArray(new String[0]);
return mIllegalClassNames.toArray(
new String[mIllegalClassNames.size()]);
}
/**
@ -240,7 +241,8 @@ public final class IllegalTypeCheck extends AbstractFormatCheck
*/
public String[] getIgnoredMethodNames()
{
return mIgnoredMethodNames.toArray(new String[0]);
return mIgnoredMethodNames.toArray(
new String[mIgnoredMethodNames.size()]);
}
/**
@ -261,6 +263,7 @@ public final class IllegalTypeCheck extends AbstractFormatCheck
*/
public String[] getLegalAbstractClassNames()
{
return mLegalAbstractClassNames.toArray(new String[0]);
return mLegalAbstractClassNames.toArray(
new String[mLegalAbstractClassNames.size()]);
}
}

View File

@ -49,29 +49,38 @@ public class TokenTypesDoclet
{
final String fileName = getDestFileName(aRoot.options());
final FileOutputStream fos = new FileOutputStream(fileName);
final PrintStream ps = new PrintStream(fos);
final ClassDoc[] classes = aRoot.classes();
if ((classes.length != 1) || !"TokenTypes".equals(classes[0].name())) {
final String message =
"The doclet should be used for TokenTypes only";
throw new IllegalArgumentException(message);
}
final FieldDoc[] fields = classes[0].fields();
for (final FieldDoc field : fields) {
if (field.isStatic() && field.isPublic() && field.isFinal()
&& "int".equals((field.type().qualifiedTypeName())))
PrintStream ps = null;
try {
ps = new PrintStream(fos);
final ClassDoc[] classes = aRoot.classes();
if ((classes.length != 1)
|| !"TokenTypes".equals(classes[0].name()))
{
if (field.firstSentenceTags().length != 1) {
final String message = "Should be only one tag.";
throw new IllegalArgumentException(message);
final String message =
"The doclet should be used for TokenTypes only";
throw new IllegalArgumentException(message);
}
final FieldDoc[] fields = classes[0].fields();
for (final FieldDoc field : fields) {
if (field.isStatic() && field.isPublic() && field.isFinal()
&& "int".equals((field.type().qualifiedTypeName())))
{
if (field.firstSentenceTags().length != 1) {
final String message = "Should be only one tag.";
throw new IllegalArgumentException(message);
}
ps.println(field.name() + "="
+ field.firstSentenceTags()[0].text());
}
ps.println(field.name() + "="
+ field.firstSentenceTags()[0].text());
}
}
finally {
if (ps != null) {
ps.close();
}
}
ps.close();
return true;
}

View File

@ -33,8 +33,11 @@
package com.puppycrawl.tools.checkstyle.gui;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.io.Serializable;
import javax.swing.event.EventListenerList;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreePath;
/**
* @version 1.2 10/27/98
@ -42,15 +45,15 @@ import javax.swing.event.*;
* of listeners.
* @author Philip Milne
*/
public abstract class AbstractTreeTableModel implements TreeTableModel
public abstract class AbstractTreeTableModel implements TreeTableModel,
Serializable
{
private final Object mRoot;
private final EventListenerList mListenerList = new EventListenerList();
public AbstractTreeTableModel(Object root)
{
this.mRoot = root;
mRoot = root;
}
//

View File

@ -22,7 +22,7 @@
lines="28"/>
<suppress id="paramNum"
files="LocalizedMessage.java"
lines="140"/>
lines="141"/>
<!--
Turn off all checks for Generated and Test code. Fixes issues with using
Eclipse plug-in.