mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 21:55:01 +07:00
IDEA-87991 Static type checking false negative ignoring automatic type conversion
This commit is contained in:
+1
-1
@@ -55,7 +55,7 @@ public class FileEqualsUsageInspection extends InternalInspection {
|
||||
if (clazz == null) return;
|
||||
|
||||
String methodName = method.getName();
|
||||
if ("java.io.File".equals(clazz.getQualifiedName())
|
||||
if (CommonClassNames.JAVA_IO_FILE.equals(clazz.getQualifiedName())
|
||||
&& ("equals".equals(methodName) || "compareTo".equals(methodName) || "hashCode".equals(methodName))) {
|
||||
holder.registerProblem(methodExpression,
|
||||
"Do not use File.equals/hashCode/compareTo as they don't honor case-sensitivity on MacOS. Use FileUtil.filesEquals/fileHashCode/compareFiles instead",
|
||||
|
||||
@@ -87,4 +87,5 @@ public interface CommonClassNames {
|
||||
@NonNls String JAVA_LANG_INVOKE_MH_POLYMORPHIC = "java.lang.invoke.MethodHandle.PolymorphicSignature";
|
||||
String TARGET_ANNOTATION_FQ_NAME = "java.lang.annotation.Target";
|
||||
@NonNls String JAVA_LANG_RUNNABLE = "java.lang.Runnable";
|
||||
@NonNls String JAVA_IO_FILE = "java.io.File";
|
||||
}
|
||||
|
||||
@@ -126,6 +126,7 @@
|
||||
<membersContributor implementation="org.jetbrains.plugins.groovy.gant.GantMemberContributor"/>
|
||||
|
||||
<typeConverter implementation="org.jetbrains.plugins.groovy.lang.psi.impl.GrContainerTypeConverter"/>
|
||||
<typeConverter implementation="org.jetbrains.plugins.groovy.lang.psi.impl.types.GrStringTypeConverter"/>
|
||||
|
||||
<typeConverter implementation="org.jetbrains.plugins.groovy.gpp.GppTypeConverter"/>
|
||||
<expectedTypesContributor implementation="org.jetbrains.plugins.groovy.gpp.GppExpectedTypesContributor"/>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package org.jetbrains.plugins.groovy.lang.psi;
|
||||
|
||||
import com.intellij.openapi.extensions.ExtensionPointName;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiClassType;
|
||||
import com.intellij.psi.PsiType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -19,4 +21,20 @@ public abstract class GrTypeConverter {
|
||||
@Nullable
|
||||
public abstract Boolean isConvertible(@NotNull PsiType lType, @NotNull PsiType rType, @NotNull GroovyPsiElement context);
|
||||
|
||||
protected static boolean resolvesTo(PsiType type, String fqn) {
|
||||
if (type instanceof PsiClassType) {
|
||||
final PsiClass resolved = ((PsiClassType)type).resolve();
|
||||
return resolved != null && fqn.equals(resolved.getQualifiedName());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected static boolean isEnum(PsiType type) {
|
||||
if (type instanceof PsiClassType) {
|
||||
final PsiClass resolved = ((PsiClassType)type).resolve();
|
||||
return resolved != null && resolved.isEnum();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2000-2012 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.lang.psi.impl.types;
|
||||
|
||||
import com.intellij.psi.PsiType;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.config.GroovyConfigUtils;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GrTypeConverter;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
|
||||
|
||||
import static com.intellij.psi.CommonClassNames.JAVA_LANG_BOOLEAN;
|
||||
import static com.intellij.psi.CommonClassNames.JAVA_LANG_CLASS;
|
||||
import static org.jetbrains.plugins.groovy.lang.psi.util.GroovyCommonClassNames.GROOVY_LANG_GSTRING;
|
||||
import static org.jetbrains.plugins.groovy.lang.psi.util.GroovyCommonClassNames.JAVA_LANG_CHAR_SEQUENCE;
|
||||
|
||||
/**
|
||||
* @author Max Medvedev
|
||||
*/
|
||||
public class GrStringTypeConverter extends GrTypeConverter {
|
||||
@Override
|
||||
public Boolean isConvertible(@NotNull PsiType lType, @NotNull PsiType rType, @NotNull GroovyPsiElement context) {
|
||||
if (isMethodCallConversion(context)) return null;
|
||||
if (!GroovyConfigUtils.getInstance().isVersionAtLeast(context, GroovyConfigUtils.GROOVY1_8)) return null;
|
||||
if (!(InheritanceUtil.isInheritor(rType, JAVA_LANG_CHAR_SEQUENCE) || InheritanceUtil.isInheritor(rType, GROOVY_LANG_GSTRING))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (lType == PsiType.BOOLEAN || resolvesTo(lType, JAVA_LANG_BOOLEAN)) return true;
|
||||
if (resolvesTo(lType, JAVA_LANG_CLASS)) return true;
|
||||
if (isEnum(lType)) return true;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+9
-8
@@ -23,6 +23,7 @@ import org.jetbrains.plugins.groovy.lang.psi.util.GroovyCommonClassNames;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.intellij.psi.CommonClassNames.JAVA_IO_FILE;
|
||||
import static org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.skipParentheses;
|
||||
|
||||
/**
|
||||
@@ -44,13 +45,13 @@ public class ClosureParameterEnhancer extends AbstractClosureParameterEnhancer {
|
||||
simpleTypes.put("withDataOutputStream", "java.io.DataOutputStream");
|
||||
simpleTypes.put("withDataInputStream", "java.io.DataInputStream");
|
||||
simpleTypes.put("eachLine", "java.lang.String");
|
||||
simpleTypes.put("eachFile", "java.io.File");
|
||||
simpleTypes.put("eachDir", "java.io.File");
|
||||
simpleTypes.put("eachFileRecurse", "java.io.File");
|
||||
simpleTypes.put("traverse", "java.io.File");
|
||||
simpleTypes.put("eachDirRecurse", "java.io.File");
|
||||
simpleTypes.put("eachFileMatch", "java.io.File");
|
||||
simpleTypes.put("eachDirMatch", "java.io.File");
|
||||
simpleTypes.put("eachFile", JAVA_IO_FILE);
|
||||
simpleTypes.put("eachDir", JAVA_IO_FILE);
|
||||
simpleTypes.put("eachFileRecurse", JAVA_IO_FILE);
|
||||
simpleTypes.put("traverse", JAVA_IO_FILE);
|
||||
simpleTypes.put("eachDirRecurse", JAVA_IO_FILE);
|
||||
simpleTypes.put("eachFileMatch", JAVA_IO_FILE);
|
||||
simpleTypes.put("eachDirMatch", JAVA_IO_FILE);
|
||||
simpleTypes.put("withReader", "java.io.Reader");
|
||||
simpleTypes.put("withWriter", "java.io.Writer");
|
||||
simpleTypes.put("withWriterAppend", "java.io.Writer");
|
||||
@@ -259,7 +260,7 @@ public class ClosureParameterEnhancer extends AbstractClosureParameterEnhancer {
|
||||
return res;
|
||||
}
|
||||
|
||||
if (TypesUtil.isClassType(iterType, CommonClassNames.JAVA_LANG_STRING) || TypesUtil.isClassType(iterType, "java.io.File")) {
|
||||
if (TypesUtil.isClassType(iterType, CommonClassNames.JAVA_LANG_STRING) || TypesUtil.isClassType(iterType, JAVA_IO_FILE)) {
|
||||
return TypesUtil.createTypeByFQClassName(CommonClassNames.JAVA_LANG_STRING, context);
|
||||
}
|
||||
|
||||
|
||||
+1
@@ -54,6 +54,7 @@ public final class GroovyCommonClassNames {
|
||||
@NonNls public static final String GROOVY_TRANSFORM_COMPILE_STATIC = "groovy.transform.CompileStatic";
|
||||
@NonNls public static final String GROOVY_TRANSFORM_TYPE_CHECKED = "groovy.transform.TypeChecked";
|
||||
@NonNls public static final String GROOVY_TRANSFORM_TYPE_CHECKING_MODE = "groovy.transform.TypeCheckingMode";
|
||||
@NonNls public static final String JAVA_LANG_CHAR_SEQUENCE = "java.lang.CharSequence";
|
||||
|
||||
|
||||
private GroovyCommonClassNames() {
|
||||
|
||||
@@ -604,11 +604,11 @@ println new Bar().zzz
|
||||
}
|
||||
|
||||
class GppProjectDescriptor extends DefaultLightProjectDescriptor {
|
||||
static def instance = new GppProjectDescriptor()
|
||||
public static final instance = new GppProjectDescriptor()
|
||||
|
||||
@Override
|
||||
public void configureModule(Module module, ModifiableRootModel model, ContentEntry contentEntry) {
|
||||
final Library.ModifiableModel modifiableModel = model.getModuleLibraryTable().createLibrary("GROOVY++").getModifiableModel();
|
||||
final Library.ModifiableModel modifiableModel = model.moduleLibraryTable.createLibrary("GROOVY++").modifiableModel;
|
||||
modifiableModel.addRoot(JarFileSystem.instance.refreshAndFindFileByPath(TestUtils.absoluteTestDataPath + "mockGroovypp/groovypp-0.9.0_1.8.2.jar!/"), OrderRootType.CLASSES)
|
||||
modifiableModel.addRoot(JarFileSystem.instance.refreshAndFindFileByPath(TestUtils.mockGroovy1_7LibraryName + "!/"), OrderRootType.CLASSES);
|
||||
modifiableModel.commit();
|
||||
|
||||
Reference in New Issue
Block a user