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.

FilteredDecList.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.ConstructorDec;
  24. import org.aspectj.compiler.base.ast.Dec;
  25. import org.aspectj.compiler.base.ast.FieldDec;
  26. import org.aspectj.compiler.base.ast.MethodDec;
  27. import org.aspectj.compiler.base.ast.Type;
  28. import org.aspectj.compiler.base.ast.TypeDec;
  29. import org.aspectj.compiler.crosscuts.ast.PointcutDec;
  30. import java.util.ArrayList;
  31. /**
  32. * Factory collection with Type-specific "add" methods which
  33. * checks if ..Dec should be included and if so constructs and adds.
  34. * A Dec should be included if AccessChecker.canAccess({Dec variants})
  35. * and the type is the same (if type is specified).
  36. * This consolidates construction of DocImpl for classes and class members.
  37. */
  38. public class FilteredDecList extends ArrayList {
  39. final protected AccessChecker filter ;
  40. final protected ClassDocImpl classDocImpl ;
  41. final protected Type declaringType ;
  42. /**
  43. * Create a declaration list enforced by a filter and
  44. * optionally a ClassDocImpl. If the ClassDocImpl is not null,
  45. * then you cannot add members outside its type; if it is null,
  46. * attempting to add members has undefined results. (You may
  47. * still create ClassDoc.)
  48. * @param filter the AccessChecker used to test for inclusion
  49. * (use AccessChecker.PUBLIC if null)
  50. * @param type the classDocImpl used to construct added members
  51. * and ensure they are declared in this type
  52. * (ignore test if null)
  53. */
  54. FilteredDecList(AccessChecker filter, ClassDocImpl classDocImpl) {
  55. this.filter = (null != filter ? filter
  56. : AccessChecker.PUBLIC);
  57. this.classDocImpl = classDocImpl;
  58. TypeDec td = (null == classDocImpl? null
  59. : classDocImpl.typeDec());
  60. this.declaringType = (null == td? null : td.getType());
  61. }
  62. /**
  63. * Check for match with our type (if set)
  64. * @param dec the Dec to get the incoming declaring type from
  65. * @return true unless our type is set and does not equals dec declaring type
  66. */
  67. protected final boolean sameType(Dec dec) {
  68. Type other = (null == dec ? null : dec.getDeclaringType());
  69. boolean result = ((null != dec)
  70. && ((declaringType == null)
  71. || declaringType.equals(other))) ;
  72. /*
  73. System.err.println("sameType("+dec+") " + declaringType
  74. + " ?= " + other);
  75. if (!result) {
  76. System.err.println("false FilteredDecList.sameType(" + dec
  77. + ") us " + declaringType);
  78. }
  79. */
  80. return result;
  81. }
  82. /**
  83. * Implements policy on incoming TypeDec.
  84. * @param dec the TypeDec to check
  85. * @throws IllegalArgumentException if null == dec
  86. */
  87. protected void checkDec(Dec dec) {
  88. if (null == dec) throw new IllegalArgumentException("null dec");
  89. }
  90. /**
  91. * Construct and add inner class from dec
  92. * if outer is included and dec should be included.
  93. * @param outer the ClassDocImpl which encloses this dec
  94. * @param dec the TypeDec for the inner class to add to this list, enclosed by outer
  95. * @return false if dec is null or true if added
  96. * @throws IllegalArgumentException if outer is null or dec is null
  97. */
  98. public boolean add(ClassDocImpl outer, TypeDec dec) {
  99. checkDec(dec);
  100. if (null == outer) throw new IllegalArgumentException("null outer");
  101. if ((filter.canAccess(outer.typeDec()) && filter.canAccess(dec))) {
  102. ClassDocImpl doc = ClassDocImpl.getInstance(outer, dec);
  103. if (null != doc) {
  104. doc.setIncluded(true);
  105. return add((Object) doc);
  106. }
  107. }
  108. denied(outer, dec);
  109. return false;
  110. }
  111. /**
  112. * Add ClassDocImpl if dec should be included
  113. * and ClassDocImpl.getInstance(..) returns something.
  114. * Also sets the included property to true;
  115. * @param dec the TypeDec for the class to add
  116. * @return false if dec is null or true if added
  117. * @throws IllegalArgumentException if outer is null or dec is null
  118. */
  119. public boolean add(TypeDec dec) {
  120. checkDec(dec);
  121. if (filter.canAccess(dec)) {
  122. ClassDocImpl doc = ClassDocImpl.getInstance(dec);
  123. if (null != doc) {
  124. doc.setIncluded(true);
  125. return add((Object) doc);
  126. }
  127. }
  128. denied(dec);
  129. return false;
  130. }
  131. /**
  132. * Add MethodDocImpl to this list if dec should be included
  133. * @param dec the MethodDoc for the method to add
  134. * @return true if added
  135. * @throws IllegalArgumentException if dec is null
  136. */
  137. public boolean add(MethodDec dec) {
  138. checkDec(dec);
  139. if (sameType(dec) && filter.canAccess(dec)) {
  140. return add((Object) new MethodDocImpl(classDocImpl, dec));
  141. }
  142. denied(dec);
  143. return false;
  144. }
  145. /**
  146. * Add ConstructorDocImpl to this list if dec should be included
  147. * @param dec the ConstructorDoc for the constructor to add
  148. * @return true if added
  149. * @throws IllegalArgumentException if dec is null
  150. */
  151. public boolean add(ConstructorDec dec) {
  152. checkDec(dec);
  153. if (sameType(dec) && filter.canAccess(dec)) {
  154. return add((Object) new ConstructorDocImpl(classDocImpl, dec));
  155. }
  156. denied(dec);
  157. return false;
  158. }
  159. /**
  160. * Add FieldDocImpl to this list if dec should be included
  161. * @param dec the FieldDoc for the field to add
  162. * @return true if added
  163. * @throws IllegalArgumentException if dec is null
  164. */
  165. public boolean add(FieldDec dec) {
  166. checkDec(dec);
  167. if (sameType(dec) && filter.canAccess(dec)) {
  168. return add((Object) new FieldDocImpl(classDocImpl, dec));
  169. }
  170. denied(dec);
  171. return false;
  172. }
  173. /**
  174. * Add PointcutDocImpl to this list if dec should be included
  175. * @param dec the PointcutDoc for the pointcut to add
  176. * @return true if added
  177. * @throws IllegalArgumentException if dec is null
  178. */
  179. public boolean add(PointcutDec dec) {
  180. checkDec(dec);
  181. if (sameType(dec) && filter.canAccess(dec)) {
  182. return add((Object) new PointcutDocImpl(classDocImpl, dec));
  183. }
  184. denied(dec);
  185. return false;
  186. }
  187. /**
  188. * Called when some dec and outer is denied addition.
  189. * Currently does nothing.
  190. * @param outer the ClassDocImpl which encloses this dec
  191. * @param dec the TypeDec for the inner class to add to this list, enclosed by outer
  192. */
  193. protected void denied(ClassDocImpl outer, TypeDec dec) {
  194. // System.err.println(this + " denied " + o + " with " + p);
  195. }
  196. /**
  197. * signalled when some dec is denied addition.
  198. * Currently does nothing.
  199. * @param dec the Dec denied addition
  200. */
  201. protected void denied(Dec dec) {
  202. // System.err.println(this + " denied " + o);
  203. }
  204. } // class FilteredDecList