Fixes sonar bugs & vulnerabilities:

createNewFile not required with Files.write "In other words, it opens the file for writing, creating the file if it doesn't exist, --"

https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#write(java.nio.file.Path,%20byte[],%20java.nio.file.OpenOption...)
This commit is contained in:
Rsl1122 2017-08-16 10:42:00 +03:00
parent 156aef8ce4
commit 6a29bfae16
5 changed files with 10 additions and 27 deletions

View File

@ -22,6 +22,7 @@ import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -83,11 +84,15 @@ public class Locale {
} }
private void writeNewDefaultLocale() throws IOException { private void writeNewDefaultLocale() throws IOException {
final int length = messages.keySet().stream()
Optional<String> key = messages.keySet().stream()
.map(Msg::getIdentifier) .map(Msg::getIdentifier)
.sorted(new StringLengthComparator()) .sorted(new StringLengthComparator())
.findFirst() .findFirst();
.get().length() + 2; if (!key.isPresent()) {
throw new IllegalStateException("Locale has not been loaded.");
}
final int length = key.get().length() + 2;
List<String> lines = messages.entrySet().stream() List<String> lines = messages.entrySet().stream()
.sorted(new LocaleEntryComparator()) .sorted(new LocaleEntryComparator())
.map(entry -> getSpacedIdentifier(entry.getKey().getIdentifier(), length) + "|| " + entry.getValue().toString()) .map(entry -> getSpacedIdentifier(entry.getKey().getIdentifier(), length) + "|| " + entry.getValue().toString())

View File

@ -80,11 +80,4 @@ public enum Html {
} }
return returnValue; return returnValue;
} }
/**
* @param html Sets the HTML String
*/
public void setHtml(String html) {
this.html = html;
}
} }

View File

@ -124,8 +124,6 @@ public class ExportUtility {
playerFolder.mkdirs(); playerFolder.mkdirs();
File inspectHtmlFile = new File(playerFolder, "index.html"); File inspectHtmlFile = new File(playerFolder, "index.html");
inspectHtmlFile.createNewFile();
Files.write(inspectHtmlFile.toPath(), Collections.singletonList(inspectHtml)); Files.write(inspectHtmlFile.toPath(), Collections.singletonList(inspectHtml));
} catch (IOException e) { } catch (IOException e) {
Log.toLog("Export.writeInspectHtml: " + name, e); Log.toLog("Export.writeInspectHtml: " + name, e);
@ -148,7 +146,6 @@ public class ExportUtility {
.replace(HtmlUtils.getInspectUrl(""), "../player/"); .replace(HtmlUtils.getInspectUrl(""), "../player/");
File analysisHtmlFile = new File(serverFolder, "index.html"); File analysisHtmlFile = new File(serverFolder, "index.html");
Log.debug("Export", "Analysis Page File: " + analysisHtmlFile.getAbsolutePath()); Log.debug("Export", "Analysis Page File: " + analysisHtmlFile.getAbsolutePath());
analysisHtmlFile.createNewFile();
Files.write(analysisHtmlFile.toPath(), Collections.singletonList(analysisHtml)); Files.write(analysisHtmlFile.toPath(), Collections.singletonList(analysisHtml));
} }
@ -157,7 +154,6 @@ public class ExportUtility {
playersFolder.mkdirs(); playersFolder.mkdirs();
File playersHtmlFile = new File(playersFolder, "index.html"); File playersHtmlFile = new File(playersFolder, "index.html");
Log.debug("Export", "Players Page File: " + playersHtmlFile.getAbsolutePath()); Log.debug("Export", "Players Page File: " + playersHtmlFile.getAbsolutePath());
playersHtmlFile.createNewFile();
Files.write(playersHtmlFile.toPath(), Collections.singletonList(playersHtml)); Files.write(playersHtmlFile.toPath(), Collections.singletonList(playersHtml));
} }

View File

@ -1,5 +1,6 @@
package main.java.com.djrapitops.plan.utilities.metrics; package main.java.com.djrapitops.plan.utilities.metrics;
import com.djrapitops.plugin.api.TimeAmount;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -203,7 +204,7 @@ public class Metrics {
// Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;) // Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;)
Bukkit.getScheduler().runTask(plugin, () -> submitData()); Bukkit.getScheduler().runTask(plugin, () -> submitData());
} }
}, 1000 * 60 * 5, 1000 * 60 * 30); }, TimeAmount.MINUTE.ms() * 5, TimeAmount.MINUTE.ms() * 30);
// Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start // Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start
// WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted! // WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted!
// WARNING: Just don't do it! // WARNING: Just don't do it!

View File

@ -44,18 +44,6 @@ public class HtmlTest {
assertEquals(expResult, result); assertEquals(expResult, result);
} }
/**
*
*/
@Test
public void testSetHtml() {
Html instance = Html.SPAN;
String expResult = "Test</span>";
instance.setHtml(expResult);
String result = instance.parse();
assertEquals(expResult, result);
}
/** /**
* *
*/ */