You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Charles Brault <ch...@brault.com> on 2002/10/25 20:49:27 UTC

OT - Can someone explain this "feature" to me

I was thinking of sending this to the Car Guys for the weekly Puzzler, 
but then decided to send to this list instead, hoping one of you wizards 
out there can explain what's going on.

Is the class name "Component" a sacred name in Introspection/BeanInfo 
land?  Here's a little test of 2 classes, where the only difference is 
the class name (one class is named "Component" the other "Cmpnt"), you 
can find a property descriptor for one but not the other. I'm running 
this with :

java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)


-- the class Component ---

package test;

import java.util.Date;

/**
  *  Description of the Class
  *
  *@author     chaz
  *@created    October 15, 2002
  */
public class Component   {
     private int key;
     private Integer mfrKey;
     private Date createDate;

     /**
      *  Constructor for the Component object
      *
      *@since
      */
     public Component() { }


     /**
      * Gets the value of key
      *
      * @return the value of key
      */
     public int getKey()  {
     return this.key;
     }

     /**
      * Sets the value of key
      *
      * @param argKey Value to assign to this.key
      */
     public void setKey(int argKey) {
     this.key = argKey;
     }


     /**
      * Gets the value of createDate
      *
      * @return the value of createDate
      */
     public Date getCreateDate()  {
     return this.createDate;
     }

     /**
      * Sets the value of createDate
      *
      * @param argCreateDate Value to assign to this.createDate
      */
     public void setCreateDate(Date argCreateDate) {
     this.createDate = argCreateDate;
     }






}


--- the class Cmpt - same as above except the  class name ----
package test;


import java.util.Date;

/**
  *  Description of the Class
  *
  *@author     chaz
  *@created    October 15, 2002
  */
public class Cmpt   {
     private int key;
     private Integer mfrKey;
     private Date createDate;

     /**
      * Gets the value of key
      *
      * @return the value of key
      */
     public int getKey()  {
     return this.key;
     }

     /**
      * Sets the value of key
      *
      * @param argKey Value to assign to this.key
      */
     public void setKey(int argKey) {
     this.key = argKey;
     }

     /**
      * Gets the value of mfrKey
      *
      * @return the value of mfrKey
      */
     public Integer getMfrKey()  {
     return this.mfrKey;
     }

     /**
      * Sets the value of mfrKey
      *
      * @param argMfrKey Value to assign to this.mfrKey
      */
     public void setMfrKey(Integer argMfrKey) {
     this.mfrKey = argMfrKey;
     }


     /**
      * Gets the value of createDate
      *
      * @return the value of createDate
      */
     public Date getCreateDate()  {
     return this.createDate;
     }

     /**
      * Sets the value of createDate
      *
      * @param argCreateDate Value to assign to this.createDate
      */
     public void setCreateDate(Date argCreateDate) {
     this.createDate = argCreateDate;
     }



     /**
      *  Constructor for the Component object
      *
      *@since
      */
     public Cmpt() { }







}


--- the test ---
package test;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class test {
     public static void main( String[] args ) throws Exception {
         System.out.println("testing Cmpt first, will work");
         findPropertyDescriptor(Cmpt.class, "key");
         System.out.println("\nTesting Component - won't work");
         findPropertyDescriptor(Component.class, "key");


     }


     protected static PropertyDescriptor findPropertyDescriptor(Class 
aClass, String aPropertyName) throws Exception
     {
         BeanInfo info;
         PropertyDescriptor[] pd;
         PropertyDescriptor descriptor = null;

         try
         {
             info = Introspector.getBeanInfo(aClass);
             pd = info.getPropertyDescriptors();
             for (int i = 0; i < pd.length; i++)
             {
                 System.out.println(pd[i].getName());
                 if (pd[i].getName().equals(aPropertyName))
                 {
                     descriptor = pd[i];
                     break;
                 }
             }
             if (descriptor == null)
             {

                 throw new Exception("Can't find property " + 
aPropertyName + " in " + aClass.getName());
             }
             return descriptor;
         }
         catch (IntrospectionException ex)
         {

             throw new Exception("Can't find property " + aPropertyName 
+ " in " + aClass.getName(), ex);
         }

     }

}

-- 
Charles E Brault
chaz@brault.com
"Where are we going, and why am I in this handbasket?"


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: OT - Can someone explain this "feature" to me

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 25 Oct 2002, Charles Brault wrote:

> Date: Fri, 25 Oct 2002 14:49:27 -0400
> From: Charles Brault <ch...@brault.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: struts-user@jakarta.apache.org
> Subject: OT - Can someone explain this "feature" to me
>
> I was thinking of sending this to the Car Guys for the weekly Puzzler,
> but then decided to send to this list instead, hoping one of you wizards
> out there can explain what's going on.
>
> Is the class name "Component" a sacred name in Introspection/BeanInfo
> land?  Here's a little test of 2 classes, where the only difference is
> the class name (one class is named "Component" the other "Cmpnt"), you
> can find a property descriptor for one but not the other. I'm running
> this with :
>

Unfortunately, it is (by default) -- at least in the Sun JVMs.

For the nitty gritty details, go read the JavaDocs for how
java.beans.Introspector looks for BeanInfo classes in the BeanInfo search
path, which (by default) includes a directory that includes the BeanInfo
for a class named Component.

The setBeanInfoSearchPath() method lets you change this, but is likely to
trigger a security exception if you run under a security manager
(sensible, because it's a JVM-wide setting).  I think there's a system
property you can set to influence this, but don't recall offhand what it
is.

Craig


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>