File: Driver.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,840 kB
  • sloc: java: 145,910; xml: 839; sh: 518; makefile: 401; perl: 26
file content (179 lines) | stat: -rw-r--r-- 5,575 bytes parent folder | download | duplicates (3)
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
// Keep somewhat in sync with
// langtools/test/tools/javac/annotations/typeAnnotations/referenceinfos/Driver.java

// I removed some unnecessary code, e.g. declarations of @TA.
// I changed expected logic to handle multiple appearances
// of the same qualifier in different positions.

import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.TypeAnnotation;
import com.sun.tools.classfile.TypeAnnotation.TargetType;
import java.io.PrintStream;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.checkerframework.javacutil.Pair;

public class Driver {

    private static final PrintStream out = System.out;

    public static void main(String[] args) throws Exception {
        if (args.length == 0 || args.length > 1) {
            throw new IllegalArgumentException("Usage: java Driver <test-name>");
        }
        String name = args[0];
        Class<?> clazz = Class.forName(name);
        new Driver().runDriver(clazz.newInstance());
    }

    protected void runDriver(Object object) throws Exception {
        int passed = 0, failed = 0;
        Class<?> clazz = object.getClass();
        out.println("Tests for " + clazz.getName());

        // Find methods
        for (Method method : clazz.getMethods()) {
            List<Pair<String, TypeAnnotation.Position>> expected = expectedOf(method);
            if (expected == null) {
                continue;
            }
            if (method.getReturnType() != String.class) {
                throw new IllegalArgumentException(
                        "Test method needs to return a string: " + method);
            }
            String testClass = PersistUtil.testClassOf(method);

            try {
                String compact = (String) method.invoke(object);
                String fullFile = PersistUtil.wrap(compact);
                ClassFile cf = PersistUtil.compileAndReturn(fullFile, testClass);
                List<TypeAnnotation> actual = ReferenceInfoUtil.extendedAnnotationsOf(cf);
                ReferenceInfoUtil.compare(expected, actual, cf);
                out.println("PASSED:  " + method.getName());
                ++passed;
            } catch (Throwable e) {
                out.println("FAILED:  " + method.getName());
                out.println("    " + e);
                ++failed;
            }
        }

        out.println();
        int total = passed + failed;
        out.println(total + " total tests: " + passed + " PASSED, " + failed + " FAILED");

        out.flush();

        if (failed != 0) {
            throw new RuntimeException(failed + " tests failed");
        }
    }

    private List<Pair<String, TypeAnnotation.Position>> expectedOf(Method m) {
        TADescription ta = m.getAnnotation(TADescription.class);
        TADescriptions tas = m.getAnnotation(TADescriptions.class);

        if (ta == null && tas == null) {
            return null;
        }

        List<Pair<String, TypeAnnotation.Position>> result = new ArrayList<>();

        if (ta != null) {
            result.add(expectedOf(ta));
        }

        if (tas != null) {
            for (TADescription a : tas.value()) {
                result.add(expectedOf(a));
            }
        }

        return result;
    }

    private Pair<String, TypeAnnotation.Position> expectedOf(TADescription d) {
        String annoName = d.annotation();

        TypeAnnotation.Position p = new TypeAnnotation.Position();
        p.type = d.type();
        if (d.offset() != NOT_SET) {
            p.offset = d.offset();
        }
        if (d.lvarOffset().length != 0) {
            p.lvarOffset = d.lvarOffset();
        }
        if (d.lvarLength().length != 0) {
            p.lvarLength = d.lvarLength();
        }
        if (d.lvarIndex().length != 0) {
            p.lvarIndex = d.lvarIndex();
        }
        if (d.boundIndex() != NOT_SET) {
            p.bound_index = d.boundIndex();
        }
        if (d.paramIndex() != NOT_SET) {
            p.parameter_index = d.paramIndex();
        }
        if (d.typeIndex() != NOT_SET) {
            p.type_index = d.typeIndex();
        }
        if (d.exceptionIndex() != NOT_SET) {
            p.exception_index = d.exceptionIndex();
        }
        if (d.genericLocation().length != 0) {
            p.location =
                    TypeAnnotation.Position.getTypePathFromBinary(
                            wrapIntArray(d.genericLocation()));
        }

        return Pair.of(annoName, p);
    }

    private List<Integer> wrapIntArray(int[] ints) {
        List<Integer> list = new ArrayList<>(ints.length);
        for (int i : ints) {
            list.add(i);
        }
        return list;
    }

    public static final int NOT_SET = -888;
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface TADescription {
    String annotation();

    TargetType type();

    int offset() default Driver.NOT_SET;

    int[] lvarOffset() default {};

    int[] lvarLength() default {};

    int[] lvarIndex() default {};

    int boundIndex() default Driver.NOT_SET;

    int paramIndex() default Driver.NOT_SET;

    int typeIndex() default Driver.NOT_SET;

    int exceptionIndex() default Driver.NOT_SET;

    int[] genericLocation() default {};
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface TADescriptions {
    TADescription[] value() default {};
}