【问题标题】:Java Array Out of Bounds Exception from Properties属性中的 Java 数组越界异常
【发布时间】:2020-08-14 06:49:11
【问题描述】:

我正在尝试使用 .properties 文件来获取字符串列表并将它们转换为数组,以便稍后在我的程序中添加 javassist。但是,我不断得到一个 arrayoutofboundsexception 0 属性中包含以下数据: 修改.spells.in.

    void configure(Properties options)
    {
        String nameSpace;
        ArrayList<String> spellList = new ArrayList<String>();
        String[] tokens;

        for(String key : options.stringPropertyNames()) {
            if(key.startsWith("modify.spells.in.")) {
                nameSpace = key.substring(17);
                tokens = options.getProperty(key).split(",");
                for(String token : tokens) {
                    if(spellList.contains(nameSpace + "." + token)) {
                        continue;
                    }

                    spellList.add(nameSpace + "." + token);
                }
            }
        }

        if(spellList.size() == 0) {
            itemSpells = new String[]{
                    "Fire1", "Fire2",  "Fire3", "Lit1", "Lit2", "Lit3", "Ice1", "Ice2",
                    "Ice3"
            };

            for(int i = 0; i < itemSpells.length; i++) {
                itemSpells[i] = "com.ragdoll.spells." + itemSpells[i];
            }
        } else {
            itemSpells = spellList.toArray(new String[0]);
        }

属性文件字符串如下: modify.spells.in.com.ragdoll.spells=Fire3,Lit3,Ice3

现有代码编辑表达式如下:

我有一个 HookManager 可以添加到 interceptSpells(itemSpells);

    private void interceptSpells(String[] classes) throws NotFoundException, CannotCompileException
    {
        CtClass c;
        ClassPool cp = HookManager.getInstance().getClassPool();
        CtMethod m = null;

        // CAST - 

        for(int i = 0; i < classes.length; i++) {
            final int j = i;
            c = cp.get(classes[i]);
            m = c.getDeclaredMethods("doEffect")[0];
            m.instrument(new ExprEditor() {
                public void edit(MethodCall m) throws CannotCompileException {
                    if (m.getMethodName().equals("addSpellEffect")) {
                        logger.info("Intercepting (1) " + classes[j]);
                        m.replace(
                              "$_ = $proceed($$);"
                            + "com.rev.spellmod.getInstance().addSpellEffectCaster($0, eff, performer); "
                        );
                    } else if(m.getMethodName().equals("improvePower")) {
                        logger.info("Intercepting (2) " + classes[j]);
                        m.replace(
                              "$_ = $proceed($$);"
                            + "com.rev.spellmod.getInstance().replaceSpellEffectCaster($0, eff, performer); "
                        );
                    }
                }
            });

我感觉好像我已经声明了命名空间并正确地拆分了列表。如果我在 .properties 文件中只添加一个咒语,它就可以工作。一旦我添加了多个,它就会指向带有 arrayoutofboundsexception 的拦截代码。如果我将其留空,它也适用于预先确定的法术,但我希望能够随时随地修改法术,而不是每次都重新编译。

任何想法或帮助将不胜感激!

【问题讨论】:

    标签: java arrays string parsing properties


    【解决方案1】:

    我意识到 doEffect 是问题所在。一些法术从另一个类继承了 doEffect ,因此产生了 ArrayOutOfBoundsException 而不是正确的字符串解析。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      相关资源
      最近更新 更多