Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

public HashSet(Collection<>) behaves differently on JDK 8 and JDK 11 #2926

Closed
smillst opened this issue Nov 22, 2019 · 1 comment
Closed
Assignees
Milestone

Comments

@smillst
Copy link
Member

smillst commented Nov 22, 2019

        for (VariableElement field : new HashSet<>(fieldElts)) {
            field.toString(); // error with JDK 8, not JDK 11
        }

The above code issues an error with JDK 8, but not JDK 11. See the full test case below. I suspect it is because JDK 8 annotations are from classf iles, but JDK 11 annotations are from stub files.

import java.util.AbstractSet;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.VariableElement;

public class HashSetTest {

    void method(Set<VariableElement> fieldElts) {
        for (VariableElement field : new MySet<>(fieldElts)) {
            field.toString(); // ok with both JDK 8 and JDK 11
        }

        for (VariableElement field : new HashSet<>(fieldElts)) {
            field.toString(); // error with JDK 8, not JDK 11
        }
    }

    static class MySet<E> extends AbstractSet<E> {
        public MySet(Collection<? extends E> other) { }

        @Override
        public Iterator<E> iterator() {
            throw new RuntimeException();
        }

        @Override
        public int size() {
            return 0;
        }
    }
}
@smillst
Copy link
Member Author

smillst commented May 21, 2024

This no longer reproduces.

@smillst smillst closed this as completed May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant