Bean Utility : Java Beans « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » Java BeansScreenshots 
Bean Utility
  
/*
 * StringUtil.java
 *
 * Created on May 7, 2004, 7:43 PM
 *
 * Copyright (C) 2004  Robert Cooper, Temple of the Screaming Penguin
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import java.beans.*;
import java.lang.reflect.*;

/** This class contains some generic methods for working with String.
 @version $Rev: 87 $
 @author  <a href="mailto:cooper@screaming-penguin.com">Robert Cooper</a>
 */
public class BeanUtility {
    
    /** Creates a new instance of StringUtil */
    private BeanUtility() { }
    
    /** This method takes a JavaBean and generates a standard toString() type result for it.
     @param o JavaBean object to stringinate
     @return STRINGIATION! Stringingating the countryside. Stringinating all the peasants.
     */
    public static String beanToStringObject o ){
        StringBuffer result = new StringBuffer();
        if(o == null)
            return "--- null";
        result.append"--- begin");result.appendo.getClass().getName()); result.append(" hash: ");
        result.appendo.hashCode() ); result.append"\r\n");
        try{
            PropertyDescriptor[] pds = Introspector.getBeanInfoo.getClass() ).getPropertyDescriptors();
            forint pdi = 0; pdi < pds.length; pdi ++ ){
                try{
                    result.append"Property: "+ pds[pdi].getName() " Value: " + pds[pdi].getReadMethod().invoke) );
                catchIllegalAccessException iae ){
                    result.append"Property: "+ pds[pdi].getName() " (Illegal Access to Value) ");
                }
                catchInvocationTargetException iae ){
                    result.append"Property: "+ pds[pdi].getName() " (InvocationTargetException) " + iae.toString() );
                }
                catchException e ){
                     result.append"Property: "+ pds[pdi].getName() +" (Other Exception )"+e.toString());
                }
                result.append"\r\n");
            }
            
            
        catchIntrospectionException ie){
            result.append"Introspection Exception: " + ie.toString() ); result.append"\r\n");
        }
        result.append"--- end " ); result.appendo.getClass().getName()); result.append(" hash: ");
        result.appendo.hashCode() ); result.append"\n");
        return result.toString();
    }
    
    /** This method takes 2 JavaBeans of the same type and copies the properties of one bean to the other.
     * Any attempts that have an IllegalAccessException will be ignored. This will also NOT recurse into nested bean
     * results. References to existing beanage will be includes. Try using .clone() for that stuff.
     @param from Source Bean
     @param to Desitnation Bean
     */
    public static void copyBeanToBeanObject from, Object to throws InvocationTargetException, IntrospectionException{
        PropertyDescriptor[] pds = Introspector.getBeanInfofrom.getClass() ).getPropertyDescriptors();
        forint i=0; i < pds.length; i++){
            try{
                if(pds[i].getName().equals("class")) continue;
                Object[] value = {pds[i].getReadMethod().invoke(from) };
                pds[i].getWriteMethod().invoketo, value ;
            catchIllegalAccessException iae ){
                //Im just going to ignore any properties I don't have access too.
            }
        }
        
    }
    
    public static String[] getPropertyNames(Object othrows IntrospectionException {
        PropertyDescriptor[] pds = Introspector.getBeanInfoo.getClass() ).getPropertyDescriptors();
        String[] propertyNames = new Stringpds.length];
        forint i=0; i< pds.length; i++){
            propertyNames[i= pds[i].getName();
        }
        return propertyNames;
    }
    
    public static Object getPropertyObject o, String propertyName throws Exception {
        PropertyDescriptor[] pds = Introspector.getBeanInfoo.getClass() ).getPropertyDescriptors();
        forint i=0; i< pds.length; i++){
            ifpds[i].getName().equals(propertyName)){
                return pds[i].getReadMethod().invoke;
            }
        }
        throw new Exception("Property not found.");
    }
    public static void setPropertyObject o, String propertyName, Object value throws Exception{
        PropertyDescriptor[] pds = Introspector.getBeanInfoo.getClass() ).getPropertyDescriptors();
        forint i=0; i< pds.length; i++){
            ifpds[i].getName().equals(propertyName)){
                pds[i].getWriteMethod().invokeo, value );
                return;
            }
        }
        throw new Exception("Property not found.");
    }
    
    public static Class getPropertyTypeObject o, String propertyName throws Exception{
        PropertyDescriptor[] pds = Introspector.getBeanInfoo.getClass() ).getPropertyDescriptors();
        forint i=0; i< pds.length; i++){
            ifpds[i].getName().equals(propertyName)){
                return pds[i].getPropertyType();
            }
        }
        throw new Exception("Property not found.");
    }
}

   
    
  
Related examples in the same category
1. JavaBean: BeanContextSupportJavaBean: BeanContextSupport
2. JavaBean: Test program that adds 100 beans to a contextJavaBean: Test program that adds 100 beans to a context
3. JavaBean: how to use the instantiateChild() convenience method to create a beanJavaBean: how to use the instantiateChild() convenience method to create a bean
4. JavaBean: illustrate delivery of the BeanContextMembershipEventJavaBean: illustrate delivery of the BeanContextMembershipEvent
5. JavaBean: creates all of the objects, a tests the service capabilitiesJavaBean: creates all of the objects, a tests the service capabilities
6. Bean ContainerBean Container
7. PropertyTablePropertyTable
8. Property Test
9. Introspecting a BeanIntrospecting a Bean
10. Listening for Changes to the Selected File in a JFileChooser Dialog
11. Get a list of selected files
12. Listening for Changes to the Current Directory in a JFileChooser Dialog
13. Displaying the Current Directory in the Title of a JFileChooser Dialog
14. Setting an Accessory Component in a JFileChooser Dialog
15. Convert a bean to XML persistence
16. Listen for bean's property change event
17. List property names of a Bean
18. Prevent bean's property being serialized to XML
19. Create an instance a Bean
20. Convert an XML persistence to bean
21. Determine bean's property type
22. Listen for a constrained property change
23. Bean has a single property called property.
24. Implementing a Bound Property
25. Implementing a Constrained Property: fires a PropertyChangeEvent whenever its value is about to be changed.
26. Instantiating a Bean
27. Listing the Property Names of a Bean
28. Getting and Setting a Property of a Bean
29. Get and set the value of a property in a bean using Expression and Statement
30. Get and set an Object type property
31. gets and sets a primitive type property
32. gets and sets an array type property
33. Serializing a Bean to XML: XMLEncoder only persists the value of public properties.
34. Deserializing a Bean from XML
35. Preventing a Bean Property from Being Serialized to XML
36. Serializing an Immutable Bean Property to XML
37. Listening for a Property Change Event: A property change event is fired when a bound property is changed.
38. Listening for a Vetoable Property Change Event
39. Read bean's property value
40. Saving and restoring the state of a pretend CAD systemSaving and restoring the state of a pretend CAD system
41. An extension of ArrayList that provides some handy utilities for working with JavaBeans
42. An extension of Vector that provides some handy utilities for working with JavaBeans
43. extends SimpleBeanInfo
44. Get and set properties on a bean
45. Is JavaBean Compliant Setter
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.