1 22 package org.aspectj.tools.ajdoc; 23 24 import org.aspectj.compiler.base.ast.Constructor; 25 import org.aspectj.compiler.base.ast.ConstructorDec; 26 import org.aspectj.compiler.base.ast.Exprs; 27 import org.aspectj.compiler.base.ast.Field; 28 import org.aspectj.compiler.base.ast.FieldDec; 29 import org.aspectj.compiler.base.ast.Formals; 30 import org.aspectj.compiler.base.ast.Method; 31 import org.aspectj.compiler.base.ast.MethodDec; 32 import org.aspectj.compiler.base.ast.NameType; 33 import org.aspectj.compiler.base.ast.Type; 34 import org.aspectj.compiler.base.ast.TypeDec; 35 import org.aspectj.compiler.crosscuts.ast.PointcutDec; 36 import org.aspectj.compiler.crosscuts.ast.PointcutSO; 37 38 import com.sun.javadoc.Doc; 39 import com.sun.javadoc.ExecutableMemberDoc; 40 import com.sun.javadoc.MemberDoc; 41 import com.sun.javadoc.Parameter; 42 import com.sun.javadoc.Tag; 43 44 import java.io.File ; 45 import java.io.FileInputStream ; 46 import java.io.IOException ; 47 import java.io.InputStream ; 48 import java.util.ArrayList ; 49 import java.util.Collections ; 50 import java.util.Comparator ; 51 import java.util.Iterator ; 52 import java.util.List ; 53 import java.util.Locale ; 54 import java.util.StringTokenizer ; 55 56 61 public class Util { 62 63 69 public final static boolean start(char c) { 70 return Character.isJavaIdentifierStart(c); 71 } 72 73 79 public final static boolean ident(char c) { 80 return Character.isJavaIdentifierPart(c); 81 } 82 83 89 public final static boolean space(char c) { 90 return Character.isWhitespace(c); 91 } 92 93 99 public final static boolean newline(char c) { 100 return c == '\n' || c == '\r'; 101 } 102 103 109 public static String [] split(String str) { 110 String [] strs = new String [2]; 111 for (int i = 0; i < str.length(); i++) { 112 if (space(str.charAt(i))) { 113 strs[0] = str.substring(0, i); 114 strs[1] = str.substring(i+1); 115 break; 116 } 117 } 118 if (strs[0] == null) { 119 strs[0] = str; 120 strs[1] = ""; 121 } 122 return strs; 123 } 124 125 135 public final static Tag[] inlineTags(Doc doc, 136 String str, 137 Locale loc, 138 ErrPrinter err) { 139 if (str == null || str.length() < 1) { 140 return new Tag[0]; 141 } 142 143 int N = str.length(); 144 145 List list = new ArrayList (); 146 147 int i = 0; 148 for (int j = i; i < N; j = i) { 149 150 int ileft = str.indexOf("{@", i); 152 153 if (ileft == -1) { 156 list.add(new TagImpl(doc, "Text", 157 str.substring(i), 158 loc, err)); 159 break; 160 } 161 162 if (j < ileft) { 163 list.add(new TagImpl(doc, "Text", 164 str.substring(j, ileft), 165 loc, err)); 166 } 167 168 i = ileft; 171 if (i+7 < N && 172 str.substring(i+2, i+7).toLowerCase().equals("link ")) { 173 i += 7; 174 for (; str.charAt(i) != '}'; i++) { 175 if (i == N-1) { 176 err.error("tag_unterminated_link_tag", 177 str.substring(i)); 178 break; 179 } 180 } 181 list.add(new SeeTagImpl(doc, "@link", 182 str.substring(ileft+7, i), 183 loc, err)); 184 } else { 185 err.error("tag_invalid_link_tag", 186 str.substring(i)); 187 } 188 189 i += 1; 191 } 192 return (Tag[])list.toArray(new Tag[list.size()]); 193 } 194 195 205 public final static Tag[] firstSentenceTags(Doc doc, 206 String str, 207 Locale loc, 208 ErrPrinter err) { 209 return inlineTags(doc, firstSentenceText(str, loc, err), loc, err); 210 } 211 212 222 private static String firstSentenceText(String str, 223 Locale loc, 224 ErrPrinter err) { 225 if (str == null || loc == null || !loc.equals(Locale.US)) { 226 return ""; 227 } 228 final int N = str.length(); 229 int i; 230 for (i = 0; i < N; i++) { 231 232 if (str.charAt(i) == '.') { 236 if (i == N-1) { 237 return str.substring(0, i+1); 238 } 239 if (space(str.charAt(i+1))) { 240 return str.substring(0, i+2); 241 } 242 } 243 244 if (str.charAt(i) == '<') { 248 int j = i+1; 249 250 while (j < N && str.charAt(j) != '>') j++; 252 253 if (j == N) { 255 err.error("unterminated_html_tag", str); 256 return str; 257 } 258 259 String innards = str.substring(i+1, j).trim().toLowerCase(); 261 if (innards.equals("p") || innards.equals("pre") || 262 innards.equals("h1") || innards.equals("h2") || 263 innards.equals("h3") || innards.equals("h4") || 264 innards.equals("h5") || innards.equals("h6") || 265 innards.equals("hr")) { 266 return str.substring(0, i+1); 267 } 268 } 269 } 270 return str; 271 } 272 273 283 public final static List findTags(Doc doc, 284 String str, 285 Locale loc, 286 ErrPrinter err) { 287 288 boolean newline = true; 290 List result = new ArrayList (); 291 if (str == null) return result; 292 final int N = str.length(); 293 int lastTag = -1; 294 for (int i = 0; i < N; i++) { 295 if (newline(str.charAt(i))) { 296 newline = true; 297 } else if (space(str.charAt(i)) && newline) { 304 } else if (str.charAt(i) == '@' && newline) { 305 if (lastTag != -1) { 306 result.add(parse(doc, str.substring(lastTag, i), 307 loc, err)); 308 } 309 lastTag = i; 310 } else { 311 newline = false; 312 } 313 } 314 if (lastTag != -1) { 315 result.add(parse(doc, str.substring(lastTag), 316 loc, err)); 317 } 318 return result; 319 } 320 321 private final static Tag parse(Doc doc, 322 String str, 323 Locale loc, 324 ErrPrinter err) { 325 Tag result = null; 326 String [] split = split(str); 327 String name = split[0]; 328 String rest = split[1]; 329 if (name.equals("@see")) { 330 result = new SeeTagImpl(doc, name, rest, loc, err); 331 } else if (name.equals("@exception") || name.equals("@throws")) { 332 result = new ThrowsTagImpl(doc, name, rest, loc, err); 333 } else if (name.equals("@serialField")) { 334 result = new SerialFieldTagImpl(doc, name, rest, loc, err); 335 } else if (name.equals("@param")) { 336 result = new ParamTagImpl(doc, name, rest, loc, err); 337 } else { 338 result = new TagImpl(doc, name, rest, loc, err); 339 } 340 return result; 341 } 342 343 351 public final static String rawCommentText(String str) { 352 if (str == null) return ""; 353 if (str.length() < 3) return ""; 354 String withstars = ""; 355 int islash = str.indexOf('/'); 356 if (islash == -1 || islash+2 >= str.length()) { 357 return ""; 358 } 359 if (str.charAt(islash+1) != '*' || 360 str.charAt(islash+2) != '*') { 361 return ""; 362 } 363 int start = islash+2+1; 364 while (str.charAt(start) == '*' || space(str.charAt(start))) start++; 365 int end = str.length()-2; 366 while (str.charAt(end) == '*') end--; 367 if (start != -1 && end > start) { 368 withstars = str.substring(start, end+1); 369 } 370 StringBuffer result = new StringBuffer (withstars.length()); 372 for (StringTokenizer t = new StringTokenizer (withstars, "\n", true); 373 t.hasMoreTokens();) { 374 String line = t.nextToken(); 375 if (line == null || line.length() == 0) continue; 376 int i; 377 for (i = 0; i < line.length(); i++) { 378 if (!(line.charAt(i) == '*' || 379 line.charAt(i) == ' ')) { 380 break; 381 } 382 } 383 result.append(line.substring(i)); 385 } 386 return result.toString(); 388 } 389 390 398 public final static String commentText(String rawCommentText) { 399 if (rawCommentText == null) { 401 return ""; 402 } 403 StringBuffer result = new StringBuffer (rawCommentText.length()); 404 outer: 405 for (StringTokenizer t = new StringTokenizer (rawCommentText, "\n", true); 406 t.hasMoreTokens();) { 407 String line = t.nextToken(); 408 if (line == null || line.length() == 0) continue; 409 int i; 410 for (i = 0; i < line.length(); i++) { 411 char c = line.charAt(i); 412 if (c == ' ' || c == '\t') { 413 } else if (c == '@') { 414 break outer; 415 } else { 416 result.append(line); 418 continue outer; 419 } 420 } 421 } 422 return result.toString(); 424 } 425 426 434 public final static int compareTo(Doc $, Doc _) { 435 return ($ == null || _ == null) ? -1 : $.name().compareTo(_.name()); 436 } 437 438 446 public final static String signature(Parameter[] parameters) { 447 return signature(parameters, false); 448 } 449 450 458 public final static String flatSignature(Parameter[] parameters) { 459 return signature(parameters, true); 460 } 461 462 471 public final static String signature(Parameter[] parameters, 472 boolean flatten) { 473 if (parameters == null || parameters.length == 0) { 474 return "()"; 475 } 476 StringBuffer str = new StringBuffer ((flatten ? 8 : 20) * 478 parameters.length); 479 str.append("("); 480 final int N = parameters.length; 481 for (int i = 0; i < N; i++) { 482 String typeName = parameters[i].typeName(); 483 if (flatten) { 484 int idot = typeName.lastIndexOf('.'); 485 if (idot != -1) { 486 typeName = typeName.substring(idot+1); 487 } 488 } 489 str.append(typeName + (i < N-1 ? "," : "")); 491 492 } 493 str.append(")"); 495 return str.toString(); 497 } 498 499 505 public final static boolean isIncluded(MemberDoc doc, long flags) { 506 return true; 507 } 508 509 517 public final static boolean isIncluded(TypeDec dec) { 518 if (dec == null) { 519 return false; 520 } 521 if (dec.isLocal() && dec.isAnonymous()) { 522 return false; 523 } 524 return true; } 526 527 public final static void dump(Object o, String prefix) { 529 System.err.println(">> Dumping:"+o); 530 java.lang.reflect.Method [] ms = o.getClass().getMethods(); 531 List list = new ArrayList (); 532 for (int i = 0; i < ms.length; i++) { 533 list.add(ms[i]); 534 } 535 Collections.sort(list, new Comparator () { 536 public int compare(Object o1, Object o2) { 537 return str(o1).compareTo(str(o2)); 538 } 539 public boolean equals(Object o1, Object o2) { 540 return str(o1).equals(str(o2)); 541 } 542 private String str(Object _) { 543 return (_ != null && _ instanceof java.lang.reflect.Method ) 544 ? ((java.lang.reflect.Method )_).getName() : _+""; 545 } 546 }); 547 for (Iterator i = list.iterator(); i.hasNext();) { 548 java.lang.reflect.Method m = (java.lang.reflect.Method )i.next(); 549 if (m.getParameterTypes().length == 0 && 550 m.getName().startsWith(prefix)) { 551 try { 552 System.err.println(" "+m.getName()+":"+ 553 m.invoke(o, new Object [0])); 554 } catch (Throwable _) {} 555 } 556 } 557 } 558 public final static void gets(Object o) { 559 dump(o, "get"); 560 } 561 public final static void array(Object [] os) { 562 array(os, false); 563 } 564 public final static void array(Object [] os, boolean gets) { 565 if (os == null) { 566 System.err.println("NULL"); 567 return; 568 } 569 System.err.println(os.getClass().getName()+":" + os.length); 570 for (int i = 0; i < os.length; i++) { 571 System.err.println(" [" + i +"]:" + os[i]); 572 if (gets) gets(os[i]); 573 } 574 } 575 576 584 public static String documentation(File html, ErrPrinter err) { 585 String str = ""; 586 InputStream in = null; 587 try { 588 in = new FileInputStream (html); 589 } catch (IOException ioe) { 590 err.ex(ioe, "ioexception_open", html.getAbsolutePath()); 591 return ""; 592 } 593 try { 594 byte[] bytes = new byte[in.available()]; 595 in.read(bytes, 0, bytes.length); 596 in.close(); 597 str = new String (bytes); 598 } catch (IOException ioe) { 599 err.ex(ioe, "ioexception_reading", html.getAbsolutePath()); 600 } 601 602 int[] is = new int[]{-10,-1}; 603 int i = 0; 604 final char[] chars = new char[]{'/','B','O','D','Y','>'}; 605 for (int j = 1; j >= 0; j--) { 606 nextTag: 607 for (; i != -1; i = str.indexOf('<', i+1)) { 608 nextLt: 609 for (int s = i+1, k = j; s < str.length(); s++, k++) { 610 char c = str.charAt(s); 611 if (k == chars.length) { 612 is[j] += s+2; 613 break nextTag; 614 } 615 if (!(c == chars[k] || c == (chars[k] | 0x01000000))) { 616 break nextLt; 617 } 618 } 619 } 620 } 621 if (is[0] > -1 && is[1] > -1) { 622 str = str.substring(is[1], is[0]); 623 } 624 return str; 625 } 626 627 640 public static Object invoke(Object target, String name, 641 Class [] params, Object [] args) { 642 return invoke(target.getClass(), target, name, params, args); 643 } 644 645 659 public static Object invoke(Class type, Object target, String name, 660 Class [] params, Object [] args) { 661 try { 662 java.lang.reflect.Method method = type.getDeclaredMethod(name, params); 663 method.setAccessible(true); 664 return method.invoke(target, args); 665 } catch (Exception e) { 666 e.printStackTrace(); } 668 return null; 669 } 670 671 682 public static Object access(Class type, Object target, String name) { 683 try { 684 java.lang.reflect.Field field = type.getDeclaredField(name); 685 field.setAccessible(true); 686 return field.get(target); 687 } catch (Exception e) { e.printStackTrace(); 689 } 690 return null; 691 } 692 693 710 public final static ExecutableMemberDoc executableMemberDoc 711 (ExecutableMemberDoc[] emds, 712 String name, 713 Parameter[] params) { 714 ExecutableMemberDoc result = null; 715 next: 716 for (int i = 0; i < emds.length; i++) { 717 ExecutableMemberDoc emd = emds[i]; 718 if (emd.name().equals(name) && 719 params.length == emd.parameters().length) { 720 for (int j = 0; j < params.length; j++) { 721 if (!params[j].typeName().equals 722 (emd.parameters()[j].typeName())) { 723 continue next; 724 } 725 result = emd; 726 break next; 727 } 728 } 729 } 730 return result; 731 } 732 733 749 public final static PointcutDec pointcutDec(NameType nameType, 750 String id, 751 Formals formals) { 752 PointcutDec result = null; 753 next: 754 for (Iterator i = nameType.getPointcuts().iterator(); i.hasNext();) { 755 PointcutDec md = ((PointcutSO)i.next()).getPointcutDec(); 756 if (md.getFormals().size() == formals.size() && 757 id.equals(md.getId())) { 758 for (int j = 0; j < formals.size(); j++) { 759 if (!md.getFormals().get(j).getType().getString(). 760 equals(formals.get(j).getType().getString())) { 761 continue next; 762 } 763 } 764 result = md; 765 break next; 766 } 767 } 768 return result; 769 } 770 771 772 773 789 public final static MethodDec methodDec(NameType nameType, 790 String id, 791 Formals formals) { 792 MethodDec result = null; 793 next: 794 for (Iterator i = nameType.getMethods().iterator(); i.hasNext();) { 795 MethodDec md = ((Method)i.next()).getMethodDec(); 797 if (md.getFormals().size() == formals.size() && 798 id.equals(md.getId())) { 799 for (int j = 0; j < formals.size(); j++) { 800 if (!md.getFormals().get(j).getType().getString(). 801 equals(formals.get(j).getType().getString())) { 802 continue next; 803 } 804 } 805 result = md; 806 break next; 807 } 808 } 809 return result; 810 } 811 812 829 public static PointcutDec getPointcutDec(Type type, 830 String name, 831 TypeDec typeDec, 832 boolean showError) { 833 PointcutSO so = ((NameType)type).getPointcut(name, typeDec, showError); 834 PointcutDec dec = null; 835 if (so != null) { 836 dec = (PointcutDec)so.getCorrespondingDec(); 837 } 838 return dec; 839 } 840 841 858 public static FieldDec getFieldDec(Type type, 859 String name, 860 TypeDec typeDec, 861 boolean showError) { 862 Field so = ((NameType)type).getField(name, typeDec, showError); 863 FieldDec dec = null; 864 if (so != null) { 865 dec = (FieldDec)so.getCorrespondingDec(); 866 } 867 return dec; 868 } 869 870 889 public static MethodDec getMethodDec(Type type, 890 String name, 891 TypeDec typeDec, 892 Exprs params, 893 boolean showError) { 894 Method so = ((NameType)type).getMethod(name, typeDec, params, showError); 895 MethodDec dec = null; 896 if (so != null) { 897 dec = so.getMethodDec(); 898 } 899 return dec; 900 } 901 902 921 public static ConstructorDec getConstructorDec(Type type, 922 TypeDec typeDec, 923 Exprs params, 924 boolean showError) { 925 Constructor so = ((NameType)type).getConstructor(typeDec, 926 params, showError); 927 ConstructorDec dec = null; 928 if (so != null) { 929 dec = (ConstructorDec)so.getCorrespondingDec(); 930 } 931 return dec; 932 } 933 } 934 | Popular Tags |