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

StringIndexOutOfBoundsException in java.beans.Introspector.getTargetEventInfo()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 5.0
    • 1.4.2
    • client-libs
    • b28
    • x86
    • windows_2000


      Name: rl43681 Date: 07/26/2003


      FULL PRODUCT VERSION :
      java version "1.4.2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
      Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

      FULL OS VERSION :
      Windows 2000 service pack 3

      A DESCRIPTION OF THE PROBLEM :
      Conditions when the bug appears:
      When you implement two classes that inherits from java.beans.Beans and implements an java.util.EventListener like the followings

      CLASS 1 : introspector_test.A

      package introspector_test;
      import java.beans.Beans;
      import java.util.EventListener;
      import java.util.Vector;
      public class A extends Beans implements EventListener{
      Vector bs=new Vector();
      public A() {
      }
      public void addB(B b) {
      bs.addElement(b);
      }
      public void removeB(B b) {
      bs.removeElement(b);
      }
      }


      CLASS 2 : introspector_test.B
      package introspector_test;
      import java.beans.Beans;
      import java.util.EventListener;
      public class B extends Beans implements EventListener {
      java.util.Vector as=new java.util.Vector();
      public B() {
      }
      public void addA(A a) {
      as.addElement(a);
      }
      public void removeA(A a) {
      as.removeElement(a);
      }
      }




      And third class to let the bug appear:

      package introspector_test;
      import java.beans.BeanInfo;
      import java.beans.Introspector;
      public class Main {

      public static void main(String[] args) {
      try {
      BeanInfo info=Introspector.getBeanInfo(A.class);
      }catch(Throwable th1) {
      th1.printStackTrace();
      }

      }
      }

      Running the Introspector like in main() function let it throw an
      java.lang.StringIndexOutOfBoundsException
      with a stack trace like that:

      java.lang.StringIndexOutOfBoundsException: String index out of range: -7
      at java.lang.String.substring(String.java:1444)
      at java.beans.Introspector.getTargetEventInfo(Introspector.java:884)
      at java.beans.Introspector.getBeanInfo(Introspector.java:371)
      at java.beans.Introspector.getBeanInfo(Introspector.java:144)
      at introspector_test.Main.main(Main.java:23)


      The exception is at line : 884 of class java.beans.Introspector, in that point your code does like that:

      > String eventName = decapitalize(listenerName.substring(0, listenerName.length()-8));


      Even if the Beans standard does reccomend to use method names add<event-name>Listener and remove<event-name>Listener
      it does not say that i must avoid to name methods with add<...> and remove<.....> starting words,so it is coherent to write
      classes like A and B. I think you forget to test if name of add/remove methods ends with the word "Listener" (that is the same stuff you are
      cutting with listenerName.length()-8 parameter).



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      CLASS 1 : introspector_test.A

      package introspector_test;
      import java.beans.Beans;
      import java.util.EventListener;
      import java.util.Vector;
      public class A extends Beans implements EventListener{
      Vector bs=new Vector();
      public A() {
      }
      public void addB(B b) {
      bs.addElement(b);
      }
      public void removeB(B b) {
      bs.removeElement(b);
      }
      }


      CLASS 2 : introspector_test.B
      package introspector_test;
      import java.beans.Beans;
      import java.util.EventListener;
      public class B extends Beans implements EventListener {
      java.util.Vector as=new java.util.Vector();
      public B() {
      }
      public void addA(A a) {
      as.addElement(a);
      }
      public void removeA(A a) {
      as.removeElement(a);
      }
      }




      And third class to let the bug appear:

      package introspector_test;
      import java.beans.BeanInfo;
      import java.beans.Introspector;
      public class Main {

      public static void main(String[] args) {
      try {
      BeanInfo info=Introspector.getBeanInfo(A.class);
      }catch(Throwable th1) {
      th1.printStackTrace();
      }

      }
      }

      Running the Introspector like in main() function let it throw an
      java.lang.StringIndexOutOfBoundsException
      with a stack trace like that:

      java.lang.StringIndexOutOfBoundsException: String index out of range: -7
      at java.lang.String.substring(String.java:1444)
      at java.beans.Introspector.getTargetEventInfo(Introspector.java:884)
      at java.beans.Introspector.getBeanInfo(Introspector.java:371)
      at java.beans.Introspector.getBeanInfo(Introspector.java:144)
      at introspector_test.Main.main(Main.java:23)

      The exception is at line : 884 of class java.beans.Introspector, in that point your code does like that:

      > String eventName = decapitalize(listenerName.substring(0, listenerName.length()-8));


      Even if the Beans standard does reccomend to use method names add<event-name>Listener and remove<event-name>Listener
      it does not say that i must avoid to name methods with add<...> and remove<.....> starting words,so it is coherent to write
      classes like A and B. I think you forget to test if name of add/remove methods ends with the word "Listener" (that is the same stuff you are
      cutting with listenerName.length()-8 parameter).



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A normal JavaBeans introspection
      ACTUAL -
      at line 884 of java.beans.Introspector an exception of type java.lang.StringIndexOutOfBoundsException is thrown

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.StringIndexOutOfBoundsException: String index out of range: -7
      at java.lang.String.substring(String.java:1444)
      at java.beans.Introspector.getTargetEventInfo(Introspector.java:884)
      at java.beans.Introspector.getBeanInfo(Introspector.java:371)
      at java.beans.Introspector.getBeanInfo(Introspector.java:144)
      at introspector_test.Main.main(Main.java:23)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      CLASS 1 : introspector_test.A

      package introspector_test;
      import java.beans.Beans;
      import java.util.EventListener;
      import java.util.Vector;
      public class A extends Beans implements EventListener{
      Vector bs=new Vector();
      public A() {
      }
      public void addB(B b) {
      bs.addElement(b);
      }
      public void removeB(B b) {
      bs.removeElement(b);
      }
      }


      CLASS 2 : introspector_test.B
      package introspector_test;
      import java.beans.Beans;
      import java.util.EventListener;
      public class B extends Beans implements EventListener {
      java.util.Vector as=new java.util.Vector();
      public B() {
      }
      public void addA(A a) {
      as.addElement(a);
      }
      public void removeA(A a) {
      as.removeElement(a);
      }
      }




      And third class to let the bug appear:
      CLASS 3:

      package introspector_test;
      import java.beans.BeanInfo;
      import java.beans.Introspector;
      public class Main {

      public static void main(String[] args) {
      try {
      BeanInfo info=Introspector.getBeanInfo(A.class);
      }catch(Throwable th1) {
      th1.printStackTrace();
      }

      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Avoid declaring class A and B as implementing java.util.EventListener
      (Incident Review ID: 189507)
      ======================================================================

            mdavidsosunw Mark Davidson (Inactive)
            rlewis Roger Lewis (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: