projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.93k stars 2.4k forks source link

[BUG] Index is not updated in getAsStringList #3761

Closed woosung1223 closed 1 month ago

woosung1223 commented 1 month ago

Describe the bug

The variable idx in getAsStringList method does not updated inside the for-loop, so idx always has the value 0. Since idx is used for Exception handling, it can make debuggers confused.

public List<String> getAsStringList(String methodName) {
        // ....
    List<String> out = new ArrayList<String>(v.valueGuesses.size());
        int idx = 0;
        for (Object guess : v.valueGuesses) {
            Object result = guess == null ? null : guessToType(guess, String.class, v, idx);
            if (result == null) {
                if (v.valueGuesses.size() == 1) {
                    String[] s = getDefaultIf(methodName, new String[0]);
                    return Collections.unmodifiableList(Arrays.asList(s));
                } 
                throw new AnnotationValueDecodeFail(v, 
                    "I can't make sense of this annotation value. Try using a fully qualified literal.", idx);
            }
            out.add((String) result);
        }
    return Collections.unmodifiableList(out);
}

Expected behavior

public List<String> getAsStringList(String methodName) {
        // ....
    List<String> out = new ArrayList<String>(v.valueGuesses.size());
        int idx = 0;
        for (Object guess : v.valueGuesses) {
            Object result = guess == null ? null : guessToType(guess, String.class, v, idx);
            if (result == null) {
                if (v.valueGuesses.size() == 1) {
                    String[] s = getDefaultIf(methodName, new String[0]);
                    return Collections.unmodifiableList(Arrays.asList(s));
                } 
                throw new AnnotationValueDecodeFail(v, 
                    "I can't make sense of this annotation value. Try using a fully qualified literal.", idx);
            }
            out.add((String) result);
            idx++;
        }
    return Collections.unmodifiableList(out);
}