Merge remote-tracking branch 'origin/master'

This commit is contained in:
Konstantin Bulenkov
2014-12-17 22:57:15 +01:00
18 changed files with 241 additions and 67 deletions
@@ -88,7 +88,10 @@ public class RedundantLambdaCodeBlockInspection extends BaseJavaBatchLocalInspec
final List<CandidateInfo> info = new ArrayList<CandidateInfo>(Arrays.asList(candidates));
final LanguageLevel level = PsiUtil.getLanguageLevel(parent);
final JavaMethodsConflictResolver conflictResolver = new JavaMethodsConflictResolver((PsiExpressionList)parent, level);
conflictResolver.checkSpecifics(info, MethodCandidateInfo.ApplicabilityLevel.FIXED_ARITY, level);
final PsiExpressionList argumentList = ((PsiCallExpression)gParent).getArgumentList();
if (argumentList == null) return;
JavaMethodsConflictResolver.checkParametersNumber(info, argumentList.getExpressions().length, false);
conflictResolver.checkSpecifics(info, MethodCandidateInfo.ApplicabilityLevel.VARARGS, level);
if (info.size() > 1) {
return;
}
@@ -18,6 +18,7 @@ package com.intellij.refactoring.introduceField;
import com.intellij.codeInsight.TestFrameworks;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.*;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.searches.ReferencesSearch;
@@ -28,12 +29,15 @@ import com.intellij.refactoring.ui.TypeSelectorManager;
import com.intellij.ui.NonFocusableCheckBox;
import com.intellij.ui.StateRestoringCheckBox;
import com.intellij.util.Processor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashSet;
import java.util.Set;
/**
* User: anna
@@ -87,45 +91,69 @@ public abstract class IntroduceFieldCentralPanel {
myTypeSelectorManager = typeSelectorManager;
}
protected boolean setEnabledInitializationPlaces(PsiElement initializerPart, PsiElement initializer) {
if (initializerPart instanceof PsiReferenceExpression) {
PsiReferenceExpression refExpr = (PsiReferenceExpression)initializerPart;
if (refExpr.getQualifierExpression() == null) {
PsiElement refElement = refExpr.resolve();
if (refElement == null ||
(refElement instanceof PsiLocalVariable ||
refElement instanceof PsiParameter ||
(refElement instanceof PsiField && !((PsiField)refElement).hasInitializer())) &&
!PsiTreeUtil.isAncestor(initializer, refElement, true)) {
return updateInitializationPlaceModel(initializedInSetUp(refElement));
protected boolean setEnabledInitializationPlaces(@NotNull final PsiElement initializer) {
final Set<PsiField> fields = new HashSet<PsiField>();
final Ref<Boolean> refsLocal = new Ref<Boolean>(false);
initializer.accept(new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitReferenceExpression(PsiReferenceExpression expression) {
super.visitReferenceExpression(expression);
if (expression.getQualifierExpression() == null) {
final PsiElement resolve = expression.resolve();
if (resolve == null ||
resolve instanceof PsiVariable && !PsiTreeUtil.isAncestor(initializer, resolve, true)) {
if (resolve instanceof PsiField) {
if (!((PsiField)resolve).hasInitializer()) {
fields.add((PsiField)resolve);
}
}
else {
refsLocal.set(true);
stopWalking();
}
}
}
}
});
final boolean locals = refsLocal.get();
if (!locals && fields.isEmpty()) {
return true;
}
PsiElement[] children = initializerPart.getChildren();
for (PsiElement child : children) {
if (!setEnabledInitializationPlaces(child, initializer)) return false;
return updateInitializationPlaceModel(!locals && initializedInSetUp(fields),
!locals && initializedInConstructor(fields));
}
private static boolean initializedInConstructor(Set<PsiField> fields) {
for (PsiField field : fields) {
if (!field.hasModifierProperty(PsiModifier.FINAL)) {
return false;
}
}
return true;
}
private boolean initializedInSetUp(PsiElement refElement) {
if (refElement instanceof PsiField && hasSetUpChoice()) {
final PsiMethod setUpMethod = TestFrameworks.getInstance().findSetUpMethod(((PsiField)refElement).getContainingClass());
if (setUpMethod != null) {
final Processor<PsiReference> initializerSearcher = new Processor<PsiReference>() {
@Override
public boolean process(PsiReference reference) {
final PsiElement referenceElement = reference.getElement();
if (referenceElement instanceof PsiExpression) {
return !PsiUtil.isAccessedForWriting((PsiExpression)referenceElement);
private boolean initializedInSetUp(Set<PsiField> fields) {
if (hasSetUpChoice()) {
for (PsiField field : fields) {
final PsiMethod setUpMethod = TestFrameworks.getInstance().findSetUpMethod((field).getContainingClass());
if (setUpMethod != null) {
final Processor<PsiReference> initializerSearcher = new Processor<PsiReference>() {
@Override
public boolean process(PsiReference reference) {
final PsiElement referenceElement = reference.getElement();
if (referenceElement instanceof PsiExpression) {
return !PsiUtil.isAccessedForWriting((PsiExpression)referenceElement);
}
return true;
}
return true;
};
if (ReferencesSearch.search(field, new LocalSearchScope(setUpMethod)).forEach(initializerSearcher)) {
return false;
}
};
if (!ReferencesSearch.search(refElement, new LocalSearchScope(setUpMethod)).forEach(initializerSearcher)) {
return true;
}
}
return true;
}
return false;
}
@@ -305,7 +333,7 @@ public abstract class IntroduceFieldCentralPanel {
}
}
protected abstract boolean updateInitializationPlaceModel(boolean initializedInsetup);
protected abstract boolean updateInitializationPlaceModel(boolean initializedInsetup, boolean initializedInConstructor);
protected abstract boolean hasSetUpChoice();
}
@@ -63,7 +63,7 @@ public class IntroduceFieldDialogPanel extends IntroduceFieldCentralPanel {
protected void initializeInitializerPlace(PsiExpression initializerExpression,
BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace) {
if (initializerExpression != null) {
setEnabledInitializationPlaces(initializerExpression, initializerExpression);
setEnabledInitializationPlaces(initializerExpression);
if (!myAllowInitInMethod) {
myRbInCurrentMethod.setEnabled(false);
}
@@ -195,9 +195,9 @@ public class IntroduceFieldDialogPanel extends IntroduceFieldCentralPanel {
}
@Override
protected boolean updateInitializationPlaceModel(boolean initializedInSetup) {
protected boolean updateInitializationPlaceModel(boolean initializedInSetup, boolean initializedInConstructor) {
myRbInFieldDeclaration.setEnabled(false);
myRbInConstructor.setEnabled(false);
myRbInConstructor.setEnabled(initializedInConstructor);
enableFinal(false);
if (myRbInSetUp != null){
if (!initializedInSetup) {
@@ -57,7 +57,7 @@ public class IntroduceFieldPopupPanel extends IntroduceFieldCentralPanel {
protected void initializeInitializerPlace(PsiExpression initializerExpression,
BaseExpressionToFieldHandler.InitializationPlace ourLastInitializerPlace) {
if (initializerExpression != null) {
setEnabledInitializationPlaces(initializerExpression, initializerExpression);
setEnabledInitializationPlaces(initializerExpression);
if (!myAllowInitInMethod) {
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_CURRENT_METHOD);
}
@@ -187,9 +187,11 @@ public class IntroduceFieldPopupPanel extends IntroduceFieldCentralPanel {
}
@Override
protected boolean updateInitializationPlaceModel(boolean initializedInSetup) {
protected boolean updateInitializationPlaceModel(boolean initializedInSetup, boolean initializedInConstructor) {
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION);
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR);
if (!initializedInConstructor) {
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR);
}
if (!initializedInSetup) {
myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_SETUP_METHOD);
} else {
@@ -465,12 +465,15 @@ public class ExceptionUtil {
final PsiLambdaExpression expression = PsiTreeUtil.getParentOfType(methodCall, PsiLambdaExpression.class);
final PsiSubstitutor substitutor;
if (expression != null) {
substitutor = ourThrowsGuard.doPreventingRecursion(expression, false, new Computable<PsiSubstitutor>() {
final PsiElement parent = methodCall.getParent();
final boolean callInReturnStatement = parent == expression ||
parent instanceof PsiReturnStatement && PsiTreeUtil.getParentOfType(parent, PsiLambdaExpression.class, true, PsiMethod.class) == expression;
substitutor = callInReturnStatement ? ourThrowsGuard.doPreventingRecursion(expression, false, new Computable<PsiSubstitutor>() {
@Override
public PsiSubstitutor compute() {
return result.getSubstitutor();
}
});
}) : result.getSubstitutor();
} else {
substitutor = result.getSubstitutor();
}
@@ -728,7 +728,7 @@ public class ControlFlowUtil {
int throwToOffset = instruction.offset;
boolean isNormal;
if (throwToOffset == nextOffset) {
isNormal = throwToOffset <= endOffset && !isLeaf(nextOffset) && canCompleteNormally[nextOffset];
isNormal = nextOffset == endOffset || throwToOffset <= endOffset && !isLeaf(nextOffset) && canCompleteNormally[nextOffset];
}
else {
isNormal = canCompleteNormally[nextOffset];
@@ -1201,7 +1201,7 @@ public class InferenceSession {
continue;
}
if (LambdaUtil.isFunctionalType(sType) && LambdaUtil.isFunctionalType(tType) && !relates(sType, tType)) {
if (!isFunctionalTypeMoreSpecific(sType, tType, session, args)) {
if (!isFunctionalTypeMoreSpecific(sType, tType, session, args[i])) {
return false;
}
} else {
@@ -308,9 +308,9 @@ public class JavaMethodsConflictResolver implements PsiConflictResolver{
return ((MethodCandidateInfo)info).getPertinentApplicabilityLevel() != MethodCandidateInfo.ApplicabilityLevel.NOT_APPLICABLE;
}
private static boolean checkParametersNumber(@NotNull List<CandidateInfo> conflicts,
final int argumentsCount,
boolean ignoreIfStaticsProblem) {
public static boolean checkParametersNumber(@NotNull List<CandidateInfo> conflicts,
final int argumentsCount,
boolean ignoreIfStaticsProblem) {
boolean atLeastOneMatch = false;
TIntArrayList unmatchedIndices = null;
for (int i = 0; i < conflicts.size(); i++) {
@@ -0,0 +1,20 @@
import java.io.IOException;
import java.util.Optional;
class Test {
interface Extractor<T> {
T extractData() throws IOException;
}
public static <T> T query(Extractor<T> rse) {
return null;
}
static {
final Optional<String> query = query(() -> {
final String type = Optional.<String>empty().orElseThrow(null);
return null;
});
}
}
@@ -0,0 +1,22 @@
import java.util.List;
import java.util.function.Function;
class Test {
{
transform(1, (String l) -> null);
}
public static <I, O> void transform(int input, Function<I, O> function) {
System.out.println(input);
System.out.println(function);
}
interface IFunction<F, T> {
List<T> apply(F var1);
}
public static <I, O> void transform(int input, IFunction<I, O> function) {
System.out.println(input);
System.out.println(function);
}
}
@@ -0,0 +1,28 @@
import java.io.IOException;
class Test {
{
query(() -> {
process();
});
}
public static void process() throws IOException {}
private <T> void <warning descr="Private method 'query(Test.B<T>)' is never used">query</warning>(B<T> var2) {
System.out.println(var2);
}
private void query( A rch) {
System.out.println(rch);
}
interface A {
void m() throws IOException;
}
interface B<T> {
T n() throws IOException;
}
}
@@ -0,0 +1,23 @@
// "Replace with expression lambda" "true"
import java.util.HashSet;
import java.util.Set;
class Test {
public static void main(String[] args) {
Set<String> strings = new HashSet<>();
new Test().query("", pResultSet -> strings.add("Col1"));
}
public Object query(String s, final ResultSetExtractor rse) {
return null;
}
public Object query(String s, final ResultSetExtractor rse, Object.. args) {
return null;
}
}
interface ResultSetExtractor {
Object extractData(ResultSet var1);
}
class ResultSet {}
@@ -0,0 +1,25 @@
// "Replace with expression lambda" "true"
import java.util.HashSet;
import java.util.Set;
class Test {
public static void main(String[] args) {
Set<String> strings = new HashSet<>();
new Test().query("", pResultSet -> <caret>{
strings.add("Col1");
});
}
public Object query(String s, final ResultSetExtractor rse) {
return null;
}
public Object query(String s, final ResultSetExtractor rse, Object.. args) {
return null;
}
}
interface ResultSetExtractor {
Object extractData(ResultSet var1);
}
class ResultSet {}
@@ -140,6 +140,10 @@ public class NewLambdaHighlightingTest extends LightDaemonAnalyzerTestCase {
doTest();
}
public void testCheckedExceptionsConstraintsSubstitutionsDeepInBody() throws Exception {
doTest();
}
public void testIDEA130129() throws Exception {
doTest();
}
@@ -43,6 +43,10 @@ public class OverloadResolutionTest extends LightDaemonAnalyzerTestCase {
doTest();
}
public void testVoidValueCompatibilityCanCompleteNormallyWithCallWithExceptionAsLastStatement() throws Exception {
doTest();
}
public void testIDEA102800() throws Exception {
doTest();
}
@@ -51,6 +55,10 @@ public class OverloadResolutionTest extends LightDaemonAnalyzerTestCase {
doTest();
}
public void testIgnoreNonFunctionalArgumentsWhenCheckIfFunctionalMoreSpecific() throws Exception {
doTest();
}
private void doTest() {
doTest(true);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -23,11 +23,13 @@ import com.intellij.remoteServer.agent.util.CloudRemoteApplication;
import com.intellij.remoteServer.configuration.deployment.DeploymentConfiguration;
import com.intellij.remoteServer.runtime.ServerTaskExecutor;
import com.intellij.remoteServer.runtime.deployment.ServerRuntimeInstance;
import com.intellij.util.Function;
import com.intellij.util.ThrowableRunnable;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@@ -86,7 +88,7 @@ public abstract class CloudServerRuntimeInstance
}
@Override
public void computeDeployments(@NotNull final ServerRuntimeInstance.ComputeDeploymentsCallback callback) {
public void computeDeployments(@NotNull final ComputeDeploymentsCallback callback) {
getTaskExecutor().submit(new ThrowableRunnable<Exception>() {
@Override
@@ -109,11 +111,16 @@ public abstract class CloudServerRuntimeInstance
@Override
public List<CloudApplicationRuntime> compute() {
List<CloudApplicationRuntime> result = new ArrayList<CloudApplicationRuntime>();
for (CloudRemoteApplication application : getAgent().getApplications()) {
result.add(createApplicationRuntime(application));
CloudRemoteApplication[] applications = getAgent().getApplications();
if (applications == null) {
return Collections.emptyList();
}
return result;
return ContainerUtil.map(applications, new Function<CloudRemoteApplication, CloudApplicationRuntime>() {
@Override
public CloudApplicationRuntime fun(CloudRemoteApplication application) {
return createApplicationRuntime(application);
}
});
}
});
}
@@ -128,10 +128,10 @@ public class JUnit4TestResultsSender extends RunListener {
notification = createExceptionNotification(assertion, message, "\nExpected: (.*)\n\\s*got: (.*)");
}
if (notification == null) {
notification = createExceptionNotification(assertion, message, "\\s*expected same:<(.*)> was not:<(.*)>");
notification = createExceptionNotification(assertion, message, ".*?\\s*expected same:<(.*)> was not:<(.*)>");
}
if (notification == null) {
notification = createExceptionNotification(assertion, message, ".*\\s*expected:<(.*)> but was:<(.*)>");
notification = createExceptionNotification(assertion, message, ".*?\\s*expected:<(.*?)> but was:<(.*?)>");
}
if (notification == null) {
notification = createExceptionNotification(assertion, message, "\nExpected: \"(.*)\"\n\\s*but: was \"(.*)\"");
@@ -148,6 +148,13 @@ public class JUnit4TestResultsSender extends RunListener {
private static PacketFactory createExceptionNotification(Throwable assertion, String message, final String regex) {
final Matcher matcher = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE).matcher(message);
boolean found = false;
while (matcher.find()) {
if (found) return null;
found = true;
}
if (matcher.matches()) {
return ComparisonDetailsExtractor.create(assertion, matcher.group(1).replaceAll("\\\\n", "\n"), matcher.group(2).replaceAll("\\\\n", "\n"));
}
@@ -126,33 +126,27 @@ public class DomStubUsingTest extends DomStubTest {
final DomFileElement<Foo> fileElement = prepare("foo.xml", Foo.class);
final Bar bar = fileElement.getRootElement().getBars().get(0);
assertNotNull(bar);
assertTrue(bar.exists());
new WriteCommandAction.Simple(null) {
@Override
protected void run() throws Throwable {
bar.undefine();
}
}.execute().throwException();
assertFalse(bar.exists());
assertUndefine(bar);
}
public void testRootElementUndefineNotExisting() {
final DomFileElement<Foo> fileElement = prepare("foo.xml", Foo.class);
final DomElement rootElement = fileElement.getRootElement();
assertNotNull(rootElement);
assertTrue(rootElement.exists());
assertUndefine(rootElement);
}
private static void assertUndefine(final DomElement domElement) {
assertNotNull(domElement);
assertTrue(domElement.exists());
new WriteCommandAction.Simple(null) {
@Override
protected void run() throws Throwable {
rootElement.undefine();
domElement.undefine();
}
}.execute().throwException();
assertFalse(rootElement.exists());
assertFalse(domElement.exists());
}
}