KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > ElementNavigator


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.netbeans.modules.javacore.jmiimpl.javamodel;
21
22 import org.netbeans.jmi.javamodel.*;
23 import org.netbeans.lib.java.parser.ASTree;
24 import org.netbeans.lib.java.parser.ParserTokens;
25 import org.netbeans.lib.java.parser.Token;
26 import org.netbeans.modules.javacore.parser.ASTProvider;
27 import java.util.Arrays JavaDoc;
28
29 /**
30  *
31  * @author Tomas Hurka
32  */

33 class ElementNavigator {
34     private int idIndexes[];
35
36     ElementNavigator(Resource r,NamedElement element) {
37         ResourceImpl impl=(ResourceImpl)r;
38         ASTProvider parser=impl.getParser();
39         ASTree tree;
40         
41         if (element instanceof Parameter)
42             tree = ((MetadataElement)element.refImmediateComposite()).getASTree();
43         else
44             tree = impl.getASTree();
45         if (parser!=null) {
46             int lastToken=tree.getLastToken();
47             int ids[]=new int[lastToken];
48             int i,idIndex=0;
49             String JavaDoc id;
50             String JavaDoc fullPackageName=null;
51
52             if (element instanceof JavaClass) {
53                 id=((JavaClass)element).getSimpleName();
54             } else if (element instanceof JavaPackage) {
55                 fullPackageName=id=element.getName();
56                 int lastDot=id.lastIndexOf('.');
57
58                 if (lastDot!=-1)
59                     id=id.substring(lastDot+1);
60             } else if (element instanceof Constructor) {
61                 id=((JavaClass)((Constructor)element).getDeclaringClass()).getSimpleName();
62             } else
63                 id=element.getName();
64
65             for (i=tree.getFirstToken();i<=lastToken;i++) {
66                 Token token=parser.getToken(i);
67                 int type=token.getType();
68                 
69                 if ((type==ParserTokens.THIS || type==ParserTokens.SUPER) && element instanceof Constructor) {
70                     Token nextToken=parser.getToken(i+1);
71                     if (nextToken.getType()==ParserTokens.L_PAR) {
72                         ids[idIndex++]=i;
73                     }
74                 } else if (type==ParserTokens.IDENTIFIER && id.equals(token.getValue())) {
75                     try {
76                         if (element instanceof Field || element instanceof CallableFeature) {
77                             Token nextToken=parser.getToken(i+1);
78
79                             if (nextToken.getType()==ParserTokens.L_PAR) {
80                                 if (element instanceof Field)
81                                     continue;
82                                 if (element instanceof Constructor) {
83                                     int offset=1;
84                                     Token prevToken;
85                                     while(true) {
86                                         prevToken=parser.getToken(i-offset);
87                                         if (prevToken.getType()!=ParserTokens.DOT)
88                                             break;
89                                         offset++;
90                                         prevToken=parser.getToken(i-offset);
91                                         if (prevToken.getType()!=ParserTokens.IDENTIFIER)
92                                             break;
93                                         offset++;
94                                     }
95                                     if (prevToken.getType()!=ParserTokens.NEW)
96                                         continue;
97                                 }
98                             } else {
99                                 if (element instanceof CallableFeature)
100                                     continue;
101                             }
102                         } else if (element instanceof JavaPackage) {
103                             int offset=1;
104                             StringBuffer JavaDoc packageName=new StringBuffer JavaDoc(id);
105
106                             while(true) {
107                                 Token prevToken=parser.getToken(i-offset);
108                                 if (prevToken.getType()!=ParserTokens.DOT)
109                                     break;
110                                 offset++;
111                                 packageName.insert(0,'.');
112                                 prevToken=parser.getToken(i-offset);
113                                 if (prevToken.getType()!=ParserTokens.IDENTIFIER)
114                                     break;
115                                 offset++;
116                                 packageName.insert(0,prevToken.getValue());
117                             }
118                             if (!packageName.toString().equals(fullPackageName))
119                                 continue;
120                         }
121                     } catch (IndexOutOfBoundsException JavaDoc ex) {
122                         // we are out of token bounds
123
continue;
124                     }
125                     ids[idIndex++]=i;
126                 }
127             }
128             idIndexes=new int[idIndex];
129             System.arraycopy(ids, 0, idIndexes, 0, idIndex);
130         } else {
131             throw new IllegalArgumentException JavaDoc("ASTProvider is null"); // NOI18N
132
}
133     }
134     
135     
136     boolean containsIdentifierIn(MetadataElement element) {
137         int first,last,index;
138         ASTree tree=element.getASTree();
139         
140         if (tree==null)
141             throw new IllegalArgumentException JavaDoc("Illegal element "+element); // NOI18N
142
first=tree.getFirstToken();
143         last=tree.getLastToken();
144         index=Arrays.binarySearch(idIndexes,first);
145         if (index>=0)
146             return true;
147         index=-index-1;
148         if (index==idIndexes.length)
149             return false;
150         return idIndexes[index]<=last;
151     }
152 }
153
Popular Tags