convert abstract class to interface when constructor is empty (IDEA-164278)

This commit is contained in:
Anna.Kozlova
2016-11-28 15:38:48 +01:00
parent 249953cfcf
commit ae6793de43
4 changed files with 60 additions and 1 deletions
@@ -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;
@@ -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 {
}
@@ -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 <caret>ConvertMe {
public ConvertMe() {}
}
@@ -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();
}
}