diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromUsageBaseFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromUsageBaseFix.java index a8bf40480a62..29a2e01f0b5e 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromUsageBaseFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassFromUsageBaseFix.java @@ -46,6 +46,10 @@ public abstract class CreateClassFromUsageBaseFix extends BaseIntentionAction { private boolean isAvailableInContext(final @NotNull PsiJavaCodeReferenceElement element) { PsiElement parent = element.getParent(); + if (myKind == CreateClassKind.ANNOTATION) { + return parent instanceof PsiAnnotation; + } + if (parent instanceof PsiJavaCodeReferenceCodeFragment) return true; if (parent instanceof PsiTypeElement) { diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassKind.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassKind.java index b202d9661d86..af7686993b79 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassKind.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateClassKind.java @@ -23,7 +23,8 @@ import com.intellij.codeInsight.daemon.QuickFixBundle; public enum CreateClassKind { CLASS (QuickFixBundle.message("create.class")), INTERFACE (QuickFixBundle.message("create.interface")), - ENUM (QuickFixBundle.message("create.enum")); + ENUM (QuickFixBundle.message("create.enum")), + ANNOTATION("annotation"); private final String myDescription; diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageUtils.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageUtils.java index 6a3a738c654a..35b416e21417 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageUtils.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/CreateFromUsageUtils.java @@ -340,6 +340,7 @@ public class CreateFromUsageUtils { PsiElementFactory elementFactory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); PsiClass result = classKind == INTERFACE ? elementFactory.createInterface(name) : classKind == CLASS ? elementFactory.createClass(name) : + classKind == ANNOTATION ? elementFactory.createAnnotationType(name) : elementFactory.createEnum(name); CreateFromUsageBaseFix.setupGenericParameters(result, referenceElement); result = (PsiClass)CodeStyleManager.getInstance(manager.getProject()).reformat(result); @@ -377,6 +378,9 @@ public class CreateFromUsageUtils { else if (classKind == ENUM) { targetClass = JavaDirectoryService.getInstance().createEnum(directory, name); } + else if (classKind == ANNOTATION) { + targetClass = JavaDirectoryService.getInstance().createAnnotationType(directory, name); + } else { LOG.error("Unknown kind of a class to create"); return null; @@ -401,6 +405,9 @@ public class CreateFromUsageUtils { else if (classKind == ENUM) { aClass = factory.createEnum(name); } + else if (classKind == ANNOTATION) { + aClass = factory.createAnnotationType(name); + } else { LOG.error("Unknown kind of a class to create"); return null; diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DefaultQuickFixProvider.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DefaultQuickFixProvider.java index 4cdc443bb792..df48370de14b 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DefaultQuickFixProvider.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/DefaultQuickFixProvider.java @@ -50,6 +50,7 @@ public class DefaultQuickFixProvider extends UnresolvedReferenceQuickFixProvider registrar.register(new CreateClassFromUsageFix(ref, CreateClassKind.INTERFACE)); if (PsiUtil.isLanguageLevel5OrHigher(ref)) { registrar.register(new CreateClassFromUsageFix(ref, CreateClassKind.ENUM)); + registrar.register(new CreateClassFromUsageFix(ref, CreateClassKind.ANNOTATION)); } PsiElement parent = PsiTreeUtil.getParentOfType(ref, PsiNewExpression.class, PsiMethod.class); final PsiExpressionList expressionList = PsiTreeUtil.getParentOfType(ref, PsiExpressionList.class); diff --git a/java/java-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java b/java/java-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java index c8d8faa795d0..5b441892b852 100644 --- a/java/java-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java +++ b/java/java-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java @@ -136,6 +136,12 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps return createClassInner("enum", name); } + @NotNull + @Override + public PsiClass createAnnotationType(@NotNull @NonNls String name) throws IncorrectOperationException { + return createClassInner("@interface", name); + } + private PsiClass createClassInner(final String type, final String name) { PsiUtil.checkIsIdentifier(myManager, name); final PsiJavaFile aFile = createDummyJavaFile(join("public ", type, " ", name, " { }")); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createAnnotationTypeFromUsage/after1.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createAnnotationTypeFromUsage/after1.java new file mode 100644 index 000000000000..7d6ead5294cb --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createAnnotationTypeFromUsage/after1.java @@ -0,0 +1,7 @@ +// "Create Annotation 'Smth'" "true" +class Test { + void foo(@Smth String s){} +} + +public @interface Smth { +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createAnnotationTypeFromUsage/before1.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createAnnotationTypeFromUsage/before1.java new file mode 100644 index 000000000000..4b79b770ef7d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createAnnotationTypeFromUsage/before1.java @@ -0,0 +1,4 @@ +// "Create Annotation 'Smth'" "true" +class Test { + void foo(@Smth String s){} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createAnnotationTypeFromUsage/beforeNotAvailableInNonAnnotationPlace.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createAnnotationTypeFromUsage/beforeNotAvailableInNonAnnotationPlace.java new file mode 100644 index 000000000000..960f229fd63a --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createAnnotationTypeFromUsage/beforeNotAvailableInNonAnnotationPlace.java @@ -0,0 +1,4 @@ +// "Create Annotation 'Smth'" "false" +class Test { + void foo(Smth s){} +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateAnnotationTypeFromUsageTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateAnnotationTypeFromUsageTest.java new file mode 100644 index 000000000000..969e27e7c73d --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateAnnotationTypeFromUsageTest.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2011 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 ven + */ +public class CreateAnnotationTypeFromUsageTest extends LightQuickFixTestCase{ + public void test() throws Exception { doAllTests(); } + + @Override + protected String getBasePath() { + return "/codeInsight/daemonCodeAnalyzer/quickFix/createAnnotationTypeFromUsage"; + } +} diff --git a/java/openapi/src/com/intellij/psi/PsiElementFactory.java b/java/openapi/src/com/intellij/psi/PsiElementFactory.java index db911af03f15..6bc1c7bff83f 100644 --- a/java/openapi/src/com/intellij/psi/PsiElementFactory.java +++ b/java/openapi/src/com/intellij/psi/PsiElementFactory.java @@ -60,6 +60,16 @@ public interface PsiElementFactory extends PsiJavaParserFacade, JVMElementFactor */ @NotNull PsiClass createEnum(@NotNull @NonNls String name) throws IncorrectOperationException; + /** + * Creates an empty annotation type with the specified name. + * + * @param name the name of the annotation type to create. + * @return the created annotation type instance. + * @throws IncorrectOperationException if name is not a valid Java identifier. + */ + @NotNull + PsiClass createAnnotationType(@NotNull @NonNls String name) throws IncorrectOperationException; + /** * Creates a field with the specified name and type. *