Package org.jboss.beans.metadata.plugins

Source Code of org.jboss.beans.metadata.plugins.StringValueMetaData

/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This 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 software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.beans.metadata.plugins;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.jboss.logging.Logger;
import org.jboss.reflect.spi.TypeInfo;
import org.jboss.util.propertyeditor.PropertyEditors;

/**
* String value.
*
* @author <a href="adrian@jboss.com">Adrian Brock</a>
* @version $Revision: 1.15 $
*/
public class StringValueMetaData extends AbstractTypeMetaData
{
   private static final Logger log = Logger.getLogger(StringValueMetaData.class);
  
   static
   {
      PropertyEditors.init();
   }

   /**
    * Create a new string value
    */
   public StringValueMetaData()
   {
   }

   /**
    * Create a new string value
    *
    * @param value the value
    */
   public StringValueMetaData(String value)
   {
      super(value);
   }

   /**
    * Set the value
    *
    * @param value the value
    */
   public void setValue(String value)
   {
      super.setValue(value);
   }

   public void setValue(Object value)
   {
      if (value != null && value instanceof String == false)
         throw new ClassCastException("value is not a String: " + value);
      super.setValue(value);
   }
  
   public Object getValue(TypeInfo info, ClassLoader cl) throws Throwable
   {
      boolean trace = log.isTraceEnabled();
      if (trace)
         log.trace("getValue value=" + value + " type=" + type + " info=" + info);
     
      TypeInfo typeInfo = getClassInfo(cl);
      if (typeInfo == null)
         typeInfo = info;
      if (typeInfo == null)
         throw new IllegalArgumentException("Unable to determine type for value: " + value);
        
      Class clazz = typeInfo.getType();
      PropertyEditor editor = PropertyEditorManager.findEditor(clazz);
      if (editor != null)
      {
         editor.setAsText((String) value);
         return editor.getValue();
      }
      else
      {
         // TODO improve <init>(String) might not be relevent?
         Constructor constructor = clazz.getConstructor(new Class[] { String.class });
         try
         {
            return constructor.newInstance(new Object[] { value });
         }
         catch (InvocationTargetException e)
         {
            throw e.getTargetException();
         }
      }
   }
}
TOP

Related Classes of org.jboss.beans.metadata.plugins.StringValueMetaData

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.