Java tutorial
/* * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package org.dbunitng.beans; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import java.util.Set; import com.google.common.base.CaseFormat; import lombok.ToString; import org.dbunitng.util.PropertyUtil; /** * ? * * @author jyukutyo * */ @ToString public class BeanMetaData { /** ? */ private Class<?> targetClass; /** ??Map */ private Map<String, BeanProperty> propertyMap; /** * ?? * * @param targetClass * ? */ public BeanMetaData(Class<?> targetClass) { this.targetClass = targetClass; propertyMap = new HashMap<String, BeanProperty>(); retrievePropertiesByMethod(); retrievePropertiesByField(); } /** * ???? */ protected void retrievePropertiesByField() { Field[] fields = targetClass.getFields(); for (int i = 0; i < fields.length; ++i) { Field field = fields[i]; field.setAccessible(true); if (PropertyUtil.isInstanceField(field)) { String fname = field.getName(); BeanProperty property = propertyMap.get(fname); if (property == null) { property = new BeanProperty(fname, field.getType(), field, null, null); propertyMap.put(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, fname), property); } else if (PropertyUtil.isPublicField(field)) { property.setField(field); } } } } /** * ???? */ protected void retrievePropertiesByMethod() { Method[] methods = targetClass.getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; String methodName = method.getName(); if (methodName.startsWith("get")) { if (method.getParameterTypes().length != 0 || methodName.equals("getClass") || method.getReturnType() == void.class) { // ????void?getClass()? getter????????? continue; } // ???get?????4?????? String propertyName = PropertyUtil.decapitalizePropertyName(methodName.substring(3)); addPropertyWithGetter(propertyName, method); } else if (methodName.startsWith("is")) { Class<?> type = method.getReturnType(); if (method.getParameterTypes().length != 0 || !type.equals(Boolean.TYPE) && !type.equals(Boolean.class)) { // ????boolean???Boolean???????? continue; } // is?????3?????? String propertyName = PropertyUtil.decapitalizePropertyName(methodName.substring(2)); addPropertyWithGetter(propertyName, method); } else if (methodName.startsWith("set")) { if (method.getParameterTypes().length != 1 || methodName.equals("setClass") || method.getReturnType() != void.class) { continue; } String propertyName = PropertyUtil.decapitalizePropertyName(methodName.substring(3)); addPropertyWithSetter(propertyName, method); } } } /** * ? * * @return */ public Class<?> getTargetClass() { return targetClass; } /** * ? * * @param name * ?? * @param setter * Setter */ protected void addPropertyWithSetter(String name, Method setter) { String lowerName = name.toLowerCase(); BeanProperty property = propertyMap.get(lowerName); Class<?> type = setter.getParameterTypes()[0]; if (property == null) { property = new BeanProperty(name, type, null, null, setter); propertyMap.put(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name), property); } else { property.setSetter(setter); } } /** * ? * * @param name * ?? * @param getter * Getter */ protected void addPropertyWithGetter(String name, Method getter) { String lowerName = name.toLowerCase(); BeanProperty property = propertyMap.get(lowerName); Class<?> type = getter.getReturnType(); if (property == null) { property = new BeanProperty(name, type, null, getter, null); propertyMap.put(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name), property); } else { property.setGetter(getter); } } /** * ???Set? * * @return ???Set */ public Set<String> getNameSet() { return propertyMap.keySet(); } /** * {@link BeanProperty} ? * * @param name * ?? * @return BeanProperty */ public BeanProperty getProperty(String name) { if (name == null) { return null; } return propertyMap.get(name.toLowerCase()); } }