diff --git a/plugins/IntentionPowerPak/src/META-INF/plugin.xml b/plugins/IntentionPowerPak/src/META-INF/plugin.xml index 63739512e46b..f27472adbbc0 100644 --- a/plugins/IntentionPowerPak/src/META-INF/plugin.xml +++ b/plugins/IntentionPowerPak/src/META-INF/plugin.xml @@ -313,6 +313,13 @@ --> + + + + com.siyeh.ipp.modifiers.MakePublicIntention + intention.category.modifiers + + com.siyeh.ipp.expression.FlipExpressionIntention diff --git a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties index 2ebc33b2f25c..024d80e77afa 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties +++ b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties @@ -154,6 +154,8 @@ obscure.thrown.exceptions.intention.family.name=Replace Exceptions in Throws Cla add.array.creation.expression.intention.family.name=Add Array Creation Expression replace.diamond.with.explicit.type.arguments.intention.name=Replace '<>' with explicit type arguments replace.diamond.with.explicit.type.arguments.intention.family.name=Replace Diamond with Explicit Type Arguments +make.public.intention.name=Make 'public' +make.public.intention.family.name=Make Public #hand made demorgans.intention.name1=Replace '\\&\\&' with '||' @@ -201,4 +203,5 @@ intention.category.imports=Imports intention.category.comments=Comments intention.category.control.flow=Control Flow intention.category.strings=Strings +intention.category.modifiers=Modifiers intention.category.other=Other diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/modifiers/MakePublicIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/modifiers/MakePublicIntention.java new file mode 100644 index 000000000000..698444e16f82 --- /dev/null +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/modifiers/MakePublicIntention.java @@ -0,0 +1,74 @@ +/* + * Copyright 2000-2012 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.modifiers; + +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.psi.*; +import com.intellij.refactoring.ui.ConflictsDialog; +import com.intellij.refactoring.util.RefactoringUIUtil; +import com.intellij.util.IncorrectOperationException; +import com.intellij.util.containers.MultiMap; +import com.siyeh.ipp.base.Intention; +import com.siyeh.ipp.base.PsiElementPredicate; +import org.jetbrains.annotations.NotNull; + +/** + * @author Bas Leijdekkers + */ +public class MakePublicIntention extends Intention { + + @NotNull + @Override + protected PsiElementPredicate getElementPredicate() { + return new MakePublicPredicate(); + } + + @Override + protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException { + final PsiModifierListOwner owner = (PsiModifierListOwner)element.getParent(); + final PsiModifierList modifierList = owner.getModifierList(); + if (modifierList == null) { + return; + } + if (!checkForConflicts(owner)) return; + modifierList.setModifierProperty(PsiModifier.PUBLIC, true); + } + + private static boolean checkForConflicts(PsiModifierListOwner owner) { + if (!(owner instanceof PsiClass)) { + return true; + } + final PsiClass aClass = (PsiClass)owner; + final PsiElement parent = aClass.getParent(); + if (!(parent instanceof PsiJavaFile)) { + return true; + } + final PsiJavaFile javaFile = (PsiJavaFile)parent; + final String name = FileUtil.getNameWithoutExtension(javaFile.getName()); + final String className = aClass.getName(); + if (name.equals(className)) { + return true; + } + final MultiMap conflicts = new MultiMap(); + conflicts.putValue(aClass, "The " + RefactoringUIUtil.getDescription(aClass, false) + " is declared in " + + RefactoringUIUtil.getDescription(javaFile, false) + + " but when public should be declared in a file named '" + className + '\''); + final ConflictsDialog conflictsDialog = new ConflictsDialog(owner.getProject(), conflicts); + conflictsDialog.show(); + return conflictsDialog.isOK(); + } +} diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/modifiers/MakePublicPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/modifiers/MakePublicPredicate.java new file mode 100644 index 000000000000..79be8959a55a --- /dev/null +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/modifiers/MakePublicPredicate.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2012 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.modifiers; + +import com.intellij.psi.*; +import com.intellij.psi.javadoc.PsiDocComment; +import com.siyeh.ipp.base.PsiElementPredicate; + +/** + * @author Bas Leijdekkers + */ +public class MakePublicPredicate implements PsiElementPredicate { + + @Override + public boolean satisfiedBy(PsiElement element) { + final PsiElement parent = element.getParent(); + if (!(parent instanceof PsiClass || parent instanceof PsiField || parent instanceof PsiMethod)) { + return false; + } + if (element instanceof PsiDocComment || element instanceof PsiCodeBlock) { + return false; + } + if (parent instanceof PsiClass) { + final PsiClass aClass = (PsiClass)parent; + final PsiElement brace = aClass.getLBrace(); + if (brace != null && brace.getTextOffset() < element.getTextOffset()) { + return false; + } + } + final PsiModifierListOwner owner = (PsiModifierListOwner)parent; + final PsiModifierList modifierList = owner.getModifierList(); + if (modifierList == null || modifierList.hasModifierProperty(PsiModifier.PUBLIC)) { + return false; + } + return true; + } +} diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/MakePublicIntention/after.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/MakePublicIntention/after.java.template new file mode 100644 index 000000000000..a5ade5c37765 --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/MakePublicIntention/after.java.template @@ -0,0 +1,2 @@ +public class X { +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/MakePublicIntention/before.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/MakePublicIntention/before.java.template new file mode 100644 index 000000000000..c1a262e69c8b --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/MakePublicIntention/before.java.template @@ -0,0 +1,2 @@ +class X { +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/MakePublicIntention/description.html b/plugins/IntentionPowerPak/src/intentionDescriptions/MakePublicIntention/description.html new file mode 100644 index 000000000000..709ad2f0a8a0 --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/MakePublicIntention/description.html @@ -0,0 +1,5 @@ + + +This intention makes a class, method or field public. + + \ No newline at end of file