Skip to content

Commit

Permalink
improve: 例外ハンドリングを改善
Browse files Browse the repository at this point in the history
  • Loading branch information
Maru32768 committed Aug 21, 2022
1 parent 869d465 commit af5c7ba
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 180 deletions.
30 changes: 10 additions & 20 deletions bukkit/src/main/java/net/kunmc/lab/configlib/BaseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
import org.bukkit.scoreboard.Team;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -59,17 +56,14 @@ public static <T extends BaseConfig> T newInstanceFrom(@NotNull File configJSON,
String json = readJson(configJSON);
Class<T> clazz = constructor.getDeclaringClass();

T config = null;
try {
config = constructor.newInstance(arguments);
T config = constructor.newInstance(arguments);
config.setEntryName(filename.substring(0, filename.lastIndexOf('.')));

replaceFields(clazz, gson.fromJson(json, clazz), config);
return config;
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
throw new RuntimeException(e);
}

return config;
}

public BaseConfig(@NotNull Plugin plugin) {
Expand Down Expand Up @@ -111,7 +105,7 @@ public void run() {
}
}, 0, 500);
} catch (IOException e) {
e.printStackTrace();
throw new UncheckedIOException(e);
}

// Pluginがenabledになっていない状態でregisterすると例外が発生するため遅延,ループさせている
Expand Down Expand Up @@ -169,7 +163,7 @@ protected void saveConfig() {
getConfigFile().createNewFile();
writeJson(getConfigFile(), gson.toJson(this));
} catch (IOException e) {
e.printStackTrace();
throw new UncheckedIOException(e);
}
}

Expand All @@ -192,7 +186,6 @@ protected boolean loadConfig() {

BaseConfig config = gson.fromJson(readJson(getConfigFile()), this.getClass());
replaceFields(this.getClass(), config, this);

return true;
}

Expand All @@ -204,22 +197,19 @@ private void onPluginDisable(PluginDisableEvent e) {
}

private static String readJson(File jsonFile) {
String json = null;
try {
json = Files.readLines(jsonFile, StandardCharsets.UTF_8).stream()
return Files.readLines(jsonFile, StandardCharsets.UTF_8).stream()
.collect(Collectors.joining(System.lineSeparator()));
} catch (IOException e) {
e.printStackTrace();
throw new UncheckedIOException(e);
}

return json;
}

private static void writeJson(File jsonFile, String json) {
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(jsonFile), StandardCharsets.UTF_8)) {
writer.write(json);
} catch (IOException e) {
e.printStackTrace();
throw new UncheckedIOException(e);
}
}

Expand All @@ -239,7 +229,7 @@ private static void replaceFields(Class<?> clazz, Object src, Object dst) {
try {
replaceField(field, src, dst);
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ private void init(BaseConfig config, Command command) {
Object o;
try {
o = field.get(config);
} catch (Exception e) {
e.printStackTrace();
continue;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}

if (o instanceof Value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ private void exec(CommandContext ctx, BaseConfig config) {
Object o;
try {
o = field.get(config);
} catch (Exception e) {
e.printStackTrace();
continue;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}

if (o instanceof Value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ public ConfigModifyCommand(Set<BaseConfig> configSet) {
}

for (BaseConfig config : configSet) {
addChildren(new Command(config.entryName()) {
{
init(config, this);
}
});
addChildren(new Command(config.entryName()) {{
init(config, this);
}});
}
}

Expand All @@ -38,85 +36,76 @@ private void init(BaseConfig config, Command command) {
if (Modifier.isTransient(field.getModifiers())) {
continue;
}

SingleValue<?, ?> v;
try {
v = ((SingleValue<?, ?>) field.get(config));
} catch (Exception e) {
e.printStackTrace();
continue;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}

if (!v.writableByCommand()) {
continue;
}

command.addChildren(new Command(field.getName()) {
{
addChildren(new ModifySetCommand(field, v, config));
command.addChildren(new Command(field.getName()) {{
addChildren(new ModifySetCommand(field, v, config));

if (v instanceof NumericValue) {
addChildren(new ModifyIncCommand(field, ((NumericValue<?, ?>) v), config));
addChildren(new ModifyDecCommand(field, ((NumericValue<?, ?>) v), config));
}
if (v instanceof NumericValue) {
addChildren(new ModifyIncCommand(field, ((NumericValue<?, ?>) v), config));
addChildren(new ModifyDecCommand(field, ((NumericValue<?, ?>) v), config));
}
});
}});
}

for (Field field : ConfigUtil.getCollectionValueFields(config)) {
CollectionValue<?, ?, ?> v;
try {
v = ((CollectionValue<?, ?, ?>) field.get(config));
} catch (Exception e) {
e.printStackTrace();
continue;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}

if (!v.addableByCommand() && !v.removableByCommand() && !v.clearableByCommand()) {
continue;
}

command.addChildren(new Command(field.getName()) {
{
if (v.addableByCommand()) {
addChildren(new ModifyAddCommand(field, v, config));
}
if (v.removableByCommand()) {
addChildren(new ModifyRemoveCommand(field, v, config));
}
if (v.clearableByCommand()) {
addChildren(new ModifyClearCommand(field, v, config));
}
command.addChildren(new Command(field.getName()) {{
if (v.addableByCommand()) {
addChildren(new ModifyAddCommand(field, v, config));
}
if (v.removableByCommand()) {
addChildren(new ModifyRemoveCommand(field, v, config));
}
});
if (v.clearableByCommand()) {
addChildren(new ModifyClearCommand(field, v, config));
}
}});
}

for (Field field : ConfigUtil.getMapValueFields(config)) {
MapValue<?, ?, ?> v;
try {
v = ((MapValue<?, ?, ?>) field.get(config));
} catch (Exception e) {
e.printStackTrace();
continue;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}

if (!v.puttableByCommand() && !v.removableByCommand() && !v.clearableByCommand()) {
continue;
}

command.addChildren(new Command(field.getName()) {
{
if (v.puttableByCommand()) {
addChildren(new ModifyMapPutCommand(field, v, config));
}
if (v.removableByCommand()) {
addChildren(new ModifyMapRemoveCommand(field, v, config));
}
if (v.clearableByCommand()) {
addChildren(new ModifyMapClearCommand(field, v, config));
}
command.addChildren(new Command(field.getName()) {{
if (v.puttableByCommand()) {
addChildren(new ModifyMapPutCommand(field, v, config));
}
if (v.removableByCommand()) {
addChildren(new ModifyMapRemoveCommand(field, v, config));
}
if (v.clearableByCommand()) {
addChildren(new ModifyMapClearCommand(field, v, config));
}
});
}});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import java.util.Set;

class ConfigReloadCommand extends Command {
private final List<BaseConfig> configList = new ArrayList<>();
private final List<BaseConfig> configs = new ArrayList<>();

public ConfigReloadCommand(@NotNull BaseConfig config) {
super(SubCommandType.Reload.name);
configList.add(config);
configs.add(config);
}

public ConfigReloadCommand(@NotNull Set<BaseConfig> configSet) {
Expand All @@ -22,7 +22,7 @@ public ConfigReloadCommand(@NotNull Set<BaseConfig> configSet) {
if (configSet.isEmpty()) {
throw new IllegalArgumentException("configSet is empty");
}
this.configList.addAll(configSet);
this.configs.addAll(configSet);

for (BaseConfig config : configSet) {
addChildren(new Command(config.entryName()) {
Expand All @@ -36,7 +36,7 @@ public void execute(@NotNull CommandContext ctx) {

@Override
public void execute(CommandContext ctx) {
configList.forEach(x -> exec(ctx, x));
configs.forEach(x -> exec(ctx, x));
}

private void exec(CommandContext ctx, BaseConfig config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ public static List<Field> getMapValueFields(BaseConfig config) {
.map(f -> {
try {
return f.get(config);
} catch (IllegalAccessException exception) {
exception.printStackTrace();
return null;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
})
.map(x -> ((SingleValue<?, ?>) x))
Expand All @@ -56,8 +55,7 @@ public static List<Field> getMapValueFields(BaseConfig config) {
try {
return f.get(config);
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
throw new RuntimeException(e);
}
})
.map(x -> ((CollectionValue<?, ?, ?>) x))
Expand All @@ -70,8 +68,7 @@ public static List<Field> getMapValueFields(BaseConfig config) {
try {
return f.get(config);
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
throw new RuntimeException(e);
}
})
.map(x -> ((MapValue<?, ?, ?>) x))
Expand Down
Loading

0 comments on commit af5c7ba

Please sign in to comment.