provide also create annotation from usage (IDEA-19746 )

This commit is contained in:
anna
2011-08-25 19:26:03 +02:00
parent 6791554063
commit 0f5615356b
10 changed files with 73 additions and 1 deletions
@@ -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) {
@@ -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;
@@ -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;
@@ -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);
@@ -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, " { }"));
@@ -0,0 +1,7 @@
// "Create Annotation 'Smth'" "true"
class Test {
void foo(@Smth String s){}
}
public @interface Smth {
}
@@ -0,0 +1,4 @@
// "Create Annotation 'Smth'" "true"
class Test {
void foo(@S<caret>mth String s){}
}
@@ -0,0 +1,4 @@
// "Create Annotation 'Smth'" "false"
class Test {
void foo(S<caret>mth s){}
}
@@ -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";
}
}
@@ -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 <code>name</code> is not a valid Java identifier.
*/
@NotNull
PsiClass createAnnotationType(@NotNull @NonNls String name) throws IncorrectOperationException;
/**
* Creates a field with the specified name and type.
*