1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.cts.apimap;
18 
19 import com.android.cts.ctsprofiles.AnnotationManagement;
20 
21 import org.objectweb.asm.AnnotationVisitor;
22 import org.objectweb.asm.Opcodes;
23 
24 /** A class for collecting annotation values. */
25 class AnnotationAnalyzer extends AnnotationVisitor {
26 
27     private final AnnotationManagement mAnnotationManagement;
28 
29     private final String mFieldSignature;
30 
AnnotationAnalyzer( AnnotationManagement annotationManagement, String fieldSignature)31     AnnotationAnalyzer(
32             AnnotationManagement annotationManagement,
33             String fieldSignature) {
34         super(Opcodes.ASM9);
35         mAnnotationManagement = annotationManagement;
36         mFieldSignature = fieldSignature;
37     }
38 
39     @Override
visitArray(String name)40     public AnnotationVisitor visitArray(String name) {
41         return new AnnotationAnalyzer(mAnnotationManagement, getFieldSignature(name));
42     }
43 
44     @Override
visit(String name, Object value)45     public void visit(String name, Object value) {
46         mAnnotationManagement.addTestMetadata(
47                 getFieldSignature(name), String.valueOf(value));
48     }
49 
50     /** Appends an annotation field name to the field signature. */
getFieldSignature(String fieldName)51     private String getFieldSignature(String fieldName) {
52         if (fieldName == null) {
53             return mFieldSignature;
54         }
55         return String.format("%s:%s", mFieldSignature, fieldName);
56     }
57 }
58