From 7031b474cff8094d7a93d619beb4eaa6a369771a Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 20 May 2015 16:08:44 +0200 Subject: [PATCH] IDEA-CR-2791 (static compilation; test) --- .../GrDependencyVisitorFactory.groovy | 10 +++-- .../GrDependencyVisitorTest.groovy | 40 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/codeInspection/dependencies/GrDependencyVisitorTest.groovy diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/dependencies/GrDependencyVisitorFactory.groovy b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/dependencies/GrDependencyVisitorFactory.groovy index cccaa3954c67..62f1f08ceb8a 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/dependencies/GrDependencyVisitorFactory.groovy +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/dependencies/GrDependencyVisitorFactory.groovy @@ -21,6 +21,8 @@ import com.intellij.packageDependencies.DependencyVisitorFactory import com.intellij.psi.PsiElement import com.intellij.psi.PsiElementVisitor import com.intellij.psi.PsiFile +import com.intellij.psi.PsiReference +import groovy.transform.CompileStatic import org.jetbrains.annotations.NotNull import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor @@ -30,6 +32,7 @@ import org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement +@CompileStatic class GrDependencyVisitorFactory extends DependencyVisitorFactory { @NotNull @Override @@ -46,6 +49,7 @@ class GrDependencyVisitorFactory extends DependencyVisitorFactory { } } + @CompileStatic private static class MyVisitor extends GroovyRecursiveElementVisitor { private final DependenciesBuilder.DependencyProcessor myProcessor private final DependencyVisitorFactory.VisitorOptions myOptions @@ -61,10 +65,10 @@ class GrDependencyVisitorFactory extends DependencyVisitorFactory { super.visitElement(element) - element.references.each { - PsiElement resolved = it.resolve(); + element.references.each { PsiReference ref -> + PsiElement resolved = ref.resolve(); if (resolved != null) { - myProcessor.process(it.element, resolved); + myProcessor.process(ref.element, resolved); } } } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/codeInspection/dependencies/GrDependencyVisitorTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/codeInspection/dependencies/GrDependencyVisitorTest.groovy new file mode 100644 index 000000000000..a83499a1cf17 --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/codeInspection/dependencies/GrDependencyVisitorTest.groovy @@ -0,0 +1,40 @@ +/* + * Copyright 2000-2015 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.plugins.groovy.codeInspection.dependencies + +import com.intellij.packageDependencies.DependenciesBuilder +import com.intellij.packageDependencies.DependencyVisitorFactory +import groovy.transform.CompileStatic +import org.jetbrains.plugins.groovy.LightGroovyTestCase + +@CompileStatic +class GrDependencyVisitorTest extends LightGroovyTestCase { + @Override + protected String getBasePath() { + return "" + } + + void test() { + def file = myFixture.addFileToProject("C.groovy", "import groovy.util.ConfigObject\nclass C { }") + + def deps = [] + DependenciesBuilder.analyzeFileDependencies(file, { _, dep -> deps << dep }, DependencyVisitorFactory.VisitorOptions.SKIP_IMPORTS) + assert deps.size() == 0 + + DependenciesBuilder.analyzeFileDependencies(file, { _, dep -> deps << dep }, DependencyVisitorFactory.VisitorOptions.INCLUDE_IMPORTS) + assert deps.size() == 3 + } +}