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

missing return type crashes compiler

XMLWordPrintable

    • 1.1
    • sparc
    • solaris_2.4
    • Not verified

          Granted this is a weird thing to try to do, but the compiler asked me
      to file a bug report, so I am.

          Someone asked me if you could have static final instances of a class
      and use final methods on those instances as compile time constants, for
      example in switch cases. (Pretty warped, huh? :-) So I tried it. The
      compiler politely says `Constant expression required'. But by accident
      while I was typing in the sample program I forgot to give the return type
      of the accessor method, and the compiler crashed with a null-pointer
      exception. Here's the report.

      analemma ... cat java/SwitchOnObject.java
      class Collection {
      public static final Collection one = new Collection(1);
      public static final Collection two = new Collection(2);
      public final /* MISSING TYPE */ getValue() { return theValue; }
      private Collection(int value) {
      theValue = value;
      }
      private int theValue;
      }

      public class SwitchOnObject {
      public String method(int arg) {
      switch (arg) {
      case Collection.one.getValue():
      return "one";
      case Collection.two.getValue():
      return "two";
      default:
      return "none";
      }
      }
      }
      % /home/analemma/java/JDK-beta2/java/bin/javac -d ./classes -classpath ./classes:./java:/home/analemma/java/JDK-beta2/java/lib/classes.zip ./java/SwitchOnObject.java
      ./java/SwitchOnObject.java:4: Invalid method declaration; return type required.
      public final /* MISSING TYPE */ getValue() { return theValue; }
      ^
      java.lang.NullPointerException
      at sun.tools.tree.MethodExpression.inlineValue(MethodExpression.java:292)
      at sun.tools.tree.CaseStatement.check(CaseStatement.java:45)
      at sun.tools.tree.SwitchStatement.check(SwitchStatement.java:61)
      at sun.tools.tree.CompoundStatement.check(CompoundStatement.java:48)
      at sun.tools.tree.Statement.checkMethod(Statement.java:64)
      at sun.tools.javac.SourceField.check(SourceField.java:209)
      at sun.tools.javac.SourceClass.check(SourceClass.java:431)
      at sun.tools.javac.SourceClass.compileClass(SourceClass.java:540)
      at sun.tools.javac.SourceClass.compile(SourceClass.java:527)
      at sun.tools.javac.Main.compile(Main.java:193)
      at sun.tools.javac.Main.main(Main.java:289)
      ./java/SwitchOnObject.java:14: Method getValue() not found in class Collection.
      case Collection.one.getValue():
      ^
      error: An exception has occurred in the compiler; please file a bug report (###@###.###).
      3 errors

          I'll make this a 3/3 because the compiler shouldn't crash, but it's not
      a big deal to me, and the compiler did manage to squeak out an error
      message for the real problem. I'll be there are places other than
      tree.CaseStatement that try to evaluate expressions and might fall over if
      the expression is bogus.

      ... peter 12/15/95

            fyellinsunw Frank Yellin (Inactive)
            pbk Peter Kessler
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: