aboutsummaryrefslogtreecommitdiffstats
path: root/tests/bugs1612/pr356612/AnnoBinding.java
blob: d03131615a50109d39ae9c474d54e879e595c8c3 (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
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import org.aspectj.lang.reflect.FieldSignature;

@Retention(RetentionPolicy.RUNTIME)
@interface Marker {
	String message();
}

public class AnnoBinding {
	public static void main(String[] argv) {
		long stime = System.nanoTime();
		// 10,000 or 100,000 rounds are too quick, making the test flaky on rare occasions
		final int ROUNDS = 1000 * 1000;
		for (int i = 0; i < ROUNDS; i++) {
			runOne();
		}
		long etime = System.nanoTime();
		long reflective = (etime - stime);
		stime = System.nanoTime();
		for (int i = 0; i < ROUNDS; i++) {
			runTwo();
		}
		etime = System.nanoTime();
		long bound = (etime - stime);
		String result = String.format("bound = %,d, reflective = %,d", bound, reflective);
		System.out.println(result);
		if (bound > reflective) {
			throw new RuntimeException(
				"Accessing annotation via bound parameter should be faster than reflective access: " + result
			);
		}
		if (X.sumReflective != X.sumBound) {
			throw new RuntimeException(
				String.format("Sums of @Marker message lengths should be equal: reflective = %,d, bound = %,d", X.sumReflective, X.sumBound)
			);
		}
	}

	@Marker(message = "string")
	static int field1;

	@Marker(message = "string")
	static int field2;

	public static void runOne() {
		field1 = field1 * 2; // set and get jps
	}

	public static void runTwo() {
		field1 = field1 * 2; // set and get jps
	}

}

aspect X {
	pointcut pReflective(): withincode(* runOne(..)) && get(@Marker * *);
	pointcut pBound(Marker marker): withincode(* runTwo(..)) && get(@Marker * * ) && @annotation(marker);

	public static int sumReflective, sumBound;

	before(): pReflective() {
		Marker marker = (Marker) ((FieldSignature) thisJoinPointStaticPart.getSignature()).getField().getAnnotation(Marker.class);
		sumReflective += marker.message().length();
	}

	before(Marker marker): pBound(marker) {
		sumBound += marker.message().length();
	}

}