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

Package protected class not protected at run time

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P5 P5
    • 1.2.0
    • 1.1.6
    • hotspot
    • 1.2fcs
    • sparc
    • solaris_2.6
    • Not verified

      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"

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: