diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/classlayout/ClassMayBeInterfaceInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/classlayout/ClassMayBeInterfaceInspection.java index a5b8c0bb6974..7f12362aaad1 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/classlayout/ClassMayBeInterfaceInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/classlayout/ClassMayBeInterfaceInspection.java @@ -111,6 +111,10 @@ public class ClassMayBeInterfaceInspection extends BaseInspection { private static void changeClassToInterface(PsiClass aClass) { for (PsiMethod method : aClass.getMethods()) { + if (isEmptyConstructor(method)) { + method.delete(); + continue; + } PsiUtil.setModifierProperty(method, PsiModifier.PUBLIC, false); if (method.hasModifierProperty(PsiModifier.STATIC) || method.hasModifierProperty(PsiModifier.ABSTRACT)) { continue; @@ -193,6 +197,16 @@ public class ClassMayBeInterfaceInspection extends BaseInspection { return new ClassMayBeInterfaceVisitor(); } + private static boolean isEmptyConstructor(@NotNull PsiMethod method) { + if (method.isConstructor()) { + PsiCodeBlock body = method.getBody(); + if (body != null && body.getStatements().length == 0 && method.getDocComment() == null) { + return true; + } + } + return false; + } + private class ClassMayBeInterfaceVisitor extends BaseInspectionVisitor { @Override @@ -250,6 +264,9 @@ public class ClassMayBeInterfaceInspection extends BaseInspection { private boolean allMethodsPublicAbstract(PsiClass aClass) { final PsiMethod[] methods = aClass.getMethods(); for (final PsiMethod method : methods) { + if (isEmptyConstructor(method)) { + continue; + } if (!method.hasModifierProperty(PsiModifier.ABSTRACT) && (!reportClassesWithNonAbstractMethods || !PsiUtil.isLanguageLevel8OrHigher(aClass))) { return false; diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/classlayout/class_may_be_interface/DefaultConstructor.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/classlayout/class_may_be_interface/DefaultConstructor.after.java new file mode 100644 index 000000000000..d10458fd9bd7 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/classlayout/class_may_be_interface/DefaultConstructor.after.java @@ -0,0 +1,19 @@ +/* + * Copyright 2000-2016 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.igfixes.classlayout.class_may_be_interface; + +interface ConvertMe { +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/classlayout/class_may_be_interface/DefaultConstructor.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/classlayout/class_may_be_interface/DefaultConstructor.java new file mode 100644 index 000000000000..5e3b2e82ef5f --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/classlayout/class_may_be_interface/DefaultConstructor.java @@ -0,0 +1,20 @@ +/* + * Copyright 2000-2016 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.igfixes.classlayout.class_may_be_interface; + +abstract class ConvertMe { + public ConvertMe() {} +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/classlayout/ClassMayBeInterfaceFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/classlayout/ClassMayBeInterfaceFixTest.java index a1e6a7bee27d..e11504b46f82 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/classlayout/ClassMayBeInterfaceFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/classlayout/ClassMayBeInterfaceFixTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -36,4 +36,7 @@ public class ClassMayBeInterfaceFixTest extends IGQuickFixesTestCase { public void testConvertMe() { doTest(); } + public void testDefaultConstructor() throws Exception { + doTest(); + } } \ No newline at end of file