aboutsummaryrefslogtreecommitdiffstats
path: root/tests/new/IndeterminateArgType.java
blob: 4beb1301e49c7df6722c1e25c599b7d44b2df4cf (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
import org.aspectj.testing.Tester;
import org.aspectj.lang.*;
import org.aspectj.lang.reflect.*;

import java.util.Arrays;

/** @testcase PR#764 check arg types with indeterminate prefix and suffix */
public class IndeterminateArgType {
    public static void main (String[] args) {
        OTarget o;   
        STarget c;   
        // both pointcuts pick out args 1..3; neither picks out args 0
        // both actual Object and String match
        c = new STarget();
        c = new STarget("s1");
        c = new STarget("s1", "s2");
        c = new STarget("s1", "s2", "s3");
        c.f();
        c.f("s1");
        c.f("s1", "s2");
        c.f("s1", "s2", "s3");
        // String pointcut should match even though declared type is Object
        o = new OTarget();
        o = new OTarget("o1");
        o = new OTarget("o1", "o2");
        o = new OTarget("o1", "o2", "o3");
        o.f();
        o.f("o1");
        o.f("o1", "o2");
        o.f("o1", "o2", "o3");

        // only actual Object types match these
        Object input = new Object();
        o = new OTarget();   
        o = new OTarget(input);
        o = new OTarget(input, input);
        o = new OTarget(input, input, input);
        o.f();
        o.f(input);
        o.f(input, input);
        o.f(input, input, input);

        Tester.checkEventsFromFile("IndeterminateArgType.events");
    } 
}

interface Safe {}
class OTarget implements Safe {
    OTarget() { }
    OTarget(Object s1) { }
    OTarget(Object s1, Object s2) { }
    OTarget(Object s1, Object s2, Object s3) { }

    void f() { }
    void f(Object s1) { }
    void f(Object s1, Object s2) { }
    void f(Object s1, Object s2, Object s3) { }
}

class STarget implements Safe {
    STarget() { }
    STarget(String s1) { }
    STarget(String s1, String s2) { }
    STarget(String s1, String s2, String s3) { }

    void f() { }
    void f(String s1) { }
    void f(String s1, String s2) { }
    void f(String s1, String s2, String s3) { }
}

class T {
    static void e(String s) { 
        Tester.event(s); 
    }
}

aspect A {

    String actualTypes(JoinPoint jp) { // XXX gather as utility
        Object[] types = jp.getArgs();
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i = 0; i < types.length; i++) {
            sb.append(types[i].getClass().getName());
            if ((1+i) < types.length) {
                sb.append(", ");
            }
        } 
        sb.append("]");
        return sb.toString();
    }
    void check(String pc, JoinPoint jp) {
        Class[] types = ((CodeSignature) jp.getSignature()).getParameterTypes();
        String name = jp.toLongString() + " " + actualTypes(jp) + ": " + pc;
        T.e(name);
    }

    pointcut safe()                                : call(Safe+.new(..)) || call(* Safe+.*(..)); 

    pointcut o1End()                               : args(.., Object);
    pointcut s1End()                               : args(.., String);
    pointcut o1Start()                             : args(Object, ..);
    pointcut s1Start()                             : args(String, ..);

    before()                                       : safe() && o1Start()   { check ("o1Start()", thisJoinPoint); }
    before()                                       : safe() && o1End()     { check ("o1End()", thisJoinPoint); }
    before()                                       : safe() && s1Start()   { check ("s1Start()", thisJoinPoint); }
    before()                                       : safe() && s1End()     { check ("s1End()", thisJoinPoint); }
}