IDEADEV-40452
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / psiutils / UtilityClassUtil.java
blob1a75e1afe9988408f8eb278e01f4a5b0dc92337a
1 /*
2 * Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.siyeh.ig.psiutils;
18 import com.intellij.psi.*;
19 import org.jetbrains.annotations.NotNull;
21 public class UtilityClassUtil {
22 private UtilityClassUtil() {
23 super();
26 public static boolean isUtilityClass(@NotNull PsiClass aClass) {
27 if (aClass.isInterface() || aClass.isEnum() || aClass.isAnnotationType()) {
28 return false;
30 if(aClass instanceof PsiTypeParameter ||
31 aClass instanceof PsiAnonymousClass){
32 return false;
34 final PsiReferenceList extendsList = aClass.getExtendsList();
35 if (extendsList != null
36 && extendsList.getReferenceElements().length > 0) {
37 return false;
39 final PsiReferenceList implementsList = aClass.getImplementsList();
40 if (implementsList != null
41 && implementsList.getReferenceElements().length > 0) {
42 return false;
44 final PsiMethod[] methods = aClass.getMethods();
45 final int staticMethodCount = countStaticMethods(methods);
46 if (staticMethodCount < 0) {
47 return false;
49 final PsiField[] fields = aClass.getFields();
50 if (!allFieldsStatic(fields)) {
51 return false;
53 return staticMethodCount != 0 || fields.length != 0;
56 private static boolean allFieldsStatic(PsiField[] fields) {
57 for(final PsiField field : fields){
58 if(!field.hasModifierProperty(PsiModifier.STATIC)){
59 return false;
62 return true;
65 /**
66 * @return -1 if an instance method was found, else the number of static
67 * methods in the class
69 private static int countStaticMethods(PsiMethod[] methods) {
70 int staticCount = 0;
71 for(final PsiMethod method : methods){
72 if (method.isConstructor()) {
73 continue;
75 if (!method.hasModifierProperty(PsiModifier.STATIC)) {
76 return -1;
78 if (method.hasModifierProperty(PsiModifier.PRIVATE)) {
79 continue;
81 staticCount++;
83 return staticCount;