Java Launcher does not fail when running compact java-file with private no-arg constructor

XMLWordPrintable

    • Type: CSR
    • Resolution: Unresolved
    • Priority: P4
    • 26
    • Component/s: tools
    • None
    • behavioral
    • minimal
    • Hide
      In general, it's already not common to define a custom constructor in compact source file programs. In addition its rarer to see a `private` access modifier on such a constructor.

      Such programs never worked with java in class mode - so only source-only programs are effected by this behavioral change. Which, by their nature, can be fixed by removing the `private` modifier from the source file.
      Show
      In general, it's already not common to define a custom constructor in compact source file programs. In addition its rarer to see a `private` access modifier on such a constructor. Such programs never worked with java in class mode - so only source-only programs are effected by this behavioral change. Which, by their nature, can be fixed by removing the `private` modifier from the source file.
    • Implementation

      Summary

      Correct Java source launcher to reject programs with a private no-arg constructor.

      Problem

      The behaviour of the java launcher for the following HelloWorld.java program defining a private no-arg constructor is different when run in source and class mode:

      class HelloWorld {
        private HelloWorld() {}
        void main() { IO.println("Hello world"); }
      }
      

      $ java HelloWorld.java

      Hello world
      

      javac HelloWorld.java | java HelloWorld

      Error: no non-private zero argument constructor found in class HelloWorld
      remove private from existing constructor or define as:
         public HelloWorld()
      

      Solution

      Perform same check as in class launch mode and fail with a similar error message.

      Specification

      The check in class mode, found in LauncherHelper.java, reads:

          if (Modifier.isPrivate(constructor.getModifiers())) {
              throw new Fault(Errors.CantAccessConstructor(mainClassName));
          }
      

      That check should be adopted and applied to SourceLauncher.java as:

          if (Modifier.isPrivate(constructor.getModifiers())) {
              throw new Fault(Errors.CantUsePrivateConstructor(mainClassName));
          }
      

            Assignee:
            Christian Stein
            Reporter:
            Robert Scholte
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: