diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromNewFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromNewFix.java index c6b5b8c25f44..43bd4c035828 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromNewFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromNewFix.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * 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. @@ -190,10 +190,12 @@ public class CreateClassFromNewFix extends CreateFromUsageBaseFix { if (aClass.isInterface()) { PsiReferenceList implementsList = targetClass.getImplementsList(); + assert implementsList != null : targetClass; implementsList.add(factory.createReferenceElementByType(classType)); } else { PsiReferenceList extendsList = targetClass.getExtendsList(); + assert extendsList != null : targetClass; if (extendsList.getReferencedTypes().length == 0 && !CommonClassNames.JAVA_LANG_OBJECT.equals(classType.getCanonicalText())) { extendsList.add(factory.createReferenceElementByType(classType)); } @@ -201,7 +203,6 @@ public class CreateClassFromNewFix extends CreateFromUsageBaseFix { } } - private static PsiFile getTargetFile(PsiElement element) { PsiJavaCodeReferenceElement referenceElement = getReferenceElement((PsiNewExpression)element); @@ -242,14 +243,18 @@ public class CreateClassFromNewFix extends CreateFromUsageBaseFix { @Override protected boolean isAvailableImpl(int offset) { - PsiElement nameElement = getNameElement(getNewExpression()); + PsiNewExpression expression = getNewExpression(); + if (expression.getQualifier() != null) { + return false; + } - PsiFile targetFile = getTargetFile(getNewExpression()); + PsiFile targetFile = getTargetFile(expression); if (targetFile != null && !targetFile.getManager().isInProject(targetFile)) { return false; } - if (CreateFromUsageUtils.shouldShowTag(offset, nameElement, getNewExpression())) { + PsiElement nameElement = getNameElement(expression); + if (CreateFromUsageUtils.shouldShowTag(offset, nameElement, expression)) { String varName = nameElement.getText(); setText(getText(varName)); return true; @@ -268,8 +273,7 @@ public class CreateClassFromNewFix extends CreateFromUsageBaseFix { private static PsiElement getNameElement(PsiNewExpression targetElement) { PsiJavaCodeReferenceElement referenceElement = getReferenceElement(targetElement); - if (referenceElement == null) return null; - return referenceElement.getReferenceNameElement(); + return referenceElement != null ? referenceElement.getReferenceNameElement() : null; } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImportClassFixBase.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImportClassFixBase.java index 7430053cc546..0a2f43c6d7fc 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImportClassFixBase.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ImportClassFixBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * 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. @@ -69,6 +69,12 @@ public abstract class ImportClassFixBase im if (!myRef.isValid()) { return false; } + + PsiElement parent = myRef.getParent(); + if (parent instanceof PsiNewExpression && ((PsiNewExpression)parent).getQualifier() != null) { + return false; + } + PsiManager manager = file.getManager(); return manager.isInProject(file) && !getClassesToImport().isEmpty(); } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createClassFromNew/beforeQualifiedNew.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createClassFromNew/beforeQualifiedNew.java new file mode 100644 index 000000000000..516a79faae83 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createClassFromNew/beforeQualifiedNew.java @@ -0,0 +1,6 @@ +// "Create Class 'ArrayList'" "false" +public class Test { + public static void main() { + q.new ArrayList(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/beforeQualifiedNew1.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/beforeQualifiedNew1.java new file mode 100644 index 000000000000..ce4542d9e4f9 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/beforeQualifiedNew1.java @@ -0,0 +1,6 @@ +// "Create Inner Class 'ArrayList'" "false" +public class Test { + public static void main() { + q.new ArrayList(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/beforeQualifiedNew2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/beforeQualifiedNew2.java new file mode 100644 index 000000000000..6fa79f02d8d9 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew/beforeQualifiedNew2.java @@ -0,0 +1,9 @@ +// "Create Inner Class 'ArrayList'" "false" +public class Test { + public static void main() { + Inner q = new Inner(); + q.new ArrayList(); + } + + static class Inner { } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateClassFromNewTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateClassFromNewTest.java index b90ee60adde4..82fcd0e2e466 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateClassFromNewTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateClassFromNewTest.java @@ -1,3 +1,18 @@ +/* + * 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.intellij.codeInsight.daemon.quickFix; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; @@ -6,10 +21,9 @@ import com.intellij.psi.codeStyle.CodeStyleSettingsManager; * @author ven */ public class CreateClassFromNewTest extends LightQuickFixTestCase { - public void test() throws Exception { CodeStyleSettingsManager.getSettings(getProject()).SPACE_BEFORE_CLASS_LBRACE = true; - doAllTests(); + doAllTests(); } @Override diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateInnerClassFromNewTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateInnerClassFromNewTest.java index 902f0ff881d9..7b53bd1505ae 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateInnerClassFromNewTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateInnerClassFromNewTest.java @@ -1,13 +1,30 @@ +/* + * 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.intellij.codeInsight.daemon.quickFix; /** * @author yole */ public class CreateInnerClassFromNewTest extends LightQuickFixTestCase { - public void test() throws Exception { doAllTests(); } + public void test() throws Exception { + doAllTests(); + } @Override protected String getBasePath() { return "/codeInsight/daemonCodeAnalyzer/quickFix/createInnerClassFromNew"; } -} \ No newline at end of file +}