mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
refactored; accept lambda as run argument (IDEA-118775)
This commit is contained in:
+5
-14
@@ -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;
|
||||
}
|
||||
|
||||
+32
-61
@@ -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;
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.siyeh.igtest.threading.defaultRun;
|
||||
class Test {
|
||||
{
|
||||
new Thread(() -> System.out.println("hello")).start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<problems/>
|
||||
+25
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user