Replace UastConverter with UastLanguagePlugin, allow additional checkers

This commit is contained in:
Yan Zhulanow
2016-03-10 16:25:18 +03:00
parent 4ef6016ee8
commit d1939d52bc
16 changed files with 105 additions and 68 deletions
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.uast.check
import org.jetbrains.uast.UastAdditionalChecker
interface AndroidUastAdditionalChecker : UastAdditionalChecker
@@ -26,7 +26,7 @@ import com.intellij.psi.PsiManager
import org.jetbrains.uast.*
import org.jetbrains.uast.UastCallKind.Companion.CONSTRUCTOR_CALL
import org.jetbrains.uast.UastCallKind.Companion.FUNCTION_CALL
import org.jetbrains.uast.java.JavaConverter
import org.jetbrains.uast.java.JavaUastLanguagePlugin
import org.jetbrains.uast.visitor.UastVisitor
import java.io.File
@@ -41,12 +41,12 @@ object UastChecker {
fun checkWithCustomHandler(
project: Project,
file: File,
converters: List<UastConverter>,
converters: List<UastLanguagePlugin>,
visitor: UastVisitor) {
check(project, file, converters, UastHandler { visitor.handle(it) })
}
fun check(project: Project, file: File, converters: List<UastConverter>, handler: UastHandler) {
fun check(project: Project, file: File, plugins: List<UastLanguagePlugin>, handler: UastHandler) {
val vfile = VirtualFileManager.getInstance().findFileByUrl("file://" + file.absolutePath) ?: return
ApplicationManager.getApplication().runReadAction {
val psiFile = PsiManager.getInstance(project).findFile(vfile)
@@ -54,11 +54,11 @@ object UastChecker {
if (psiFile != null) {
when (psiFile) {
is PsiJavaFile -> {
val ufile = JavaConverter.convert(psiFile)
ufile.handleTraverse(handler)
val ufile = JavaUastLanguagePlugin.converter.convertWithParent(psiFile)
ufile?.handleTraverse(handler)
}
else -> for (converter in converters) {
val ufile = converter.convertWithParent(psiFile)
else -> for (plugin in plugins) {
val ufile = plugin.converter.convertWithParent(psiFile)
if (ufile != null) {
ufile.handleTraverse(handler)
break
@@ -69,12 +69,20 @@ object UastChecker {
}
}
fun check(project: Project, file: File, scanner: UastScanner, converters: List<UastConverter>, context: UastAndroidContext) {
fun check(project: Project, file: File, scanner: UastScanner, plugins: List<UastLanguagePlugin>, context: UastAndroidContext) {
val applicableFunctionNames = scanner.applicableFunctionNames ?: emptyList()
val applicableSuperClasses = scanner.applicableSuperClasses ?: emptyList()
val applicableConstructorTypes = scanner.applicableConstructorTypes ?: emptyList()
check(project, file, converters, UastHandler { element ->
val additionalCheckers = plugins.fold(mutableListOf<UastAdditionalChecker>()) { list, plugin ->
for (checker in plugin.additionalCheckers) {
if (checker is AndroidUastAdditionalChecker) list += checker
}
list
}
var handler: UastHandler? = null
handler = UastHandler { element ->
when (element) {
is UCallExpression -> {
if (applicableFunctionNames.isNotEmpty()) {
@@ -99,7 +107,13 @@ object UastChecker {
}
}
}
})
for (checker in additionalCheckers) {
checker(element, handler!!, context)
}
}
check(project, file, plugins, handler)
}
}