IDEADEV-40041: the getter and setter method doesn't follow naming convention
[fedora-idea.git] / plugins / groovy / src / org / jetbrains / plugins / groovy / lang / psi / util / GroovyPropertyUtils.java
blob5e191f42d23e37e5540fbdec23028e692f1df7d3
1 /*
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.
16 package org.jetbrains.plugins.groovy.lang.psi.util;
18 import com.intellij.openapi.project.Project;
19 import com.intellij.openapi.util.text.StringUtil;
20 import com.intellij.psi.*;
21 import com.intellij.psi.util.PropertyUtil;
22 import org.jetbrains.annotations.NonNls;
23 import org.jetbrains.annotations.NotNull;
24 import org.jetbrains.annotations.Nullable;
25 import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField;
26 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod;
28 import java.beans.Introspector;
30 /**
31 * @author ilyas
33 public class GroovyPropertyUtils {
34 private GroovyPropertyUtils() {
37 @Nullable
38 public static PsiMethod findSetterForField(PsiField field) {
39 final PsiClass containingClass = field.getContainingClass();
40 final Project project = field.getProject();
41 final String propertyName = PropertyUtil.suggestPropertyName(project, field);
42 final boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
43 return findPropertySetter(containingClass, propertyName, isStatic, true);
46 @Nullable
47 public static PsiMethod findGetterForField(PsiField field) {
48 final PsiClass containingClass = field.getContainingClass();
49 final Project project = field.getProject();
50 final String propertyName = PropertyUtil.suggestPropertyName(project, field);
51 final boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
52 return PropertyUtil.findPropertyGetter(containingClass, propertyName, isStatic, true);
55 @Nullable
56 public static PsiMethod findPropertySetter(PsiClass aClass, String propertyName, boolean isStatic, boolean checkSuperClasses) {
57 if (aClass == null) return null;
58 PsiMethod[] methods;
59 if (checkSuperClasses) {
60 methods = aClass.getAllMethods();
62 else {
63 methods = aClass.getMethods();
66 for (PsiMethod method : methods) {
67 if (method.hasModifierProperty(PsiModifier.STATIC) != isStatic) continue;
69 if (isSimplePropertySetter(method)) {
70 if (getPropertyNameBySetter(method).equals(propertyName)) {
71 return method;
76 return null;
79 @Nullable
80 public static PsiMethod findPropertyGetter(PsiClass aClass, String propertyName, boolean isStatic, boolean checkSuperClasses) {
81 if (aClass == null) return null;
82 PsiMethod[] methods;
83 if (checkSuperClasses) {
84 methods = aClass.getAllMethods();
86 else {
87 methods = aClass.getMethods();
90 for (PsiMethod method : methods) {
91 if (method.hasModifierProperty(PsiModifier.STATIC) != isStatic) continue;
93 if (isSimplePropertyGetter(method)) {
94 if (getPropertyNameByGetter(method).equals(propertyName)) {
95 return method;
100 return null;
103 public static boolean isSimplePropertyAccessor(PsiMethod method) {
104 return isSimplePropertyGetter(method) || isSimplePropertySetter(method);
105 }//do not check return type
107 public static boolean isSimplePropertyGetter(PsiMethod method) {
108 return isSimplePropertyGetter(method, null);
109 }//do not check return type
111 public static boolean isSimplePropertyGetter(PsiMethod method, String propertyName) {
112 if (method == null || method.isConstructor()) return false;
113 if (method.getParameterList().getParametersCount() != 0) return false;
114 if (!isGetterName(method.getName())) return false;
115 return (propertyName == null || propertyName.equals(getPropertyNameByGetter(method))) && method.getReturnType() != PsiType.VOID;
118 public static boolean isSimplePropertySetter(PsiMethod method) {
119 return isSimplePropertySetter(method, null);
122 public static boolean isSimplePropertySetter(PsiMethod method, String propertyName) {
123 if (method == null || method.isConstructor()) return false;
124 if (method.getParameterList().getParametersCount() != 1) return false;
125 if (!isSetterName(method.getName())) return false;
126 return propertyName == null || propertyName.equals(getPropertyNameBySetter(method));
129 public static String getPropertyNameByGetter(PsiMethod getterMethod) {
130 if (getterMethod instanceof GrAccessorMethod) {
131 return ((GrAccessorMethod)getterMethod).getProperty().getName();
134 @NonNls String methodName = getterMethod.getName();
135 if (methodName.startsWith("get") && methodName.length() > 3) {
136 return decapitalize(methodName.substring(3));
138 else if (methodName.startsWith("is") && methodName.length() > 2 && PsiType.BOOLEAN.equals(getterMethod.getReturnType())) {
139 return decapitalize(methodName.substring(2));
141 return methodName;
144 public static String getPropertyNameBySetter(PsiMethod setterMethod) {
145 if (setterMethod instanceof GrAccessorMethod) {
146 return ((GrAccessorMethod)setterMethod).getProperty().getName();
149 @NonNls String methodName = setterMethod.getName();
150 if (methodName.startsWith("set") && methodName.length() > 3) {
151 return StringUtil.decapitalize(methodName.substring(3));
153 else {
154 return methodName;
158 public static String getPropertyName(PsiMethod accessor) {
159 if (isSimplePropertyGetter(accessor)) return getPropertyNameByGetter(accessor);
160 if (isSimplePropertySetter(accessor)) return getPropertyNameBySetter(accessor);
161 return accessor.getName();
164 public static boolean isGetterName(@NotNull String name) {
165 if (name.startsWith("get") && name.length() > 3 && isUpperCase(name.charAt(3))) return true;
166 if (name.startsWith("is") && name.length() > 2 && isUpperCase(name.charAt(2))) return true;
167 return false;
170 public static boolean isSetterName(String name) {
171 return name != null && name.startsWith("set") && name.length() > 3 && isUpperCase(name.charAt(3));
174 public static boolean isProperty(@Nullable PsiClass aClass, @Nullable String propertyName, boolean isStatic) {
175 if (aClass == null || propertyName == null) return false;
176 final PsiField field = aClass.findFieldByName(propertyName, true);
177 if (field instanceof GrField && ((GrField)field).isProperty() && field.hasModifierProperty(PsiModifier.STATIC) == isStatic) return true;
179 final PsiMethod getter = findPropertyGetter(aClass, propertyName, isStatic, true);
180 if (getter != null && getter.hasModifierProperty(PsiModifier.PUBLIC)) return true;
182 final PsiMethod setter = findPropertySetter(aClass, propertyName, isStatic, true);
183 return setter != null && setter.hasModifierProperty(PsiModifier.PUBLIC);
186 public static boolean isProperty(GrField field) {
187 final PsiClass clazz = field.getContainingClass();
188 return isProperty(clazz, field.getName(), field.hasModifierProperty(PsiModifier.STATIC));
191 private static boolean isUpperCase(char c) {
192 return Character.toUpperCase(c) == c;
195 /*public static boolean canBePropertyName(String name) {
196 return !(name.length() > 1 && Character.isUpperCase(name.charAt(1)) && Character.isLowerCase(name.charAt(0)));
199 public static String capitalize(String s) {
200 if (s.length() == 0) return s;
201 if (s.length() == 1) return s.toUpperCase();
202 if (Character.isUpperCase(s.charAt(1))) return s;
203 final char[] chars = s.toCharArray();
204 chars[0] = Character.toUpperCase(chars[0]);
205 return new String(chars);
208 public static String decapitalize(String s) {
209 return Introspector.decapitalize(s);