diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/OverrideImplementTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/OverrideImplementTest.java index 5a4b795126df..e7524c3975fe 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/OverrideImplementTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/OverrideImplementTest.java @@ -1,3 +1,18 @@ +/* + * 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; import com.intellij.codeInsight.generation.OverrideImplementUtil; @@ -13,45 +28,47 @@ import com.intellij.psi.util.MethodSignature; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.TypeConversionUtil; import com.intellij.testFramework.LightCodeInsightTestCase; -import com.intellij.util.Function; +import com.intellij.util.FunctionUtil; import com.intellij.util.containers.ContainerUtil; import java.util.Collection; import java.util.Collections; +import java.util.List; /** * @author ven */ public class OverrideImplementTest extends LightCodeInsightTestCase { + private static final String BASE_DIR = "/codeInsight/overrideImplement/"; + @Override protected void setUp() throws Exception { super.setUp(); setLanguageLevel(LanguageLevel.JDK_1_5); } - public void testSimple() throws Exception { doTest(true); } - public void testAnnotation() throws Exception { doTest(true); } - public void testIncomplete() throws Exception { doTest(false); } - public void testSubstitutionInTypeParametersList() throws Exception { doTest(false); } - public void testTestMissed() throws Exception { doTest(false); } - public void testWildcard() throws Exception { doTest(false); } - public void testTypeParam() throws Exception { doTest(false); } - public void testInterfaceAndAbstractClass() throws Exception { doTest(false); } - public void testRawSuper() throws Exception { doTest(false); } - public void testSubstituteBoundInMethodTypeParam() throws Exception { doTest(false); } + public void testSimple() { doTest(true); } + public void testAnnotation() { doTest(true); } + public void testIncomplete() { doTest(false); } + public void testSubstitutionInTypeParametersList() { doTest(false); } + public void testTestMissed() { doTest(false); } + public void testWildcard() { doTest(false); } + public void testTypeParam() { doTest(false); } + public void testInterfaceAndAbstractClass() { doTest(false); } + public void testRawSuper() { doTest(false); } + public void testSubstituteBoundInMethodTypeParam() { doTest(false); } + public void testClone() { doTest(false); } + public void testOnTheLineWithExistingExpression() { doTest(false); } - public void testLongFinalParameterList() throws Exception { + public void testLongFinalParameterList() { CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(getProject()).clone(); - try { CommonCodeStyleSettings javaSettings = codeStyleSettings.getCommonSettings(JavaLanguage.INSTANCE); codeStyleSettings.RIGHT_MARGIN = 80; javaSettings.KEEP_LINE_BREAKS = true; codeStyleSettings.GENERATE_FINAL_PARAMETERS = true; javaSettings.METHOD_PARAMETERS_WRAP = CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM; - CodeStyleSettingsManager.getInstance(getProject()).setTemporarySettings(codeStyleSettings); - doTest(false); } finally { @@ -59,9 +76,8 @@ public class OverrideImplementTest extends LightCodeInsightTestCase { } } - public void testLongParameterList() throws Exception { + public void testLongParameterList() { CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(getProject()).clone(); - try { CommonCodeStyleSettings javaSettings = codeStyleSettings.getCommonSettings(JavaLanguage.INSTANCE); codeStyleSettings.RIGHT_MARGIN = 80; @@ -69,7 +85,6 @@ public class OverrideImplementTest extends LightCodeInsightTestCase { codeStyleSettings.GENERATE_FINAL_PARAMETERS = false; javaSettings.METHOD_PARAMETERS_WRAP = CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM; CodeStyleSettingsManager.getInstance(getProject()).setTemporarySettings(codeStyleSettings); - doTest(false); } finally { @@ -77,34 +92,23 @@ public class OverrideImplementTest extends LightCodeInsightTestCase { } } - public void testClone() throws Exception { - doTest(false); - } - - public void testOnTheLineWithExistingExpression() throws Exception { - doTest(false); - } - - public void testImplementedConstructorsExcluded() throws Exception { - String name = getTestName(false); - configureByFile("/codeInsight/overrideImplement/" + name + ".java"); + public void testImplementedConstructorsExcluded() { + configureByFile(BASE_DIR + getTestName(false) + ".java"); int offset = getEditor().getCaretModel().getOffset(); PsiElement context = getFile().findElementAt(offset); PsiClass psiClass = PsiTreeUtil.getParentOfType(context, PsiClass.class); assert psiClass != null; final Collection signatures = OverrideImplementUtil.getMethodSignaturesToOverride(psiClass); - final Collection strings = ContainerUtil.map(signatures, new Function() { - public String fun(MethodSignature signature) { return signature.toString(); } - }); + final Collection strings = ContainerUtil.map(signatures, FunctionUtil.string()); assertTrue(strings.toString(), strings.contains("HierarchicalMethodSignatureImpl: A([PsiType:String])")); assertFalse(strings.toString(), strings.contains("HierarchicalMethodSignatureImpl: A([])")); } - public void testEnumConstant() throws Exception { + public void testEnumConstant() { String name = getTestName(false); - configureByFile("/codeInsight/overrideImplement/before" + name + ".java"); + configureByFile(BASE_DIR + "before" + name + ".java"); int offset = getEditor().getCaretModel().getOffset(); PsiElement context = getFile().findElementAt(offset); PsiMethod psiMethod = PsiTreeUtil.getParentOfType(context, PsiMethod.class); @@ -113,12 +117,12 @@ public class OverrideImplementTest extends LightCodeInsightTestCase { assert aClass != null && aClass.isEnum(); final PsiField[] fields = aClass.getFields(); new ImplementAbstractMethodHandler(getProject(), getEditor(), psiMethod).implementInClass(fields); - checkResultByFile("/codeInsight/overrideImplement/after" + name + ".java"); + checkResultByFile(BASE_DIR + "after" + name + ".java"); } - - private void doTest(boolean copyJavadoc) throws Exception { + + private void doTest(boolean copyJavadoc) { String name = getTestName(false); - configureByFile("/codeInsight/overrideImplement/before" + name + ".java"); + configureByFile(BASE_DIR + "before" + name + ".java"); int offset = getEditor().getCaretModel().getOffset(); PsiElement context = getFile().findElementAt(offset); PsiClass psiClass = PsiTreeUtil.getParentOfType(context, PsiClass.class); @@ -126,12 +130,10 @@ public class OverrideImplementTest extends LightCodeInsightTestCase { PsiClassType[] implement = psiClass.getImplementsListTypes(); final PsiClass superClass = implement.length == 0 ? psiClass.getSuperClass() : implement[0].resolve(); assert superClass != null; - PsiMethod method = superClass.getMethods()[0]; - final PsiMethodMember member2Override = new PsiMethodMember(method, - TypeConversionUtil.getSuperClassSubstitutor(superClass, psiClass, - PsiSubstitutor.EMPTY)); - OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(getEditor(), psiClass, Collections.singletonList(member2Override), - copyJavadoc, true); - checkResultByFile("/codeInsight/overrideImplement/after" + name + ".java"); - } + PsiMethod method = superClass.getMethods()[0]; + final PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(superClass, psiClass, PsiSubstitutor.EMPTY); + final List candidates = Collections.singletonList(new PsiMethodMember(method, substitutor)); + OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(getEditor(), psiClass, candidates, copyJavadoc, true); + checkResultByFile(BASE_DIR + "after" + name + ".java"); + } } \ No newline at end of file diff --git a/platform/util-rt/src/com/intellij/util/Function.java b/platform/util-rt/src/com/intellij/util/Function.java index fef2a94aaf2d..f3828f1f8867 100644 --- a/platform/util-rt/src/com/intellij/util/Function.java +++ b/platform/util-rt/src/com/intellij/util/Function.java @@ -25,19 +25,21 @@ import java.util.Collection; public interface Function { Result fun(Param param); - /** - * @see FunctionUtil#id() - */ Function ID = new Function() { public Object fun(final Object o) { return o; } }; - /** - * @see FunctionUtil#nullConstant() - */ + Function NULL = NullableFunction.NULL; + Function TO_STRING = new Function() { + @Override + public Object fun(Object o) { + return String.valueOf(o); + } + }; + final class Self implements Function { @Override public R fun(P p) { diff --git a/platform/util/src/com/intellij/util/FunctionUtil.java b/platform/util/src/com/intellij/util/FunctionUtil.java index 4faee47c5951..f3da5c8042f2 100644 --- a/platform/util/src/com/intellij/util/FunctionUtil.java +++ b/platform/util/src/com/intellij/util/FunctionUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * 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. @@ -21,20 +21,27 @@ import org.jetbrains.annotations.NotNull; * @author nik */ public class FunctionUtil { - private FunctionUtil() { - } + private FunctionUtil() { } @NotNull public static Function id() { - //noinspection unchecked - return Function.ID; + @SuppressWarnings("unchecked") Function id = Function.ID; + return id; } + @NotNull public static NullableFunction nullConstant() { - //noinspection unchecked - return NullableFunction.NULL; + @SuppressWarnings("unchecked") NullableFunction function = NullableFunction.NULL; + return function; } + @NotNull + public static Function string() { + @SuppressWarnings("unchecked") Function function = Function.TO_STRING; + return function; + } + + @NotNull public static Function constant(final B b) { return new Function() { @Override @@ -54,5 +61,4 @@ public class FunctionUtil { } }; } - }