diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntention.java index 2d9e5fdd3f96..22bfffc6d2e4 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntention.java @@ -30,7 +30,7 @@ public class ExpandToNormalAnnotationIntention extends MutablyNamedIntention { @Override protected String getTextForElement(PsiElement element) { - final PsiAnnotation annotation = (PsiAnnotation)element; + final PsiNameValuePair annotation = (PsiNameValuePair)element; final String text = buildReplacementText(annotation); return IntentionPowerPackBundle.message( "expand.to.normal.annotation.name", text); @@ -42,51 +42,26 @@ public class ExpandToNormalAnnotationIntention extends MutablyNamedIntention { return new ExpandToNormalAnnotationPredicate(); } - public static String buildReplacementText(PsiAnnotation annotation) { - final StringBuilder text = new StringBuilder("@"); - final PsiAnnotationParameterList parameterList = - annotation.getParameterList(); - if (parameterList.getChildren().length == 0) { - final PsiJavaCodeReferenceElement nameReferenceElement = - annotation.getNameReferenceElement(); - if (nameReferenceElement != null) { - text.append(nameReferenceElement.getText()); - } - text.append("()"); - } - else { - final PsiNameValuePair[] attributes = parameterList.getAttributes(); - final PsiNameValuePair attribute = attributes[0]; - final PsiAnnotationMemberValue value = attribute.getValue(); - final PsiJavaCodeReferenceElement nameReferenceElement = - annotation.getNameReferenceElement(); - if (nameReferenceElement != null) { - text.append(nameReferenceElement.getText()); - } - text.append("(value = "); - if (value != null) { - text.append(value.getText()); - } - text.append(')'); + public static String buildReplacementText(PsiNameValuePair attribute) { + final StringBuilder text = new StringBuilder(); + final PsiAnnotationMemberValue value = attribute.getValue(); + text.append("value = "); + if (value != null) { + text.append(value.getText()); } return text.toString(); } @Override - protected void processIntention(@NotNull PsiElement element) - throws IncorrectOperationException { - final PsiAnnotation annotation = (PsiAnnotation)element; - final int textOffset = annotation.getTextOffset(); - final Project project = annotation.getProject(); - final String text = buildReplacementText(annotation); - final PsiElementFactory factory = - JavaPsiFacade.getElementFactory(project); - final PsiAnnotation newAnnotation = - factory.createAnnotationFromText( - text, annotation); - annotation.replace(newAnnotation); - final FileEditorManager editorManager = - FileEditorManager.getInstance(project); + protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException { + final PsiNameValuePair attribute = (PsiNameValuePair)element; + final int textOffset = attribute.getTextOffset(); + final Project project = attribute.getProject(); + final String text = buildReplacementText(attribute); + final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); + final PsiAnnotation newAnnotation = factory.createAnnotationFromText("@A(" + text +" )", attribute); + attribute.replace(newAnnotation.getParameterList().getAttributes()[0]); + final FileEditorManager editorManager = FileEditorManager.getInstance(project); final Editor editor = editorManager.getSelectedTextEditor(); if (editor == null) { return; diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/annotation/ExpandToNormalAnnotationPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/annotation/ExpandToNormalAnnotationPredicate.java index 5a7f25b7a4cc..83e343314576 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/annotation/ExpandToNormalAnnotationPredicate.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/annotation/ExpandToNormalAnnotationPredicate.java @@ -21,25 +21,17 @@ import com.siyeh.ipp.base.PsiElementPredicate; class ExpandToNormalAnnotationPredicate implements PsiElementPredicate { public boolean satisfiedBy(PsiElement element) { - if (!(element instanceof PsiAnnotation)) { + if (!(element instanceof PsiNameValuePair)) { return false; } - final PsiAnnotation annotation = (PsiAnnotation)element; - final PsiAnnotationParameterList parameterList = - annotation.getParameterList(); - if (parameterList.getChildren().length == 0) { - return true; - } - final PsiNameValuePair[] attributes = parameterList.getAttributes(); - if (attributes.length != 1) { - return false; - } - final PsiNameValuePair attribute = attributes[0]; + final PsiNameValuePair attribute = (PsiNameValuePair)element; + if (attribute.getName() != null) return false; + final PsiAnnotationMemberValue value = attribute.getValue(); if (value == null) { return false; } - final String name = attribute.getName(); - return name == null; + + return true; } } diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/AlreadyHasName.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/AlreadyHasName.java new file mode 100644 index 000000000000..6823f4ec30f6 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/AlreadyHasName.java @@ -0,0 +1,3 @@ +@SuppressWarnings(value = "foo") +public class Main { +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/MultiAttr.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/MultiAttr.java new file mode 100644 index 000000000000..47ce5f8d780d --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/MultiAttr.java @@ -0,0 +1,3 @@ +@SuppressWarnings("foo", f = "bar") +public class Main { +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/MultiAttr_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/MultiAttr_after.java new file mode 100644 index 000000000000..f86d257bad4b --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/MultiAttr_after.java @@ -0,0 +1,3 @@ +@SuppressWarnings(value = "foo", f = "bar") +public class Main { +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/OneAttr.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/OneAttr.java new file mode 100644 index 000000000000..54c7e943b6d9 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/OneAttr.java @@ -0,0 +1,3 @@ +@SuppressWarnings("foo") +public class Main { +} diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/OneAttr_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/OneAttr_after.java new file mode 100644 index 000000000000..43f2c1cc726b --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/expandToNormal/OneAttr_after.java @@ -0,0 +1,3 @@ +@SuppressWarnings(value = "foo") +public class Main { +} diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntentionTest.java new file mode 100644 index 000000000000..dea52b1d28d3 --- /dev/null +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntentionTest.java @@ -0,0 +1,42 @@ +/* + * 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.siyeh.ipp.annotation; + +import com.siyeh.ipp.IPPTestCase; + +public class ExpandToNormalAnnotationIntentionTest extends IPPTestCase { + public void testOneAttr() throws Exception { + doTest(); + } + + public void testMultiAttr() throws Exception { + doTest(); + } + + public void testAlreadyHasName() throws Exception { + assertIntentionNotAvailable(); + } + + @Override + protected String getIntentionName() { + return "Expand to 'value = \"foo\"'"; + } + + @Override + protected String getRelativePath() { + return "expandToNormal"; + } +}