1/*
2 * Copyright 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.tools.build.jetifier.core.proguard
18
19import com.android.tools.build.jetifier.core.type.JavaType
20import java.util.regex.Pattern
21
22/**
23 * Represents a type reference in ProGuard file. This type is similar to the regular java type but
24 * can also contain wildcards (*,**,?).
25 *
26 * ProGuard can also contain token {any}. This comes from the configuration and is simply used as
27 * a shortcut for multiple different wildcards (such as. "*", "**", "***", "*.*", "**.*").
28 */
29data class ProGuardType(val value: String) {
30
31    companion object {
32        val EXPANSION_TOKENS = listOf("*", "**", "***", "*/*", "**/*")
33
34        val TRIVIAL_SELECTOR_MATCHER: Pattern = Pattern.compile("^[/?*]*$")
35
36        /** Creates the type reference from notation where packages are separated using '.' */
37        fun fromDotNotation(type: String): ProGuardType {
38            return ProGuardType(type.replace('.', '/'))
39        }
40    }
41
42    init {
43        if (value.contains('.')) {
44            throw IllegalArgumentException("The type does not support '.' as package separator!")
45        }
46    }
47
48    /**
49     * Whether the type reference is trivial such as "*".
50     */
51    fun isTrivial() = TRIVIAL_SELECTOR_MATCHER.matcher(value).matches()
52
53    fun toJavaType(): JavaType? {
54        if (value.contains('*') || value.contains('?')) {
55            return null
56        }
57        return JavaType(value)
58    }
59
60    fun needsExpansion(): Boolean {
61        return value.contains("{any}")
62    }
63
64    fun expandWith(token: String): ProGuardType {
65        return ProGuardType(value.replace("{any}", token))
66    }
67
68    /** Returns the type reference as a string where packages are separated using '.' */
69    fun toDotNotation(): String {
70        return value.replace('/', '.')
71    }
72}