Merge branch 'sonarqubefixes01' into 'master'

Fixes some SonarQube detected bugs

See merge request core-developers/forge!2019
This commit is contained in:
swordshine
2019-08-10 10:32:18 +00:00
17 changed files with 67 additions and 176 deletions

View File

@@ -228,7 +228,7 @@ public class ComputerUtilCard {
Card cheapest = null;
for (Card c : all) {
if (cheapest == null || cheapest.getManaCost().getCMC() <= cheapest.getManaCost().getCMC()) {
if (cheapest == null || c.getManaCost().getCMC() <= cheapest.getManaCost().getCMC()) {
cheapest = c;
}
}

View File

@@ -283,7 +283,7 @@ public final class ColorSet implements Comparable<ColorSet>, Iterable<Byte>, Ser
return "n/a";
}
final String toReturn = MagicColor.toLongString(myColor);
if (toReturn == MagicColor.Constant.COLORLESS && myColor != 0) {
if (toReturn.equals(MagicColor.Constant.COLORLESS) && myColor != 0) {
return "multi";
}
return toReturn;

View File

@@ -306,7 +306,7 @@ public class BoosterGenerator {
// 1 out of ~30 normal and mythic rares are foil,
// match that.
// If not special card, make it always foil.
if ((MyRandom.getRandom().nextInt(30) == 1) || (foilSlot != BoosterSlots.SPECIAL)) {
if ((MyRandom.getRandom().nextInt(30) == 1) || (!foilSlot.equals(BoosterSlots.SPECIAL))) {
foilCardGeneratedAndHeld.add(generateFoilCard(ps));
} else {
// Otherwise it's not foil (even though this is the

View File

@@ -106,11 +106,8 @@ public final class FileUtil {
File source = new File(sourceFilename);
if (!source.exists()) { return; } //if source doesn't exist, nothing to copy
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(new File(destFilename));
try (InputStream is = new FileInputStream(source);
OutputStream os = new FileOutputStream(new File(destFilename))){
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
@@ -120,15 +117,6 @@ public final class FileUtil {
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
is.close();
os.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public static void writeFile(String filename, String text) {
@@ -136,10 +124,8 @@ public final class FileUtil {
}
public static void writeFile(File file, String text) {
try {
PrintWriter p = new PrintWriter(file);
try (PrintWriter p = new PrintWriter(file)) {
p.print(text);
p.close();
} catch (final Exception ex) {
throw new RuntimeException("FileUtil : writeFile() error, problem writing file - " + file + " : " + ex);
}
@@ -174,12 +160,10 @@ public final class FileUtil {
* a {@link java.util.List} object.
*/
public static void writeFile(File file, Collection<?> data) {
try {
PrintWriter p = new PrintWriter(file);
try (PrintWriter p = new PrintWriter(file)) {
for (Object o : data) {
p.println(o);
}
p.close();
} catch (final Exception ex) {
throw new RuntimeException("FileUtil : writeFile() error, problem writing file - " + file + " : " + ex);
}
@@ -291,10 +275,11 @@ public final class FileUtil {
ThreadUtil.executeWithTimeout(new Callable<Void>() {
@Override
public Void call() throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
lines.add(line);
try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) {
String line;
while ((line = in.readLine()) != null) {
lines.add(line);
}
}
return null;
}

View File

@@ -370,7 +370,7 @@ public class GameFormat implements Comparable<GameFormat> {
rarities = Lists.newArrayList();
for (String s: Arrays.asList(strCars.split(", "))) {
cr = CardRarity.smartValueOf(s);
if (cr.name() != "Unknown") {
if (!cr.name().equals("Unknown")) {
rarities.add(cr);
}
}

View File

@@ -180,7 +180,7 @@ public class ChangeZoneEffect extends SpellAbilityEffect {
sb.append(num).append(" of those ").append(type).append(" card(s)");
} else {
sb.append(destination.equals("Exile") ? " exiles " : " puts ");
if (type == "Card") {
if (type.equals("Card")) {
sb.append(num);
} else {
sb.append(num).append(" ").append(type);
@@ -208,7 +208,7 @@ public class ChangeZoneEffect extends SpellAbilityEffect {
}
sb.append(" of ").append(fetchPlayer);
if (fetchPlayer != "their") {
if (!fetchPlayer.equals("their")) {
sb.append("'s");
}
sb.append(" library");

View File

@@ -1011,21 +1011,12 @@ public class ImportDialog {
destFile.createNewFile();
}
FileInputStream srcStream = null;
FileChannel src = null;
FileOutputStream destStream = null;
FileChannel dest = null;
try {
srcStream = new FileInputStream(srcFile);
src = srcStream.getChannel();
destStream = new FileOutputStream(destFile);
dest = destStream.getChannel();
try (FileInputStream srcStream = new FileInputStream(srcFile);
FileChannel src = srcStream.getChannel();
FileOutputStream destStream = new FileOutputStream(destFile);
FileChannel dest = destStream.getChannel()
) {
dest.transferFrom(src, 0, src.size());
} finally {
if (src != null) { src.close(); }
if (srcStream != null) { srcStream.close(); }
if (dest != null) { dest.close(); }
if (destStream != null) { destStream.close(); }
}
if (deleteSrcAfter) {

View File

@@ -174,8 +174,7 @@ public class CardReaderExperiments {
}
if (updated) {
try {
PrintWriter p = new PrintWriter(file);
try (PrintWriter p = new PrintWriter(file)) {
for (int i = 0; i < lines.size(); i++) {
if (i < lines.size() - 1) {
p.println(lines.get(i));
@@ -184,7 +183,6 @@ public class CardReaderExperiments {
p.print(lines.get(i));
}
}
p.close();
output.add(rules.getName());
} catch (final Exception ex) {
}

View File

@@ -49,13 +49,11 @@ public final class CardScriptInfo {
public boolean trySetText(final String text0) {
if (file == null) { return false; }
try {
final PrintWriter p = new PrintWriter(file);
try (PrintWriter p = new PrintWriter(file)) {
p.print(text0);
if (!text0.endsWith(("\n"))){
p.print("\n");
}
p.close();
text = text0;
return true;

View File

@@ -19,33 +19,21 @@ public class CardThemedLDAIO {
public static void saveRawLDA(String format, List<Archetype> lda){
File file = getRAWLDAFile(format);
ObjectOutputStream s = null;
try {
FileOutputStream f = new FileOutputStream(file);
s = new ObjectOutputStream(f);
try (FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(f)){
s.writeObject(lda);
s.close();
} catch (IOException e) {
System.out.println("Error writing matrix data: " + e);
} finally {
if(s!=null) {
try {
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public static List<Archetype> loadRawLDA(String format){
try {
FileInputStream fin = new FileInputStream(getRAWLDAFile(format));
ObjectInputStream s = new ObjectInputStream(fin);
try (FileInputStream fin = new FileInputStream(getRAWLDAFile(format));
ObjectInputStream s = new ObjectInputStream(fin)) {
List<Archetype> matrix = (List<Archetype>) s.readObject();
s.close();
return matrix;
}catch (Exception e){
} catch (Exception e){
System.out.println("Error reading LDA data: " + e);
return null;
}
@@ -54,37 +42,24 @@ public class CardThemedLDAIO {
public static void saveLDA(String format, Map<String,List<List<Pair<String, Double>>>> map){
File file = getLDAFile(format);
ObjectOutputStream s = null;
try {
FileOutputStream f = new FileOutputStream(file);
s = new ObjectOutputStream(f);
try (FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(f)){
s.writeObject(map);
s.close();
} catch (IOException e) {
System.out.println("Error writing matrix data: " + e);
} finally {
if(s!=null) {
try {
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public static Map<String,List<List<Pair<String, Double>>>> loadLDA(String format){
try {
FileInputStream fin = new FileInputStream(getLDAFile(format));
ObjectInputStream s = new ObjectInputStream(fin);
try (FileInputStream fin = new FileInputStream(getLDAFile(format));
ObjectInputStream s = new ObjectInputStream(fin)) {
Map<String,List<List<Pair<String, Double>>>> matrix = (Map<String,List<List<Pair<String, Double>>>>) s.readObject();
s.close();
return matrix;
}catch (Exception e){
} catch (Exception e){
System.out.println("Error reading LDA data: " + e);
return null;
}
}
public static File getLDAFile(final String name) {

View File

@@ -24,37 +24,23 @@ public class CardThemedMatrixIO {
public static void saveMatrix(String format, HashMap<String,List<Map.Entry<PaperCard,Integer>>> map){
File file = getMatrixFile(format);
ObjectOutputStream s = null;
try {
FileOutputStream f = new FileOutputStream(file);
s = new ObjectOutputStream(f);
try (FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(f)) {
s.writeObject(map);
s.close();
} catch (IOException e) {
System.out.println("Error writing matrix data: " + e);
} finally {
if(s!=null) {
try {
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public static HashMap<String,List<Map.Entry<PaperCard,Integer>>> loadMatrix(String format){
try {
FileInputStream fin = new FileInputStream(getMatrixFile(format));
ObjectInputStream s = new ObjectInputStream(fin);
try (FileInputStream fin = new FileInputStream(getMatrixFile(format));
ObjectInputStream s = new ObjectInputStream(fin)){
HashMap<String, List<Map.Entry<PaperCard,Integer>>> matrix = (HashMap<String, List<Map.Entry<PaperCard,Integer>>>) s.readObject();
s.close();
return matrix;
}catch (Exception e){
System.out.println("Error reading matrix data: " + e);
return null;
}
}
public static File getMatrixFile(final String name) {

View File

@@ -211,7 +211,7 @@ public class GuiDownloadZipService extends GuiDownloadService {
}
}
protected void copyInputStream(final InputStream in, final String outPath) throws IOException{
protected void copyInputStream(final InputStream in, final String outPath) throws IOException {
final byte[] buffer = new byte[1024];
int len;
final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outPath));

View File

@@ -145,12 +145,9 @@ public class BugReporter {
f = GuiBase.getInterface().getSaveFile(f);
try {
final BufferedWriter bw = new BufferedWriter(new FileWriter(f));
try (BufferedWriter bw = new BufferedWriter(new FileWriter(f))){
bw.write(text);
bw.close();
}
catch (final IOException ex) {
} catch (final IOException ex) {
SOptionPane.showMessageDialog("There was an error during saving. Sorry!\n" + ex,
"Error saving file", SOptionPane.ERROR_ICON);
}

View File

@@ -109,37 +109,23 @@ public class GauntletIO {
}
public static GauntletData loadGauntlet(final File xmlSaveFile) {
GZIPInputStream zin = null;
boolean isCorrupt = false;
try {
zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
final InputStreamReader reader = new InputStreamReader(zin);
try (GZIPInputStream zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
InputStreamReader reader = new InputStreamReader(zin)) {
final GauntletData data = (GauntletData)GauntletIO.getSerializer(true).fromXML(reader);
final String filename = xmlSaveFile.getName();
data.setName(filename.substring(0, filename.length() - SUFFIX_DATA.length()));
return data;
}
catch (final IOException e) {
} catch (final IOException e) {
e.printStackTrace();
}
catch (final ConversionException e) {
} catch (final ConversionException e) {
BugReporter.reportException(e);
}
catch (final Exception e) { //if there's a non-IO exception, delete the corrupt file
} catch (final Exception e) { //if there's a non-IO exception, delete the corrupt file
e.printStackTrace();
isCorrupt = true;
}
finally {
if (zin != null) {
try {
zin.close();
} catch (final IOException e) {
System.out.println("error closing gauntlet data reader: " + e);
}
}
}
if (isCorrupt) {
try {
xmlSaveFile.delete();

View File

@@ -120,20 +120,19 @@ public class QuestDataIO {
*/
public static QuestData loadData(final File xmlSaveFile) throws IOException {
QuestData data;
final GZIPInputStream zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
final StringBuilder xml = new StringBuilder();
final char[] buf = new char[1024];
final InputStreamReader reader = new InputStreamReader(zin);
while (reader.ready()) {
final int len = reader.read(buf);
if (len == -1) {
break;
} // when end of stream was reached
xml.append(buf, 0, len);
}
zin.close();
try (GZIPInputStream zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
InputStreamReader reader = new InputStreamReader(zin)) {
final char[] buf = new char[1024];
while (reader.ready()) {
final int len = reader.read(buf);
if (len == -1) {
break;
} // when end of stream was reached
xml.append(buf, 0, len);
}
}
String bigXML = xml.toString();
try {

View File

@@ -83,34 +83,21 @@ public class TournamentIO {
}
public static TournamentData loadTournament(final File xmlSaveFile) {
GZIPInputStream zin = null;
boolean isCorrupt = false;
try {
zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
final InputStreamReader reader = new InputStreamReader(zin);
try (GZIPInputStream zin = new GZIPInputStream(new FileInputStream(xmlSaveFile));
InputStreamReader reader = new InputStreamReader(zin)) {
final TournamentData data = (TournamentData)TournamentIO.getSerializer(true).fromXML(reader);
final String filename = xmlSaveFile.getName();
data.setName(filename.substring(0, filename.length() - SUFFIX_DATA.length()));
return data;
}
catch (final IOException e) {
} catch (final IOException e) {
e.printStackTrace();
}
catch (final Exception e) { //if there's a non-IO exception, delete the corrupt file
} catch (final Exception e) { //if there's a non-IO exception, delete the corrupt file
e.printStackTrace();
isCorrupt = true;
}
finally {
if (zin != null) {
try {
zin.close();
} catch (final IOException e) {
System.out.println("error closing tournament data reader: " + e);
}
}
}
if (isCorrupt) {
try {
xmlSaveFile.delete();

View File

@@ -86,15 +86,9 @@ public class HttpUtil {
return;
}
FileInputStream uploadFileReader = null;
try {
uploadFileReader = new FileInputStream(f);
} catch (final FileNotFoundException e) {
return;
}
final int numBytesToRead = 1024;
int availableBytesToRead;
try {
try (FileInputStream uploadFileReader = new FileInputStream(f)) {
while ((availableBytesToRead = uploadFileReader.available()) > 0) {
byte[] bufferBytesRead;
bufferBytesRead = availableBytesToRead >= numBytesToRead ? new byte[numBytesToRead]
@@ -103,10 +97,10 @@ public class HttpUtil {
httpOut.write(bufferBytesRead);
httpOut.flush();
}
uploadFileReader.close();
} catch (final IOException e) {
return;
}
try {
httpOut.write(("--" + HttpUtil.BOUNDARY + "--\r\n").getBytes());
} catch (final IOException e) {
@@ -150,22 +144,17 @@ public class HttpUtil {
}
}
public static String getURL(final String sURL) {
public static String getURL(final String sURL) {
URL url = null;
try {
url = new URL(sURL);
} catch (final MalformedURLException e) {
return null;
}
InputStream is = null;
try {
is = url.openStream();
} catch (final IOException e) {
return null;
}
int ptr = 0;
final StringBuffer buffer = new StringBuffer();
try {
try (InputStream is = url.openStream()) {
while ((ptr = is.read()) != -1) {
buffer.append((char) ptr);
}