反射配置加载

时间:2016-08-10 18:30:23

标签: java reflection configuration config

我正在尝试加载配置,只是为了让它更容易我决定使字段与配置中的属性完全相同。这使得以后更容易添加更多属性。当尝试加载配置时,它似乎能够读取前两个属性,这两个属性都是整数,但一旦它击中第三个也是一个int,它会吐出这个错误:

IV
80
int
CP
500
int
MinEvolveUseXpEgg
0
int
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.pokemongobot.PokemonGoBot.main(PokemonGoBot.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.IllegalArgumentException: Can not set int field com.pokemongobot.Config.MinEvolveUseXpEgg to java.lang.Class
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeIntegerFieldAccessorImpl.setInt(UnsafeIntegerFieldAccessorImpl.java:128)
    at java.lang.reflect.Field.setInt(Field.java:949)
    at com.pokemongobot.Config.loadConfig(Config.java:65)
    at com.pokemongobot.Config.<clinit>(Config.java:38)
    ... 6 more

我已经放入了一些调试行,以便查看它失败的内容

private static int MinEvolveUseXpEgg = 0;

public static void loadConfig(Properties config) throws IllegalAccessException {
        Field[] allFields = Config.class.getDeclaredFields();
        for (Field field : allFields) {
            if(field.getType().isPrimitive()) {
                if(field.getType().isAssignableFrom(int.class)) {
                    System.out.println(field.getName());
                    System.out.println(config.getProperty(field.getName()));
                    System.out.println(field.getType().toString());
                    field.setInt(Config.class, Integer.parseInt(config.getProperty(field.getName())));
                } else if(field.getType().isAssignableFrom(double.class)) {
                    field.setDouble(Config.class, Double.parseDouble(config.getProperty(field.getName())));
                } else if(field.getType().isAssignableFrom(boolean.class)) {
                    field.setBoolean(Config.class, Boolean.parseBoolean(config.getProperty(field.getName())));
                }
            } else {
                if(field.getType().isAssignableFrom(String.class)) {
                    field.set(Config.class, config.getProperty(field.getName()));
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

配置:

# Percentage of perfect
IV=80
CP=500

AutoDrop=true

# Inventory

Potion=0
SuperPotion=10
HyperPotion=50
Revive=20
Pokeball=10
GreatBall=40
UltraBall=50
RazzBerry=150

Evolve=true
XPEggEvolve=false
MinEvolveUseXpEgg=0

HandleSoftBan=false

0 个答案:

没有答案