1 24 25 package org.aspectj.tools.ide; 26 import java.util.Enumeration ; 27 import java.io.IOException ; 28 import java.io.ObjectOutputStream ; 29 import java.io.Serializable ; 30 31 32 import java.util.*; 33 34 import java.util.Vector ; 37 38 public class Declaration implements Serializable { 39 private int beginLine; 40 private int endLine; 41 private int beginColumn; 42 private int endColumn; 43 44 private String modifiers; 45 private String fullSignature; 46 private String signature; 47 private String crosscutDesignator; 48 49 private String packageName; 50 51 private String kind; 52 private String declaringType; 53 54 private String filename; 55 private String formalComment; 56 57 private Declaration[] declarations; 58 59 private Handle crosscutDeclarationHandle; 60 private Handle[] pointedToByHandles; 61 private Handle[] pointsToHandles; 62 63 transient private Declaration crosscutDeclaration; 64 transient private Declaration[] pointedToBy = null; 65 transient private Declaration[] pointsTo = null; 66 67 private Declaration parentDeclaration = null; 68 69 public Declaration(int beginLine, int endLine, int beginColumn, int endColumn, 70 String modifiers, String signature, String fullSignature, 71 String crosscutDesignator, 72 String declaringType, String kind, 73 String filename, String formalComment, 74 String packageName) 75 { 76 this.beginLine = beginLine; 77 this.endLine = endLine; 78 this.beginColumn = beginColumn; 79 this.endColumn = endColumn; 80 81 this.modifiers = modifiers; 82 this.signature = signature; 83 this.fullSignature = fullSignature; 84 85 this.crosscutDesignator = crosscutDesignator; 86 87 this.declaringType = declaringType; 88 this.kind = kind; 89 90 this.filename = filename; 91 this.formalComment = formalComment; 92 93 this.packageName = packageName; 94 95 this.pointedToByHandles = new Handle[0]; 96 this.pointsToHandles = new Handle[0]; 97 this.declarations = new Declaration[0]; 99 } 100 101 public int getBeginLine() { return beginLine; } 102 public int getEndLine() { return endLine; } 103 public int getBeginColumn() { return beginColumn; } 104 public int getEndColumn() { return endColumn; } 105 106 public String getModifiers() { return modifiers; } 107 public String getFullSignature() { return fullSignature; } 108 public String getSignature() { return signature; } 109 110 public String getPackageName() { return packageName; } 111 112 public String getCrosscutDesignator() { return crosscutDesignator; } 113 114 public Declaration getParentDeclaration() { return parentDeclaration; } 115 116 public Declaration getCrosscutDeclaration() { 117 if (crosscutDeclaration == null && crosscutDeclarationHandle != null) { 118 crosscutDeclaration = crosscutDeclarationHandle.resolve(); 119 } 120 return crosscutDeclaration; 121 } 122 123 public void setCrosscutDeclaration(Declaration _crosscutDeclaration) { 124 crosscutDeclaration = _crosscutDeclaration; 125 } 126 127 public String getDeclaringType() { return declaringType; } 128 public String getKind() { 129 if (kind.startsWith("introduced-")) { 130 return kind.substring(11); 131 } else { 132 return kind; 133 } 134 } 135 136 public String getFilename() { return filename; } 137 public String getFormalComment() { return formalComment; } 138 139 public Declaration[] getDeclarations() { 140 return declarations; 141 } 142 public void setDeclarations(Declaration[] decs) { 143 declarations = decs; 144 if (decs != null) { 145 for (int i = 0; i < decs.length; i++) { 146 decs[i].parentDeclaration = this; 147 } 148 } 149 } 150 public void setPointedToBy(Declaration[] decs) { pointedToBy = decs; } 151 public void setPointsTo(Declaration[] decs) { pointsTo = decs; } 152 153 154 public Declaration[] getPointedToBy() { 155 if (pointedToBy == null) { 156 pointedToBy = resolveHandles(pointedToByHandles); 157 } 158 return pointedToBy; } 160 161 public Declaration[] getPointsTo() { 162 if (pointsTo == null) { 163 pointsTo = resolveHandles(pointsToHandles); 164 } 165 return pointsTo; } 167 168 private Declaration[] filterTypes(Declaration[] a_decs) { 169 List decs = new LinkedList(Arrays.asList(a_decs)); 170 for(Iterator i = decs.iterator(); i.hasNext(); ) { 171 Declaration dec = (Declaration)i.next(); 172 if (!dec.isType()) i.remove(); 173 } 174 return (Declaration[])decs.toArray(new Declaration[decs.size()]); 175 } 176 177 178 public Declaration[] getTargets() { 179 Declaration[] pointsTo = getPointsTo(); 180 181 if (kind.equals("advice")) { 182 return pointsTo; 183 } else if (kind.equals("introduction")) { 184 return filterTypes(pointsTo); 185 } else { 186 return new Declaration[0]; 187 } 188 } 189 190 private Handle getHandle() { 192 return new Handle(filename, beginLine, beginColumn); 193 } 194 195 private Declaration[] resolveHandles(Handle[] handles) { 196 Declaration[] declarations = new Declaration[handles.length]; 197 int missed = 0; 198 for(int i=0; i<handles.length; i++) { 199 declarations[i] = handles[i].resolve(); 201 if (declarations[i] == null) missed++; 202 } 203 if (missed > 0) { 204 Declaration[] decs = new Declaration[declarations.length - missed]; 205 for (int i=0, j=0; i < declarations.length; i++) { 206 if (declarations[i] != null) decs[j++] = declarations[i]; 207 } 208 declarations = decs; 209 } 210 return declarations; 211 } 212 213 private Handle[] getHandles(Declaration[] declarations) { 214 Handle[] handles = new Handle[declarations.length]; 215 for(int i=0; i<declarations.length; i++) { 216 handles[i] = declarations[i].getHandle(); 218 } 219 return handles; 220 } 221 222 private void writeObject(ObjectOutputStream out) throws IOException { 224 pointedToByHandles = getHandles(getPointedToBy()); 225 pointsToHandles = getHandles(getPointsTo()); 226 if (crosscutDeclaration != null) { 227 crosscutDeclarationHandle = crosscutDeclaration.getHandle(); 228 } 229 out.defaultWriteObject(); 230 } 231 232 public Declaration[] getCrosscutDeclarations() { 234 return getDeclarationsHelper("pointcut"); 235 } 236 237 public Declaration[] getAdviceDeclarations() { 238 return getDeclarationsHelper("advice"); 239 } 240 241 public Declaration[] getIntroductionDeclarations() { 242 return getDeclarationsHelper("introduction"); 243 } 244 245 private Declaration[] getDeclarationsHelper(String kind) { 246 Declaration[] decls = getDeclarations(); 247 List result = new ArrayList(); 248 for ( int i = 0; i < decls.length; i++ ) { 249 Declaration decl = decls[i]; 250 if ( decl.getKind().equals(kind) ) { 251 result.add(decl); 252 } 253 } 254 return (Declaration[])result.toArray(new Declaration[result.size()]); 255 } 256 257 258 public boolean isType() { 259 return getKind().equals("interface") || getKind().equals("class"); 260 } 261 262 public boolean hasBody() { 263 String kind = getKind(); 264 return kind.equals("class") || kind.endsWith("constructor") || 265 (kind.endsWith("method") && getModifiers().indexOf("abstract") == -1 && 266 getModifiers().indexOf("native") == -1); 267 } 268 269 public boolean isIntroduced() { 270 return kind.startsWith("introduced-"); 271 } 272 273 public boolean hasSignature() { 274 String kind = getKind(); 275 if ( kind.equals( "class" ) || 276 kind.equals( "interface" ) || 277 kind.equals( "initializer" ) || 278 kind.equals( "field" ) || 279 kind.equals( "constructor" ) || 280 kind.equals( "method" ) ) { 281 return true; 282 } 283 else { 284 return false; 285 } 286 } 287 288 private static class Handle implements Serializable { 289 public String filename; 290 public int line, column; 291 292 public Handle(String filename, int line, int column) { 293 this.filename = filename; 294 this.line = line; 295 this.column = column; 296 } 297 298 public Declaration resolve() { 299 SymbolManager manager = SymbolManager.getSymbolManager(); 300 return manager.getDeclarationAtPoint(filename, line, column); 301 } 302 } 303 } 304 | Popular Tags |