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

Package protected class not protected at run time

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P5
    • Resolution: Fixed
    • Affects Version/s: 1.1.6
    • Fix Version/s: 1.2.0
    • Component/s: hotspot
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      1.2fcs
    • CPU:
      sparc
    • OS:
      solaris_2.6
    • Verification:
      Not verified

      Description

      Package protected class can be accessed from another package.

      Here are the sample code:
      ::::::::::::::
      Bar.java
      ::::::::::::::

      package bar;

      import foo.Secrete;

      public class Bar {
        public static void main(String args[]) {
          try {
            Secrete s = new Secrete(" Hacker breakin");
            System.out.println("The secrete message is \"" + s.getMsg() + "\"");
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
      ::::::::::::::
      Foo.java
      ::::::::::::::

      package foo;

      public class Foo {
        public static void main(String args[]) {
          Secrete s = new Secrete("Package only");
          System.out.println("The secrete message is \"" + s.getMsg() + "\"");
        }
      }

      class Secrete {
        private String message;

        Secrete(String msg) {
          message = msg;
        }

        public String getMsg() {
          return "Package secrete" + message;
        }

        private void setMsg(String msg) {
          message = msg;
        }
      }

      ::::::::::::::
      Secrete.java
      ::::::::::::::

      package foo;

      public class Secrete {
        private String message;

        public Secrete(String msg) {
          message = msg;
        }

        public String getMsg() {
          return "Fake class message" + message;
        }

        public void setMsg(String msg) {
          message = msg;
        }
      }


      Foo.java contained a package protected class Secrete in package foo.
      Bar.java imports foo.Secrete class
      Secrete.java is a public version of the Secrete class also packaged in foo

      Compile Foo.java first:
      % javac -d . Foo.java
      % javac -d . Bar.java
      Bar.java:4: Can't access class foo.Secrete. Only public classes and interfaces in other packages can be accessed.
      import foo.Secrete;
             ^
      1 error

      Now compile the fake Secrete class and make it work:
      % javac -d . Secrete.java
      % javac -d . Bar.java
      % java bar.Bar
      The secrete message is "Fake class message Hacker breakin"

      Now compile back the protected Secrete class and break into the Secrete class
      with the precompiled Bar.class:
      % javac -d . Foo.java
      % java bar.Bar
      The secrete message is "Package secrete Hacker breakin"

        Attachments

          Activity

            People

            Assignee:
            sliangsunw Sheng Liang (Inactive)
            Reporter:
            duke J. Duke (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: