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

Class#getResource doesn't reject resource names with "." and ".." elements for resources in exploded modules

    XMLWordPrintable

Details

    • b148
    • Verified

    Description

      Class#getResource spec has following assertion which mentions that ,

      "If this class is in a named Module then this method will attempt to find the resource in the module by means of the absolute resource name, subject to the rules for encapsulation specified in the Module getResourceAsStream method. "

      Module getResourceAsStream says:
      If the caller is in a seperate module and calling this method, then the package of the resource in named module should be
      opened to the caller.

      But what is observed is if the resource path name
      * is prefixed with "./" (for resource in same directory as class package)
      * is prefixed with "../" (for resource in parent directory of the class package)

      Then even whenthe package in named module is not opened to the caller(unnamed) ,the resource is found by Class#getResource.

      For E.g: (Attached the files)

      getresource\Test.java
       System.out.println(Layer.boot().findModule("m2").get().getClassLoader().loadClass("p2.C2").getResource(
                        "./abc.txt"));

      getresource\m2
      getresource\m2\module-info.java
      module m2{}

      getresource\m2\p2
      getresource\m2\p2\C2.java
      package p2;
      public class C2{}

      compile:
      "C:\Program Files\java\jdk-b148\jdk-9\bin\javac" --module-source-path . m2/*.java m2/p2/*.java -d classes
      "C:\Program Files\java\jdk-b148\jdk-9\bin\javac" Test.java

      classes output:
      getresource\classes\m2\p2\abc.txt --> resource in the same pacakge as class
      getresource\classes\m2\p2\C2.class
      getresource\Test.class

      execute:
      "C:\Program Files\java\jdk-b148\jdk-9\bin\java" --module-path classes --add-modules m2 Test
      output:
      file:/D:/intellij/getresource/classes/m2/p2/./abc.txt
      -------------------------------------
      Ideally it should give null here because p2 is not opened by m2 to unnamed module (Test)
      -----------------------------------------------------
      Lets say if the resource is parent package:
      getresource\classes\m2\abc.txt

      and we change the "Test.java" to "../abc.txt" (instead of ./abc.txt)

      compile:
      "C:\Program Files\java\jdk-b148\jdk-9\bin\javac" Test.java

      execute:
      "C:\Program Files\java\jdk-b148\jdk-9\bin\java" --module-path classes --add-modules m2 Test
      output:
      file:/D:/intellij/getresource/classes/m2/p2/../abc.txt

      Attachments

        Activity

          People

            alanb Alan Bateman
            bnallakaluva Bharath Nallakaluva (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: