Merge remote-tracking branch 'origin/master'

This commit is contained in:
Dmitry Trofimov
2012-08-30 18:29:10 +02:00
16 changed files with 187 additions and 9 deletions
@@ -331,7 +331,9 @@ public class ExpectedTypesProvider {
}
@Override public void visitReturnStatement(PsiReturnStatement statement) {
final PsiMethod scopeMethod = PsiTreeUtil.getParentOfType(statement, PsiMethod.class);
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(statement, PsiLambdaExpression.class);
final PsiMethod interfaceMethod = lambdaExpression != null ? LambdaUtil.getFunctionalInterfaceMethod(lambdaExpression.getFunctionalInterfaceType()) : null;
final PsiMethod scopeMethod = interfaceMethod != null ? interfaceMethod : PsiTreeUtil.getParentOfType(statement, PsiMethod.class);
if (scopeMethod != null) {
PsiType type = scopeMethod.getReturnType();
if (type != null) {
@@ -145,6 +145,14 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaLocalInspectionTool
buf.append("->");
final PsiCodeBlock body = method.getBody();
LOG.assertTrue(body != null);
final PsiStatement[] statements = body.getStatements();
if (statements.length == 1 && statements[0] instanceof PsiReturnStatement) {
PsiExpression value = ((PsiReturnStatement)statements[0]).getReturnValue();
if (value != null) {
buf.append(value.getText());
return buf.toString();
}
}
buf.append(body.getText());
return buf.toString();
}
@@ -0,0 +1,26 @@
/*
* 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.
*/
interface I {
boolean foo(String s);
}
public class Test {
public int m() {
I i = s -> {
return s.contains();
};
return 0;
}
}
@@ -0,0 +1,26 @@
/*
* 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.
*/
interface I {
boolean foo(String s);
}
public class Test {
public int m() {
I i = s -> {
return s.<caret>
};
return 0;
}
}
@@ -1,8 +1,6 @@
// "Replace with lambda" "true"
class Test {
{
Comparable<String> c = o -> {
return 0;
};
Comparable<String> c = o -> 0;
}
}
@@ -9,8 +9,6 @@ class Test2 {
static <T> I<T> bar(I<T> i){return i;}
{
bar((List<String> list) -> {
return null; //To change body of implemented methods use File | Settings | File Templates.
});
bar((List<String> list) -> null);
}
}
@@ -0,0 +1,9 @@
// "Replace with lambda" "true"
class Test {
{
Comparable<String> c = o -> {
System.out.println();
return 0;
};
}
}
@@ -0,0 +1,12 @@
// "Replace with lambda" "true"
class Test {
{
Comparable<String> c = new Compa<caret>rable<String>() {
@Override
public int compareTo(String o) {
System.out.println();
return 0;
}
};
}
}
@@ -0,0 +1,56 @@
/*
* 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 com.intellij.codeInsight.completion;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.codeInsight.lookup.LookupElementPresentation;
import com.intellij.testFramework.LightProjectDescriptor;
import org.jetbrains.annotations.NotNull;
public class SmartType18CompletionTest extends LightFixtureCompletionTestCase {
@Override
protected String getBasePath() {
return JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInsight/completion/smartType/";
}
@Override
protected void complete() {
myItems = myFixture.complete(CompletionType.SMART);
}
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_LATEST;
}
public void testExpectedReturnType() {
doTest();
}
private void doTest() {
configureByFile("/" + getTestName(false) + ".java");
assertNotNull(myItems);
assertTrue(myItems.length > 0);
final Lookup lookup = getLookup();
if (lookup != null) {
selectItem(lookup.getCurrentItem(), Lookup.NORMAL_SELECT_CHAR);
}
checkResultByFile("/" + getTestName(false) + "-out.java");
}
}
@@ -84,7 +84,9 @@ public class InferLambdaParameterTypeIntention extends Intention {
public boolean satisfiedBy(PsiElement element) {
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class);
if (lambdaExpression != null) {
for (PsiParameter parameter : lambdaExpression.getParameterList().getParameters()) {
PsiParameter[] parameters = lambdaExpression.getParameterList().getParameters();
if (parameters.length == 0) return false;
for (PsiParameter parameter : parameters) {
if (parameter.getTypeElement() != null) {
return false;
}
@@ -15,6 +15,7 @@
*/
package com.siyeh.ipp.types;
import com.intellij.codeInsight.ChangeContextUtil;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil;
import com.intellij.codeInsight.generation.GenerateMembersUtil;
import com.intellij.codeInsight.generation.OverrideImplementUtil;
@@ -25,6 +26,7 @@ import com.intellij.psi.*;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
@@ -62,7 +64,12 @@ public class ReplaceLambdaWithAnonymousIntention extends Intention {
if (blockText == null) return;
final PsiElementFactory psiElementFactory = JavaPsiFacade.getElementFactory(element.getProject());
PsiCodeBlock blockFromText = psiElementFactory.createCodeBlockFromText(blockText, lambdaExpression);
ChangeContextUtil.encodeContextInfo(blockFromText, true);
PsiNewExpression newExpression = (PsiNewExpression)psiElementFactory.createExpressionFromText("new " + functionalInterfaceType.getCanonicalText() + "(){}", lambdaExpression);
PsiClass thisClass = PsiTreeUtil.getParentOfType(lambdaExpression, PsiClass.class, true);
ChangeContextUtil.decodeContextInfo(blockFromText, thisClass, RefactoringUtil.createThisExpression(lambdaExpression.getManager(), thisClass));
blockFromText = psiElementFactory.createCodeBlockFromText(blockFromText.getText(), null);
newExpression = (PsiNewExpression)lambdaExpression.replace(newExpression);
final PsiAnonymousClass anonymousClass = newExpression.getAnonymousClass();
@@ -80,7 +87,8 @@ public class ReplaceLambdaWithAnonymousIntention extends Intention {
}
PsiCodeBlock codeBlock = member.getBody();
LOG.assertTrue(codeBlock != null);
codeBlock = (PsiCodeBlock)codeBlock.replace(psiElementFactory.createCodeBlockFromText(blockText, null));
codeBlock = (PsiCodeBlock)codeBlock.replace(blockFromText);
final Set<PsiVariable> vars2BeFinal = new HashSet<PsiVariable>();
codeBlock.accept(new JavaRecursiveElementWalkingVisitor() {
@Override
@@ -99,6 +107,7 @@ public class ReplaceLambdaWithAnonymousIntention extends Intention {
for (PsiVariable var : vars2BeFinal) {
PsiUtil.setModifierProperty(var, PsiModifier.FINAL, true);
}
GenerateMembersUtil.positionCaret(editor, member, true);
}
}
@@ -0,0 +1,7 @@
class Test2 {
static void bar(Runnable r){}
{
bar((<caret>) -> {});
}
}
@@ -0,0 +1,6 @@
class Test {
public void m() {
Runnable r = (<caret>) -> {System.out.println(this);};
r.run();
}
}
@@ -0,0 +1,11 @@
class Test {
public void m() {
Runnable r = new Runnable() {
@Override
public void run() {
<selection>System.out.println(Test.this);</selection>
}
};
r.run();
}
}
@@ -39,6 +39,10 @@ public class InferLambdaParameterTypeIntentionTest extends IPPTestCase {
assertIntentionNotAvailable("Expand lambda to (Object x) -> {...}");
}
public void testNoParams() throws Exception {
assertIntentionNotAvailable("Expand lambda to () -> {...}");
}
@Override
protected String getIntentionName() {
return "";
@@ -55,6 +55,10 @@ public class ReplaceLambdaWithAnonymousIntentionTest extends IPPTestCase {
doTest();
}
public void testQualifyThis() {
doTest();
}
@Override
protected String getIntentionName() {
return IntentionPowerPackBundle.message("replace.lambda.with.anonymous.intention.name");