diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java index 6c39ca9b6ad9..29fd34f58574 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java @@ -95,19 +95,7 @@ public class TypeUtils { } public static boolean expressionHasTypeOrSubtype(@Nullable PsiExpression expression, @NonNls @NotNull String typeName) { - if (expression == null) { - return false; - } - final PsiType type = expression.getType(); - if (type == null) { - return false; - } - if (!(type instanceof PsiClassType)) { - return false; - } - final PsiClassType classType = (PsiClassType)type; - final PsiClass aClass = classType.resolve(); - return aClass != null && InheritanceUtil.isInheritor(aClass, typeName); + return expressionHasTypeOrSubtype(expression, new String[] {typeName}) != null; } //getTypeIfOneOfOrSubtype @@ -115,7 +103,10 @@ public class TypeUtils { if (expression == null) { return null; } - final PsiType type = expression.getType(); + PsiType type = expression.getType(); + if (type instanceof PsiLambdaExpressionType) { + type = ((PsiLambdaExpressionType)type).getExpression().getFunctionalInterfaceType(); + } if (type == null) { return null; } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/threading/ThreadWithDefaultRunMethodInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/threading/ThreadWithDefaultRunMethodInspection.java index 92e207373cac..249335f95f99 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/threading/ThreadWithDefaultRunMethodInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/threading/ThreadWithDefaultRunMethodInspection.java @@ -56,81 +56,52 @@ public class ThreadWithDefaultRunMethodInspection extends BaseInspection { @Override public void visitNewExpression(@NotNull PsiNewExpression expression) { super.visitNewExpression(expression); - final PsiAnonymousClass anonymousClass = - expression.getAnonymousClass(); - + final PsiAnonymousClass anonymousClass = expression.getAnonymousClass(); if (anonymousClass != null) { - final PsiJavaCodeReferenceElement baseClassReference = - anonymousClass.getBaseClassReference(); - final PsiElement referent = baseClassReference.resolve(); - if (referent == null) { - return; - } - final PsiClass referencedClass = (PsiClass)referent; - final String referencedClassName = - referencedClass.getQualifiedName(); - if (!"java.lang.Thread".equals(referencedClassName)) { - return; - } if (definesRun(anonymousClass)) { return; } - final PsiExpressionList argumentList = - expression.getArgumentList(); - if (argumentList == null) { - return; - } - final PsiExpression[] arguments = argumentList.getExpressions(); - for (PsiExpression argument : arguments) { - if (TypeUtils.expressionHasTypeOrSubtype(argument, - "java.lang.Runnable")) { - return; - } - } - registerNewExpressionError(expression); + processExpression(expression, anonymousClass.getBaseClassReference()); } else { - final PsiJavaCodeReferenceElement classReference = - expression.getClassReference(); + final PsiJavaCodeReferenceElement classReference = expression.getClassReference(); if (classReference == null) { return; } - final PsiElement referent = classReference.resolve(); - if (referent == null) { - return; - } - final PsiClass referencedClass = (PsiClass)referent; - final String referencedClassName = - referencedClass.getQualifiedName(); - if (!"java.lang.Thread".equals(referencedClassName)) { - return; - } - final PsiExpressionList argumentList = - expression.getArgumentList(); - if (argumentList == null) { - return; - } - final PsiExpression[] arguments = argumentList.getExpressions(); - for (PsiExpression argument : arguments) { - if (TypeUtils.expressionHasTypeOrSubtype(argument, - "java.lang.Runnable")) { - return; - } - } - registerNewExpressionError(expression); + processExpression(expression, classReference); } } + private void processExpression(PsiNewExpression expression, PsiJavaCodeReferenceElement baseClassReference) { + final PsiElement referent = baseClassReference.resolve(); + if (referent == null) { + return; + } + final PsiClass referencedClass = (PsiClass)referent; + final String referencedClassName = referencedClass.getQualifiedName(); + if (!"java.lang.Thread".equals(referencedClassName)) { + return; + } + final PsiExpressionList argumentList = expression.getArgumentList(); + if (argumentList == null) { + return; + } + final PsiExpression[] arguments = argumentList.getExpressions(); + for (PsiExpression argument : arguments) { + if (TypeUtils.expressionHasTypeOrSubtype(argument, "java.lang.Runnable")) { + return; + } + } + + registerNewExpressionError(expression); + } + private static boolean definesRun(PsiAnonymousClass aClass) { - final PsiMethod[] methods = aClass.getMethods(); + final PsiMethod[] methods = aClass.findMethodsByName(HardcodedMethodConstants.RUN, false); for (final PsiMethod method : methods) { - final String methodName = method.getName(); - if (HardcodedMethodConstants.RUN.equals(methodName)) { - final PsiParameterList parameterList = - method.getParameterList(); - if (parameterList.getParametersCount() == 0) { - return true; - } + final PsiParameterList parameterList = method.getParameterList(); + if (parameterList.getParametersCount() == 0) { + return true; } } return false; diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/threading/defaultRun/LambdaExpr.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/threading/defaultRun/LambdaExpr.java new file mode 100644 index 000000000000..453a9e956bbc --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/threading/defaultRun/LambdaExpr.java @@ -0,0 +1,6 @@ +package com.siyeh.igtest.threading.defaultRun; +class Test { + { + new Thread(() -> System.out.println("hello")).start(); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/threading/defaultRun/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/threading/defaultRun/expected.xml new file mode 100644 index 000000000000..216a575c3ed4 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/threading/defaultRun/expected.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/threading/ThreadWithDefaultRunInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/threading/ThreadWithDefaultRunInspectionTest.java new file mode 100644 index 000000000000..5160c9812ec7 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/threading/ThreadWithDefaultRunInspectionTest.java @@ -0,0 +1,25 @@ +/* + * Copyright 2000-2013 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.siyeh.ig.threading; + +import com.siyeh.ig.IGInspectionTestCase; + +public class ThreadWithDefaultRunInspectionTest extends IGInspectionTestCase { + + public void test() throws Exception { + doTest("com/siyeh/igtest/threading/defaultRun", new ThreadWithDefaultRunMethodInspection()); + } +} \ No newline at end of file