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

Add method to java.lang.Class to find main method

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Withdrawn
    • Icon: P4 P4
    • 25
    • core-libs
    • None
    • minimal
    • Addition of a new method in a final class has minimal compatibility risk.
    • Java API
    • SE

      Summary

      Introduce a new method that returns the main method to invoke per JLS of the given presumably initial class or interface.

      Problem

      With the introduction of JEP 512, Compact Source Files and Instance Main Methods, the determination of an initial class or interface's main method to be invoked is no longer a simple algorithm. There are now many candidate methods.

      Solution

      Introduce a new method, getMainClass, in java.lang.Class that returns the main method to invoke. This is helpful for when a class has many candidate methods.

      This new method does not involve determining if a class or interface is the initial class or interface, initialization of that class or interface, or how the main method should be invoked, such as pushing arguments or initializing an object instance.

      Specification

          /**
           * Return the {@code main} method to be invoked per JLS {@jls 12.1.4} when
           * this {@code Class} object represents the initial class or interface, or
           * {@code null} if no candidate method exists.
           * <p>
           * A candidate method:
           * <ul>
           * <li>has a single formal parameter of type {@code String[]}, or has no
           * formal parameter</li>
           * <li>has a return type of {@code void}</li>
           * <li>is public, protected or package private</li>
           * </ul>
           * Multiple candidate methods may be present in a class or interface.  In
           * that case, this method returns the one selected per JLS.
           *
           * @apiNote
           * It is the caller's responsibility to determine whether this {@code Class}
           * object represents the initial class or interface, initialize this class
           * or interface before invocation, and find out how to invoke the returned
           * {@code main} method.
           *
           * @return the main method to be invoked, may be {@code null}
           *
           * @jls 8.2 Class Members
           * @jls 8.4 Method Declarations
           * @jls 8.4.2 Method Signature
           * @jls 12.1.4 Invoke a {@code main} method
           * @since 25
           */

            liach Chen Liang
            darcy Joe Darcy
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: