001    /*
002      Copyright (C) 2002 Renaud Pawlak <renaud@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    package org.objectweb.jac.ide;
019    
020    import org.apache.log4j.Logger;
021    import org.objectweb.jac.aspects.persistence.StringConverter;
022    import org.objectweb.jac.core.rtti.ClassItem;
023    import org.objectweb.jac.core.rtti.ClassRepository;
024    import org.objectweb.jac.core.rtti.MethodItem;
025    
026    /**
027     * Persistance for a MethodItem Class
028     */
029    public class MethodItemConverter implements StringConverter {
030        static Logger logger = Logger.getLogger("ide");
031    
032        /**
033         * Persistance storage : translate the object into String
034         * @param obj the object to translate
035         * @return a string representing the object ClassItem:MethodItem
036         */
037        public String objectToString(Object obj){
038            String result=null;
039            try{
040                MethodItem method=(MethodItem)obj;
041                ClassItem cl=method.getClassItem();
042                result=cl.getName()+":"+method.getFullName();
043            }catch(Exception e){
044                result=null;
045            }
046            return result;
047        }
048    
049        /**
050         * Trying to convert a String into a MethodItem: the string must
051         * be like ClassItem:MethodItem (fully qualified)
052         *
053         * @param str the input string  ClassItem:MethodItem
054         * @return a MethodItem 
055         */
056        public Object stringToObject(String str){
057            //The position of ':' in the string
058            int pos = str.indexOf(":");
059            //If not found error
060            if (pos==-1) {
061                logger.warn("Malformed method string (must be <classname>:<methodname>)"+str);
062            }
063            ClassItem classItem = ClassRepository.get().getClass(str.substring(0,pos));
064            //if classItem doesn't exist
065            if (classItem==null) {
066                logger.warn("The class "+str.substring(0,pos)+" couldn't be found in the Repository");
067                return null;
068            }
069            return classItem.getMethod(str.substring(pos+1));
070        }
071    }