Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8252781

Prototype "validated" inline classes

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Not an Issue
    • P4
    • repo-valhalla
    • repo-valhalla
    • tools

    Description

      1) Allow inline classes to be marked as "validated", using whatever keyword syntax is convenient.

      __validated inline class Foo {
          String s;

          public Foo(String arg) {
              Objects.requireNonNull(arg);
              this.s = arg;
          }

      }

      2) Find a convenient way to encode this property in the class file.

      3) For now, the class implicitly implements 'NonTearable' (eventually, the JVM should recognize the property from (2) without needing that interface).

      4) It's an error to use the 'default' expression with the class:

      Foo f = Foo.default; // compiler error

      5) The class implicitly declares an extra field 'isValid':

      /* implicit */ private final boolean isValid = true;

      6) Each instance method declared in the class implicitly checks the isValid field:

      void m() {
          /* implicit */ if (!this.isValid) throw new InvalidInstanceException();
          ...
      }

      (new Foo[1])[0].m(); // throws InvalidInstanceException

      (InvalidInstanceException is a new RuntimeException.)

      7) A command-line flag (something like '-XXcheckUninitializedReads') leaves out the checks from (6) and instead inserts checks after every field/array read of the inline type:

      class Test {
          static Foo fld;
          static void m(Foo[] arr) {
              Foo f = fld;
              // 'fld' translates to:
              // { Foo $temp = fld;
              // if (!$temp.isValid) throw new InvalidInstanceException();
              // yield $temp;
              // }

              f = arr[0];
              // 'arr[0]' translates to:
              // { Foo $temp = arr[0];
              // if (!$temp.isValid) throw new InvalidInstanceException();
              // yield $temp;
              // }
          }
      }

      Attachments

        Activity

          People

            jlaskey Jim Laskey
            dlsmith Dan Smith
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: