1 /*
2 * Copyright (C) 2025 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 package com.android.platform.test.ravenwood.ravenhelper.psi
17
18 import com.android.tools.lint.UastEnvironment
19
20 // PSI is a library to parse Java/Kotlin source files, which is part of JetBrains' IntelliJ/
21 // Android Studio, and other IDEs.
22 //
23 // PSI is normally used by IntelliJ's plugins, and as such, there isn't really a good documentation
24 // on how to use it from a standalone program. However, fortunately, Android Studio's Lint
25 // and Metalava both use PSI. Metalava reuses some of the APIs exposed by Lint. We also use the
26 // same Lint APIs used by Metalava here.
27 //
28 // Some code pointers around the relevant projects:
29 //
30 // - We stole code from Metalava, but the recent version of Metalava is too complicated,
31 // and hard to understand. Older Metalava, such as this one:
32 // https://android.git.corp.google.com/platform/tools/metalava/+/refs/heads/android13-dev
33 // is easier to understand.
34 //
35 // - PSI is source code is available in IntelliJ's code base:
36 // https://github.com/JetBrains/intellij-community.git
37 //
38 // - Lint is in Android studio.
39 // https://android.googlesource.com/platform/tools/base/+/studio-master-dev/source.md
40
41
42 /**
43 * Create [UastEnvironment] enough to parse Java source files.
44 */
createUastEnvironmentnull45 fun createUastEnvironment(): UastEnvironment {
46 val config = UastEnvironment.Configuration.create(
47 enableKotlinScripting = false,
48 useFirUast = false,
49 )
50
51 config.javaLanguageLevel = com.intellij.pom.java.LanguageLevel.JDK_21
52
53 // The following code exists in Metalava, but we don't seem to need it.
54 // We may need to when we need to support kotlin.
55 // config.kotlinLanguageLevel = kotlinLanguageLevel
56 // config.addSourceRoots(listOf(File(root)))
57 // config.addClasspathRoots(classpath.map { it.absoluteFile })
58 // options.jdkHome?.let {
59 // if (options.isJdkModular(it)) {
60 // config.kotlinCompilerConfig.put(JVMConfigurationKeys.JDK_HOME, it)
61 // config.kotlinCompilerConfig.put(JVMConfigurationKeys.NO_JDK, false)
62 // }
63 // }
64
65 return UastEnvironment.create(config)
66 }
67