java: always return not-null when correcting types based on resolve scope

This commit is contained in:
peter
2015-04-07 18:23:00 +02:00
parent 6f97db6bb8
commit 2018eb9176
3 changed files with 70 additions and 20 deletions
@@ -34,10 +34,7 @@ import com.intellij.psi.scope.NameHint;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.scope.processor.FilterScopeProcessor;
import com.intellij.psi.scope.processor.MethodResolverProcessor;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.PackageScope;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.*;
import com.intellij.psi.util.*;
import com.intellij.ui.IconDeferrer;
import com.intellij.ui.RowIcon;
@@ -738,7 +735,7 @@ public class PsiClassImplUtil {
final PsiClassType.ClassResolveResult superTypeResolveResult = superType.resolveGenerics();
PsiClass superClass = superTypeResolveResult.getElement();
if (superClass == null) continue;
if (superClass == null || !PsiSearchScopeUtil.isInScope(resolveScope, superClass)) continue;
PsiSubstitutor finalSubstitutor = obtainFinalSubstitutor(superClass, superTypeResolveResult.getSubstitutor(), aClass,
state.get(PsiSubstitutor.KEY), factory, languageLevel);
if (!processDeclarationsInClass(superClass, processor, state.put(PsiSubstitutor.KEY, finalSubstitutor), visited, last, place,
@@ -61,7 +61,7 @@ class TypeCorrector extends PsiTypeMapper {
PsiUtilCore.ensureValid(psiClass);
final PsiClass mappedClass = mapClass(psiClass);
if (mappedClass == null) return null;
if (mappedClass == null) return classType;
PsiClassType mappedType = new PsiCorrectedClassType(classType.getLanguageLevel(),
classType,
@@ -70,6 +70,7 @@ class TypeCorrector extends PsiTypeMapper {
return mappedType;
}
@Nullable
private PsiClass mapClass(@NotNull PsiClass psiClass) {
String qualifiedName = psiClass.getQualifiedName();
if (qualifiedName == null) {
@@ -84,18 +85,18 @@ class TypeCorrector extends PsiTypeMapper {
return JavaPsiFacade.getInstance(psiClass.getProject()).findClass(qualifiedName, myResolveScope);
}
@Nullable
@NotNull
private PsiSubstitutor mapSubstitutor(PsiClass originalClass, PsiClass mappedClass, PsiSubstitutor substitutor) {
PsiTypeParameter[] typeParameters = mappedClass.getTypeParameters();
PsiTypeParameter[] originalTypeParameters = originalClass.getTypeParameters();
if (typeParameters.length != originalTypeParameters.length) return null;
if (typeParameters.length != originalTypeParameters.length) return substitutor;
PsiSubstitutor mappedSubstitutor = PsiSubstitutor.EMPTY;
for (int i = 0; i < originalTypeParameters.length; i++) {
PsiType originalSubstitute = substitutor.substitute(originalTypeParameters[i]);
if (originalSubstitute != null) {
PsiType substitute = mapType(originalSubstitute);
if (substitute == null) return null;
if (substitute == null) return substitutor;
mappedSubstitutor = mappedSubstitutor.put(typeParameters[i], substitute);
}
@@ -109,15 +110,13 @@ class TypeCorrector extends PsiTypeMapper {
}
PsiClass mappedContaining = mappedClass.getContainingClass();
PsiClass originalContaining = originalClass.getContainingClass();
//noinspection DoubleNegation
if ((mappedContaining != null) != (originalContaining != null)) {
return null;
return substitutor;
}
if (mappedContaining != null) {
PsiSubstitutor outerSubstitutor = mapSubstitutor(originalContaining, mappedContaining, substitutor);
if (outerSubstitutor == null) return null;
return mappedSubstitutor.putAll(outerSubstitutor);
return mappedSubstitutor.putAll(mapSubstitutor(originalContaining, mappedContaining, substitutor));
}
return mappedSubstitutor;
@@ -244,12 +243,7 @@ class TypeCorrector extends PsiTypeMapper {
public PsiSubstitutor getSubstitutor() {
PsiSubstitutor result = myLazySubstitutor;
if (result == null) {
result = mapSubstitutor(myPsiClass, myMappedClass, mySubstitutor);
if (result == null) {
//better ideas?
result = JavaPsiFacade.getElementFactory(myMappedClass.getProject()).createRawSubstitutor(myMappedClass);
}
myLazySubstitutor = result;
myLazySubstitutor = result = mapSubstitutor(myPsiClass, myMappedClass, mySubstitutor);
}
return result;
}
@@ -0,0 +1,59 @@
/*
* 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 com.intellij.codeInsight
import com.intellij.codeInspection.InspectionEngine
import com.intellij.codeInspection.InspectionManager
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper
import com.intellij.codeInspection.uncheckedWarnings.UncheckedWarningLocalInspection
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.testFramework.PsiTestUtil
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase
/**
* @author peter
*/
class NonSourceInspectionTest extends JavaCodeInsightFixtureTestCase {
public void "test inspection outside source root"() {
PsiTestUtil.removeAllRoots(myModule, ModuleRootManager.getInstance(myModule).sdk)
PsiTestUtil.addSourceRoot(myModule, myFixture.tempDirFixture.findOrCreateDir("src"))
myFixture.addFileToProject("src/foo/GenericQuery.java", """
package foo;
public interface GenericQuery<T> {
public T execute();
}
""")
def file = myFixture.addFileToProject("SomeClass.java", """
import foo.GenericQuery;
import java.util.Collection;
class SomeClass {
Collection<User> foo(GenericQuery<Collection<User>> query) {
return query.execute();
}
class User {}
}
""")
def wrapper = new LocalInspectionToolWrapper(new UncheckedWarningLocalInspection())
def context = InspectionManager.getInstance(project).createNewGlobalContext(false)
assertEmpty InspectionEngine.runInspectionOnFile(file, wrapper, context)
}
}