Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
User Journal

Journal cyranoVR's Journal: JavaBeans introspection example 11

MyJavaBean.java

public class MyJavaBean {
 
    private int height;
    private int weight;
    private String meatSpaceName;
    private String slashdotNick;
    private int age;
    private double karma;
 
    public int getHeight() {
        return height;
    }
 
    public void setHeight(int height) {
        this.height = height;
    }
 
    public int getWeight() {
        return weight;
    }
 
    public void setWeight(int weight) {
        this.weight = weight;
    }
 
    public String getMeatSpaceName() {
        return meatSpaceName;
    }
 
    public void setMeatSpaceName(String meatSpaceName) {
        this.meatSpaceName = meatSpaceName;
    }
 
    public String getSlashdotNick() {
        return slashdotNick;
    }
 
    public void setSlashdotNick(String slashdotNick) {
        this.slashdotNick = slashdotNick;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public double getKarma() {
        return karma;
    }
 
    public void setKarma(double karma) {
        this.karma = karma;
    }
}

JavaBeansExample.java

import java.beans.PropertyDescriptor;
import java.beans.Introspector;
import java.beans.IntrospectionException;
 
public class JavaBeansExample {
 
    public static void main(String[] args) {
        try {
            PropertyDescriptor[] fields = Introspector.getBeanInfo(MyJavaBean.class)
.getPropertyDescriptors();
            System.out.println("Inspecting JavaBean fields for " + MyJavaBean.class.getName() + "...");
            for (int i = 0; i < fields.length; i++) {
                PropertyDescriptor field = fields[i];
                String name = field.getName();
                System.out.println("- " + name + " aka " + field.getReadMethod());
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
 
    }
}

If you want to know what this is good for, check out Spring Framework's use of PropertyEditors in its DataBinding api.

This discussion was created by cyranoVR (518628) for Friends and Friends of Friends only, but now has been archived. No new comments can be posted.

JavaBeans introspection example

Comments Filter:
  • Now you are speaking my language.... Introspection is super handy. We use Hibernate & webwork to automatically call any "get/set" on methods names submitted as form fields. So use a webwork field called MeatSpaceName and webwork will automatically call the setter/getter for that based on if you just posted or get from a jsp. Very handy.

    jason
  • Comment removed based on user account deletion
  • Um yah, subject says it all.

    We had to do some java bean stuff in my Java II class, and encounted various issues, the least of which being that Sun has pretty much stopped updating all the documentation about Java Beans. The BeanBox is dead! Long live the BeanBox!

    Apparnetly Sun is going to attempt to revitalize Java as a desktop technology and will be bringing back Java Beans.

    Not having 500 barely documented CLI based steps would help for starters....
    • Excuse me, but what the fuck are you talking about?

      JavaBeans is alive in well, in Spring Framework, WebWork, Hibernate and others.

      Just because it's not being used exactly as it was envisioned seven years ago doesn't mean it's a failure.
      • by Com2Kid ( 142006 )

        JavaBeans is alive in well, in Spring Framework, WebWork, Hibernate and others.

        Just because it's not being used exactly as it was envisioned seven years ago doesn't mean it's a failure.


        Trying to find any information about them from Sun is darn nearly impossible though. EJB are alive, but JB seem almost dead.
        • Trying to find any information about them from Sun is darn nearly impossible though. EJB are alive, but JB seem almost dead.

          If you are talking about JavaBeans as a method of making VisualStudio-style editors, then you're right. But the java.beans.* package is alive and well.
          • by Com2Kid ( 142006 )

            If you are talking about JavaBeans as a method of making VisualStudio-style editors, then you're right.

            That I am. These JB were so horribly defined, that about the only way to learn about the Magic Function Naming Scheme is to try and copy how the tutorials name their functions, the tutorials never actually GIVE the rules for function naming. I am sure that they are given somewhere, but not in the JB tutorials!

            Yet another reason that the (original) Java Beans failed.

            • 1) getFoo setFoo - what is so hard about that?

              2) for booleans you have isFoo

              3) In Java they're called "methods" not "functions."
              • by Com2Kid ( 142006 )

                1) getFoo setFoo - what is so hard about that?

                2) for booleans you have isFoo


                None of which is documented anywhere! It has to be guessed at. IIRC there has to be some relationship between fields and function names, but I may be mistaken. All in all it is just one more annoyance amongst the many problems in the JB tutorials.

                • If you need documentation for that simple naming convention, you've got bigger problems on your hands.

                  PS you must not be trying very hard [sun.com]
                  • by Com2Kid ( 142006 )
                    That would have been immensly useful.

                    As it was, we were told to "Make a javabean" and pointed at the Javabean tutorial [sun.com]

                    The problem is, MOST of the Java Tutorial trails are really well done, if a bit stale in some places. The JB tutorial trail is some hidiously out of date inadequent.... thing. It

                    If you need documentation for that simple naming convention, you've got bigger problems on your hands.

                    Being TOLD about it would have been a good idea, no one mentioned to us (the class) that there was a naming conv

It's not an optical illusion, it just looks like one. -- Phil White

Working...