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.

SimpleScopeTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import org.aspectj.weaver.UnresolvedType;
  14. import org.aspectj.weaver.World;
  15. import org.aspectj.weaver.reflect.ReflectionWorld;
  16. public class SimpleScopeTest extends PatternsTestCase {
  17. public World getWorld() {
  18. return new ReflectionWorld(true, this.getClass().getClassLoader());
  19. }
  20. public void testTestScope() {
  21. SimpleScope scope = makeTestScope();
  22. FormalBinding formalBinding = scope.lookupFormal("i");
  23. assertEquals("i", formalBinding.getName());
  24. assertEquals("I", formalBinding.getType().getSignature());
  25. formalBinding = scope.lookupFormal("string");
  26. assertEquals("string", formalBinding.getName());
  27. assertEquals("Ljava/lang/String;", formalBinding.getType().getSignature());
  28. }
  29. public void test1() {
  30. SimpleScope scope = makeTestScope();
  31. UnresolvedType unresolvedType = scope.lookupType("void", null);
  32. System.out.println(unresolvedType);
  33. }
  34. public static final String[] ZERO_STRINGS = new String[0];
  35. private TestScope makeTestScope() {
  36. // i = int
  37. // string = String
  38. return new TestScope(new String[] { "int", "java.lang.String" }, new String[] { "i", "string" }, world);
  39. }
  40. //
  41. // public void testStaticMatch() {
  42. // checkMatch("java.lang.Object", "java.lang.Object", true);
  43. // checkMatch("java.lang.Object+", "java.lang.Object", true);
  44. // checkMatch("java.lang.Object+", "java.lang.String", true);
  45. // checkMatch("java.lang.String+", "java.lang.Object", false);
  46. // checkMatch("java.lang.Integer", "java.lang.String", false);
  47. //
  48. // checkMatch("java.lang.Integer", "int", false);
  49. //
  50. // checkMatch("java.lang.Number+", "java.lang.Integer", true);
  51. //
  52. // checkMatch("java..*", "java.lang.Integer", true);
  53. // checkMatch("java..*", "java.lang.reflect.Modifier", true);
  54. // checkMatch("java..*", "int", false);
  55. // checkMatch("java..*", "javax.swing.Action", false);
  56. // checkMatch("java..*+", "javax.swing.Action", true);
  57. //
  58. // checkMatch("*.*.Object", "java.lang.Object", true);
  59. // checkMatch("*.Object", "java.lang.Object", false);
  60. // checkMatch("*..*", "java.lang.Object", true);
  61. // checkMatch("*..*", "int", false);
  62. // checkMatch("java..Modifier", "java.lang.reflect.Modifier", true);
  63. // checkMatch("java.lang.reflect.Mod..ifier", "java.lang.reflect.Modifier", false);
  64. //
  65. // checkMatch("java..reflect..Modifier", "java.lang.reflect.Modifier", true);
  66. // checkMatch("java..lang..Modifier", "java.lang.reflect.Modifier", true);
  67. // checkMatch("java..*..Modifier", "java.lang.reflect.Modifier", true);
  68. // checkMatch("java..*..*..Modifier", "java.lang.reflect.Modifier", true);
  69. // checkMatch("java..*..*..*..Modifier", "java.lang.reflect.Modifier", false);
  70. // // checkMatch("java..reflect..Modifier", "java.lang.reflect.Modxifier", false);
  71. // checkMatch("ja*va..Modifier", "java.lang.reflect.Modifier", true);
  72. // checkMatch("java..*..Mod*ifier", "java.lang.reflect.Modifier", true);
  73. //
  74. // }
  75. //
  76. // // three levels:
  77. // // 0. defined in current compilation unit, or imported by name
  78. // // 1. defined in current package/type/whatever
  79. // // 2. defined in package imported by *
  80. // /**
  81. // * We've decided not to test this here, but rather in any compilers
  82. // */
  83. // public void testImportResolve() {
  84. // // checkIllegalImportResolution("List", new String[] { "java.util", "java.awt", },
  85. // // ZERO_STRINGS);
  86. //
  87. // }
  88. //
  89. // // Assumption for bcweaver: Already resolved type patterns with no *s or ..'s into exact type
  90. // // patterns. Exact type patterns don't have import lists. non-exact-type pattens don't
  91. // // care about precedence, so the current package can be included with all the other packages,
  92. // // and we don't care about compilation units, and we don't care about ordering.
  93. //
  94. // // only giving this wild-type patterns
  95. // public void testImportMatch() {
  96. //
  97. // checkImportMatch("*List", new String[] { "java.awt.", }, ZERO_STRINGS, "java.awt.List", true);
  98. // checkImportMatch("*List", new String[] { "java.awt.", }, ZERO_STRINGS, "java.awt.List", true);
  99. // checkImportMatch("*List", new String[] { "java.awt.", }, ZERO_STRINGS, "java.util.List", false);
  100. // checkImportMatch("*List", new String[] { "java.util.", }, ZERO_STRINGS, "java.awt.List", false);
  101. // checkImportMatch("*List", new String[] { "java.util.", }, ZERO_STRINGS, "java.util.List", true);
  102. //
  103. // checkImportMatch("*List", ZERO_STRINGS, new String[] { "java.awt.List", }, "java.awt.List", true);
  104. //
  105. // checkImportMatch("awt.*List", ZERO_STRINGS, new String[] { "java.awt.List", }, "java.awt.List", false);
  106. // checkImportMatch("*Foo", ZERO_STRINGS, new String[] { "java.awt.List", }, "java.awt.List", false);
  107. //
  108. // checkImportMatch("*List", new String[] { "java.util.", "java.awt.", }, ZERO_STRINGS, "java.util.List", true);
  109. // checkImportMatch("*List", new String[] { "java.util.", "java.awt.", }, ZERO_STRINGS, "java.awt.List", true);
  110. //
  111. // checkImportMatch("*..List", new String[] { "java.util." }, ZERO_STRINGS, "java.util.List", true);
  112. // checkImportMatch("*..List", new String[] { "java.util." }, ZERO_STRINGS, "java.awt.List", true);
  113. //
  114. // }
  115. //
  116. // public void testImportMatchWithInners() {
  117. // // checkImportMatch("*Entry", new String[] { "java.util.", "java.util.Map$" }, ZERO_STRINGS, "java.util.Map$Entry", true);
  118. // //
  119. // // checkImportMatch("java.util.Map.*Entry", ZERO_STRINGS, ZERO_STRINGS, "java.util.Map$Entry", true);
  120. // //
  121. // // checkImportMatch("*Entry", new String[] { "java.util.", }, ZERO_STRINGS, "java.util.Map$Entry", false);
  122. // //
  123. // // checkImportMatch("*.Entry", new String[] { "java.util.", }, ZERO_STRINGS, "java.util.Map$Entry", true);
  124. // //
  125. // // checkImportMatch("Map.*", new String[] { "java.util.", }, ZERO_STRINGS, "java.util.Map$Entry", true);
  126. //
  127. // checkImportMatch("Map.*", ZERO_STRINGS, new String[] { "java.util.Map" }, "java.util.Map$Entry", true);
  128. // }
  129. //
  130. // private void checkImportMatch(String wildPattern, String[] importedPackages, String[] importedNames, String matchName,
  131. // boolean shouldMatch) {
  132. // WildTypePattern p = makeResolvedWildTypePattern(wildPattern, importedPackages, importedNames);
  133. // checkPatternMatch(p, matchName, shouldMatch);
  134. // }
  135. //
  136. // private WildTypePattern makeResolvedWildTypePattern(String wildPattern, String[] importedPackages, String[] importedNames) {
  137. // WildTypePattern unresolved = (WildTypePattern) new PatternParser(wildPattern).parseTypePattern();
  138. //
  139. // WildTypePattern resolved = resolve(unresolved, importedPackages, importedNames);
  140. // return resolved;
  141. //
  142. // }
  143. //
  144. // private WildTypePattern resolve(WildTypePattern unresolved, String[] importedPrefixes, String[] importedNames) {
  145. //
  146. // TestScope scope = makeTestScope();
  147. // scope.setImportedPrefixes(importedPrefixes);
  148. // scope.setImportedNames(importedNames);
  149. // return (WildTypePattern) unresolved.resolveBindings(scope, Bindings.NONE, false, false);
  150. // }
  151. //
  152. //
  153. // public void testInstanceofMatch() {
  154. //
  155. // checkInstanceofMatch("java.lang.Object", "java.lang.Object", FuzzyBoolean.YES);
  156. //
  157. // checkIllegalInstanceofMatch("java.lang.Object+", "java.lang.Object");
  158. // checkIllegalInstanceofMatch("java.lang.Object+", "java.lang.String");
  159. // checkIllegalInstanceofMatch("java.lang.String+", "java.lang.Object");
  160. // checkIllegalInstanceofMatch("java.lang.*", "java.lang.Object");
  161. // checkInstanceofMatch("java.lang.Integer", "java.lang.String", FuzzyBoolean.NO);
  162. //
  163. // checkInstanceofMatch("java.lang.Number", "java.lang.Integer", FuzzyBoolean.YES);
  164. // checkInstanceofMatch("java.lang.Integer", "java.lang.Number", FuzzyBoolean.MAYBE);
  165. //
  166. // checkIllegalInstanceofMatch("java..Integer", "java.lang.Integer");
  167. //
  168. // checkInstanceofMatch("*", "java.lang.Integer", FuzzyBoolean.YES);
  169. //
  170. // }
  171. //
  172. // public void testArrayMatch() {
  173. // checkMatch("*[][]", "java.lang.Object", false);
  174. // checkMatch("*[]", "java.lang.Object[]", true);
  175. // checkMatch("*[][]", "java.lang.Object[][]", true);
  176. // checkMatch("java.lang.Object+", "java.lang.Object[]", true);
  177. // checkMatch("java.lang.Object[]", "java.lang.Object", false);
  178. // checkMatch("java.lang.Object[]", "java.lang.Object[]", true);
  179. // checkMatch("java.lang.Object[][]", "java.lang.Object[][]", true);
  180. // checkMatch("java.lang.String[]", "java.lang.Object", false);
  181. // checkMatch("java.lang.String[]", "java.lang.Object[]", false);
  182. // checkMatch("java.lang.String[][]", "java.lang.Object[][]", false);
  183. // checkMatch("java.lang.Object+[]", "java.lang.String[][]", true);
  184. // checkMatch("java.lang.Object+[]", "java.lang.String[]", true);
  185. // checkMatch("java.lang.Object+[]", "int[][]", true);
  186. // checkMatch("java.lang.Object+[]", "int[]", false);
  187. // }
  188. //
  189. // private void checkIllegalInstanceofMatch(String pattern, String name) {
  190. // try {
  191. // TypePattern p = makeTypePattern(pattern);
  192. // ResolvedType type = world.resolve(name);
  193. // p.matchesInstanceof(type);
  194. // } catch (Throwable e) {
  195. // return;
  196. // }
  197. // assertTrue("matching " + pattern + " with " + name + " should fail", false);
  198. // }
  199. //
  200. // private void checkInstanceofMatch(String pattern, String name, FuzzyBoolean shouldMatch) {
  201. // TypePattern p = makeTypePattern(pattern);
  202. // ResolvedType type = world.resolve(name);
  203. //
  204. // p = p.resolveBindings(makeTestScope(), null, false, false);
  205. //
  206. // // System.out.println("type: " + p);
  207. // FuzzyBoolean result = p.matchesInstanceof(type);
  208. // String msg = "matches " + pattern + " to " + type;
  209. // assertEquals(msg, shouldMatch, result);
  210. // }
  211. //
  212. //
  213. // private TypePattern makeTypePattern(String pattern) {
  214. // PatternParser pp = new PatternParser(pattern);
  215. // TypePattern tp = pp.parseSingleTypePattern();
  216. // pp.checkEof();
  217. // return tp;
  218. // }
  219. //
  220. // private void checkMatch(String pattern, String name, boolean shouldMatch) {
  221. // TypePattern p = makeTypePattern(pattern);
  222. // p = p.resolveBindings(makeTestScope(), null, false, false);
  223. // checkPatternMatch(p, name, shouldMatch);
  224. // }
  225. //
  226. // private void checkPatternMatch(TypePattern p, String name, boolean shouldMatch) {
  227. // ResolvedType type = world.resolve(name);
  228. // // System.out.println("type: " + type);
  229. // boolean result = p.matchesStatically(type);
  230. // String msg = "matches " + p + " to " + type + " expected ";
  231. // if (shouldMatch) {
  232. // assertTrue(msg + shouldMatch, result);
  233. // } else {
  234. // assertTrue(msg + shouldMatch, !result);
  235. // }
  236. // }
  237. //
  238. // public void testSerialization() throws IOException {
  239. // String[] patterns = new String[] { "java.lang.Object", "java.lang.Object+", "java.lang.Integer", "int", "java..*",
  240. // "java..util..*", "*.*.Object", "*", };
  241. //
  242. // for (int i = 0, len = patterns.length; i < len; i++) {
  243. // checkSerialization(patterns[i]);
  244. // }
  245. // }
  246. //
  247. // /**
  248. // * Method checkSerialization.
  249. // *
  250. // * @param string
  251. // */
  252. // private void checkSerialization(String string) throws IOException {
  253. // TypePattern p = makeTypePattern(string);
  254. // ByteArrayOutputStream bo = new ByteArrayOutputStream();
  255. // ConstantPoolSimulator cps = new ConstantPoolSimulator();
  256. // CompressingDataOutputStream out = new CompressingDataOutputStream(bo, cps);
  257. // p.write(out);
  258. // out.close();
  259. //
  260. // ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
  261. // VersionedDataInputStream in = new VersionedDataInputStream(bi, cps);
  262. // TypePattern newP = TypePattern.read(in, null);
  263. //
  264. // assertEquals("write/read", p, newP);
  265. // }
  266. }