Scala Programming Language
  1. Scala Programming Language
  2. SI-4023

getDeclaredClasses seems to miss some (REPL) or all (scalac) classes (objects) declared

    Details

      Description

      This Java code works:

      class A {
        static class A1{}
        private static class A2{}
        class A3{}
        private class A4{}
      
        public static void main(String[] args){
          A dow = new A();
          Class[] classes = dow.getClass().getDeclaredClasses();
          System.out.println("Amount: " + classes.length + " classes");
          for(Class cls : classes){
            System.out.println(cls);
          }
        } 
      }
      /* 
      > java A
      Amount: 4 classes
      class A$$A4
      class A$$A3
      class A$$A2
      class A$$A1
      

      But this Scala code doesn't:

      object B {
        class B1
        private class B2
        object B3
        private object B4
        object B5 extends B1
        private object B6 extends B2 
       
        val valuesTry1 = this.getClass.getDeclaredClasses
        val valuesTry2 = B.getClass.getDeclaredClasses
        val valuesTry3 = getClass.getDeclaredClasses
              
        def main(args: Array[String]) {
          println("Try 1: (" + valuesTry1.length + " classes)")
          valuesTry1.foreach(println)
          println("Try 2: (" + valuesTry2.length + " classes)")
          valuesTry2.foreach(println)
          println("Try 3: (" + valuesTry3.length + " classes)")
          valuesTry3.foreach(println)
        }
      }
      

      In fact the result depends on how you run the file.

      If the file is compiled it prints:

      > scalac B.scala
      > scala B
      Try 1: (0 classes)
      Try 2: (0 classes)
      Try 3: (0 classes)
      

      If the code is run on the REPL I get at least classes B1 and B2, but the rest is still missing:

      scala> F.main(Array(""))
      Try 1: (5 classes)
      class line5$$object$$$$iw$$$$iw$$F$$B2
      class line5$$object$$$$iw$$$$iw$$F$$B1
      class line5$$object$$$$iw$$$$iw$$F$$$$anonfun$$main$$3
      class line5$$object$$$$iw$$$$iw$$F$$$$anonfun$$main$$2
      class line5$$object$$$$iw$$$$iw$$F$$$$anonfun$$main$$1
      Try 2: (5 classes)
      class line5$$object$$$$iw$$$$iw$$F$$B2
      class line5$$object$$$$iw$$$$iw$$F$$B1
      class line5$$object$$$$iw$$$$iw$$F$$$$anonfun$$main$$3
      class line5$$object$$$$iw$$$$iw$$F$$$$anonfun$$main$$2
      class line5$$object$$$$iw$$$$iw$$F$$$$anonfun$$main$$1
      Try 3: (5 classes)
      class line5$$object$$$$iw$$$$iw$$F$$B2
      class line5$$object$$$$iw$$$$iw$$F$$B1
      class line5$$object$$$$iw$$$$iw$$F$$$$anonfun$$main$$3
      class line5$$object$$$$iw$$$$iw$$F$$$$anonfun$$main$$2
      class line5$$object$$$$iw$$$$iw$$F$$$$anonfun$$main$$1
      

      In my opinion it should be investigated why Java reflection doesn't work as it is supposed to be.

      Considering that the object declaration does also create a class (/class file), it should also show up in getDeclaredClasses.

        Issue Links

          Activity

          Hide
          Grzegorz Kossakowski added a comment -

          Yup:

          object A {
            object B {
              class C
            }
          }
          
          object Test {
            def main(args: Array[String]) {
              val c = Class.forName(classOf[A.B.C].getName)
              println(c)
            }
          }
          

          prints

          class A$B$C
          

          I would have suggested that before if I understood your reasoning. That's exactly why we ask for more information here and there

          Show
          Grzegorz Kossakowski added a comment - Yup: object A { object B { class C } } object Test { def main(args: Array[String]) { val c = Class.forName(classOf[A.B.C].getName) println(c) } } prints class A$B$C I would have suggested that before if I understood your reasoning. That's exactly why we ask for more information here and there
          Hide
          Grzegorz Kossakowski added a comment -

          Let's move this discussion to internals mailing list. We abused this ticket for too long. Describe what you need exactly and I (and probably others) might be able to help.

          Show
          Grzegorz Kossakowski added a comment - Let's move this discussion to internals mailing list. We abused this ticket for too long. Describe what you need exactly and I (and probably others) might be able to help.
          Show
          Eugene Burmako added a comment - http://groups.google.com/group/scala-internals/browse_thread/thread/858af21e0f1096cf
          Hide
          Simon Ochsenreither added a comment -

          Weird ... just saw the mail that somehow I assigned this ticket to me. No idea how that happened.

          Show
          Simon Ochsenreither added a comment - Weird ... just saw the mail that somehow I assigned this ticket to me. No idea how that happened.
          Hide
          Simon Ochsenreither added a comment -

          Testcase in 07824e5.

          Show
          Simon Ochsenreither added a comment - Testcase in 07824e5.

            People

            • Assignee:
              Grzegorz Kossakowski
              Reporter:
              Simon Ochsenreither
              TracCC:
              Paul Phillips, Simon Ochsenreither
            • Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development