File: AnnotationDefaultVerifier.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 65,172 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (287 lines) | stat: -rw-r--r-- 12,437 bytes parent folder | download | duplicates (16)
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
/*
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import com.sun.tools.classfile.Annotation;
import com.sun.tools.classfile.AnnotationDefault_attribute;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.ConstantPool;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class AnnotationDefaultVerifier {

    private final Map<Integer, TestElementValue> verifiers;

    public AnnotationDefaultVerifier() {
        this.verifiers = new HashMap<>();
        verifiers.put((int) 'B', new TestIntegerElementValue());
        verifiers.put((int) 'C', new TestIntegerElementValue());
        verifiers.put((int) 'D', new TestDoubleElementValue());
        verifiers.put((int) 'F', new TestFloatElementValue());
        verifiers.put((int) 'I', new TestIntegerElementValue());
        verifiers.put((int) 'J', new TestLongElementValue());
        verifiers.put((int) 'S', new TestIntegerElementValue());
        verifiers.put((int) 'Z', new TestIntegerElementValue());
        verifiers.put((int) 's', new TestStringElementValue());
        verifiers.put((int) 'e', new TestEnumElementValue());
        verifiers.put((int) 'c', new TestClassElementValue());
        verifiers.put((int) '[', new TestArrayElementValue());
        verifiers.put((int) '@', new TestAnnotationElementValue());
    }

    public void testLength(int tag, TestResult testResult, AnnotationDefault_attribute attr) {
        verifiers.get(tag).testLength(testResult, attr);
    }

    public void testElementValue(int tag, TestResult testResult, ClassFile classFile,
                                 Annotation.element_value element_value, String[] values)
            throws ConstantPool.UnexpectedEntry, ConstantPool.InvalidIndex {
        get(tag).testElementValue(testResult, classFile, element_value, values);
    }

    private TestElementValue get(int tag) {
        TestElementValue ev = verifiers.get(tag);
        if (ev == null) {
            throw new IllegalArgumentException("Unknown tag : " + (char) tag);
        }
        return ev;
    }

    private abstract class TestElementValue {
        public void testLength(TestResult testCase, AnnotationDefault_attribute attr) {
            testCase.checkEquals(attr.attribute_length, 1 + attr.default_value.length(),
                    "attribute_length");
        }

        public String[] getValues(String[] values, int index, int length) {
            return Arrays.copyOfRange(values, index, index + length);
        }

        public int getLength() {
            return 1;
        }

        public abstract void testElementValue(
                TestResult testCase,
                ClassFile classFile,
                Annotation.element_value element_value,
                String[] values)
                throws ConstantPool.InvalidIndex, ConstantPool.UnexpectedEntry;
    }

    private class TestIntegerElementValue extends TestElementValue {

        @Override
        public void testElementValue(
                TestResult testCase,
                ClassFile classFile,
                Annotation.element_value element_value,
                String[] values) throws ConstantPool.InvalidIndex {
            Annotation.Primitive_element_value ev =
                    (Annotation.Primitive_element_value) element_value;
            ConstantPool.CONSTANT_Integer_info info =
                    (ConstantPool.CONSTANT_Integer_info)
                            classFile.constant_pool.get(ev.const_value_index);
            testCase.checkEquals(info.value, Integer.parseInt(values[0]), "const_value_index");
        }
    }

    private class TestLongElementValue extends TestElementValue {
        @Override
        public void testElementValue(
                TestResult testCase,
                ClassFile classFile,
                Annotation.element_value element_value,
                String[] values) throws ConstantPool.InvalidIndex {
            Annotation.Primitive_element_value ev =
                    (Annotation.Primitive_element_value) element_value;
            ConstantPool.CONSTANT_Long_info info =
                    (ConstantPool.CONSTANT_Long_info)
                            classFile.constant_pool.get(ev.const_value_index);
            testCase.checkEquals(info.value, Long.parseLong(values[0]), "const_value_index");
        }
    }

    private class TestFloatElementValue extends TestElementValue {
        @Override
        public void testElementValue(
                TestResult testCase,
                ClassFile classFile,
                Annotation.element_value element_value,
                String[] values) throws ConstantPool.InvalidIndex {
            Annotation.Primitive_element_value ev =
                    (Annotation.Primitive_element_value) element_value;
            ConstantPool.CONSTANT_Float_info info =
                    (ConstantPool.CONSTANT_Float_info)
                            classFile.constant_pool.get(ev.const_value_index);
            testCase.checkEquals(info.value, Float.parseFloat(values[0]), "const_value_index");
        }
    }

    private class TestDoubleElementValue extends TestElementValue {
        @Override
        public void testElementValue(
                TestResult testCase,
                ClassFile classFile,
                Annotation.element_value element_value,
                String[] values) throws ConstantPool.InvalidIndex {
            Annotation.Primitive_element_value ev =
                    (Annotation.Primitive_element_value) element_value;
            ConstantPool.CONSTANT_Double_info info =
                    (ConstantPool.CONSTANT_Double_info)
                            classFile.constant_pool.get(ev.const_value_index);
            testCase.checkEquals(info.value, Double.parseDouble(values[0]), "const_value_index");
        }
    }

    private class TestStringElementValue extends TestElementValue {
        @Override
        public void testElementValue(
                TestResult testCase,
                ClassFile classFile,
                Annotation.element_value element_value,
                String[] values) throws ConstantPool.InvalidIndex {
            Annotation.Primitive_element_value ev =
                    (Annotation.Primitive_element_value) element_value;
            ConstantPool.CONSTANT_Utf8_info info =
                    (ConstantPool.CONSTANT_Utf8_info)
                            classFile.constant_pool.get(ev.const_value_index);
            testCase.checkEquals(info.value, values[0], "const_value_index");
        }
    }

    private class TestEnumElementValue extends TestElementValue {

        @Override
        public int getLength() {
            return 2;
        }

        @Override
        public void testElementValue(
                TestResult testCase,
                ClassFile classFile,
                Annotation.element_value element_value,
                String[] values)
                throws ConstantPool.InvalidIndex, ConstantPool.UnexpectedEntry {
            Annotation.Enum_element_value ev = (Annotation.Enum_element_value) element_value;
            testCase.checkEquals(classFile.constant_pool.getUTF8Info(ev.type_name_index).value,
                    values[0], "type_name_index");
            testCase.checkEquals(classFile.constant_pool.getUTF8Info(ev.const_name_index).value,
                    values[1], "const_name_index");
        }
    }

    private class TestClassElementValue extends TestElementValue {
        @Override
        public void testElementValue(
                TestResult testCase,
                ClassFile classFile,
                Annotation.element_value element_value,
                String[] values)
                throws ConstantPool.InvalidIndex, ConstantPool.UnexpectedEntry {
            Annotation.Class_element_value ev = (Annotation.Class_element_value) element_value;
            testCase.checkEquals(
                    classFile.constant_pool.getUTF8Info(ev.class_info_index).value,
                    values[0], "class_info_index");
        }
    }

    private class TestAnnotationElementValue extends TestElementValue {
        @Override
        public void testLength(TestResult testCase, AnnotationDefault_attribute attr) {
            // Suppress, since it is hard to test the length of this kind of element values.
        }

        @Override
        public int getLength() {
            // Expected that the test uses DefaultAnnotation
            // tag (1 byte) + annotation_value (2 bytes) which contains const_value
            return 3;
        }

        @Override
        public void testElementValue(
                TestResult testCase,
                ClassFile classFile,
                Annotation.element_value element_value,
                String[] values)
                throws ConstantPool.InvalidIndex, ConstantPool.UnexpectedEntry {
            Annotation ev = ((Annotation.Annotation_element_value) element_value)
                    .annotation_value;
            testCase.checkEquals(
                    classFile.constant_pool.getUTF8Info(ev.type_index).value,
                    values[0],
                    "type_index");
            for (int i = 0; i < ev.num_element_value_pairs; ++i) {
                Annotation.element_value_pair pair = ev.element_value_pairs[i];
                testCase.checkEquals(
                        classFile.constant_pool.getUTF8Info(pair.element_name_index).value,
                        values[2 * i + 1],
                        "element_name_index");
                TestElementValue testElementValue = verifiers.get(pair.value.tag);
                testElementValue.testElementValue(
                        testCase,
                        classFile,
                        pair.value,
                        new String[]{values[2 * i + 2]});
            }
        }
    }

    private class TestArrayElementValue extends TestElementValue {
        @Override
        public void testLength(TestResult testCase, AnnotationDefault_attribute attr) {
            Annotation.Array_element_value ev =
                    (Annotation.Array_element_value) attr.default_value;
            int sizeOfTag = ev.values[0].tag == 'e' ? 0 : 1;
            // tag (1 byte) + array header (2 byte) + length of entries
            testCase.checkEquals(attr.attribute_length, 1 + 2 +
                    (sizeOfTag + ev.length() / ev.num_values) * ev.num_values, "attribute_length");
        }

        @Override
        public void testElementValue(
                TestResult testCase,
                ClassFile classFile,
                Annotation.element_value element_value,
                String[] values)
                throws ConstantPool.InvalidIndex, ConstantPool.UnexpectedEntry {
            Annotation.Array_element_value ev =
                    (Annotation.Array_element_value) element_value;
            int index = 0;
            for (int i = 0; i < ev.num_values; ++i) {
                TestElementValue testElementValue = verifiers.get(ev.values[i].tag);
                int length = testElementValue.getLength();
                testElementValue.testElementValue(
                        testCase,
                        classFile,
                        ev.values[i],
                        testElementValue.getValues(values, index, length));
                index += length;
            }
        }
    }
}