diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml index a206d5754c91..dddb6e1d3988 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml @@ -503,6 +503,10 @@ key="method.return.always.constant.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.class.structure" enabledByDefault="false" level="WARNING" implementationClass="com.siyeh.ig.classlayout.MethodReturnAlwaysConstantInspection"/> + #ref() uses the platform's default charset implicit.default.charset.usage.constructor.problem.descriptor=new #ref() call uses the platform's default charset +interface.may.be.annotated.functional.display.name=Interface may be annotated @FunctionalInterface +interface.may.be.annotated.functional.problem.descriptor=Interface #ref may be annotated with @FunctionalInterface diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/classlayout/InterfaceMayBeAnnotatedFunctionalInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/classlayout/InterfaceMayBeAnnotatedFunctionalInspection.java new file mode 100644 index 000000000000..13b134dcceb9 --- /dev/null +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/classlayout/InterfaceMayBeAnnotatedFunctionalInspection.java @@ -0,0 +1,80 @@ +/* + * 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. + * 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.ig.classlayout; + +import com.intellij.codeInsight.intention.AddAnnotationPsiFix; +import com.intellij.psi.LambdaHighlightingUtil; +import com.intellij.psi.LambdaUtil; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiNameValuePair; +import com.intellij.psi.util.PsiUtil; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.BaseInspection; +import com.siyeh.ig.BaseInspectionVisitor; +import com.siyeh.ig.DelegatingFix; +import com.siyeh.ig.InspectionGadgetsFix; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class InterfaceMayBeAnnotatedFunctionalInspection extends BaseInspection { + @Nls + @NotNull + @Override + public String getDisplayName() { + return InspectionGadgetsBundle.message("interface.may.be.annotated.functional.display.name"); + } + + @NotNull + @Override + protected String buildErrorString(Object... infos) { + return InspectionGadgetsBundle.message("interface.may.be.annotated.functional.problem.descriptor"); + } + + @Nullable + @Override + protected InspectionGadgetsFix buildFix(Object... infos) { + final PsiClass aClass = (PsiClass)infos[0]; + return new DelegatingFix(new AddAnnotationPsiFix(LambdaUtil.JAVA_LANG_FUNCTIONAL_INTERFACE, aClass, PsiNameValuePair.EMPTY_ARRAY)); + + } + + @Override + public BaseInspectionVisitor buildVisitor() { + return new InterfaceMayBeAnnotatedFunctionalVisitor(); + } + + private static class InterfaceMayBeAnnotatedFunctionalVisitor extends BaseInspectionVisitor { + + @Override + public void visitClass(PsiClass aClass) { + if (!PsiUtil.isLanguageLevel8OrHigher(aClass)) { + return; + } + super.visitClass(aClass); + if (!aClass.isInterface()) { + return; + } + if (LambdaHighlightingUtil.checkInterfaceFunctional(aClass) != null) { + return; + } + registerClassError(aClass, aClass); + } + } +} diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/InterfaceMayBeAnnotatedFunctional.html b/plugins/InspectionGadgets/src/inspectionDescriptions/InterfaceMayBeAnnotatedFunctional.html new file mode 100644 index 000000000000..66ea1b4ac432 --- /dev/null +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/InterfaceMayBeAnnotatedFunctional.html @@ -0,0 +1,10 @@ + + +Reports any interface that may be annotated as @FunctionalInterface, which is available in JDK 1.8. +Annotating an interface with @FunctionalInterface can be useful to indicate that the interface is supposed to be functional and +further abstract methods should not be added to it. +This inspection only reports if the project or module is configured to use a language level of 8.0 or higher. +

+New in 13.1 + + \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/classlayout/InterfaceMayBeAnnotatedFunctionalInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/classlayout/InterfaceMayBeAnnotatedFunctionalInspectionTest.java new file mode 100644 index 000000000000..ab4e3e8f77f5 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/classlayout/InterfaceMayBeAnnotatedFunctionalInspectionTest.java @@ -0,0 +1,51 @@ +/* + * 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. + * 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.ig.classlayout; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; + +/** + * @author Bas Leijdekkers + */ +public class InterfaceMayBeAnnotatedFunctionalInspectionTest extends LightInspectionTestCase { + + public void testSimple() { + doTest("interface /*Interface 'Simple' may be annotated with @FunctionalInterface*/Simple/**/ {" + + " void f();" + + "}"); + } + + public void testLessSimple() { + doTest("interface /*Interface 'F' may be annotated with @FunctionalInterface*/F/**/ {" + + " boolean equals(Object o);" + + " static void f00() {}\n" + + " default void g() {}\n" + + " void f();\n" + + "}"); + } + + public void testNotFunctional() { + doTest("interface G {" + + " void f();" + + "}"); + } + + @Override + protected InspectionProfileEntry getInspection() { + return new InterfaceMayBeAnnotatedFunctionalInspection(); + } +}