source: tags/0.00.5/j/src/org/armedbear/j/JavaTag.java

Last change on this file was 225, checked in by piso, 23 years ago

Added support for explicit tags in constructors.

File size: 6.8 KB
Line 
1/*
2 * JavaTag.java
3 *
4 * Copyright (C) 2002 Peter Graves
5 * $Id: JavaTag.java,v 1.2 2002-11-10 01:05:48 piso Exp $
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21
22package org.armedbear.j;
23
24import java.util.StringTokenizer;
25
26public final class JavaTag extends LocalTag
27{
28    private final JavaClass parent;
29
30    public JavaTag(String name, Position pos, int type, int flags)
31    {
32        this(name, pos, type, flags, null);
33    }
34
35    public JavaTag(String name, Position pos, int type, int flags,
36        JavaClass parent)
37    {
38        super(name, pos, type, flags);
39        this.parent = parent;
40        switch (type) {
41            case TAG_METHOD:
42                canonicalSignature = parseCanonicalSignatureForMethod();
43                break;
44            case TAG_EXPLICIT:
45                canonicalSignature = pos.getLine().trim();
46                break;
47        }
48    }
49
50    public final JavaClass getParent()
51    {
52        return parent;
53    }
54
55    public String getMethodName()
56    {
57        return getShortName();
58    }
59
60    private String getShortName()
61    {
62        int index = name.lastIndexOf('.');
63        if (index >= 0)
64            return name.substring(index+1);
65        else
66            return name;
67    }
68
69    public String getLongName()
70    {
71        if (name.startsWith("class "))
72            return name;
73        if (canonicalSignature != null)
74            return canonicalSignature;
75        if (getType() == TAG_FIELD) {
76            // Since we don't save information about fields in tag files, we
77            // don't need to initialize a field's canonical signature in the
78            // JavaTag constructor.
79            return canonicalSignature = parseCanonicalSignatureForField();
80        }
81        String s = signature.trim();
82        // Strip comment if any.
83        int index = s.indexOf("//");
84        if (index >= 0)
85            s = s.substring(0, index).trim();
86        index = s.indexOf('=');
87        if (index >= 0)
88            s = s.substring(0, index).trim();
89        index = s.indexOf(';');
90        if (index >= 0)
91            s = s.substring(0, index).trim();
92        return s;
93    }
94
95    public String getClassName()
96    {
97        if (parent != null)
98            return parent.getName();
99        return null;
100    }
101
102    private String parseCanonicalSignatureForField()
103    {
104        String s = signature.trim();
105        // Strip comment if any.
106        int index = s.indexOf("//");
107        if (index >= 0)
108            s = s.substring(0, index).trim();
109        // There might be multiple declarations on the same line:
110        //     "private int x = 1, y = 2, z = 3;"
111        index = s.indexOf(',');
112        if (index >= 0)
113            s = s.substring(0, index).trim();
114        index = s.indexOf('=');
115        if (index >= 0)
116            s = s.substring(0, index).trim();
117        index = s.indexOf(';');
118        if (index >= 0)
119            s = s.substring(0, index).trim();
120        // If there were multiple declarations on the same line, what we have
121        // now is basically the first of them ("private int x"), even though
122        // we might actually want one of the others. So we copy all but the
123        // last token and then append the short name of this tag. This also
124        // lets us convert tabs to spaces and remove excess whitespace from
125        // the canonical signature.
126        StringTokenizer st = new StringTokenizer(s);
127        int count = st.countTokens();
128        FastStringBuffer sb = new FastStringBuffer();
129        for (int i = 0; i < count-1; i++) {
130            sb.append(st.nextToken());
131            sb.append(' ');
132        }
133        sb.append(getShortName());
134        return sb.toString();
135    }
136
137    private String parseCanonicalSignatureForMethod()
138    {
139        FastStringBuffer sb = new FastStringBuffer();
140        Position pos = getPosition().copy();
141        pos.setOffset(0);
142        while (Character.isWhitespace(pos.getChar()))
143            if (!pos.next())
144                return null;
145        char lastChar = 0;
146        char c;
147        while ((c = pos.getChar()) != ')') {
148            if (c == '/') {
149                if (!pos.next())
150                    return null;
151                skipComment(pos);
152                if (pos.atEnd())
153                    return null;
154                continue;
155            }
156            if (c == '\n' || c == '\t')
157                c = ' ';
158            if (c != ' ' || lastChar != ' ') {
159                sb.append(c);
160                lastChar = c;
161            }
162            if (!pos.next())
163                return null;
164        }
165        if (c == ')')
166            sb.append(c);
167        return sb.toString();
168    }
169
170    // On entry, pos points at second char of "//" or "/*" (which might not
171    // actually be '/' or '*').
172    private static void skipComment(Position pos)
173    {
174        char c = pos.getChar();
175        if (!pos.next())
176            return;
177        if (c == '/') {
178            while ((c = pos.getChar()) != '\n')
179                if (!pos.next())
180                    return;
181            pos.next();
182        } else if (c == '*') {
183            while (!pos.lookingAt("*/"))
184                if (!pos.next())
185                    return;
186            pos.skip(2);
187        }
188    }
189
190    public String getSidebarText()
191    {
192        switch (getType()) {
193            case TAG_EXTENDS:
194                return "extends ".concat(getMethodName());
195            case TAG_IMPLEMENTS:
196                return "implements ".concat(getMethodName());
197            default:
198                return getMethodName();
199        }
200    }
201
202    public String getToolTipText()
203    {
204        switch (getType()) {
205            case TAG_EXTENDS:
206                return "class ".concat(getMethodName());
207            case TAG_IMPLEMENTS:
208                return "interface ".concat(getMethodName());
209            default:
210                return getLongName();
211        }
212    }
213
214    public void gotoTag(Editor editor)
215    {
216        switch (getType()) {
217            case TAG_EXTENDS:
218            case TAG_IMPLEMENTS:
219                if (!TagCommands.findClass(editor, getMethodName(), false))
220                    super.gotoTag(editor);
221                break;
222            default:
223                super.gotoTag(editor);
224                break;
225        }
226    }
227}
Note: See TracBrowser for help on using the repository browser.