diff options
Diffstat (limited to 'plugins/IntentionPowerPak')
4 files changed, 32 insertions, 45 deletions
diff --git a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties index dc1b4c6f62d8..30e64929f8b2 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties +++ b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties @@ -145,7 +145,7 @@ convert.j.unit3.test.case.to.j.unit4.intention.name=Convert to JUnit4 Test Case if.to.assertion.intention.family.name=Replace If with Assert Statement if.to.assertion.intention.name=Replace 'if' with 'assert' statement expand.to.normal.annotation.intention.family.name=Expand Annotation to Normal Form -expand.to.normal.annotation.name=Expand to ''{0}'' +expand.to.normal.annotation.intention.name=Add 'value=' to annotation attribute annotate.overridden.methods.intention.family.name=Annotate overriding methods and their parameters annotate.overridden.methods.intention.method.name=Annotate overriding methods as ''@{0}'' annotate.overridden.methods.intention.parameters.name=Annotate same parameter of overriding methods as ''@{0}'' diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntention.java index e9cd879df58e..a1563b807212 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntention.java @@ -15,25 +15,12 @@ */ package com.siyeh.ipp.annotation; -import com.intellij.openapi.editor.CaretModel; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.fileEditor.FileEditorManager; -import com.intellij.openapi.project.Project; import com.intellij.psi.*; -import com.siyeh.IntentionPowerPackBundle; -import com.siyeh.ipp.base.MutablyNamedIntention; +import com.siyeh.ipp.base.Intention; import com.siyeh.ipp.base.PsiElementPredicate; import org.jetbrains.annotations.NotNull; -public class ExpandToNormalAnnotationIntention extends MutablyNamedIntention { - - @Override - protected String getTextForElement(PsiElement element) { - final PsiNameValuePair annotation = (PsiNameValuePair)element; - final String text = buildReplacementText(annotation); - return IntentionPowerPackBundle.message( - "expand.to.normal.annotation.name", text); - } +public class ExpandToNormalAnnotationIntention extends Intention { @NotNull @Override @@ -41,38 +28,28 @@ public class ExpandToNormalAnnotationIntention extends MutablyNamedIntention { return new ExpandToNormalAnnotationPredicate(); } - public static String buildReplacementText(PsiNameValuePair attribute) { + public static String buildReplacementText(PsiAnnotationParameterList annotationParameterList) { final StringBuilder text = new StringBuilder(); - final PsiAnnotationMemberValue value = attribute.getValue(); - text.append("value = "); - if (value != null) { - text.append(value.getText()); + for (PsiNameValuePair nameValuePair : annotationParameterList.getAttributes()) { + if (text.length() != 0) { + text.append(','); + } + final String name = nameValuePair.getName(); + text.append(name != null ? name : "value").append('='); + final PsiAnnotationMemberValue value = nameValuePair.getValue(); + if (value != null) { + text.append(value.getText()); + } } return text.toString(); } @Override protected void processIntention(@NotNull PsiElement element) { - 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); - final PsiAnnotationParameterList parent = (PsiAnnotationParameterList)attribute.getParent(); - for (PsiElement child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { - if (child instanceof PsiErrorElement) { - child.delete(); - break; - } - } - attribute.replace(newAnnotation.getParameterList().getAttributes()[0]); - final FileEditorManager editorManager = FileEditorManager.getInstance(project); - final Editor editor = editorManager.getSelectedTextEditor(); - if (editor == null) { - return; - } - final CaretModel caretModel = editor.getCaretModel(); - caretModel.moveToOffset(textOffset + text.length() - 1); + final PsiAnnotationParameterList annotationParameterList = (PsiAnnotationParameterList)element.getParent(); + final String text = buildReplacementText(annotationParameterList); + final PsiElementFactory factory = JavaPsiFacade.getElementFactory(annotationParameterList.getProject()); + final PsiAnnotation newAnnotation = factory.createAnnotationFromText("@A(" + text +" )", element); + annotationParameterList.replace(newAnnotation.getParameterList()); } } diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/asserttoif/ObjectsRequireNonNullIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/asserttoif/ObjectsRequireNonNullIntention.java index 10caec211a5d..e3617711d6c0 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/asserttoif/ObjectsRequireNonNullIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/asserttoif/ObjectsRequireNonNullIntention.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -22,6 +22,7 @@ import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtil; import com.siyeh.ig.PsiReplacementUtil; +import com.siyeh.ig.psiutils.ClassUtils; import com.siyeh.ig.psiutils.ParenthesesUtils; import com.siyeh.ig.psiutils.VariableAccessUtils; import com.siyeh.ipp.base.Intention; @@ -58,6 +59,9 @@ public class ObjectsRequireNonNullIntention extends Intention { annotation.delete(); } else { final PsiStatement referenceStatement = PsiTreeUtil.getParentOfType(referenceExpression, PsiStatement.class); + if (referenceStatement == null) { + return; + } final PsiElement parent = referenceStatement.getParent(); if (!(parent instanceof PsiCodeBlock)) { return; @@ -103,6 +107,9 @@ public class ObjectsRequireNonNullIntention extends Intention { return false; } final PsiVariable variable = (PsiVariable)target; + if (ClassUtils.findClass("java.util.Objects", element) == null) { + return false; + } if (NullableNotNullManager.isNotNull(variable)) { return true; } diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntentionTest.java index dea52b1d28d3..31a5b061f291 100644 --- a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntentionTest.java +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/annotation/ExpandToNormalAnnotationIntentionTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -17,6 +17,9 @@ package com.siyeh.ipp.annotation; import com.siyeh.ipp.IPPTestCase; +/** + * @see ExpandToNormalAnnotationIntention + */ public class ExpandToNormalAnnotationIntentionTest extends IPPTestCase { public void testOneAttr() throws Exception { doTest(); @@ -32,7 +35,7 @@ public class ExpandToNormalAnnotationIntentionTest extends IPPTestCase { @Override protected String getIntentionName() { - return "Expand to 'value = \"foo\"'"; + return "Add 'value=' to annotation attribute"; } @Override |