1 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 ; 28 29 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 id; 50 String 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 packageName=new StringBuffer (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 ex) { 122 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 ("ASTProvider is null"); } 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 ("Illegal element "+element); 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 |