15-Jun-17 (Created: 15-Jun-17) | More in 'Old Jaxb 2.0 Tutorial 2014'

jaxb 2.0 tutorial: Rolling your own reflection: BeanInfo

Manage this page

1. Back to tutorial

BeanInfo.java


//Get your own package name

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

//********************************************************************
// Given an object manipulate it using reflection  
//********************************************************************
public class BeanInfo 
{
   //primary members
   public String xmlNodeName = null;
   private Class m_class;

   private List m_fieldList = new ArrayList();
   private List m_FieldInfoList = new ArrayList();
   private Map  m_FieldInfoMap = new HashMap();
   
   private static boolean xmlMapInitialized = false;
   private static Map m_xmlNameMap = new HashMap();

//********************************************************************
// Primary constructor  
//********************************************************************
   public BeanInfo(Class inClass) 
      throws NoSuchMethodException, NoSuchFieldException
   {
      m_class = inClass;
      Field fieldArray[] = m_class.getDeclaredFields();
      for(int i=0;i<fieldArray.length;i++)
      {
         Field thisField = fieldArray[i];
         String fieldName = thisField.getName();
         registerField(fieldName);
      }
      xmlNodeName = m_class.getName();
   }
   
   
   public BeanInfo(Object bean) 
   throws NoSuchMethodException, NoSuchFieldException
   {
      this(bean.getClass());
   }
   
//********************************************************************
// Give me all the field infos for this bean  
//********************************************************************
   public Iterator getFieldInfos()
   {
      return m_FieldInfoList.iterator();
   }
   
//********************************************************************
// Give me all the field names for this bean in a string format  
//********************************************************************
   public Iterator getFieldNames()
   {
      return m_fieldList.iterator();
   }
//********************************************************************
// Register an xml mapping for a particualr field
// This is like supplying metadata to the field
//********************************************************************
   public void registerMapping(String fieldName, String xmlName
                     , boolean isAttribute)
   {
      FieldInfo fi = (FieldInfo)m_FieldInfoMap.get(fieldName);
      if (fi == null)
      {
         throw new RuntimeException(
            "No field information found for field:" + fieldName);
      }
      fi.m_xmlName=xmlName;
      fi.m_attribute = isAttribute;
   }
   
//********************************************************************
// Remove a field from xml mapping
//********************************************************************
   public void unRegisterMapping(String fieldName)
   {
      FieldInfo fi = (FieldInfo)m_FieldInfoMap.get(fieldName);
      if (fi == null)
      {
         throw new RuntimeException(
            "No field information found for field:" + fieldName);
      }
      m_FieldInfoMap.remove(fi);
   }
   
//********************************************************************
// Access the fieldinfo for a more closer manipulation
//********************************************************************
   public FieldInfo getFieldInfo(String xmlName)
   {
      initializeXmlMap();
      FieldInfo fi = (FieldInfo)m_xmlNameMap.get(xmlName);
      return fi;
   }
   
//********************************************************************
// Once the bean mappings have been setup for a class
// Call this function to optimize it
// This might be mandatory
//********************************************************************
   public void optimize()
   {
      initializeXmlMap();
   }
   
   private void registerField(String fieldName)
      throws NoSuchMethodException, NoSuchFieldException
   {
      m_fieldList.add(fieldName);
      FieldInfo fi = new FieldInfo(m_class,fieldName);
      this.m_FieldInfoMap.put(fieldName, fi);
      m_FieldInfoList.add(fi);
   }
   
   public String getValueAsString(String fieldName, Object o)
      throws InvocationTargetException, IllegalAccessException
   {
      FieldInfo fi = (FieldInfo)m_FieldInfoMap.get(fieldName);
      return fi.getValueAsString(o);
      
   }
   public void registerMapping(String fieldName, String xmlName)
   {
      registerMapping(fieldName, xmlName, false);
   }
   
   
   private void initializeXmlMap()
   {
      if (xmlMapInitialized == true) return;
      
      Iterator fieldInfos = getFieldInfos();
      while(fieldInfos.hasNext())
      {
         FieldInfo fi = (FieldInfo)fieldInfos.next();
         String xmlName = fi.m_xmlName;
         m_xmlNameMap.put(xmlName,fi);
      }
   }
   
//********************************************************************
// Converting a bean to a string
// Exercises a good bit of the functionality of bean reflection
//********************************************************************
   public String toString(Object o)
   {
      //Make sure the types are right 
      String myClassName = m_class.getName();
      String inClassName = o.getClass().getName();
      if (!(myClassName.equals(inClassName)))
      {
         throw new RuntimeException(
            "Unexpected classname. Expecting " 
            + myClassName 
            + " but got " 
            + inClassName);
      }
      
      //Get a buffer
      StringBuffer sbuf = new StringBuffer();
      
      sbuf.append("Start writing the bean");
      
      //good to go
      //Get all public fields
      Iterator fieldInfos = this.getFieldInfos();
      while(fieldInfos.hasNext())
      {
         FieldInfo fi = (FieldInfo)fieldInfos.next();
         String fieldName = fi.m_name;
         String fieldXmlName = fi.m_xmlName;
         String value = "";
         try
         {
            value = fi.getValueAsString(o);
         }
         catch(Exception x)
         {
            x.printStackTrace();
            value="Exception. The message is:" + x.getMessage();
         }
         sbuf.append("\n" + fieldName + "='" + value + "'");
      }
      return sbuf.toString();
   }
   
}//eof-class