summaryrefslogtreecommitdiffstats
path: root/tests/java5/ataspectj/annotationGen/ITDTest.aj
blob: dc8089f702b9b6db4d3c3452d2d4947931c02654 (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
package a.b.c;

import org.aspectj.lang.reflect.*;
import java.lang.reflect.*;
import org.aspectj.lang.annotation.*;

public aspect ITDTest {
	
    public void A.a(String s) {}

    private void A.b(String s) {}

    int A.c(String s) { return 1; }

    public A.new(String s) { this(); }

    private A.new(String s,String s2) { this(); }
	
    A.new(String s, Object o) { this(); }

    public int A.f ;

    private int A.g;

    int A.h;
    
    public static void main(String[] args) throws ClassNotFoundException {
    	AjType<ITDTest> myType = AjTypeSystem.getAjType(ITDTest.class);
    	checkITDMs(myType);
    	checkITDFs(myType);
    	checkITDCs(myType);
    	checkAnnStyle();
    }
    
    private static void checkITDMs(AjType<?> itdTest) throws ClassNotFoundException {
    	InterTypeMethodDeclaration[] itdms = itdTest.getDeclaredITDMethods();
    	assertEquals("expecting 3 declared methods, got: ",3,itdms.length);
    	InterTypeMethodDeclaration a = null,b = null,c = null;
    	for (int i = 0; i < itdms.length; i++) {
	    	if (itdms[i].getName().equals("a")) {
	    		a = itdms[i];
	    	} else if (itdms[i].getName().equals("b")) {
	    		b = itdms[i];
	    	} else if (itdms[i].getName().equals("c")) {
	    		c = itdms[i];
	    	}
    	}
    	assertNotNull("expecting method name a",a);
    	assertNotNull("expecting method name b",b);
    	assertNotNull("expecting method name c",c);
    	assertEquals("expecting AjType<a.b.c.A>",AjTypeSystem.getAjType(A.class),a.getTargetType());
    	assertEquals("expecting public method, got:",true,Modifier.isPublic(a.getModifiers()));
    	assertEquals("expecting private method, got:",true,Modifier.isPrivate(b.getModifiers()));
    	assertEquals("expecting non-public method, got:",false,Modifier.isPublic(c.getModifiers()));
    	assertEquals("one param, got: ",1,a.getParameterTypes().length);
    	assertEquals("expecting String, got: ",String.class,a.getParameterTypes()[0].getJavaClass());
    	assertEquals("nothing thrown, but: ",0,b.getExceptionTypes().length);
    	assertEquals("expecting int, got: ",int.class,c.getReturnType().getJavaClass());
    	itdms = itdTest.getITDMethods();
    	assertEquals("expecting 1 method, got: ",1,itdms.length);
       	assertEquals("expecting method name a, got: ","a",itdms[0].getName());
       	try {
       		InterTypeMethodDeclaration m = itdTest.getDeclaredITDMethod("b",AjTypeSystem.getAjType(A.class),AjTypeSystem.getAjType(String.class));
       		assertEquals("expecting b, got: ","b",m.getName());
       	} catch (NoSuchMethodException ex) { throw new RuntimeException("didn't find expected itdm"); }
       	try {
       		InterTypeMethodDeclaration m = itdTest.getITDMethod("d",AjTypeSystem.getAjType(A.class),AjTypeSystem.getAjType(String.class));
       		throw new RuntimeException("Expected NoSuchMethodException not thrown");
       	} catch (NoSuchMethodException ex) { }
    }
    
    private static void checkITDFs(AjType<?> itdTest) throws ClassNotFoundException {
    	InterTypeFieldDeclaration[] itdfs = itdTest.getDeclaredITDFields();
    	assertEquals("expecting 3 declared fields, got: ",3, itdfs.length);
    	InterTypeFieldDeclaration f = null,g = null,h = null;
    	for (int i = 0; i < itdfs.length; i++) {
	    	if (itdfs[i].getName().equals("f")) {
	    		f = itdfs[i];
	    	} else if (itdfs[i].getName().equals("g")) {
	    		g = itdfs[i];
	    	} else if (itdfs[i].getName().equals("h")) {
	    		h = itdfs[i];
	    	}
    	}
    	assertNotNull("expecting field name f",f);
    	assertNotNull("expecting field name g",g);
    	assertNotNull("expecting field name h",h);
    	assertEquals("expecting AjType<a.b.c.A>",AjTypeSystem.getAjType(A.class),f.getTargetType());
       	assertEquals("expecting public field, got:",true,Modifier.isPublic(f.getModifiers()));
    	assertEquals("expecting private field, got:",true,Modifier.isPrivate(g.getModifiers()));
    	assertEquals("expecting non-public field, got:",false,Modifier.isPublic(h.getModifiers()));
       	assertEquals("expecting int, got: ",int.class,h.getType().getJavaClass());
        itdfs = itdTest.getITDFields();
    	assertEquals("expecting 1 field, got: ",1, itdfs.length);
    	assertEquals("expecting field name f, got: ","f",itdfs[0].getName());   
    	try {
    		f = itdTest.getDeclaredITDField("f",AjTypeSystem.getAjType(A.class));
       		assertEquals("expecting f, got: ","f",f.getName());
    	} catch(NoSuchFieldException ex) { throw new RuntimeException("didn't find expected itdf"); }
       	try {
       		g = itdTest.getITDField("g",AjTypeSystem.getAjType(A.class));
       		throw new RuntimeException("Expected NoSuchFieldException not thrown");
       	} catch (NoSuchFieldException ex) { }
    }
    
    private static void checkITDCs(AjType<?> itdTest) throws ClassNotFoundException {
    	InterTypeConstructorDeclaration[] itdcs = itdTest.getDeclaredITDConstructors();
    	assertEquals("expecting 3 declared constructors, got: ",3, itdcs.length);
    	InterTypeConstructorDeclaration pubDec = findPublicCons(itdcs);
    	InterTypeConstructorDeclaration privDec = findPrivateCons(itdcs);
    	InterTypeConstructorDeclaration defDec = findDefaultCons(itdcs);
    	if (pubDec == null || privDec == null || defDec == null) throw new RuntimeException("failed to find expected constructors");
       	assertEquals("two params, got: ",2,defDec.getParameterTypes().length);
    	assertEquals("expecting String, got: ",String.class,defDec.getParameterTypes()[0].getJavaClass());
    	assertEquals("expecting Object, got: ",Object.class,defDec.getParameterTypes()[1].getJavaClass());
    	assertEquals("nothing thrown, but: ",0,privDec.getExceptionTypes().length);
      	itdcs = itdTest.getITDConstructors();
    	assertEquals("expecting 1 cons, got: ",1,itdcs.length);    	
    	try {
       		InterTypeConstructorDeclaration c = itdTest.getDeclaredITDConstructor(AjTypeSystem.getAjType(A.class),AjTypeSystem.getAjType(String.class));
       	} catch (NoSuchMethodException ex) { throw new RuntimeException("didn't find expected itdm"); }
       	try {
       		InterTypeConstructorDeclaration c = itdTest.getITDConstructor(AjTypeSystem.getAjType(A.class),AjTypeSystem.getAjType(String.class),AjTypeSystem.getAjType(Object.class));
       		throw new RuntimeException("Expected NoSuchMethodException not thrown");
       	} catch (NoSuchMethodException ex) { }
    }
    
    private static InterTypeConstructorDeclaration findPublicCons(InterTypeConstructorDeclaration[] itcds) {
    	for( InterTypeConstructorDeclaration i : itcds) {
    		if (Modifier.isPublic(i.getModifiers())) {
    			return i;
    		}
    	}
    	return null;
    }
    
    private static InterTypeConstructorDeclaration findPrivateCons(InterTypeConstructorDeclaration[] itcds) {
    	for( InterTypeConstructorDeclaration i : itcds) {
    		if (Modifier.isPrivate(i.getModifiers())) {
    			return i;
    		}
    	}
    	return null;
    }
    
    private static InterTypeConstructorDeclaration findDefaultCons(InterTypeConstructorDeclaration[] itcds) {
    	for( InterTypeConstructorDeclaration i : itcds) {
    		if (!Modifier.isPublic(i.getModifiers()) && !Modifier.isPrivate(i.getModifiers())) {
    			return i;
    		}
    	}
    	return null;
    }
    
    private static void checkAnnStyle() {
    	AjType<X> x = AjTypeSystem.getAjType(X.class);
    	org.aspectj.lang.reflect.DeclareParents[] decps = x.getDeclareParents();
    	assertEquals("1 declare parents",1,decps.length);
    	assertEquals("implements",true,decps[0].isImplements());
    	assertEquals("X",x,decps[0].getDeclaringType());
    	assertEquals("org.xyz..*, got: ","org.xyz..*",decps[0].getTargetTypesPattern().asString());
    	try {
    		assertEquals("1: ",1,decps[0].getParentTypes().length);
    		assertEquals("I: ",I.class,((AjType<?>)decps[0].getParentTypes()[0]).getJavaClass());
    	} catch (ClassNotFoundException cnf) {
    		throw new RuntimeException(cnf);
    	}
    	//assertEquals("ITD field 1: ",1,x.getDeclaredITDFields().length);
    	//assertEquals("ITD filed name i: ","i",x.getDeclaredITDFields()[0].getName());
    	assertEquals("ITD method 1: ",1,x.getITDMethods().length);
    	assertEquals("getNumber: ","getNumber",x.getITDMethods()[0].getName());
    }
    
    private static void assertEquals(String msg, Object expected, Object actual) {
    	if (!expected.equals(actual)) throw new RuntimeException(msg + " " + actual.toString());
    }
    
    private static void assertNotNull(String msg, Object obj) {
    	if (obj == null) throw new RuntimeException(msg);
    }
}


class A {}

@Aspect
class X {
	
	@org.aspectj.lang.annotation.DeclareParents("org.xyz..*")
	public static I myMixin = new Mixin();


	public static class Mixin implements I {
		
		private int i = 0;
		
		public int getNumber() { return i; }
		
	}
	
}

interface I {
	
	int getNumber();
	
}