-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_annotation_attributes.js
More file actions
155 lines (121 loc) · 6.38 KB
/
test_annotation_attributes.js
File metadata and controls
155 lines (121 loc) · 6.38 KB
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
#!/usr/bin/env node
/**
* Test to verify that RuntimeVisibleAnnotations attributes are properly
* exposed in the AST for fields and methods.
*
* This test validates the fix for the annotation parsing bug.
*/
const fs = require('fs');
const { getAST, getClassFileStruct } = require('./lib');
function test_annotation_attributes_exposure() {
console.log('Testing annotation attributes exposure in AST...\n');
try {
// Ensure test class is compiled
if (!fs.existsSync('test/AnnotationReflectionTest.class')) {
console.error('❌ test/AnnotationReflectionTest.class not found. Please compile first.');
process.exit(1);
}
const classFileContent = fs.readFileSync('test/AnnotationReflectionTest.class');
const ast = getAST(classFileContent);
const struct = getClassFileStruct(classFileContent);
console.log('✓ Testing field annotation exposure...');
// Test field annotations
if (!ast.ast.fields || ast.ast.fields.length === 0) {
throw new Error('No fields found in AST');
}
const annotatedField = ast.ast.fields[0]; // annotatedField should be first
if (annotatedField.name !== 'annotatedField') {
throw new Error(`Expected field name 'annotatedField', got '${annotatedField.name}'`);
}
if (!annotatedField.attributes) {
throw new Error('Field attributes array is missing from AST');
}
if (annotatedField.attributes.length === 0) {
throw new Error('Field attributes array is empty in AST');
}
// Find RuntimeVisibleAnnotations attribute
const fieldAnnotationAttr = annotatedField.attributes.find(attr => {
return attr.attribute_name_index.name &&
attr.attribute_name_index.name.tag === 1 &&
attr.attribute_name_index.name.info.bytes === 'RuntimeVisibleAnnotations';
});
if (!fieldAnnotationAttr) {
throw new Error('RuntimeVisibleAnnotations attribute not found in field attributes');
}
if (!fieldAnnotationAttr.info.annotations || fieldAnnotationAttr.info.annotations.length === 0) {
throw new Error('Field annotation info is missing or empty');
}
console.log(` - Field has ${fieldAnnotationAttr.info.num_annotations} annotation(s)`);
console.log(` - Field annotation type index: ${fieldAnnotationAttr.info.annotations[0].type_index}`);
console.log(` - Field annotation element pairs: ${fieldAnnotationAttr.info.annotations[0].num_element_value_pairs}`);
console.log('✓ Testing method annotation exposure...');
// Test method annotations
if (!ast.ast.methods || ast.ast.methods.length < 2) {
throw new Error('Expected at least 2 methods in AST');
}
const annotatedMethod = ast.ast.methods[1]; // annotatedMethod should be second
if (annotatedMethod.name !== 'annotatedMethod') {
throw new Error(`Expected method name 'annotatedMethod', got '${annotatedMethod.name}'`);
}
if (!annotatedMethod.attributes) {
throw new Error('Method attributes array is missing from AST');
}
if (annotatedMethod.attributes.length === 0) {
throw new Error('Method attributes array is empty in AST');
}
// Find RuntimeVisibleAnnotations attribute
const methodAnnotationAttr = annotatedMethod.attributes.find(attr => {
return attr.attribute_name_index.name &&
attr.attribute_name_index.name.tag === 1 &&
attr.attribute_name_index.name.info.bytes === 'RuntimeVisibleAnnotations';
});
if (!methodAnnotationAttr) {
throw new Error('RuntimeVisibleAnnotations attribute not found in method attributes');
}
if (!methodAnnotationAttr.info.annotations || methodAnnotationAttr.info.annotations.length === 0) {
throw new Error('Method annotation info is missing or empty');
}
console.log(` - Method has ${methodAnnotationAttr.info.num_annotations} annotation(s)`);
console.log(` - Method annotation type index: ${methodAnnotationAttr.info.annotations[0].type_index}`);
console.log(` - Method annotation element pairs: ${methodAnnotationAttr.info.annotations[0].num_element_value_pairs}`);
console.log('✓ Testing annotation data integrity...');
// Test annotation data can be resolved through constant pool
const constantPool = struct.constant_pool.entries;
// Verify field annotation type
const fieldAnnotationType = constantPool[fieldAnnotationAttr.info.annotations[0].type_index];
if (!fieldAnnotationType || fieldAnnotationType.tag !== 1 || fieldAnnotationType.info.bytes !== 'LCustomAnnotation;') {
throw new Error('Field annotation type resolution failed');
}
// Verify method annotation type
const methodAnnotationType = constantPool[methodAnnotationAttr.info.annotations[0].type_index];
if (!methodAnnotationType || methodAnnotationType.tag !== 1 || methodAnnotationType.info.bytes !== 'LCustomAnnotation;') {
throw new Error('Method annotation type resolution failed');
}
console.log(' - Annotation type references are valid');
// Verify annotation values can be resolved
const fieldAnnotation = fieldAnnotationAttr.info.annotations[0];
for (const pair of fieldAnnotation.element_value_pairs) {
const elementName = constantPool[pair.element_name_index];
if (!elementName || elementName.tag !== 1) {
throw new Error('Annotation element name resolution failed');
}
const elementValue = constantPool[pair.value.value.const_value_index];
if (!elementValue) {
throw new Error('Annotation element value resolution failed');
}
}
console.log(' - Annotation element values are resolvable');
console.log('\n🎉 All tests passed! RuntimeVisibleAnnotations are properly exposed in AST.');
return true;
} catch (error) {
console.error('\n❌ Test failed:', error.message);
throw error;
}
}
// Run the test
try {
test_annotation_attributes_exposure();
process.exit(0);
} catch (error) {
process.exit(1);
}