aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/org/aspectj/systemtest/ajc164/Ajc164Tests.java
blob: 74175ea6a8be3441b3a9a558fa523da86b176821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*******************************************************************************
 * Copyright (c) 2008 Contributors 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Andy Clement - initial API and implementation
 *******************************************************************************/
package org.aspectj.systemtest.ajc164;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import junit.framework.Test;

import org.aspectj.apache.bcel.classfile.JavaClass;
import org.aspectj.apache.bcel.classfile.LocalVariable;
import org.aspectj.apache.bcel.classfile.LocalVariableTable;
import org.aspectj.apache.bcel.classfile.Method;
import org.aspectj.apache.bcel.util.ClassPath;
import org.aspectj.apache.bcel.util.SyntheticRepository;
import org.aspectj.asm.AsmManager;
import org.aspectj.asm.IHierarchy;
import org.aspectj.asm.IProgramElement;
import org.aspectj.asm.IRelationship;
import org.aspectj.testing.XMLBasedAjcTestCase;

public class Ajc164Tests extends org.aspectj.testing.XMLBasedAjcTestCase {

	/**
	 * This test program can be used to compare handles for faulted in binary
	 * aspects with handles that would be used if the aspect was available as
	 * source. There are two compile steps in the xml for the test - commenting
	 * out the first will allow the source handles to be seen, leaving it in
	 * will switch to binary. Effectively the only difference should be that in
	 * the binary case the handles are prefixed 'binaries'.
	 */
	public void testItdsAspectPathModel_pr265729_1() {
		runTest("aspectpath model");
		AsmManager model = AsmManager.lastActiveStructureModel;
		IHierarchy top = model.getHierarchy();
		printModel(model);
		IProgramElement ipe = null;

		// ITD METHOD
		// should be the ITD method from the binary aspect:
		// public Color Orange.getColor() { return Color.orange; }
		ipe = top.findElementForType("demo", "Orange");
		assertNotNull(ipe);
		assertEquals("<demo{Orange.java[Orange", ipe.getHandleIdentifier());
		IRelationship ir = (IRelationship) model.getRelationshipMap().get(ipe).get(0);
		String itdMethodHandle = (String) ir.getTargets().get(0);
		// handle when all source: <{Aspect.java}Aspect)Orange.getColor
		assertEquals("/binaries<{Aspect.java}Aspect)Orange.getColor", itdMethodHandle);
		IProgramElement itdpe = model.getHierarchy().findElementForHandle(itdMethodHandle);
		assertEquals("java.awt.Color", itdpe.getCorrespondingType(true));

		// ITD FIELD
		// should be the ITD field from the binary aspect:
		// public Color Strawberry.color
		ipe = top.findElementForType("demo", "Strawberry");
		assertNotNull(ipe);
		assertEquals("<demo{Strawberry.java[Strawberry", ipe.getHandleIdentifier());
		ir = (IRelationship) model.getRelationshipMap().get(ipe).get(0);
		String itdFieldHandle = (String) ir.getTargets().get(0);
		// source handle <{Aspect.java}Aspect)Strawberry.color
		assertEquals("/binaries<{Aspect.java}Aspect)Strawberry.color", itdFieldHandle);
		IProgramElement itdfpe = model.getHierarchy().findElementForHandle(itdMethodHandle);
		assertEquals("java.awt.Color", itdfpe.getCorrespondingType(true));

		// ITD CONSTRUCTOR
		// /binaries< Aspect.java}Aspect)java.awt.Color demo.Strawberry.color
		ipe = top.findElementForType("demo", "Fruit");
		assertNotNull(ipe);
		assertEquals("<demo{Fruit.java[Fruit", ipe.getHandleIdentifier());
		ir = (IRelationship) model.getRelationshipMap().get(ipe).get(0);
		String itdCtorHandle = (String) ir.getTargets().get(0);
		// source handle <{Aspect.java}Aspect)Fruit.Fruit_new)QColor;)QString;
		assertEquals("/binaries<{Aspect.java}Aspect)Fruit.Fruit_new)QColor;)QString;", itdCtorHandle);
		IProgramElement itdcpe = model.getHierarchy().findElementForHandle(itdCtorHandle);
		List ptypes = itdcpe.getParameterTypes();
		assertEquals("java.awt.Color", new String((char[]) ptypes.get(0)));
		assertEquals("java.lang.String", new String((char[]) ptypes.get(1)));
	}

	private void printModel(AsmManager model) {
		try {
			model.dumptree(model.getHierarchy().getRoot(), 0);
			model.dumprels(new PrintWriter(System.out));
		} catch (Exception e) {
		}
	}

	public void testGenericsAopXml_pr266220() {
		runTest("generics and aop.xml");
	}

	public void testOptimizingIf_pr266165_1() {
		runTest("optimizing if for constant reference - 1");
	}

	public void testOptimizingIf_pr266165_2() {
		runTest("optimizing if for constant reference - 2");
	}

	public void testOptimizingIf_pr266165_3() {
		runTest("optimizing if for constant reference - 3");
	}

	public void testOptimizingIf_pr266165_4() {
		runTest("optimizing if for constant reference - 4");
	}

	// public void testAnnoInherited_pr265695() {
	// runTest("anno inherited");
	// }
	//
	// public void testAnnoInherited_pr265695_2() {
	// runTest("new syntax for inherited anno - 1");
	// }
	//
	// public void testAnnoInherited_pr265695_3() {
	// runTest("new syntax for inherited anno - 3");
	// }

	public void testParserProblemSubArrayPatterns_pr148508() {
		runTest("parser problem for array subtypes");
	}

	public void testVarargs_pr265418() {
		runTest("varargs");
	}

	public void testIncorrectDateResolution_pr265360() {
		runTest("incorrect resolution of Date");
	}

	public void testDualPreClinit_pr233032() {
		runTest("dual preClinit");
	}

	public void testHandles_pr263310() {
		runTest("inner handles");
		IHierarchy top = AsmManager.lastActiveStructureModel.getHierarchy();
		IProgramElement ipe = null;
		ipe = findElementAtLine(top.getRoot(), 13);
		assertEquals("<p{HandleTestingAspect.java}HandleTestingAspect[InnerClass}InnerInnerAspect|1", ipe.getHandleIdentifier());
		// ipe = findElementAtLine(top.getRoot(), 29);
		// assertEquals("<x*OverrideOptions.aj}OverrideOptions&around!2",
		// ipe.getHandleIdentifier());
	}

	public void testHandles_pr263666() {
		runTest("around advice handles");
		IHierarchy top = AsmManager.lastActiveStructureModel.getHierarchy();
		IProgramElement ipe = null;
		ipe = findElementAtLine(top.getRoot(), 22);
		assertEquals("<x*OverrideOptions.aj}OverrideOptions&around", ipe.getHandleIdentifier());
		ipe = findElementAtLine(top.getRoot(), 29);
		assertEquals("<x*OverrideOptions.aj}OverrideOptions&around!2", ipe.getHandleIdentifier());
	}

	// Only one of two aspects named
	public void testAopConfig1() {
		runTest("aop config - 1");
	}

	// Only one of two aspects named - and named one is scoped to only affect
	// one type
	public void testAopConfig2() {
		runTest("aop config - 2");
	}

	// Invalid scope specified - cannot be parsed as type pattern
	public void testAopConfig3() {
		runTest("aop config - 3");
	}

	public void testAjcThisNotRead() {
		runTest("ajcthis not read");
	}

	public void testRecursiveCflow() {
		runTest("recursive cflow");
	}

	public void testAnnoDecprecedence_pr256779() {
		runTest("anno decprecedence");
	}

	// 
	public void testBrokenLVT_pr194314_1() throws Exception {
		runTest("broken lvt - 1");
		Method m = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "Service"), "method_aroundBody1$advice");
		if (m.getLocalVariableTable() == null) {
			fail("Local variable table should not be null");
		}
		// Method:
		// private static final void method_aroundBody1$advice(Service, long,
		// JoinPoint, ServiceInterceptor, ProceedingJoinPoint);
		LocalVariable[] lvt = m.getLocalVariableTable().getLocalVariableTable();
		assertEquals(7, lvt.length); // no aroundClosure compared to second
		// version of this test
		assertEquals("LService; this(0) start=0 len=86", stringify(m.getLocalVariableTable(), 0));
		assertEquals("J l(1) start=0 len=86", stringify(m.getLocalVariableTable(), 1));
		assertEquals("Lorg/aspectj/lang/JoinPoint; thisJoinPoint(3) start=0 len=86", stringify(m.getLocalVariableTable(), 2));
		assertEquals("LServiceInterceptor; ajc$aspectInstance(4) start=0 len=86", stringify(m.getLocalVariableTable(), 3));
		assertEquals("Lorg/aspectj/lang/ProceedingJoinPoint; pjp(5) start=0 len=86", stringify(m.getLocalVariableTable(), 4));
		assertEquals("[Ljava/lang/Object; args(6) start=9 len=77", stringify(m.getLocalVariableTable(), 5));
		assertEquals("J id(7) start=21 len=65", stringify(m.getLocalVariableTable(), 6));

	}

	public void testBrokenLVT_pr194314_2() throws Exception {
		runTest("broken lvt - 2");
		Method m = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "Service"), "method_aroundBody1$advice");
		if (m.getLocalVariableTable() == null) {
			fail("Local variable table should not be null");
		}
		System.out.println(m.getLocalVariableTable());
		LocalVariable[] lvt = m.getLocalVariableTable().getLocalVariableTable();
		assertEquals(8, lvt.length);
		// private static final void method_aroundBody1$advice(Service, long,
		// JoinPoint, ServiceInterceptorCodeStyle, AroundClosure,
		// JoinPoint);
		assertEquals("LService; this(0) start=0 len=68", stringify(m.getLocalVariableTable(), 0));
		assertEquals("J l(1) start=0 len=68", stringify(m.getLocalVariableTable(), 1));
		assertEquals("Lorg/aspectj/lang/JoinPoint; thisJoinPoint(3) start=0 len=68", stringify(m.getLocalVariableTable(), 2));
		assertEquals("LServiceInterceptorCodeStyle; ajc$aspectInstance(4) start=0 len=68", stringify(m.getLocalVariableTable(), 3));
		assertEquals("Lorg/aspectj/runtime/internal/AroundClosure; ajc$aroundClosure(5) start=0 len=68", stringify(m
				.getLocalVariableTable(), 4));
		assertEquals("Lorg/aspectj/lang/JoinPoint; thisJoinPoint(6) start=0 len=68", stringify(m.getLocalVariableTable(), 5));
		assertEquals("[Ljava/lang/Object; args(7) start=9 len=59", stringify(m.getLocalVariableTable(), 6));
		assertEquals("J id(8) start=21 len=47", stringify(m.getLocalVariableTable(), 7));
	}

	/**
	 * This test checks that local variable table for the interMethodDispatcher
	 * is built correctly, for the related code see
	 * IntertypeMethodDeclaration.generateDispatchMethod(). It checks non-static
	 * and static ITDs. Once the information here is correct then around advice
	 * on ITDs can also be correct.
	 */
	public void testBrokenLVT_pr194314_3() throws Exception {
		runTest("broken lvt - 3");
		// Check intermethoddispatchers have the lvts correct
		// first ITD: public void I.foo(String s,int i,String[] ss) {}

		Method m = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "X"), "ajc$interMethodDispatch1$X$I$foo");
		LocalVariableTable lvt = m.getLocalVariableTable();
		assertNotNull(lvt);
		assertEquals("LI; ajc$this_(0) start=0 len=10", stringify(lvt, 0));
		assertEquals("Ljava/lang/String; s(1) start=0 len=10", stringify(lvt, 1));
		assertEquals("I i(2) start=0 len=10", stringify(lvt, 2));
		assertEquals("[Ljava/lang/String; ss(3) start=0 len=10", stringify(lvt, 3));

		// second ITD: public void I.fooStatic(Long l,int i,String[] ss) {}
		m = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "X"), "ajc$interMethodDispatch1$X$C$fooStatic");
		lvt = m.getLocalVariableTable();
		assertNotNull(lvt);
		assertEquals("J l(0) start=0 len=7", stringify(lvt, 0));
		assertEquals("I i(2) start=0 len=7", stringify(lvt, 1));
		assertEquals("[Ljava/lang/String; ss(3) start=0 len=7", stringify(lvt, 2));

		// Now let's check the around advice on the calls to those ITDs

		// non-static:

		m = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "C"), "foo_aroundBody1$advice");
		lvt = m.getLocalVariableTable();
		assertNotNull(lvt);

		assertEquals("LC; this(0) start=0 len=0", stringify(lvt, 0));
		assertEquals("LI; target(1) start=0 len=0", stringify(lvt, 1));
		assertEquals("Ljava/lang/String; s(2) start=0 len=0", stringify(lvt, 2));
		assertEquals("I i(3) start=0 len=0", stringify(lvt, 3));
		assertEquals("[Ljava/lang/String; ss(4) start=0 len=0", stringify(lvt, 4));
		assertEquals("LX; ajc$aspectInstance(5) start=0 len=0", stringify(lvt, 5));
		assertEquals("Lorg/aspectj/runtime/internal/AroundClosure; ajc$aroundClosure(6) start=0 len=0", stringify(lvt, 6));

		// static:

		m = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "C"), "fooStatic_aroundBody3$advice");
		lvt = m.getLocalVariableTable();
		assertNotNull(lvt);

		assertEquals("LC; this(0) start=0 len=0", stringify(lvt, 0));
		assertEquals("J l(1) start=0 len=0", stringify(lvt, 1));
		assertEquals("I i(3) start=0 len=0", stringify(lvt, 2));
		assertEquals("[Ljava/lang/String; ss(4) start=0 len=0", stringify(lvt, 3));
		assertEquals("LX; ajc$aspectInstance(5) start=0 len=0", stringify(lvt, 4));
		assertEquals("Lorg/aspectj/runtime/internal/AroundClosure; ajc$aroundClosure(6) start=0 len=0", stringify(lvt, 5));

	}

	// Single piece of advice on before execution of a method with a this and a
	// parameter
	public void testDebuggingBeforeAdvice_pr262509() {
		runTest("debugging before advice");
		Method method = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "Foo"), "foo");
		assertEquals("LFoo; this(0) start=0 len=13", stringify(method.getLocalVariableTable(), 0));
		assertEquals("LBar; bar(1) start=0 len=13", stringify(method.getLocalVariableTable(), 1));
	}

	// Single piece of advice on before execution of a method with a this and a
	// parameter and other various locals within it
	public void testDebuggingBeforeAdvice_pr262509_2() {
		// Compile with -preserveAllLocals
		runTest("debugging before advice - 2");
		Method method = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "Foo2"), "foo");
		// System.out.println(stringify(method.getLocalVariableTable()));
		List l = sortedLocalVariables(method.getLocalVariableTable());
		assertEquals("LBar; bar(1) start=0 len=34", stringify(l, 0));
		assertEquals("Ljava/lang/Exception; e(3) start=29 len=4", stringify(l, 1));
		assertEquals("LFoo2; this(0) start=0 len=34", stringify(l, 4));
		assertEquals("Ljava/lang/String; s(2) start=15 len=19", stringify(l, 2));
		assertEquals("Ljava/lang/String; s2(3) start=18 len=10", stringify(l, 3));
	}

	/**
	 * Sort it by name then start position
	 */
	private List sortedLocalVariables(LocalVariableTable lvt) {
		List l = new ArrayList();
		StringBuffer sb = new StringBuffer();
		LocalVariable lv[] = lvt.getLocalVariableTable();
		for (int i = 0; i < lv.length; i++) {
			LocalVariable lvEntry = lv[i];
			l.add(lvEntry);
		}
		Collections.sort(l, new MyComparator());
		return l;
	}

	private static class MyComparator implements Comparator {
		public int compare(Object o1, Object o2) {
			LocalVariable l1 = (LocalVariable) o1;
			LocalVariable l2 = (LocalVariable) o2;
			if (l1.getName().equals(l2.getName())) {
				return l1.getStartPC() - l2.getStartPC();
			} else {
				return l1.getName().compareTo(l2.getName());
			}
		}

	}

	// Two pieces of advice on before execution of a method with a this and a
	// parameter and another local within it
	public void testDebuggingBeforeAdvice_pr262509_3() {
		// Compile with -preserveAllLocals
		runTest("debugging before advice - 3");
		Method method = getMethodFromClass(getClassFrom(ajc.getSandboxDirectory(), "Foo3"), "foo");
		System.out.println(stringify(method.getLocalVariableTable()));
		assertEquals("LFoo3; this(0) start=0 len=35", stringify(method.getLocalVariableTable(), 0));
		assertEquals("LBar; bar(1) start=0 len=35", stringify(method.getLocalVariableTable(), 1));
		assertEquals("Ljava/lang/Exception; e(2) start=30 len=4", stringify(method.getLocalVariableTable(), 2));
	}

	public void testRogueErrors_pr246393_1() {
		runTest("rogue errors - 1");
	}

	// public void testNameClash_pr262257() {
	// runTest("name clash");
	// fail("incomplete");
	// }

	public void testCompilingSpring_pr260384() {
		runTest("compiling spring");
	}

	public void testCompilingSpring_pr260384_2() {
		runTest("compiling spring - 2");
	}

	public void testCompilingSpring_pr260384_3() {
		runTest("compiling spring - 3");
	}

	public void testCompilingSpring_pr260384_4() {
		runTest("compiling spring - 4");
	}

	public void testAtAspectJDecp_pr164016() {
		runTest("ataspectj decp 164016");
	}

	public void testAtAspectJDecp_pr258788() {
		runTest("ataspectj decp 258788");
	}

	// ---

	public static Test suite() {
		return XMLBasedAjcTestCase.loadSuite(Ajc164Tests.class);
	}

	protected File getSpecFile() {
		return new File("../tests/src/org/aspectj/systemtest/ajc164/ajc164.xml");
	}

	private SyntheticRepository createRepos(File cpentry) {
		ClassPath cp = new ClassPath(cpentry + File.pathSeparator + System.getProperty("java.class.path"));
		return SyntheticRepository.getInstance(cp);
	}

	private JavaClass getClassFrom(File where, String clazzname) {
		try {
			SyntheticRepository repos = createRepos(where);
			return repos.loadClass(clazzname);
		} catch (ClassNotFoundException cnfe) {
			throw new RuntimeException("Failed to find class " + clazzname + " at " + where.toString());
		}
	}

	private Method getMethodFromClass(JavaClass clazz, String methodName) {
		Method[] meths = clazz.getMethods();
		for (int i = 0; i < meths.length; i++) {
			Method method = meths[i];
			if (method.getName().equals(methodName)) {
				return meths[i];
			}
		}
		return null;
	}

	private String stringify(LocalVariableTable lvt, int slotIndex) {
		LocalVariable lv[] = lvt.getLocalVariableTable();
		LocalVariable lvEntry = lv[slotIndex];
		StringBuffer sb = new StringBuffer();
		sb.append(lvEntry.getSignature()).append(" ").append(lvEntry.getName()).append("(").append(lvEntry.getIndex()).append(
				") start=").append(lvEntry.getStartPC()).append(" len=").append(lvEntry.getLength());
		return sb.toString();
	}

	private String stringify(List l, int slotIndex) {
		LocalVariable lvEntry = (LocalVariable) l.get(slotIndex);
		StringBuffer sb = new StringBuffer();
		sb.append(lvEntry.getSignature()).append(" ").append(lvEntry.getName()).append("(").append(lvEntry.getIndex()).append(
				") start=").append(lvEntry.getStartPC()).append(" len=").append(lvEntry.getLength());
		return sb.toString();
	}

	private String stringify(LocalVariableTable lvt) {
		StringBuffer sb = new StringBuffer();
		sb.append("LocalVariableTable.  Entries=#" + lvt.getTableLength()).append("\n");
		LocalVariable lv[] = lvt.getLocalVariableTable();
		for (int i = 0; i < lv.length; i++) {
			LocalVariable lvEntry = lv[i];
			sb.append(lvEntry.getSignature()).append(" ").append(lvEntry.getName()).append("(").append(lvEntry.getIndex()).append(
					") start=").append(lvEntry.getStartPC()).append(" len=").append(lvEntry.getLength()).append("\n");
		}

		return sb.toString();
	}

	private IProgramElement findElementAtLine(IProgramElement whereToLook, int line) {
		if (whereToLook == null) {
			return null;
		}
		if (whereToLook.getSourceLocation() != null && whereToLook.getSourceLocation().getLine() == line) {
			return whereToLook;
		}
		List kids = whereToLook.getChildren();
		for (Iterator iterator = kids.iterator(); iterator.hasNext();) {
			IProgramElement object = (IProgramElement) iterator.next();
			if (object.getSourceLocation() != null && object.getSourceLocation().getLine() == line) {
				return object;
			}
			IProgramElement gotSomething = findElementAtLine(object, line);
			if (gotSomething != null) {
				return gotSomething;
			}
		}
		return null;
	}
}