TypeUtils#getMethodReturnType extracted

This commit is contained in:
Tagir Valeev
2016-11-10 11:48:45 +07:00
parent 898b985223
commit 4465e4420b
8 changed files with 33 additions and 76 deletions
@@ -25,6 +25,7 @@ import com.intellij.psi.controlFlow.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.util.RefactoringUtil;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import gnu.trove.THashMap;
import gnu.trove.THashSet;
import org.jetbrains.annotations.Nls;
@@ -60,7 +61,7 @@ public class ReturnSeparatedFromComputationInspection extends BaseJavaBatchLocal
final PsiCodeBlock returnScope = (PsiCodeBlock)returnParent;
final PsiStatement[] statements = returnScope.getStatements();
if (statements.length != 0 && statements[statements.length - 1] == returnStatement) {
final PsiType returnType = getReturnType(returnScope);
final PsiType returnType = TypeUtils.getMethodReturnType(returnStatement);
if (returnType != null) {
PsiStatement refactoredStatement = getPrevNonEmptyStatement(returnStatement, null);
if (refactoredStatement != null) {
@@ -82,18 +83,6 @@ public class ReturnSeparatedFromComputationInspection extends BaseJavaBatchLocal
return null;
}
@Nullable
private static PsiType getReturnType(PsiCodeBlock returnScope) {
NavigatablePsiElement returnFrom = PsiTreeUtil.getNonStrictParentOfType(returnScope, PsiMethod.class, PsiLambdaExpression.class);
if (returnFrom instanceof PsiMethod) {
return ((PsiMethod)returnFrom).getReturnType();
}
if (returnFrom instanceof PsiLambdaExpression) {
return LambdaUtil.getFunctionalInterfaceReturnType((PsiLambdaExpression)returnFrom);
}
return null;
}
@Nullable
private static PsiCodeBlock getVariableScopeBlock(@Nullable PsiVariable variable) {
if (variable instanceof PsiLocalVariable) {
@@ -249,8 +238,6 @@ public class ReturnSeparatedFromComputationInspection extends BaseJavaBatchLocal
returnStatement = (PsiReturnStatement)returnStatement.copy();
PsiElement lastReturnChild = returnStatement.getLastChild();
Project project = returnStatement.getProject();
PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(project);
PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
for (PsiComment comment : keptComments) {
lastReturnChild = returnStatement.addAfter(comment, lastReturnChild);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -41,10 +41,10 @@ import com.intellij.refactoring.util.MoveRenameUsageInfo;
import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.usageView.UsageInfo;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.Processor;
import com.intellij.util.containers.HashMap;
import com.intellij.util.containers.HashSet;
import com.intellij.util.containers.Queue;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -532,9 +532,7 @@ public abstract class TurnRefsToSuperProcessorBase extends BaseRefactoringProces
final PsiElement parent = element.getParent();
if (parent instanceof PsiReturnStatement) {
final PsiElement el = PsiTreeUtil.getParentOfType(parent, PsiMethod.class, PsiLambdaExpression.class);
constrainingType = el instanceof PsiMethod ? ((PsiMethod)el).getReturnType()
: el instanceof PsiLambdaExpression ? LambdaUtil.getFunctionalInterfaceReturnType((PsiLambdaExpression)el) : null;
constrainingType = TypeUtils.getMethodReturnType(parent);
}
else if (parent instanceof PsiAssignmentExpression) {
constrainingType = ((PsiAssignmentExpression)parent).getLExpression().getType();