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() {
26 public static boolean isUtilityClass(@NotNull PsiClass aClass
) {
27 if (aClass
.isInterface() || aClass
.isEnum() || aClass
.isAnnotationType()) {
30 if(aClass
instanceof PsiTypeParameter
||
31 aClass
instanceof PsiAnonymousClass
){
34 final PsiReferenceList extendsList
= aClass
.getExtendsList();
35 if (extendsList
!= null
36 && extendsList
.getReferenceElements().length
> 0) {
39 final PsiReferenceList implementsList
= aClass
.getImplementsList();
40 if (implementsList
!= null
41 && implementsList
.getReferenceElements().length
> 0) {
44 final PsiMethod
[] methods
= aClass
.getMethods();
45 final int staticMethodCount
= countStaticMethods(methods
);
46 if (staticMethodCount
< 0) {
49 final PsiField
[] fields
= aClass
.getFields();
50 if (!allFieldsStatic(fields
)) {
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
)){
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
) {
71 for(final PsiMethod method
: methods
){
72 if (method
.isConstructor()) {
75 if (!method
.hasModifierProperty(PsiModifier
.STATIC
)) {
78 if (method
.hasModifierProperty(PsiModifier
.PRIVATE
)) {