1/*
2 * Copyright (C) 2018 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
17package com.android.sdkparcelables
18
19import org.objectweb.asm.ClassVisitor
20import java.util.*
21
22data class Ancestors(val superName: String?, val interfaces: List<String>?)
23
24/** A class that implements an ASM ClassVisitor that collects super class and
25 * implemented interfaces for each class that it visits.
26 */
27class AncestorCollector(api: Int, dest: ClassVisitor?) : ClassVisitor(api, dest) {
28    private val _ancestors = LinkedHashMap<String, Ancestors>()
29
30    val ancestors: Map<String, Ancestors>
31        get() = _ancestors
32
33    override fun visit(version: Int, access: Int, name: String?, signature: String?,
34                       superName: String?, interfaces: Array<out String>?) {
35        name!!
36
37        val old = _ancestors.put(name, Ancestors(superName, interfaces?.toList()))
38        if (old != null) {
39            throw RuntimeException("class $name already found")
40        }
41
42        super.visit(version, access, name, signature, superName, interfaces)
43    }
44}
45