1 /*
2  * Copyright (C) 2022 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.traceinjection;
18 
19 import org.objectweb.asm.ClassVisitor;
20 import org.objectweb.asm.MethodVisitor;
21 import org.objectweb.asm.Opcodes;
22 
23 /**
24  * {@link ClassVisitor} that injects tracing code to methods annotated with the configured
25  * annotation.
26  */
27 public class TraceInjectionClassVisitor extends ClassVisitor {
28     private final TraceInjectionConfiguration mParams;
TraceInjectionClassVisitor(ClassVisitor classVisitor, TraceInjectionConfiguration params)29     public TraceInjectionClassVisitor(ClassVisitor classVisitor,
30             TraceInjectionConfiguration params) {
31         super(Opcodes.ASM9, classVisitor);
32         mParams = params;
33     }
34 
35     @Override
visitMethod(int access, String name, String desc, String signature, String[] exceptions)36     public MethodVisitor visitMethod(int access, String name, String desc, String signature,
37             String[] exceptions) {
38         MethodVisitor chain = super.visitMethod(access, name, desc, signature, exceptions);
39         return new TraceInjectionMethodAdapter(chain, access, name, desc, mParams);
40     }
41 }
42