|
7 | 7 |
|
8 | 8 | import static java.util.Collections.emptyList; |
9 | 9 |
|
| 10 | +import java.io.BufferedReader; |
10 | 11 | import java.io.IOException; |
11 | 12 | import java.nio.charset.Charset; |
12 | 13 | import java.nio.file.Files; |
@@ -37,15 +38,19 @@ private ConfigWrapper(Map<String, ?> config) { |
37 | 38 | this.config = config; |
38 | 39 | } |
39 | 40 |
|
40 | | - @SuppressWarnings( |
41 | | - "unchecked") // snakeyaml-engine returns Object; a valid config file is always a map |
42 | 41 | public static ConfigWrapper parse(String configFile) throws IOException { |
43 | 42 | Load load = new Load(LoadSettings.builder().build()); |
44 | | - Map<String, ?> config = |
45 | | - (Map<String, ?>) |
46 | | - load.loadFromReader( |
47 | | - Files.newBufferedReader(Paths.get(configFile), Charset.defaultCharset())); |
48 | | - return new ConfigWrapper(config); |
| 43 | + try (BufferedReader reader = |
| 44 | + Files.newBufferedReader(Paths.get(configFile), Charset.defaultCharset())) { |
| 45 | + Object loaded = load.loadFromReader(reader); |
| 46 | + if (!(loaded instanceof Map)) { |
| 47 | + throw new IllegalArgumentException( |
| 48 | + "Config file does not contain a YAML map: " + configFile); |
| 49 | + } |
| 50 | + @SuppressWarnings("unchecked") // Verified above |
| 51 | + Map<String, ?> config = (Map<String, ?>) loaded; |
| 52 | + return new ConfigWrapper(config); |
| 53 | + } |
49 | 54 | } |
50 | 55 |
|
51 | 56 | public int getNumberOfThreads() { |
|
0 commit comments