001    /*
002      Copyright (C) 2001 Renaud Pawlak, Laurent Martelli
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 License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.core.rtti;
019    
020    import java.lang.reflect.*;
021    
022    /**
023     * This class defines the super class for all the meta items whithin
024     * the rtti aspect.<p>
025     *
026     * A meta item encapsulates a <code>java.lang.reflect</code> item so
027     * that the user of this item can add extra informations
028     * (attributes). Typically this feature can be used by an aspect to
029     * tag an element of the model to react to this tag later on.<p>
030     *
031     * Examples:<p> <ul> 
032     *
033     * <li>A persistence aspect can tag some field persistent, add methods
034     * that change the object states even if they do not fit naming
035     * conventions...<p>
036     *
037     * <li>A GUI can tag a given field to be invisible or a class to be
038     * displayed by a special view (eg a given Swing component)...
039     *
040     * </ul>
041     *
042     * @author Renaud Pawlak
043     * @author Laurent Martelli
044     */
045    
046    public abstract class MetaItemDelegate extends MetaItem {
047    
048        /** Stores the corresponding <code>jav.lang.reflect</code>
049            meta item. */
050        protected Object delegate;
051    
052        /** Stores the parent of this meta item */
053        protected MetaItemDelegate parent = null;
054    
055        public Object getDelegate() {
056            return delegate;
057        }
058    
059        /**
060         * Sets the parent.<p>
061         *
062         * For any type of meta item, the only possible type of the parent
063         * is a class item. For a class item, the parent is
064         * <code>null</code> in most cases (except in the case of
065         * inner-classes).
066         *
067         * @param parent the new parent 
068         */
069        public final void setParent(MetaItemDelegate parent) 
070            throws InvalidParentException 
071        {
072            if ( ! (parent.getClass() == ClassItem.class) ) {
073                throw new InvalidParentException();
074            }
075            this.parent = parent;
076        }
077    
078        /**
079         * Gets the parent class item of this meta item.<p>
080         *
081         * @return the parent 
082         */
083        public final MetaItemDelegate getParent() {
084            return parent;
085        }
086       
087        /**
088         * Default contructor to create a new meta item object.<p>
089         *
090         * @param delegate the <code>java.lang.reflect</code> actual
091         * meta item 
092         */
093        public MetaItemDelegate(Object delegate) throws InvalidDelegateException {
094            if (! (delegate instanceof Member) && ! (delegate instanceof Class)) {
095                throw new InvalidDelegateException(delegate, "must be a Member or a Class");
096            }
097            this.delegate = delegate;
098        }
099    
100        public MetaItemDelegate() {
101            delegate = null;
102        }
103    
104        /**
105         * Get the modifiers (see java.lang.reflect) of the meta item.
106         *
107         * @return an int representing the modifiers
108         * @see java.lang.reflect.Modifier 
109         */
110        public int getModifiers() {
111            return ((Member)delegate).getModifiers();
112        }
113    
114        /**
115         * This method gets the type of the meta item by delegating to the
116         * actual <code>java.lang.reflect</code> meta item.<p>
117         *
118         * @return the item type 
119         */
120        public abstract Class getType();
121    
122        /**
123         * Overloads the default method to call the delegate one.
124         *
125         * @return a textual representation of the object 
126         */
127        public String toString() {
128            return getName();
129        }
130    
131    }
132    
133    class InvalidParentException extends Exception {
134    }
135