You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Util.java 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * This file is part of the debugger and core tools for the AspectJ(tm)
  4. * programming language; see http://aspectj.org
  5. *
  6. * The contents of this file are subject to the Mozilla Public License
  7. * Version 1.1 (the "License"); you may not use this file except in
  8. * compliance with the License. You may obtain a copy of the License at
  9. * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is AspectJ.
  17. *
  18. * The Initial Developer of the Original Code is Xerox Corporation. Portions
  19. * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
  20. * All Rights Reserved.
  21. */
  22. package org.aspectj.tools.ajdoc;
  23. import org.aspectj.compiler.base.ast.Constructor;
  24. import org.aspectj.compiler.base.ast.ConstructorDec;
  25. import org.aspectj.compiler.base.ast.Exprs;
  26. import org.aspectj.compiler.base.ast.Field;
  27. import org.aspectj.compiler.base.ast.FieldDec;
  28. import org.aspectj.compiler.base.ast.Formals;
  29. import org.aspectj.compiler.base.ast.Method;
  30. import org.aspectj.compiler.base.ast.MethodDec;
  31. import org.aspectj.compiler.base.ast.NameType;
  32. import org.aspectj.compiler.base.ast.Type;
  33. import org.aspectj.compiler.base.ast.TypeDec;
  34. import org.aspectj.compiler.crosscuts.ast.PointcutDec;
  35. import org.aspectj.compiler.crosscuts.ast.PointcutSO;
  36. import com.sun.javadoc.Doc;
  37. import com.sun.javadoc.ExecutableMemberDoc;
  38. import com.sun.javadoc.MemberDoc;
  39. import com.sun.javadoc.Parameter;
  40. import com.sun.javadoc.Tag;
  41. import java.io.File;
  42. import java.io.FileInputStream;
  43. import java.io.IOException;
  44. import java.io.InputStream;
  45. import java.util.ArrayList;
  46. import java.util.Collections;
  47. import java.util.Comparator;
  48. import java.util.Iterator;
  49. import java.util.List;
  50. import java.util.Locale;
  51. import java.util.StringTokenizer;
  52. /**
  53. * A utility class used by lots of folks.
  54. *
  55. * @author Jeff Palm
  56. */
  57. public class Util {
  58. /**
  59. * Delegate to Character.isJavaIdentifierStart(char).
  60. *
  61. * @return <code>true</code> if <code>c</code> can
  62. * start a java identifier.
  63. */
  64. public final static boolean start(char c) {
  65. return Character.isJavaIdentifierStart(c);
  66. }
  67. /**
  68. * Delegate to Character.isJavaIdentifierPart(char).
  69. *
  70. * @return <code>true</code> if <code>c</code> can
  71. * be a part of a java identifier.
  72. */
  73. public final static boolean ident(char c) {
  74. return Character.isJavaIdentifierPart(c);
  75. }
  76. /**
  77. * Delegate to Character.isWhitespace(char).
  78. *
  79. * @return <code>true</code> if <code>c</code> is
  80. * valid white space.
  81. */
  82. public final static boolean space(char c) {
  83. return Character.isWhitespace(c);
  84. }
  85. /**
  86. * Returns true is <code>c</code> is a newline character.
  87. *
  88. * @return <code>true</code> if
  89. * <code>c == '\n' || c == '\r'</code>.
  90. */
  91. public final static boolean newline(char c) {
  92. return c == '\n' || c == '\r';
  93. }
  94. /**
  95. * Returns two strings split at the first white space.
  96. *
  97. * @return an array of two Strings split at
  98. * the first white space.
  99. */
  100. public static String[] split(String str) {
  101. String[] strs = new String[2];
  102. for (int i = 0; i < str.length(); i++) {
  103. if (space(str.charAt(i))) {
  104. strs[0] = str.substring(0, i);
  105. strs[1] = str.substring(i+1);
  106. break;
  107. }
  108. }
  109. if (strs[0] == null) {
  110. strs[0] = str;
  111. strs[1] = "";
  112. }
  113. return strs;
  114. }
  115. /**
  116. * Returns the inline tags found in <code>str</code>.
  117. *
  118. * @param doc Doc to give to the new tag.
  119. * @param str String from which to create the tags.
  120. * @param loc Locale to give to the new tag.
  121. * @param err ErrPrinter to give to the new tag.
  122. * @return an array of Tag representing the inline
  123. * tags found in str.
  124. */
  125. public final static Tag[] inlineTags(Doc doc,
  126. String str,
  127. Locale loc,
  128. ErrPrinter err) {
  129. if (str == null || str.length() < 1) {
  130. return new Tag[0];
  131. }
  132. int N = str.length();
  133. List list = new ArrayList();
  134. int i = 0;
  135. for (int j = i; i < N; j = i) {
  136. // Try to match a link tag
  137. int ileft = str.indexOf("{@", i);
  138. // If there are no more link tags, return the rest
  139. // of str as a 'Text' Tag
  140. if (ileft == -1) {
  141. list.add(new TagImpl(doc, "Text",
  142. str.substring(i),
  143. loc, err));
  144. break;
  145. }
  146. if (j < ileft) {
  147. list.add(new TagImpl(doc, "Text",
  148. str.substring(j, ileft),
  149. loc, err));
  150. }
  151. // If there is a tag and it's name is 'link ' try to
  152. // match it
  153. i = ileft;
  154. if (i+7 < N &&
  155. str.substring(i+2, i+7).toLowerCase().equals("link ")) {
  156. i += 7;
  157. for (; str.charAt(i) != '}'; i++) {
  158. if (i == N-1) {
  159. err.error("tag_unterminated_link_tag",
  160. str.substring(i));
  161. break;
  162. }
  163. }
  164. list.add(new SeeTagImpl(doc, "@link",
  165. str.substring(ileft+7, i),
  166. loc, err));
  167. } else {
  168. err.error("tag_invalid_link_tag",
  169. str.substring(i));
  170. }
  171. // Don't want to include the right brace
  172. i += 1;
  173. }
  174. return (Tag[])list.toArray(new Tag[list.size()]);
  175. }
  176. /**
  177. * Returns the first sentence tags found in <code>str</code>.
  178. *
  179. * @param doc Doc to give to the new tag.
  180. * @param str String from which to create the tags.
  181. * @param loc Locale to give to the new tag.
  182. * @param err ErrPrinter to give to the new tag.
  183. * @return an array of Tag representing the first
  184. * sentence tags found in str.
  185. */
  186. public final static Tag[] firstSentenceTags(Doc doc,
  187. String str,
  188. Locale loc,
  189. ErrPrinter err) {
  190. return inlineTags(doc, firstSentenceText(str, loc, err), loc, err);
  191. }
  192. /**
  193. * Returns the first sentence tags found in <code>str</code>,
  194. * using <code>Locale.US</code> as the default locale.
  195. *
  196. * @param doc Doc to give to the new tag.
  197. * @param str String from which to create the tags.
  198. * @param err ErrPrinter to give to the new tag.
  199. * @return an array of Tag representing the first
  200. * sentence tags found in str.
  201. */
  202. private static String firstSentenceText(String str,
  203. Locale loc,
  204. ErrPrinter err) {
  205. if (str == null || loc == null || !loc.equals(Locale.US)) {
  206. return "";
  207. }
  208. final int N = str.length();
  209. int i;
  210. for (i = 0; i < N; i++) {
  211. // A period at the end of the text or a
  212. // period followed by white space is the
  213. // end of a sentence
  214. if (str.charAt(i) == '.') {
  215. if (i == N-1) {
  216. return str.substring(0, i+1);
  217. }
  218. if (space(str.charAt(i+1))) {
  219. return str.substring(0, i+2);
  220. }
  221. }
  222. // An HTML tag the signals the end -- one of:
  223. // <p> </p> <h1> <h2> <h3> <h4>
  224. // <h5> <h6> <hr> <pre> or </pre>
  225. if (str.charAt(i) == '<') {
  226. int j = i+1;
  227. // Find the closing '>'
  228. while (j < N && str.charAt(j) != '>') j++;
  229. // If there's no closing '>' signal an error
  230. if (j == N) {
  231. err.error("unterminated_html_tag", str);
  232. return str;
  233. }
  234. // Inspect the inside of the tag
  235. String innards = str.substring(i+1, j).trim().toLowerCase();
  236. if (innards.equals("p") || innards.equals("pre") ||
  237. innards.equals("h1") || innards.equals("h2") ||
  238. innards.equals("h3") || innards.equals("h4") ||
  239. innards.equals("h5") || innards.equals("h6") ||
  240. innards.equals("hr")) {
  241. return str.substring(0, i+1);
  242. }
  243. }
  244. }
  245. return str;
  246. }
  247. /**
  248. * Returns the tags found in <code>str</code>.
  249. *
  250. * @param doc Doc to give to the new tag.
  251. * @param str String from which to create the tags.
  252. * @param loc Locale to give to the new tag.
  253. * @param err ErrPrinter to give to the new tag.
  254. * @return an array of Tag representing the
  255. * tags found in str.
  256. */
  257. public final static List findTags(Doc doc,
  258. String str,
  259. Locale loc,
  260. ErrPrinter err) {
  261. //XXX This sucks!!! Will redo later.
  262. boolean newline = true;
  263. List result = new ArrayList();
  264. if (str == null) return result;
  265. final int N = str.length();
  266. int lastTag = -1;
  267. for (int i = 0; i < N; i++) {
  268. if (newline(str.charAt(i))) {
  269. newline = true;
  270. // XXX need to evaluate - some tags can span newlines?
  271. // if (lastTag != -1) { // now requiring tags not to span newlines
  272. // result.add(parse(doc, str.substring(lastTag, i),
  273. // loc, err));
  274. // }
  275. // lastTag = -1
  276. } else if (space(str.charAt(i)) && newline) {
  277. } else if (str.charAt(i) == '@' && newline) {
  278. if (lastTag != -1) {
  279. result.add(parse(doc, str.substring(lastTag, i),
  280. loc, err));
  281. }
  282. lastTag = i;
  283. } else {
  284. newline = false;
  285. }
  286. }
  287. if (lastTag != -1) {
  288. result.add(parse(doc, str.substring(lastTag),
  289. loc, err));
  290. }
  291. return result;
  292. }
  293. private final static Tag parse(Doc doc,
  294. String str,
  295. Locale loc,
  296. ErrPrinter err) {
  297. Tag result = null;
  298. String[] split = split(str);
  299. String name = split[0];
  300. String rest = split[1];
  301. if (name.equals("@see")) {
  302. result = new SeeTagImpl(doc, name, rest, loc, err);
  303. } else if (name.equals("@exception") || name.equals("@throws")) {
  304. result = new ThrowsTagImpl(doc, name, rest, loc, err);
  305. } else if (name.equals("@serialField")) {
  306. result = new SerialFieldTagImpl(doc, name, rest, loc, err);
  307. } else if (name.equals("@param")) {
  308. result = new ParamTagImpl(doc, name, rest, loc, err);
  309. } else {
  310. result = new TagImpl(doc, name, rest, loc, err);
  311. }
  312. return result;
  313. }
  314. /**
  315. * Returns the raw comment text found in <code>str</code>.
  316. *
  317. * @param str String containing comment from which
  318. * the raw comment is found.
  319. * @return String with the raw comment taken
  320. * from <code>str</code>.
  321. */
  322. public final static String rawCommentText(String str) {
  323. if (str == null) return "";
  324. if (str.length() < 3) return "";
  325. String withstars = "";
  326. int islash = str.indexOf('/');
  327. if (islash == -1 || islash+2 >= str.length()) {
  328. return "";
  329. }
  330. if (str.charAt(islash+1) != '*' ||
  331. str.charAt(islash+2) != '*') {
  332. return "";
  333. }
  334. int start = islash+2+1;
  335. while (str.charAt(start) == '*' || space(str.charAt(start))) start++;
  336. int end = str.length()-2;
  337. while (str.charAt(end) == '*') end--;
  338. if (start != -1 && end > start) {
  339. withstars = str.substring(start, end+1);
  340. }
  341. //String result = "";
  342. StringBuffer result = new StringBuffer(withstars.length());
  343. for (StringTokenizer t = new StringTokenizer(withstars, "\n", true);
  344. t.hasMoreTokens();) {
  345. String line = t.nextToken();
  346. if (line == null || line.length() == 0) continue;
  347. int i;
  348. for (i = 0; i < line.length(); i++) {
  349. if (!(line.charAt(i) == '*' ||
  350. line.charAt(i) == ' ')) {
  351. break;
  352. }
  353. }
  354. //result += line.substring(i);
  355. result.append(line.substring(i));
  356. }
  357. //return result;
  358. return result.toString();
  359. }
  360. /**
  361. * Returns the comment text from the passed in
  362. * raw comment text -- e.g. no tags at the end.
  363. *
  364. * @param rawCommentText raw comment text to search.
  365. * @return the comment text from
  366. * <code>rawCommentText</code>.
  367. */
  368. public final static String commentText(String rawCommentText) {
  369. //String result = "";
  370. if (rawCommentText == null) {
  371. return "";
  372. }
  373. StringBuffer result = new StringBuffer(rawCommentText.length());
  374. outer:
  375. for (StringTokenizer t = new StringTokenizer(rawCommentText, "\n", true);
  376. t.hasMoreTokens();) {
  377. String line = t.nextToken();
  378. if (line == null || line.length() == 0) continue;
  379. int i;
  380. for (i = 0; i < line.length(); i++) {
  381. char c = line.charAt(i);
  382. if (c == ' ' || c == '\t') {
  383. } else if (c == '@') {
  384. break outer;
  385. } else {
  386. //result += line;
  387. result.append(line);
  388. continue outer;
  389. }
  390. }
  391. }
  392. //return result;
  393. return result.toString();
  394. }
  395. /**
  396. * Compares using names.
  397. *
  398. * @param $ First Doc.
  399. * @param _ Second Doc.
  400. * @return -1 if either are null, else
  401. * <code>$.name.compareTo(_.name</code>.
  402. */
  403. public final static int compareTo(Doc $, Doc _) {
  404. return ($ == null || _ == null) ? -1 : $.name().compareTo(_.name());
  405. }
  406. /**
  407. * Returns the signature, given <code>parameters</code>,
  408. * without flattening.
  409. *
  410. * @param parameters an array of Parameter.
  411. * @return String representation of the parameters.
  412. * @see #signature(Parameter[],boolean)
  413. */
  414. public final static String signature(Parameter[] parameters) {
  415. return signature(parameters, false);
  416. }
  417. /**
  418. * Returns the signature, given <code>parameters</code>,
  419. * with flattening.
  420. *
  421. * @param parameters an array of Parameter.
  422. * @return String representation of the parameters.
  423. * @see #signature(Parameter[],boolean)
  424. */
  425. public final static String flatSignature(Parameter[] parameters) {
  426. return signature(parameters, true);
  427. }
  428. /**
  429. * Returns the signature, given <code>parameters</code>
  430. * and flattens if <code>flatten</code>.
  431. *
  432. * @param parameters an array of Parameter.
  433. * @param flatten <code>true</code> if the parameter names
  434. * should be flattened.
  435. * @return String representation of the parameters.
  436. */
  437. public final static String signature(Parameter[] parameters,
  438. boolean flatten) {
  439. if (parameters == null || parameters.length == 0) {
  440. return "()";
  441. }
  442. //String str = "(";
  443. StringBuffer str = new StringBuffer((flatten ? 8 : 20) *
  444. parameters.length);
  445. str.append("(");
  446. final int N = parameters.length;
  447. for (int i = 0; i < N; i++) {
  448. String typeName = parameters[i].typeName();
  449. if (flatten) {
  450. int idot = typeName.lastIndexOf('.');
  451. if (idot != -1) {
  452. typeName = typeName.substring(idot+1);
  453. }
  454. }
  455. //str += typeName + (i < N-1 ? "," : "");
  456. str.append(typeName + (i < N-1 ? "," : ""));
  457. }
  458. //str += ")";
  459. str.append(")");
  460. //return str;
  461. return str.toString();
  462. }
  463. /**
  464. * Returns <code>true</code> -- include all members for now.
  465. *
  466. * @param doc member to consider.
  467. * @param flags access flags.
  468. */
  469. public final static boolean isIncluded(MemberDoc doc, long flags) {
  470. return true;
  471. }
  472. /**
  473. * Returns <code>true</code> if <code>dec</code>
  474. * isn't local or annonymous or <code>null</code>.
  475. *
  476. * @param dec TypeDec to consider.
  477. * @return <code>true</code> isn't dec is local or
  478. * annonymous or <code>null</code>.
  479. */
  480. public final static boolean isIncluded(TypeDec dec) {
  481. if (dec == null) {
  482. return false;
  483. }
  484. if (dec.isLocal() && dec.isAnonymous()) {
  485. return false;
  486. }
  487. return true; //XXX to do
  488. }
  489. //XXX Debugging
  490. public final static void dump(Object o, String prefix) {
  491. System.err.println(">> Dumping:"+o);
  492. java.lang.reflect.Method[] ms = o.getClass().getMethods();
  493. List list = new ArrayList();
  494. for (int i = 0; i < ms.length; i++) {
  495. list.add(ms[i]);
  496. }
  497. Collections.sort(list, new Comparator() {
  498. public int compare(Object o1, Object o2) {
  499. return str(o1).compareTo(str(o2));
  500. }
  501. public boolean equals(Object o1, Object o2) {
  502. return str(o1).equals(str(o2));
  503. }
  504. private String str(Object _) {
  505. return (_ != null && _ instanceof java.lang.reflect.Method)
  506. ? ((java.lang.reflect.Method)_).getName() : _+"";
  507. }
  508. });
  509. for (Iterator i = list.iterator(); i.hasNext();) {
  510. java.lang.reflect.Method m = (java.lang.reflect.Method)i.next();
  511. if (m.getParameterTypes().length == 0 &&
  512. m.getName().startsWith(prefix)) {
  513. try {
  514. System.err.println(" "+m.getName()+":"+
  515. m.invoke(o, new Object[0]));
  516. } catch (Throwable _) {}
  517. }
  518. }
  519. }
  520. public final static void gets(Object o) {
  521. dump(o, "get");
  522. }
  523. public final static void array(Object[] os) {
  524. array(os, false);
  525. }
  526. public final static void array(Object[] os, boolean gets) {
  527. if (os == null) {
  528. System.err.println("NULL");
  529. return;
  530. }
  531. System.err.println(os.getClass().getName()+":" + os.length);
  532. for (int i = 0; i < os.length; i++) {
  533. System.err.println(" [" + i +"]:" + os[i]);
  534. if (gets) gets(os[i]);
  535. }
  536. }
  537. /**
  538. * Returns the HTML documentation found in <code>html</code>
  539. * using <code>err</code> to report errors.
  540. *
  541. * @param html File in which to look.
  542. * @param err ErrPrinter to use to report errors.
  543. * @return HTML documentaiton found in <code>html</code>.
  544. */
  545. public static String documentation(File html, ErrPrinter err) {
  546. String str = "";
  547. InputStream in = null;
  548. try {
  549. in = new FileInputStream(html);
  550. } catch (IOException ioe) {
  551. err.ex(ioe, "ioexception_open", html.getAbsolutePath());
  552. return "";
  553. }
  554. try {
  555. byte[] bytes = new byte[in.available()];
  556. in.read(bytes, 0, bytes.length);
  557. in.close();
  558. str = new String(bytes);
  559. } catch (IOException ioe) {
  560. err.ex(ioe, "ioexception_reading", html.getAbsolutePath());
  561. }
  562. int[] is = new int[]{-10,-1};
  563. int i = 0;
  564. final char[] chars = new char[]{'/','B','O','D','Y','>'};
  565. for (int j = 1; j >= 0; j--) {
  566. nextTag:
  567. for (; i != -1; i = str.indexOf('<', i+1)) {
  568. nextLt:
  569. for (int s = i+1, k = j; s < str.length(); s++, k++) {
  570. char c = str.charAt(s);
  571. if (k == chars.length) {
  572. is[j] += s+2;
  573. break nextTag;
  574. }
  575. if (!(c == chars[k] || c == (chars[k] | 0x01000000))) {
  576. break nextLt;
  577. }
  578. }
  579. }
  580. }
  581. if (is[0] > -1 && is[1] > -1) {
  582. str = str.substring(is[1], is[0]);
  583. }
  584. return str;
  585. }
  586. /**
  587. * Returns the result of invoking the method <code>name</code>
  588. * on <code>target</code> with parameters <code>params</code>
  589. * declared in the <code>target</code>'s class using arguments
  590. * <code>args</code>.
  591. *
  592. * @param target target Object.
  593. * @param name name of the method.
  594. * @param params array of Class of parameters of the method.
  595. * @param args array of Object of arguments to the method.
  596. * @return the result of invoking the method.
  597. * @see #invoke(Class,Object,String,Class[],Object[])
  598. */
  599. public static Object invoke(Object target, String name,
  600. Class[] params, Object[] args) {
  601. return invoke(target.getClass(), target, name, params, args);
  602. }
  603. /**
  604. * Returns the result of invoking the method <code>name</code>
  605. * on <code>target</code> with parameters <code>params</code>
  606. * declared in the <code>type</code> using arguments
  607. * <code>args</code>.
  608. * This method handles any errors that arise in doing so.
  609. *
  610. * @param type type in which the method is declared.
  611. * @param target target Object -- null for static methods.
  612. * @param name name of the method.
  613. * @param params array of Class of parameters of the method.
  614. * @param args array of Object of arguments to the method.
  615. * @return the result of invoking the method.
  616. */
  617. public static Object invoke(Class type, Object target, String name,
  618. Class[] params, Object[] args) {
  619. try {
  620. java.lang.reflect.Method method = type.getDeclaredMethod(name, params);
  621. method.setAccessible(true);
  622. return method.invoke(target, args);
  623. } catch (Exception e) {
  624. e.printStackTrace(); //TODO
  625. }
  626. return null;
  627. }
  628. /**
  629. * Returns the value of access the field <code>name</code>
  630. * declared in <code>type</code> on <code>target</code>.
  631. * This method handles any errors that arise in doing so.
  632. *
  633. * @param type type in which the field is declared.
  634. * @param target target that is currently holding the field --
  635. * null for static fields.
  636. * @param name name of the field.
  637. * @return the result of accessing this field.
  638. */
  639. public static Object access(Class type, Object target, String name) {
  640. try {
  641. java.lang.reflect.Field field = type.getDeclaredField(name);
  642. field.setAccessible(true);
  643. return field.get(target);
  644. } catch (Exception e) { //TODO
  645. e.printStackTrace();
  646. }
  647. return null;
  648. }
  649. /**
  650. * Returns the ExecutableMemberDoc from the array passed
  651. * in whose parameters <i>weakly</i> match those of
  652. * <code>params</code> and whose name matches exactly
  653. * with <code>name</code>.
  654. * This method <b>can</b> return <code>null</code>.
  655. *
  656. * @param emds an array of ExecutableMemberDoc from which
  657. * the returned value comes.
  658. * @param name the name of the member to return.
  659. * @param params an array of Parameter that represent
  660. * the parameters we're trying to match.
  661. * @return an ExecutableMemberDoc whose parameters
  662. * match the names and order found in
  663. * <code>params</code> and whose name
  664. * exactly equals <code>name</code>.
  665. */
  666. public final static ExecutableMemberDoc executableMemberDoc
  667. (ExecutableMemberDoc[] emds,
  668. String name,
  669. Parameter[] params) {
  670. ExecutableMemberDoc result = null;
  671. next:
  672. for (int i = 0; i < emds.length; i++) {
  673. ExecutableMemberDoc emd = emds[i];
  674. if (emd.name().equals(name) &&
  675. params.length == emd.parameters().length) {
  676. for (int j = 0; j < params.length; j++) {
  677. if (!params[j].typeName().equals
  678. (emd.parameters()[j].typeName())) {
  679. continue next;
  680. }
  681. result = emd;
  682. break next;
  683. }
  684. }
  685. }
  686. return result;
  687. }
  688. /**
  689. * Returns the PointcutDoc from the array passed
  690. * in whose parameters <i>weakly</i> match those of
  691. * <code>formals</code> and whose name matches exactly
  692. * with <code>id</code>.
  693. * This method <b>can</b> return <code>null</code>.
  694. *
  695. * @param nameType the type in which we're searching.
  696. * @param id the name of the pointcut to return.
  697. * @param formals the Formals whose name and order
  698. * must match to return a pointcut.
  699. * @return a PointcutDoc whose parameters
  700. * match the names and order found in
  701. * <code>formals</code> and whose name
  702. * exactly equals <code>id</code>.
  703. */
  704. public final static PointcutDec pointcutDec(NameType nameType,
  705. String id,
  706. Formals formals) {
  707. PointcutDec result = null;
  708. next:
  709. for (Iterator i = nameType.getPointcuts().iterator(); i.hasNext();) {
  710. PointcutDec md = ((PointcutSO)i.next()).getPointcutDec();
  711. if (md.getFormals().size() == formals.size() &&
  712. id.equals(md.getId())) {
  713. for (int j = 0; j < formals.size(); j++) {
  714. if (!md.getFormals().get(j).getType().getString().
  715. equals(formals.get(j).getType().getString())) {
  716. continue next;
  717. }
  718. }
  719. result = md;
  720. break next;
  721. }
  722. }
  723. return result;
  724. }
  725. /**
  726. * Returns the MethodDoc from the array passed
  727. * in whose parameters <i>weakly</i> match those of
  728. * <code>formals</code> and whose name matches exactly
  729. * with <code>id</code>.
  730. * This method <b>can</b> return <code>null</code>.
  731. *
  732. * @param nameType the type in which we're searching.
  733. * @param id the name of the method to return.
  734. * @param formals the Formals whose name and order
  735. * must match to return a method.
  736. * @return a MethodDoc whose parameters
  737. * match the names and order found in
  738. * <code>formals</code> and whose name
  739. * exactly equals <code>id</code>.
  740. */
  741. public final static MethodDec methodDec(NameType nameType,
  742. String id,
  743. Formals formals) {
  744. MethodDec result = null;
  745. next:
  746. for (Iterator i = nameType.getMethods().iterator(); i.hasNext();) {
  747. //MethodDec md = (MethodDec)i.next();
  748. MethodDec md = ((Method)i.next()).getMethodDec();
  749. if (md.getFormals().size() == formals.size() &&
  750. id.equals(md.getId())) {
  751. for (int j = 0; j < formals.size(); j++) {
  752. if (!md.getFormals().get(j).getType().getString().
  753. equals(formals.get(j).getType().getString())) {
  754. continue next;
  755. }
  756. }
  757. result = md;
  758. break next;
  759. }
  760. }
  761. return result;
  762. }
  763. /**
  764. * Returns the PointcutDec named <code>name</code>,
  765. * contained in <code>typeDec</code> with type <code>type</code>.
  766. * <code>showError</code> is passed to subsequent methods
  767. * to supress or warrant error printing.
  768. * This may return null.
  769. *
  770. * @param type Type in which this pointcut was declared.
  771. * @param name name of the pointcut.
  772. * @param typeDec TypeDec in which we're searching.
  773. * @param showError <code>true</code> is an error should
  774. * be printed upon not finding a pointcut.
  775. * @return the pointcut declared in <code>type</code>
  776. * named <code>name</code>, found by searching
  777. * from <code>typeDec</code>. This may be
  778. * null.
  779. */
  780. public static PointcutDec getPointcutDec(Type type,
  781. String name,
  782. TypeDec typeDec,
  783. boolean showError) {
  784. PointcutSO so = ((NameType)type).getPointcut(name, typeDec, showError);
  785. PointcutDec dec = null;
  786. if (so != null) {
  787. dec = (PointcutDec)so.getCorrespondingDec();
  788. }
  789. return dec;
  790. }
  791. /**
  792. * Returns the FieldDec named <code>name</code>,
  793. * contained in <code>typeDec</code> with type <code>type</code>.
  794. * <code>showError</code> is passed to subsequent methods
  795. * to supress or warrant error printing.
  796. * This may return null.
  797. *
  798. * @param type Type in which this field was declared.
  799. * @param name name of the field.
  800. * @param typeDec TypeDec in which we're searching.
  801. * @param showError <code>true</code> is an error should
  802. * be printed upon not finding a field.
  803. * @return the field declared in <code>type</code>
  804. * named <code>name</code>, found by searching
  805. * from <code>typeDec</code>. This may be
  806. * null.
  807. */
  808. public static FieldDec getFieldDec(Type type,
  809. String name,
  810. TypeDec typeDec,
  811. boolean showError) {
  812. Field so = ((NameType)type).getField(name, typeDec, showError);
  813. FieldDec dec = null;
  814. if (so != null) {
  815. dec = (FieldDec)so.getCorrespondingDec();
  816. }
  817. return dec;
  818. }
  819. /**
  820. * Returns the MethodDec named <code>name</code>, with
  821. * formals <code>params</code>,
  822. * contained in <code>typeDec</code> with type <code>type</code>.
  823. * <code>showError</code> is passed to subsequent methods
  824. * to supress or warrant error printing.
  825. * This may return null.
  826. *
  827. * @param type Type in which this method was declared.
  828. * @param name name of the method.
  829. * @param typeDec TypeDec in which we're searching.
  830. * @param params the method's formal parameters.
  831. * @param showError <code>true</code> is an error should
  832. * be printed upon not finding a method.
  833. * @return the method declared in <code>type</code>
  834. * named <code>name</code>, found by searching
  835. * from <code>typeDec</code>. This may be
  836. * null.
  837. */
  838. public static MethodDec getMethodDec(Type type,
  839. String name,
  840. TypeDec typeDec,
  841. Exprs params,
  842. boolean showError) {
  843. Method so = ((NameType)type).getMethod(name, typeDec, params, showError);
  844. MethodDec dec = null;
  845. if (so != null) {
  846. dec = so.getMethodDec();
  847. }
  848. return dec;
  849. }
  850. /**
  851. * Returns the ConstructorDec named <code>name</code>, with
  852. * formals <code>params</code>,
  853. * contained in <code>typeDec</code> with type <code>type</code>.
  854. * <code>showError</code> is passed to subsequent constructors
  855. * to supress or warrant error printing.
  856. * This may return null.
  857. *
  858. * @param type Type in which this constructor was declared.
  859. * @param name name of the constructor.
  860. * @param typeDec TypeDec in which we're searching.
  861. * @param params the constructor's formal parameters.
  862. * @param showError <code>true</code> is an error should
  863. * be printed upon not finding a constructor.
  864. * @return the constructor declared in <code>type</code>
  865. * named <code>name</code>, found by searching
  866. * from <code>typeDec</code>. This may be
  867. * null.
  868. */
  869. public static ConstructorDec getConstructorDec(Type type,
  870. TypeDec typeDec,
  871. Exprs params,
  872. boolean showError) {
  873. Constructor so = ((NameType)type).getConstructor(typeDec,
  874. params, showError);
  875. ConstructorDec dec = null;
  876. if (so != null) {
  877. dec = (ConstructorDec)so.getCorrespondingDec();
  878. }
  879. return dec;
  880. }
  881. }