1 /* *******************************************************************
2 * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
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
10 * PARC initial implementation
11 * ******************************************************************/
13 package org.aspectj.weaver.patterns;
15 import org.aspectj.weaver.UnresolvedType;
16 import org.aspectj.weaver.World;
17 import org.aspectj.weaver.reflect.ReflectionWorld;
19 public class SimpleScopeTests extends PatternsTestCase {
21 public World getWorld() {
22 return new ReflectionWorld(true, this.getClass().getClassLoader());
25 public void testTestScope() {
26 SimpleScope scope = makeTestScope();
28 FormalBinding formalBinding = scope.lookupFormal("i");
29 assertEquals("i", formalBinding.getName());
30 assertEquals("I", formalBinding.getType().getSignature());
32 formalBinding = scope.lookupFormal("string");
33 assertEquals("string", formalBinding.getName());
34 assertEquals("Ljava/lang/String;", formalBinding.getType().getSignature());
38 SimpleScope scope = makeTestScope();
39 UnresolvedType unresolvedType = scope.lookupType("void", null);
40 System.out.println(unresolvedType);
43 public static final String[] ZERO_STRINGS = new String[0];
45 private TestScope makeTestScope() {
48 return new TestScope(new String[] { "int", "java.lang.String" }, new String[] { "i", "string" }, world);
51 // public void testStaticMatch() {
52 // checkMatch("java.lang.Object", "java.lang.Object", true);
53 // checkMatch("java.lang.Object+", "java.lang.Object", true);
54 // checkMatch("java.lang.Object+", "java.lang.String", true);
55 // checkMatch("java.lang.String+", "java.lang.Object", false);
56 // checkMatch("java.lang.Integer", "java.lang.String", false);
58 // checkMatch("java.lang.Integer", "int", false);
60 // checkMatch("java.lang.Number+", "java.lang.Integer", true);
62 // checkMatch("java..*", "java.lang.Integer", true);
63 // checkMatch("java..*", "java.lang.reflect.Modifier", true);
64 // checkMatch("java..*", "int", false);
65 // checkMatch("java..*", "javax.swing.Action", false);
66 // checkMatch("java..*+", "javax.swing.Action", true);
68 // checkMatch("*.*.Object", "java.lang.Object", true);
69 // checkMatch("*.Object", "java.lang.Object", false);
70 // checkMatch("*..*", "java.lang.Object", true);
71 // checkMatch("*..*", "int", false);
72 // checkMatch("java..Modifier", "java.lang.reflect.Modifier", true);
73 // checkMatch("java.lang.reflect.Mod..ifier", "java.lang.reflect.Modifier", false);
75 // checkMatch("java..reflect..Modifier", "java.lang.reflect.Modifier", true);
76 // checkMatch("java..lang..Modifier", "java.lang.reflect.Modifier", true);
77 // checkMatch("java..*..Modifier", "java.lang.reflect.Modifier", true);
78 // checkMatch("java..*..*..Modifier", "java.lang.reflect.Modifier", true);
79 // checkMatch("java..*..*..*..Modifier", "java.lang.reflect.Modifier", false);
80 // // checkMatch("java..reflect..Modifier", "java.lang.reflect.Modxifier", false);
81 // checkMatch("ja*va..Modifier", "java.lang.reflect.Modifier", true);
82 // checkMatch("java..*..Mod*ifier", "java.lang.reflect.Modifier", true);
87 // // 0. defined in current compilation unit, or imported by name
88 // // 1. defined in current package/type/whatever
89 // // 2. defined in package imported by *
91 // * We've decided not to test this here, but rather in any compilers
93 // public void testImportResolve() {
94 // // checkIllegalImportResolution("List", new String[] { "java.util", "java.awt", },
99 // // Assumption for bcweaver: Already resolved type patterns with no *s or ..'s into exact type
100 // // patterns. Exact type patterns don't have import lists. non-exact-type pattens don't
101 // // care about precedence, so the current package can be included with all the other packages,
102 // // and we don't care about compilation units, and we don't care about ordering.
104 // // only giving this wild-type patterns
105 // public void testImportMatch() {
107 // checkImportMatch("*List", new String[] { "java.awt.", }, ZERO_STRINGS, "java.awt.List", true);
108 // checkImportMatch("*List", new String[] { "java.awt.", }, ZERO_STRINGS, "java.awt.List", true);
109 // checkImportMatch("*List", new String[] { "java.awt.", }, ZERO_STRINGS, "java.util.List", false);
110 // checkImportMatch("*List", new String[] { "java.util.", }, ZERO_STRINGS, "java.awt.List", false);
111 // checkImportMatch("*List", new String[] { "java.util.", }, ZERO_STRINGS, "java.util.List", true);
113 // checkImportMatch("*List", ZERO_STRINGS, new String[] { "java.awt.List", }, "java.awt.List", true);
115 // checkImportMatch("awt.*List", ZERO_STRINGS, new String[] { "java.awt.List", }, "java.awt.List", false);
116 // checkImportMatch("*Foo", ZERO_STRINGS, new String[] { "java.awt.List", }, "java.awt.List", false);
118 // checkImportMatch("*List", new String[] { "java.util.", "java.awt.", }, ZERO_STRINGS, "java.util.List", true);
119 // checkImportMatch("*List", new String[] { "java.util.", "java.awt.", }, ZERO_STRINGS, "java.awt.List", true);
121 // checkImportMatch("*..List", new String[] { "java.util." }, ZERO_STRINGS, "java.util.List", true);
122 // checkImportMatch("*..List", new String[] { "java.util." }, ZERO_STRINGS, "java.awt.List", true);
126 // public void testImportMatchWithInners() {
127 // // checkImportMatch("*Entry", new String[] { "java.util.", "java.util.Map$" }, ZERO_STRINGS, "java.util.Map$Entry", true);
129 // // checkImportMatch("java.util.Map.*Entry", ZERO_STRINGS, ZERO_STRINGS, "java.util.Map$Entry", true);
131 // // checkImportMatch("*Entry", new String[] { "java.util.", }, ZERO_STRINGS, "java.util.Map$Entry", false);
133 // // checkImportMatch("*.Entry", new String[] { "java.util.", }, ZERO_STRINGS, "java.util.Map$Entry", true);
135 // // checkImportMatch("Map.*", new String[] { "java.util.", }, ZERO_STRINGS, "java.util.Map$Entry", true);
137 // checkImportMatch("Map.*", ZERO_STRINGS, new String[] { "java.util.Map" }, "java.util.Map$Entry", true);
140 // private void checkImportMatch(String wildPattern, String[] importedPackages, String[] importedNames, String matchName,
141 // boolean shouldMatch) {
142 // WildTypePattern p = makeResolvedWildTypePattern(wildPattern, importedPackages, importedNames);
143 // checkPatternMatch(p, matchName, shouldMatch);
146 // private WildTypePattern makeResolvedWildTypePattern(String wildPattern, String[] importedPackages, String[] importedNames) {
147 // WildTypePattern unresolved = (WildTypePattern) new PatternParser(wildPattern).parseTypePattern();
149 // WildTypePattern resolved = resolve(unresolved, importedPackages, importedNames);
154 // private WildTypePattern resolve(WildTypePattern unresolved, String[] importedPrefixes, String[] importedNames) {
156 // TestScope scope = makeTestScope();
157 // scope.setImportedPrefixes(importedPrefixes);
158 // scope.setImportedNames(importedNames);
159 // return (WildTypePattern) unresolved.resolveBindings(scope, Bindings.NONE, false, false);
164 // public void testInstanceofMatch() {
166 // checkInstanceofMatch("java.lang.Object", "java.lang.Object", FuzzyBoolean.YES);
168 // checkIllegalInstanceofMatch("java.lang.Object+", "java.lang.Object");
169 // checkIllegalInstanceofMatch("java.lang.Object+", "java.lang.String");
170 // checkIllegalInstanceofMatch("java.lang.String+", "java.lang.Object");
171 // checkIllegalInstanceofMatch("java.lang.*", "java.lang.Object");
172 // checkInstanceofMatch("java.lang.Integer", "java.lang.String", FuzzyBoolean.NO);
174 // checkInstanceofMatch("java.lang.Number", "java.lang.Integer", FuzzyBoolean.YES);
175 // checkInstanceofMatch("java.lang.Integer", "java.lang.Number", FuzzyBoolean.MAYBE);
177 // checkIllegalInstanceofMatch("java..Integer", "java.lang.Integer");
179 // checkInstanceofMatch("*", "java.lang.Integer", FuzzyBoolean.YES);
183 // public void testArrayMatch() {
184 // checkMatch("*[][]", "java.lang.Object", false);
185 // checkMatch("*[]", "java.lang.Object[]", true);
186 // checkMatch("*[][]", "java.lang.Object[][]", true);
187 // checkMatch("java.lang.Object+", "java.lang.Object[]", true);
188 // checkMatch("java.lang.Object[]", "java.lang.Object", false);
189 // checkMatch("java.lang.Object[]", "java.lang.Object[]", true);
190 // checkMatch("java.lang.Object[][]", "java.lang.Object[][]", true);
191 // checkMatch("java.lang.String[]", "java.lang.Object", false);
192 // checkMatch("java.lang.String[]", "java.lang.Object[]", false);
193 // checkMatch("java.lang.String[][]", "java.lang.Object[][]", false);
194 // checkMatch("java.lang.Object+[]", "java.lang.String[][]", true);
195 // checkMatch("java.lang.Object+[]", "java.lang.String[]", true);
196 // checkMatch("java.lang.Object+[]", "int[][]", true);
197 // checkMatch("java.lang.Object+[]", "int[]", false);
200 // private void checkIllegalInstanceofMatch(String pattern, String name) {
202 // TypePattern p = makeTypePattern(pattern);
203 // ResolvedType type = world.resolve(name);
204 // p.matchesInstanceof(type);
205 // } catch (Throwable e) {
208 // assertTrue("matching " + pattern + " with " + name + " should fail", false);
211 // private void checkInstanceofMatch(String pattern, String name, FuzzyBoolean shouldMatch) {
212 // TypePattern p = makeTypePattern(pattern);
213 // ResolvedType type = world.resolve(name);
215 // p = p.resolveBindings(makeTestScope(), null, false, false);
217 // // System.out.println("type: " + p);
218 // FuzzyBoolean result = p.matchesInstanceof(type);
219 // String msg = "matches " + pattern + " to " + type;
220 // assertEquals(msg, shouldMatch, result);
224 // private TypePattern makeTypePattern(String pattern) {
225 // PatternParser pp = new PatternParser(pattern);
226 // TypePattern tp = pp.parseSingleTypePattern();
231 // private void checkMatch(String pattern, String name, boolean shouldMatch) {
232 // TypePattern p = makeTypePattern(pattern);
233 // p = p.resolveBindings(makeTestScope(), null, false, false);
234 // checkPatternMatch(p, name, shouldMatch);
237 // private void checkPatternMatch(TypePattern p, String name, boolean shouldMatch) {
238 // ResolvedType type = world.resolve(name);
239 // // System.out.println("type: " + type);
240 // boolean result = p.matchesStatically(type);
241 // String msg = "matches " + p + " to " + type + " expected ";
242 // if (shouldMatch) {
243 // assertTrue(msg + shouldMatch, result);
245 // assertTrue(msg + shouldMatch, !result);
249 // public void testSerialization() throws IOException {
250 // String[] patterns = new String[] { "java.lang.Object", "java.lang.Object+", "java.lang.Integer", "int", "java..*",
251 // "java..util..*", "*.*.Object", "*", };
253 // for (int i = 0, len = patterns.length; i < len; i++) {
254 // checkSerialization(patterns[i]);
259 // * Method checkSerialization.
263 // private void checkSerialization(String string) throws IOException {
264 // TypePattern p = makeTypePattern(string);
265 // ByteArrayOutputStream bo = new ByteArrayOutputStream();
266 // ConstantPoolSimulator cps = new ConstantPoolSimulator();
267 // CompressingDataOutputStream out = new CompressingDataOutputStream(bo, cps);
271 // ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
272 // VersionedDataInputStream in = new VersionedDataInputStream(bi, cps);
273 // TypePattern newP = TypePattern.read(in, null);
275 // assertEquals("write/read", p, newP);