KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > src > MemberElement


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.src;
21
22 import java.io.*;
23
24 /** Superclass for containable Java source members
25 * (fields, methods and classes). Provides support
26 * for associating this element with a declaring class.
27 *
28 * @author Petr Hamernik, Jaroslav Tulach
29 */

30 public abstract class MemberElement extends Element implements Cloneable JavaDoc {
31     /** the class this element belongs to */
32     private ClassElement declaringClass;
33
34     static final long serialVersionUID =7896378970641663987L;
35
36     /** Create a member element.
37     * @param impl the pluggable implementation
38     * @param declaringClass the class this element belongs to, or <code>null</code> if unattached
39     */

40     protected MemberElement(MemberElement.Impl impl, ClassElement declaringClass) {
41         super(impl);
42         this.declaringClass = declaringClass;
43     }
44
45     /** @return the current implementation. */
46     final MemberElement.Impl getMemberImpl() {
47         return (MemberElement.Impl) impl;
48     }
49
50     // [PENDING] Modifier explicitly disallows assuming its constants
51
// are bitwise composable--this is technically illegal
52
// (although in fact they are and this will probably never change)
53

54     /** Get the modifier flags for this element.
55     * Constrained by {@link #getModifiersMask}.
56     * @return disjunction of constants from <CODE>java.lang.reflect.Modifier</CODE>
57     */

58     public final int getModifiers() {
59         return getMemberImpl().getModifiers();
60     }
61
62     /** Set the modifier flags for this element.
63     * @param mod disjunction of constants from <CODE>java.lang.reflect.Modifier</CODE>
64     * @throws SourceException if impossible (e.g. if <code>mod & ~ getModifiersMask() != 0</code>)
65     */

66     public final void setModifiers(int mod) throws SourceException {
67         getMemberImpl().setModifiers(mod);
68     }
69
70     /** Get the permitted modifiers for this type of element.
71     * @return disjunction of constants from <CODE>java.lang.reflect.Modifier</CODE>
72     */

73     public abstract int getModifiersMask();
74
75     /** Test whether declaring class is interface or class.
76     * @return <CODE>true</CODE> for interfaces otherwise <CODE>false</CODE>
77     */

78     boolean isDeclaredInInterface() {
79         return (declaringClass != null) && (declaringClass.isInterface());
80     }
81
82     /** Get the name of this member.
83     * @return the name
84     */

85     public final Identifier getName() {
86         return getMemberImpl().getName();
87     }
88
89     /** Set the name of this member.
90     * @param name the name
91     * @throws SourceException if impossible
92     */

93     public void setName(Identifier name) throws SourceException {
94         getMemberImpl().setName(name);
95         updateConstructorsNames(name);
96     }
97
98     /** Implemented in ClassElement - update names of the constructors.
99     */

100     void updateConstructorsNames(Identifier name) throws SourceException {
101     }
102
103     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
104         return super.clone();
105     }
106
107     // no --jglick
108
/* This field is automaticly sychnronized
109     * when a MemberElement is added to the class. */

110     /** Get the declaring class.
111     *
112     * @return the class that owns this member element, or <code>null</code> if the element is not
113     * attached to any class
114     */

115     public final ClassElement getDeclaringClass () {
116         return declaringClass;
117     }
118
119     /** Pluggable implementation of member elements.
120     * @see MemberElement
121     */

122     public interface Impl extends Element.Impl {
123         /** @deprecated Only public by accident. */
124         /* public static final */ long serialVersionUID = 2037286733482347462L;
125         /** Get the modifier flags for this element.
126          * Constrained by {@link MemberElement#getModifiersMask}.
127          * @return disjunction of constants from <CODE>java.lang.reflect.Modifier</CODE>
128          */

129         public int getModifiers();
130
131         /** Set the modifier flags for this element.
132          * @param mod disjunction of constants from <CODE>java.lang.reflect.Modifier</CODE>
133          * @throws SourceException if impossible (e.g. if <code>mod & ~ memberElt.getModifiersMask() != 0</code>)
134          */

135         public void setModifiers(int mod) throws SourceException;
136
137         /** Get the name of this member.
138          * @return the name
139          */

140         public Identifier getName();
141
142         /** Set the name of this member.
143          * @param name the name
144          * @throws SourceException if impossible
145          */

146         public void setName(Identifier name) throws SourceException;
147     }
148
149     /** Default implementation of the Impl interface.
150     * It just holds the property values.
151     */

152     static abstract class Memory extends Element.Memory implements MemberElement.Impl {
153         /** Modifiers for this element */
154         private int mod;
155
156         /** Name of this element */
157         private Identifier name;
158
159         static final long serialVersionUID =1876531129266668488L;
160         /** Default */
161         public Memory () {
162         }
163
164         /** Copy */
165         public Memory (MemberElement el) {
166             mod = el.getModifiers ();
167             name = el.getName ();
168         }
169
170         /** Getter for modifiers for this element.
171         * @see java.lang.reflect.Modifier
172         * @return constants from <CODE>java.lang.reflect.Modifier</CODE>
173         */

174         public int getModifiers() {
175             return mod;
176         }
177
178         /** Setter for modifiers for this element.
179         * @see java.lang.reflect.Modifier
180         * @param mod constants from <CODE>java.lang.reflect.Modifier</CODE>
181         */

182         public void setModifiers(int mod) {
183             int old = this.mod;
184             this.mod = mod;
185             firePropertyChange (PROP_MODIFIERS, new Integer JavaDoc (old), new Integer JavaDoc (mod));
186         }
187
188         /** Getter for name of the field.
189         * @return the name
190         */

191         public synchronized Identifier getName() {
192             if (name == null) {
193                 // lazy initialization !?
194
name = Identifier.create(""); // NOI18N
195
}
196             return name;
197         }
198
199         /** Setter for name of the field.
200         * @param name the name of the field
201         */

202         public synchronized void setName(Identifier name) {
203             Identifier old = this.name;
204             this.name = name;
205             firePropertyChange (PROP_NAME, old, name);
206         }
207     
208         public void markCurrent(boolean after) {
209             ClassElement decl = ((MemberElement)element).getDeclaringClass();
210             if (decl == null) {
211                 throw new IllegalStateException JavaDoc();
212             }
213             ((ClassElement.Memory)decl.getCookie(ClassElement.Memory.class)).markCurrent(element, after);
214         }
215     }
216 }
217
Popular Tags