Error when binding a field which is not from the current class but from an enclosing class or the current class

Remi Forax forax at univ-mlv.fr
Sat Sep 29 17:21:26 UTC 2018


This code compile but doesn't pass the verifier

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

import static java.util.Objects.requireNonNull;

public class StudentList {
  private final ArrayList<String> list = new ArrayList<>();
  
  public void add(String student) -> list.add(requireNonNull(student));
  
  public List<String> asList() -> new AbstractList<>() {
    public int size() = list::size;
    public String get(int index) = list::get;
  };
  
  public static void main(String[] args) {
    var students = new StudentList();
    students.add("John");
    students.add("Jane");
    System.out.println(students.asList());
  }
}

get() (or size()) is compiled as
public java.lang.String get(int);
    Code:
       0: aload_0
       1: getfield      #3                  // Field StudentList.list:Ljava/util/ArrayList;
       4: iload_1
       5: invokevirtual #5                  // Method java/util/ArrayList.get:(I)Ljava/lang/Object;
       8: areturn

the owner of getfield should be StudentList$1 not StudentList.

cheers,
Rémi



More information about the amber-dev mailing list