001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 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: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.ruby.elements;
042:
043: import java.util.ArrayList;
044: import java.util.Collections;
045: import java.util.List;
046:
047: import org.netbeans.modules.gsf.api.ElementKind;
048: import org.netbeans.modules.ruby.RubyIndex;
049:
050: /**
051: * A class describing a Ruby method that is in "textual form" (signature, filename, etc.)
052: * obtained from the code index.
053: *
054: * @todo Consider computing a lot of the extra info (requires, filenames, etc.)
055: * lazily - by just stashing the index map into the RubyMethod. Hopefully Lucene
056: * won't mind if we hang on to this information...
057: * @todo Make an IndexedAttribute subclass which just has a different kind!
058: *
059: * @author Tor Norbye
060: */
061: public final class IndexedMethod extends IndexedElement implements
062: MethodElement {
063: /** This method takes a (possibly optional, see BLOCK_OPTIONAL) block */
064: public static final int BLOCK = 1 << 6;
065: /** This method takes an optional block */
066: public static final int BLOCK_OPTIONAL = 1 << 7;
067:
068: /** Deprecated? */
069: /** Parenthesis or space delimited? */
070:
071: public static enum MethodType {
072: METHOD, ATTRIBUTE, DBCOLUMN
073: };
074:
075: protected final String signature;
076: private String[] args;
077: private String name;
078: private List<String> parameters;
079: private boolean smart;
080: private boolean inherited;
081: private MethodType methodType = MethodType.METHOD;
082:
083: private IndexedMethod(String signature, RubyIndex index,
084: String fileUrl, String fqn, String clz, String require,
085: String attributes, int flags) {
086: super (index, fileUrl, fqn, clz, require, attributes, flags);
087: this .signature = signature;
088: }
089:
090: public static IndexedMethod create(RubyIndex index,
091: String signature, String fqn, String clz, String fileUrl,
092: String require, String attributes, int flags) {
093: IndexedMethod m = new IndexedMethod(signature, index, fileUrl,
094: fqn, clz, require, attributes, flags);
095:
096: return m;
097: }
098:
099: public MethodType getMethodType() {
100: return methodType;
101: }
102:
103: public void setMethodType(MethodType methodType) {
104: this .methodType = methodType;
105: }
106:
107: @Override
108: public String toString() {
109: return getSignature();
110: }
111:
112: public String getName() {
113: if (name == null) {
114: int parenIndex = signature.indexOf('(');
115:
116: if (parenIndex == -1) {
117: name = signature;
118: } else {
119: name = signature.substring(0, parenIndex);
120: }
121: }
122:
123: return name;
124: }
125:
126: @Override
127: public String getSignature() {
128: return fqn + "#" + signature;
129: }
130:
131: public String[] getArgs() {
132: if (args == null) {
133: // Parse signature
134: int parenIndex = signature.indexOf('(');
135:
136: if (parenIndex == -1) {
137: return new String[0];
138: }
139:
140: String argsPortion = signature.substring(parenIndex + 1,
141: signature.length() - 1);
142: args = argsPortion.split(","); // NOI18N
143: }
144:
145: return args;
146: }
147:
148: public List<String> getParameters() {
149: if (parameters == null) {
150: String[] args = getArgs();
151:
152: if ((args != null) && (args.length > 0)) {
153: parameters = new ArrayList<String>(args.length);
154:
155: for (String arg : args) {
156: parameters.add(arg);
157: }
158: } else {
159: parameters = Collections.emptyList();
160: }
161: }
162:
163: return parameters;
164: }
165:
166: public boolean isDeprecated() {
167: return false;
168: }
169:
170: public ElementKind getKind() {
171: if (((name == null) && signature.startsWith("initialize(")) || // NOI18N
172: ((name != null) && name.equals("initialize"))) { // NOI18N
173:
174: return ElementKind.CONSTRUCTOR;
175: } else {
176: return ElementKind.METHOD;
177: }
178: }
179:
180: public boolean isSmart() {
181: return smart;
182: }
183:
184: public void setSmart(boolean smart) {
185: this .smart = smart;
186: }
187:
188: @Override
189: public boolean equals(Object obj) {
190: if (obj == null) {
191: return false;
192: }
193: if (getClass() != obj.getClass()) {
194: return false;
195: }
196: final IndexedMethod other = (IndexedMethod) obj;
197: if (this .signature != other.signature
198: && (this .signature == null || !this .signature
199: .equals(other.signature))) {
200: return false;
201: }
202: if (this .fqn != other.fqn
203: && (this .fqn == null || !this .fqn.equals(other.fqn))) {
204: return false;
205: }
206: if (this .flags != other.flags) {
207: return false;
208: }
209: return true;
210: }
211:
212: @Override
213: public int hashCode() {
214: int hash = 7;
215: hash = 53
216: * hash
217: + (this .signature != null ? this .signature.hashCode()
218: : 0);
219: hash = 53 * hash + (this .fqn != null ? this .fqn.hashCode() : 0);
220: hash = 53 * hash + flags;
221: return hash;
222: }
223:
224: public boolean isInherited() {
225: return inherited;
226: }
227:
228: public void setInherited(boolean inherited) {
229: this .inherited = inherited;
230: }
231:
232: public boolean hasBlock() {
233: return (flags & BLOCK) != 0;
234: }
235:
236: public boolean isBlockOptional() {
237: return (flags & BLOCK_OPTIONAL) != 0;
238: }
239:
240: public static String decodeFlags(int flags) {
241: StringBuilder sb = new StringBuilder();
242: sb.append(IndexedElement.decodeFlags(flags));
243:
244: if ((flags & BLOCK) != 0) {
245: sb.append("|BLOCK");
246: }
247: if ((flags & BLOCK_OPTIONAL) != 0) {
248: sb.append("|BLOCK_OPTIONAL");
249: }
250: if (sb.length() > 0) {
251: sb.append("|");
252: }
253:
254: return sb.toString();
255: }
256:
257: public String getEncodedAttributes() {
258: return attributes;
259: }
260: }
|