1 /*
2 * Copyright (C) 2017 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.tools.lint.checks.infrastructure
18
19 import com.android.tools.metalava.model.source.utils.DOT_JAVA
20 import com.android.tools.metalava.model.source.utils.DOT_KT
21 import java.util.regex.Pattern
22
23 // Copy in metalava from lint to avoid compilation dependency directly on lint-tests
24
25 /**
26 * A pair of package name and class name inferred from Java or Kotlin source code. The [source] is
27 * the source code, and the [extension] is the file extension (including the leading dot) which
28 * states whether this is a Kotlin source file, a Java source file, a Groovy source file, etc.
29 */
30 class ClassName(source: String, extension: String = DOT_JAVA) {
31 val packageName: String?
32 val className: String?
33
34 init {
35 val withoutComments = stripComments(source, extension)
36 packageName = getPackage(withoutComments)
37 className = getClassName(withoutComments)
38 }
39
packageNameWithDefaultnull40 fun packageNameWithDefault() = packageName ?: ""
41 }
42
43 /** Strips line and block comments from the given Java or Kotlin source file. */
44 @Suppress("LocalVariableName")
45 fun stripComments(source: String, extension: String, stripLineComments: Boolean = true): String {
46 val sb = StringBuilder(source.length)
47 var state = 0
48 val INIT = 0
49 val INIT_SLASH = 1
50 val LINE_COMMENT = 2
51 val BLOCK_COMMENT = 3
52 val BLOCK_COMMENT_ASTERISK = 4
53 val BLOCK_COMMENT_SLASH = 5
54 val IN_STRING = 6
55 val IN_STRING_ESCAPE = 7
56 val IN_CHAR = 8
57 val AFTER_CHAR = 9
58 var blockCommentDepth = 0
59 for (c in source) {
60 when (state) {
61 INIT -> {
62 when (c) {
63 '/' -> state = INIT_SLASH
64 '"' -> {
65 state = IN_STRING
66 sb.append(c)
67 }
68 '\'' -> {
69 state = IN_CHAR
70 sb.append(c)
71 }
72 else -> sb.append(c)
73 }
74 }
75 INIT_SLASH -> {
76 when {
77 c == '*' -> {
78 blockCommentDepth++
79 state = BLOCK_COMMENT
80 }
81 c == '/' && stripLineComments -> state = LINE_COMMENT
82 else -> {
83 state = INIT
84 sb.append('/') // because we skipped it in init
85 sb.append(c)
86 }
87 }
88 }
89 LINE_COMMENT -> {
90 when (c) {
91 '\n' -> state = INIT
92 }
93 }
94 BLOCK_COMMENT -> {
95 when (c) {
96 '*' -> state = BLOCK_COMMENT_ASTERISK
97 '/' -> state = BLOCK_COMMENT_SLASH
98 }
99 }
100 BLOCK_COMMENT_ASTERISK -> {
101 state =
102 when (c) {
103 '/' -> {
104 blockCommentDepth--
105 if (blockCommentDepth == 0) {
106 INIT
107 } else {
108 BLOCK_COMMENT
109 }
110 }
111 '*' -> BLOCK_COMMENT_ASTERISK
112 else -> BLOCK_COMMENT
113 }
114 }
115 BLOCK_COMMENT_SLASH -> {
116 if (c == '*' && extension == DOT_KT) {
117 blockCommentDepth++
118 }
119 if (c != '/') {
120 state = BLOCK_COMMENT
121 }
122 }
123 IN_STRING -> {
124 when (c) {
125 '\\' -> state = IN_STRING_ESCAPE
126 '"' -> state = INIT
127 }
128 sb.append(c)
129 }
130 IN_STRING_ESCAPE -> {
131 sb.append(c)
132 state = IN_STRING
133 }
134 IN_CHAR -> {
135 if (c != '\\') {
136 state = AFTER_CHAR
137 }
138 sb.append(c)
139 }
140 AFTER_CHAR -> {
141 sb.append(c)
142 if (c == '\\') {
143 state = INIT
144 }
145 }
146 }
147 }
148
149 return sb.toString()
150 }
151
152 private val PACKAGE_PATTERN = Pattern.compile("""package\s+([\S&&[^;]]*)""")
153
154 private val CLASS_PATTERN =
155 Pattern.compile(
156 """(\bclass\b|\binterface\b|\benum class\b|\benum\b|\bobject\b)+?\s*([^\s:(]+)""",
157 Pattern.MULTILINE
158 )
159
getPackagenull160 fun getPackage(source: String): String? {
161 val matcher = PACKAGE_PATTERN.matcher(source)
162 return if (matcher.find()) {
163 matcher.group(1).trim { it <= ' ' }
164 } else {
165 null
166 }
167 }
168
getClassNamenull169 fun getClassName(source: String): String? {
170 val matcher = CLASS_PATTERN.matcher(source.replace('\n', ' '))
171 var start = 0
172 while (matcher.find(start)) {
173 val cls = matcher.group(2)
174 val groupStart = matcher.start(1)
175
176 // Make sure this "class" reference isn't part of an annotation on the class
177 // referencing a class literal -- Foo.class, or in Kotlin, Foo::class.java)
178 if (groupStart == 0 || source[groupStart - 1] != '.' && source[groupStart - 1] != ':') {
179 val trimmed = cls.trim { it <= ' ' }
180 val typeParameter = trimmed.indexOf('<')
181 return if (typeParameter != -1) {
182 trimmed.substring(0, typeParameter)
183 } else {
184 trimmed
185 }
186 }
187 start = matcher.end(2)
188 }
189
190 return null
191 }
192