001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039: package org.netbeans.modules.php.editor.parser.astnodes;
040:
041: /**
042: * Base class for class member declarations
043: */
044: public abstract class BodyDeclaration extends Statement {
045:
046: private int modifier;
047:
048: public BodyDeclaration(int start, int end, int modifier,
049: boolean shouldComplete) {
050: super (start, end);
051:
052: this .modifier = (shouldComplete ? completeModifier(modifier)
053: : modifier);
054: }
055:
056: public BodyDeclaration(int start, int end, int modifier) {
057: this (start, end, modifier, false);
058: }
059:
060: public String getModifierString() {
061: return Modifier.toString(modifier);
062: }
063:
064: public int getModifier() {
065: return modifier;
066: }
067:
068: /**
069: * Complets the modidifer to public if needed
070: * @param mod
071: */
072: private static int completeModifier(int mod) {
073: if (!BodyDeclaration.Modifier.isPrivate(mod)
074: && !BodyDeclaration.Modifier.isProtected(mod)) {
075: mod |= BodyDeclaration.Modifier.PUBLIC;
076: }
077: return mod;
078: }
079:
080: /**
081: * This is a utility for member modifiers
082: */
083: public static class Modifier {
084:
085: /**
086: * The <code>int</code> value representing the <code>public</code> modifier.
087: */
088: public static final int PUBLIC = 0x00000001;
089: /**
090: * The <code>int</code> value representing the <code>private</code> modifier.
091: */
092: public static final int PRIVATE = 0x00000002;
093: /**
094: * The <code>int</code> value representing the <code>protected</code> modifier.
095: */
096: public static final int PROTECTED = 0x00000004;
097: /**
098: * The <code>int</code> value representing the <code>static</code> modifier.
099: */
100: public static final int STATIC = 0x00000008;
101: /**
102: * The <code>int</code> value representing the <code>final</code> modifier.
103: */
104: public static final int FINAL = 0x00000010;
105: /**
106: * The <code>int</code> value representing the <code>abstract</code> modifier.
107: */
108: public static final int ABSTRACT = 0x00000400;
109:
110: /**
111: * Return <tt>true</tt> if the integer argument includes the
112: * <tt>public</tt> modifer, <tt>false</tt> otherwise.
113: *
114: * @param mod a set of modifers
115: * @return <tt>true</tt> if <code>mod</code> includes the
116: * <tt>public</tt> modifier; <tt>false</tt> otherwise.
117: */
118: public static boolean isPublic(int mod) {
119: return (mod & PUBLIC) != 0;
120: }
121:
122: /**
123: * Return <tt>true</tt> if the integer argument includes the
124: * <tt>private</tt> modifer, <tt>false</tt> otherwise.
125: *
126: * @param mod a set of modifers
127: * @return <tt>true</tt> if <code>mod</code> includes the
128: * <tt>private</tt> modifier; <tt>false</tt> otherwise.
129: */
130: public static boolean isPrivate(int mod) {
131: return (mod & PRIVATE) != 0;
132: }
133:
134: /**
135: * Return <tt>true</tt> if the integer argument includes the
136: * <tt>protected</tt> modifer, <tt>false</tt> otherwise.
137: *
138: * @param mod a set of modifers
139: * @return <tt>true</tt> if <code>mod</code> includes the
140: * <tt>protected</tt> modifier; <tt>false</tt> otherwise.
141: */
142: public static boolean isProtected(int mod) {
143: return (mod & PROTECTED) != 0;
144: }
145:
146: /**
147: * Return <tt>true</tt> if the integer argument includes the
148: * <tt>static</tt> modifer, <tt>false</tt> otherwise.
149: *
150: * @param mod a set of modifers
151: * @return <tt>true</tt> if <code>mod</code> includes the
152: * <tt>static</tt> modifier; <tt>false</tt> otherwise.
153: */
154: public static boolean isStatic(int mod) {
155: return (mod & STATIC) != 0;
156: }
157:
158: /**
159: * Return <tt>true</tt> if the integer argument includes the
160: * <tt>final</tt> modifer, <tt>false</tt> otherwise.
161: *
162: * @param mod a set of modifers
163: * @return <tt>true</tt> if <code>mod</code> includes the
164: * <tt>final</tt> modifier; <tt>false</tt> otherwise.
165: */
166: public static boolean isFinal(int mod) {
167: return (mod & FINAL) != 0;
168: }
169:
170: /**
171: * Return <tt>true</tt> if the integer argument includes the
172: * <tt>abstract</tt> modifer, <tt>false</tt> otherwise.
173: *
174: * @param mod a set of modifers
175: * @return <tt>true</tt> if <code>mod</code> includes the
176: * <tt>abstract</tt> modifier; <tt>false</tt> otherwise.
177: */
178: public static boolean isAbstract(int mod) {
179: return (mod & ABSTRACT) != 0;
180: }
181:
182: public static String toString(int mod) {
183: StringBuffer sb = new StringBuffer();
184:
185: if ((mod & PUBLIC) != 0) {
186: sb.append("public "); //$NON-NLS-1$
187: }
188: if ((mod & PROTECTED) != 0) {
189: sb.append("protected "); //$NON-NLS-1$
190: }
191: if ((mod & PRIVATE) != 0) {
192: sb.append("private "); //$NON-NLS-1$
193: }
194:
195: //Canonical order
196: if ((mod & ABSTRACT) != 0) {
197: sb.append("abstract "); //$NON-NLS-1$
198: }
199: if ((mod & STATIC) != 0) {
200: sb.append("static "); //$NON-NLS-1$
201: }
202: if ((mod & FINAL) != 0) {
203: sb.append("final "); //$NON-NLS-1$
204: }
205:
206: int len;
207: if ((len = sb.length()) > 0) { /* trim trailing space */
208: return sb.toString().substring(0, len - 1);
209: }
210: return ""; //$NON-NLS-1$
211: }
212: }
213: }
|