More SonarQube detected bugs fixed:

- Use try-with-resources in AutoCloseable objects.
- End this switch case with an unconditional break, return or throw statement.
- A "NullPointerException" could be thrown.
- Use the "equals" method if value comparison was intended.
- Correct this "&" to "&&".
This commit is contained in:
klaxnek
2019-08-10 13:57:06 +02:00
parent c9744bea27
commit d04fae8e96
12 changed files with 88 additions and 59 deletions

View File

@@ -150,12 +150,15 @@ public class AiCardMemory {
*/ */
public boolean isRememberedCardByName(String cardName, MemorySet set) { public boolean isRememberedCardByName(String cardName, MemorySet set) {
Set<Card> memorySet = getMemorySet(set); Set<Card> memorySet = getMemorySet(set);
Iterator<Card> it = memorySet.iterator();
while (it.hasNext()) { if (memorySet != null) {
Card c = it.next(); Iterator<Card> it = memorySet.iterator();
if (c.getName().equals(cardName)) {
return true; while (it.hasNext()) {
Card c = it.next();
if (c.getName().equals(cardName)) {
return true;
}
} }
} }
@@ -174,12 +177,15 @@ public class AiCardMemory {
*/ */
public boolean isRememberedCardByName(String cardName, MemorySet set, Player owner) { public boolean isRememberedCardByName(String cardName, MemorySet set, Player owner) {
Set<Card> memorySet = getMemorySet(set); Set<Card> memorySet = getMemorySet(set);
Iterator<Card> it = memorySet.iterator();
while (it.hasNext()) { if (memorySet != null) {
Card c = it.next(); Iterator<Card> it = memorySet.iterator();
if (c.getName().equals(cardName) && c.getOwner().equals(owner)) {
return true; while (it.hasNext()) {
Card c = it.next();
if (c.getName().equals(cardName) && c.getOwner().equals(owner)) {
return true;
}
} }
} }
@@ -197,7 +203,12 @@ public class AiCardMemory {
if (c == null) if (c == null)
return false; return false;
getMemorySet(set).add(c); Set<Card> memorySet = getMemorySet(set);
if (memorySet != null) {
memorySet.add(c);
}
return true; return true;
} }
@@ -216,7 +227,12 @@ public class AiCardMemory {
return false; return false;
} }
getMemorySet(set).remove(c); Set<Card> memorySet = getMemorySet(set);
if (memorySet != null) {
memorySet.remove(c);
}
return true; return true;
} }
@@ -229,12 +245,15 @@ public class AiCardMemory {
*/ */
public boolean forgetAnyCardWithName(String cardName, MemorySet set) { public boolean forgetAnyCardWithName(String cardName, MemorySet set) {
Set<Card> memorySet = getMemorySet(set); Set<Card> memorySet = getMemorySet(set);
Iterator<Card> it = memorySet.iterator();
while (it.hasNext()) { if (memorySet != null) {
Card c = it.next(); Iterator<Card> it = memorySet.iterator();
if (c.getName().equals(cardName)) {
return forgetCard(c, set); while (it.hasNext()) {
Card c = it.next();
if (c.getName().equals(cardName)) {
return forgetCard(c, set);
}
} }
} }
@@ -251,12 +270,15 @@ public class AiCardMemory {
*/ */
public boolean forgetAnyCardWithName(String cardName, MemorySet set, Player owner) { public boolean forgetAnyCardWithName(String cardName, MemorySet set, Player owner) {
Set<Card> memorySet = getMemorySet(set); Set<Card> memorySet = getMemorySet(set);
Iterator<Card> it = memorySet.iterator();
while (it.hasNext()) { if (memorySet != null) {
Card c = it.next(); Iterator<Card> it = memorySet.iterator();
if (c.getName().equals(cardName) && c.getOwner().equals(owner)) {
return forgetCard(c, set); while (it.hasNext()) {
Card c = it.next();
if (c.getName().equals(cardName) && c.getOwner().equals(owner)) {
return forgetCard(c, set);
}
} }
} }
@@ -269,14 +291,16 @@ public class AiCardMemory {
* @return true, if the given memory set contains no remembered cards. * @return true, if the given memory set contains no remembered cards.
*/ */
public boolean isMemorySetEmpty(MemorySet set) { public boolean isMemorySetEmpty(MemorySet set) {
return getMemorySet(set).isEmpty(); return set == null ? true : getMemorySet(set).isEmpty();
} }
/** /**
* Clears the given memory set. * Clears the given memory set.
*/ */
public void clearMemorySet(MemorySet set) { public void clearMemorySet(MemorySet set) {
getMemorySet(set).clear(); if (set != null) {
getMemorySet(set).clear();
}
} }
/** /**

View File

@@ -658,7 +658,7 @@ public class AiController {
AiCardMemory.MemorySet memSet; AiCardMemory.MemorySet memSet;
if (phaseType == null && forNextSpell) { if (phaseType == null && forNextSpell) {
memSet = AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_NEXT_SPELL; memSet = AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_NEXT_SPELL;
} else { } else if (phaseType != null) {
switch (phaseType) { switch (phaseType) {
case MAIN2: case MAIN2:
memSet = AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_MAIN2; memSet = AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_MAIN2;

View File

@@ -1612,7 +1612,7 @@ public class ComputerUtil {
} }
if (saviourApi == ApiType.PutCounter || saviourApi == ApiType.PutCounterAll) { if (saviourApi == ApiType.PutCounter || saviourApi == ApiType.PutCounterAll) {
if (saviour.getParam("CounterType").equals("P1P1")) { if (saviour != null && saviour.getParam("CounterType").equals("P1P1")) {
toughness = AbilityUtils.calculateAmount(saviour.getHostCard(), saviour.getParam("CounterNum"), saviour); toughness = AbilityUtils.calculateAmount(saviour.getHostCard(), saviour.getParam("CounterNum"), saviour);
} else { } else {
return threatened; return threatened;

View File

@@ -926,7 +926,7 @@ public class ComputerUtilCard {
} }
else if (logic.equals("MostProminentInComputerDeckButGreen")) { else if (logic.equals("MostProminentInComputerDeckButGreen")) {
List<String> prominence = ComputerUtilCard.getColorByProminence(CardLists.filterControlledBy(game.getCardsInGame(), ai)); List<String> prominence = ComputerUtilCard.getColorByProminence(CardLists.filterControlledBy(game.getCardsInGame(), ai));
if (prominence.get(0) == MagicColor.Constant.GREEN) { if (prominence.get(0).equals(MagicColor.Constant.GREEN)) {
chosen.add(prominence.get(1)); chosen.add(prominence.get(1));
} else { } else {
chosen.add(prominence.get(0)); chosen.add(prominence.get(0));
@@ -1878,7 +1878,7 @@ public class ComputerUtilCard {
// A special case which checks that this creature will attack if it's the AI's turn // A special case which checks that this creature will attack if it's the AI's turn
if (needsToPlay.equalsIgnoreCase("WillAttack")) { if (needsToPlay.equalsIgnoreCase("WillAttack")) {
if (game.getPhaseHandler().isPlayerTurn(sa.getActivatingPlayer())) { if (sa != null && game.getPhaseHandler().isPlayerTurn(sa.getActivatingPlayer())) {
return ComputerUtilCard.doesSpecifiedCreatureAttackAI(sa.getActivatingPlayer(), card) ? return ComputerUtilCard.doesSpecifiedCreatureAttackAI(sa.getActivatingPlayer(), card) ?
AiPlayDecision.WillPlay : AiPlayDecision.BadEtbEffects; AiPlayDecision.WillPlay : AiPlayDecision.BadEtbEffects;
} else { } else {

View File

@@ -631,7 +631,9 @@ public abstract class GameState {
// Note: triggers may fire during combat declarations ("whenever X attacks, ...", etc.) // Note: triggers may fire during combat declarations ("whenever X attacks, ...", etc.)
if (newPhase == PhaseType.COMBAT_DECLARE_ATTACKERS || newPhase == PhaseType.COMBAT_DECLARE_BLOCKERS) { if (newPhase == PhaseType.COMBAT_DECLARE_ATTACKERS || newPhase == PhaseType.COMBAT_DECLARE_BLOCKERS) {
boolean toDeclareBlockers = newPhase == PhaseType.COMBAT_DECLARE_BLOCKERS; boolean toDeclareBlockers = newPhase == PhaseType.COMBAT_DECLARE_BLOCKERS;
handleCombat(game, newPlayerTurn, newPlayerTurn.getSingleOpponent(), toDeclareBlockers); if (newPlayerTurn != null) {
handleCombat(game, newPlayerTurn, newPlayerTurn.getSingleOpponent(), toDeclareBlockers);
}
} }
game.getStack().setResolving(false); game.getStack().setResolving(false);
@@ -891,7 +893,9 @@ public abstract class GameState {
} }
} }
sa.setActivatingPlayer(c.getController()); if (sa != null) {
sa.setActivatingPlayer(c.getController());
}
handleScriptedTargetingForSA(game, sa, tgtID); handleScriptedTargetingForSA(game, sa, tgtID);
if (putOnStack) { if (putOnStack) {

View File

@@ -733,6 +733,7 @@ public class PlayerControllerAi extends PlayerController {
return true; return true;
} }
} }
break;
case "BetterTgtThanRemembered": case "BetterTgtThanRemembered":
if (source.getRememberedCount() > 0) { if (source.getRememberedCount() > 0) {
Card rem = (Card) source.getFirstRemembered(); Card rem = (Card) source.getFirstRemembered();
@@ -746,6 +747,7 @@ public class PlayerControllerAi extends PlayerController {
} }
return false; return false;
} }
break;
default: default:
break; break;
} }

View File

@@ -172,7 +172,7 @@ public class SpecialCardAi {
} }
} }
return best.getName(); return best != null ? best.getName() : "";
} }
} }
@@ -1231,7 +1231,7 @@ public class SpecialCardAi {
// no options with smaller CMC, so discard the one that is harder to cast for the one that is // no options with smaller CMC, so discard the one that is harder to cast for the one that is
// easier to cast right now, but only if the best card in the library is at least CMC 3 // easier to cast right now, but only if the best card in the library is at least CMC 3
// (probably not worth it to grab low mana cost cards this way) // (probably not worth it to grab low mana cost cards this way)
if (maxCMC != null && maxCMC.getCMC() < bestInLib.getCMC() && bestInLib.getCMC() >= 3) { if (maxCMC != null && bestInLib != null && maxCMC.getCMC() < bestInLib.getCMC() && bestInLib.getCMC() >= 3) {
return maxCMC; return maxCMC;
} }
// We appear to be playing Reanimator (or we have a reanimator card in hand already), so it's // We appear to be playing Reanimator (or we have a reanimator card in hand already), so it's

View File

@@ -311,6 +311,7 @@ public final class GameActionUtil {
break; break;
case Flash: case Flash:
result.getRestrictions().setInstantSpeed(true); result.getRestrictions().setInstantSpeed(true);
break;
default: default:
break; break;
} }

View File

@@ -111,7 +111,7 @@ public class SaveOpenDialog extends JPanel {
RetFile = fc.getSelectedFile(); RetFile = fc.getSelectedFile();
/* Adds extension if it is known and not given */ /* Adds extension if it is known and not given */
if (type != null & !(RetFile.getAbsolutePath().endsWith(type.TypeExtension))) { if (type != null && !(RetFile.getAbsolutePath().endsWith(type.TypeExtension))) {
RetFile = new File(RetFile.getAbsolutePath() + "." + type.TypeExtension); RetFile = new File(RetFile.getAbsolutePath() + "." + type.TypeExtension);
} }

View File

@@ -214,13 +214,13 @@ 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]; final byte[] buffer = new byte[1024];
int len; int len;
final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outPath));
while((len = in.read(buffer)) >= 0) { try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outPath))) {
out.write(buffer, 0, len); while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
}
} }
in.close(); in.close();
out.close();
} }
} }

View File

@@ -218,28 +218,26 @@ public final class FServerManager {
// https://stackoverflow.com/a/34873630 // https://stackoverflow.com/a/34873630
// https://stackoverflow.com/a/901943 // https://stackoverflow.com/a/901943
private String getRoutableAddress(boolean preferIpv4, boolean preferIPv6) throws SocketException, UnknownHostException { private String getRoutableAddress(boolean preferIpv4, boolean preferIPv6) throws SocketException, UnknownHostException {
DatagramSocket s = new DatagramSocket(); try (DatagramSocket s = new DatagramSocket()) {
s.connect(InetAddress.getByAddress(this.externalAddress), 0); s.connect(InetAddress.getByAddress(this.externalAddress), 0);
NetworkInterface n = NetworkInterface.getByInetAddress(s.getLocalAddress()); NetworkInterface n = NetworkInterface.getByInetAddress(s.getLocalAddress());
Enumeration<InetAddress> en = n.getInetAddresses(); Enumeration<InetAddress> en = n.getInetAddresses();
while (en.hasMoreElements()) { while (en.hasMoreElements()) {
InetAddress addr = (InetAddress) en.nextElement(); InetAddress addr = (InetAddress) en.nextElement();
if (addr instanceof Inet4Address) { if (addr instanceof Inet4Address) {
if (preferIPv6) { if (preferIPv6) {
continue; continue;
}
return addr.getHostAddress();
} }
s.close(); if (addr instanceof Inet6Address) {
return addr.getHostAddress(); if (preferIpv4) {
} continue;
if (addr instanceof Inet6Address) { }
if (preferIpv4) { return addr.getHostAddress();
continue;
} }
s.close();
return addr.getHostAddress();
} }
} }
s.close();
return null; return null;
} }

View File

@@ -2040,9 +2040,9 @@ public class PlayerControllerHuman extends PlayerController implements IGameCont
final File f = GuiBase.getInterface().getSaveFile(new File(ForgeConstants.USER_GAMES_DIR, "state.txt")); final File f = GuiBase.getInterface().getSaveFile(new File(ForgeConstants.USER_GAMES_DIR, "state.txt"));
if (f != null if (f != null
&& (!f.exists() || getGui().showConfirmDialog("Overwrite existing file?", "File exists!"))) { && (!f.exists() || getGui().showConfirmDialog("Overwrite existing file?", "File exists!"))) {
final BufferedWriter bw = new BufferedWriter(new FileWriter(f)); try (BufferedWriter bw = new BufferedWriter(new FileWriter(f))) {
bw.write(state.toString()); bw.write(state.toString());
bw.close(); }
} }
} catch (final Exception e) { } catch (final Exception e) {
String err = e.getClass().getName(); String err = e.getClass().getName();