2 * Copyright 2000-2009 JetBrains s.r.o.
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.
21 package com
.intellij
.refactoring
.util
;
23 import com
.intellij
.psi
.*;
24 import com
.intellij
.psi
.util
.TypeConversionUtil
;
25 import com
.intellij
.util
.IncorrectOperationException
;
27 public class EnumConstantsUtil
{
28 private EnumConstantsUtil() {
31 public static boolean isSuitableForEnumConstant(PsiType constantType
, PsiClass enumClass
) {
32 if (enumClass
!= null && enumClass
.isEnum()) {
33 for (PsiMethod constructor
: enumClass
.getConstructors()) {
34 final PsiParameter
[] parameters
= constructor
.getParameterList().getParameters();
35 if (parameters
.length
== 1 && TypeConversionUtil
.isAssignable(parameters
[0].getType(), constantType
)) return true;
41 public static PsiEnumConstant
createEnumConstant(PsiClass enumClass
, String constantName
, PsiExpression initializerExpr
) throws
42 IncorrectOperationException
{
43 final PsiElementFactory elementFactory
= JavaPsiFacade
.getInstance(enumClass
.getProject()).getElementFactory();
44 final String enumConstantText
= initializerExpr
!= null ? constantName
+ "(" + initializerExpr
.getText() + ")" : constantName
;
45 return elementFactory
.createEnumConstantFromText(enumConstantText
, enumClass
);
48 public static PsiEnumConstant
createEnumConstant(PsiClass enumClass
, PsiLocalVariable local
, final String fieldName
) throws IncorrectOperationException
{
49 return createEnumConstant(enumClass
, fieldName
, local
.getInitializer());