KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > rtti > MemberItem


1 /*
2   Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.core.rtti;
19
20 import java.lang.reflect.*;
21 /**
22  * This class defines a meta item that corresponds to a field or a
23  * method.
24  *
25  * @author Renaud Pawlak
26  * @author Laurent Martelli
27  */

28
29 public abstract class MemberItem extends MetaItemDelegate {
30
31     /**
32      * A util method to get a member item reference from a full name.
33      *
34      * @param str member's full name, E.g.: myPackage.Person.name.
35      */

36     public static MemberItem getMemberFromFullName(String JavaDoc str) throws Exception JavaDoc {
37         MemberItem ret=null;
38         int index = -1;
39         int paren = str.indexOf("(");
40         if (paren==-1)
41             index = str.lastIndexOf(".");
42         else
43             index = str.lastIndexOf(".",paren);
44         if (index!=-1) {
45             ClassItem classItem =
46                 ClassRepository.get().getClass
47                 (str.substring(0,index));
48             ret = classItem.getMember(str.substring(index+1));
49         } else {
50             new Exception JavaDoc("Failed to convert "+str+
51                           " into a class member");
52         }
53         return ret;
54     }
55
56     static Class JavaDoc wrappeeClass = ClassRepository.wrappeeClass;
57
58     public MemberItem(ClassItem parent) {
59         this.parent = parent;
60     }
61
62     public MemberItem(Object JavaDoc delegate, ClassItem parent) throws InvalidDelegateException {
63         super(delegate);
64         this.parent = parent;
65     }
66
67     public abstract Class JavaDoc getType();
68
69     public final ClassItem getTypeItem() {
70         return ClassRepository.get().getClass(getType());
71     }
72
73     /** Returns the class item that owns the field (like getParent). */
74     public final ClassItem getClassItem() {
75         return (ClassItem)getParent();
76     }
77
78     MethodItem[] dependentMethods = MethodItem.emptyArray;
79     /**
80      * @see #getDependentMethods()
81      */

82     public final void addDependentMethod(MethodItem method) {
83         MethodItem[] tmp = new MethodItem[dependentMethods.length+1];
84         System.arraycopy(dependentMethods, 0, tmp, 0, dependentMethods.length);
85         tmp[dependentMethods.length] = method;
86         dependentMethods = tmp;
87         ClassItem superClass = getClassItem().getSuperclass();
88         if (superClass!=null) {
89             FieldItem superField = superClass.getFieldNoError(getName());
90             if (superField!=null)
91                 superField.addDependentMethod(method);
92         }
93     }
94     /**
95      * Returns an array of methods whose result depend on the member.
96      * @see #addDependentMethod(MethodItem)
97      */

98     public final MethodItem[] getDependentMethods() {
99         return dependentMethods;
100     }
101
102
103     protected boolean role;
104    
105     /**
106      * Tells if this field is actually implemented by a role wrapper
107      * field.
108      *
109      * @return value of role
110      * @see #setRole(ClassItem,String,String) */

111
112     public boolean isRole() {
113         return role;
114     }
115
116     protected ClassItem roleClassType = null;
117     protected String JavaDoc roleType = null;
118     protected String JavaDoc roleName = null;
119    
120     /**
121      * Sets this field to be actually implemented by a field of a role
122      * wrapper.
123      *
124      * <p>When this method is called once, the <code>isRole()</code>
125      * method will return true. Moreover, the actually accessed and
126      * modified field when using set, get, etc, is the field of the
127      * role wrapper.
128      */

129     public void setRole(ClassItem roleClassType, String JavaDoc roleType, String JavaDoc roleName) {
130         this.role = true;
131         this.roleClassType = roleClassType;
132         this.roleType = roleType;
133         this.roleName = roleName;
134     }
135    
136     /**
137      * Returns the name prfixed with the owning class name.
138      */

139     public String JavaDoc getLongName() {
140         return parent!=null ? parent.getName()+"."+getName() : "???";
141     }
142
143     public int getModifiers() {
144         return ((Member)delegate).getModifiers();
145     }
146
147     public String JavaDoc toString() {
148         return getLongName()+":"+getType().getName();
149     }
150
151     /**
152      * Two members are equal if the class of one is a subclass of the
153      * other's class and they have the same name
154      */

155     public boolean equals(Object JavaDoc o) {
156         if (!(o instanceof MemberItem))
157             return false;
158         MemberItem m = (MemberItem)o;
159         return (m.getClassItem().isSubClassOf(getClassItem()) ||
160                 getClassItem().isSubClassOf(m.getClassItem()))
161             && getName().equals(m.getName());
162     }
163 }
164
Popular Tags