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

java.lang.reflect breaks java generics

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 5.0
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      FULL PRODUCT VERSION :
      1.5

      A DESCRIPTION OF THE PROBLEM :
      You can trick, and in-doing-so break, java generics into accepting invalid class types as method parameters by using java.lang.reflect framework. No exception gets thrown.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      execute the attached code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      an exception to get thrown or some indication that something went wrong
      ACTUAL -
      no exception and no indication

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      would be nice

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.lang.reflect.*;

      class Z {}
      class Z1 extends Z {}
      class Z2 extends Z{}

      class A<T extends Z> {
        T hold;
        public void set(T value) {
          hold = value;
        }
      }

      class B extends A<Z1> {}
      class C extends A<Z2> {}

      class Test {
        public static void main(String[] args) throws Exception {
          B type = new B();
          A<?> type_super = (A<?>) type;
          type_super.getClass().getMethod("set", new Class[] { Z.class }).invoke(type_super, new Z2());
          //You are essentally creating an instance of class B, then calling
          // B.set() with parameter of type Z2 even though you can only
          // call B.set() with a parameter of type Z1
          //An exception doesn't get thrown
          //This is a bug!
          System.out.println("This is a bug!");
        }
      }
      ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                darcy Joe Darcy
                Reporter:
                ndcosta Nelson Dcosta (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: