PropertyNamer.java

  1. /*
  2.  *    Copyright 2009-2023 the original author or authors.
  3.  *
  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
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  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.
  15.  */
  16. package org.apache.ibatis.reflection.property;

  17. import java.util.Locale;

  18. import org.apache.ibatis.reflection.ReflectionException;

  19. /**
  20.  * @author Clinton Begin
  21.  */
  22. public final class PropertyNamer {

  23.   private PropertyNamer() {
  24.     // Prevent Instantiation of Static Class
  25.   }

  26.   public static String methodToProperty(String name) {
  27.     if (name.startsWith("is")) {
  28.       name = name.substring(2);
  29.     } else if (name.startsWith("get") || name.startsWith("set")) {
  30.       name = name.substring(3);
  31.     } else {
  32.       throw new ReflectionException(
  33.           "Error parsing property name '" + name + "'.  Didn't start with 'is', 'get' or 'set'.");
  34.     }

  35.     if (name.length() == 1 || name.length() > 1 && !Character.isUpperCase(name.charAt(1))) {
  36.       name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
  37.     }

  38.     return name;
  39.   }

  40.   public static boolean isProperty(String name) {
  41.     return isGetter(name) || isSetter(name);
  42.   }

  43.   public static boolean isGetter(String name) {
  44.     return name.startsWith("get") && name.length() > 3 || name.startsWith("is") && name.length() > 2;
  45.   }

  46.   public static boolean isSetter(String name) {
  47.     return name.startsWith("set") && name.length() > 3;
  48.   }

  49. }