All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.assertj.util.introspection.Introspection Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/**
 * 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.
 *
 * Copyright 2012-2016 the original author or authors.
 */
package com.fitbur.assertj.util.introspection;

import static java.lang.String.format;
import static java.lang.reflect.Modifier.isPublic;
import static java.util.Locale.ENGLISH;
import static com.fitbur.assertj.util.Preconditions.checkNotNull;
import static com.fitbur.assertj.util.Preconditions.checkNotNullOrEmpty;
import static com.fitbur.assertj.util.Strings.quote;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * Utility methods related to JavaBeans Introspection.
 *
 * @author Alex Ruiz
 */
public final class Introspection {
  /**
   * Returns a {@link PropertyDescriptor} for a property matching the given name in the given object.
   *
   * @param propertyName the given property name.
   * @param target the given object.
   * @return a {@code PropertyDescriptor} for a property matching the given name in the given object.
   * @throws NullPointerException if the given property name is {@code null}.
   * @throws IllegalArgumentException if the given property name is empty.
   * @throws NullPointerException if the given object is {@code null}.
   * @throws IntrospectionError if a matching property cannot be found or accessed.
   */
  public static PropertyDescriptor getProperty(String propertyName, Object target) {
	checkNotNullOrEmpty(propertyName);
	checkNotNull(target);
	PropertyDescriptor prop = getBeanProperty(target.getClass(), propertyName);
	// if not a java 7 property, check if there is a default getter method
	if (prop == null) prop = digForDefaultImplementations(target.getClass(), propertyName);
	if (prop != null) return prop;
    throw new IntrospectionError(propertyNotFoundErrorMessage(propertyName, target));
  }

  private static PropertyDescriptor digForDefaultImplementations(Class type, String propertyName) {
	for (Class interfaz : type.getInterfaces()) {
	  PropertyDescriptor prop = getBeanProperty(interfaz, propertyName);
	  if (prop == null) prop = digForDefaultImplementations(interfaz, propertyName);
	  if (prop != null) return prop;
	}
	return null;
  }

  private static PropertyDescriptor getBeanProperty(Class type, String propertyName) {
	BeanInfo beanInfo;
	try {
	  beanInfo = Introspector.getBeanInfo(type);
	} catch (Exception t) {
	  throw new IntrospectionError(format("Unable to get BeanInfo for type %s", type.getName()), t);
	}
	for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
	  if (propertyName.equals(descriptor.getName())) return descriptor;
	}
	return null;
  }

  private static String propertyNotFoundErrorMessage(String propertyName, Object target) {
	String targetTypeName = target.getClass().getName();
	String property = quote(propertyName);
	Method getter = findGetter(propertyName, target);
	if (getter == null) return format("No getter for property %s in %s", property, targetTypeName);
	if (!isPublic(getter.getModifiers()))
	  return format("No public getter for property %s in %s", property, targetTypeName);
	return format("Unable to find property %s in %s", property, targetTypeName);
  }

  private static Method findGetter(String propertyName, Object target) {
	String capitalized = propertyName.substring(0, 1).toUpperCase(ENGLISH) + propertyName.substring(1);
	// try to find getProperty
	Method getter = findMethod("get" + capitalized, target);
	if (getter != null) return getter;
	// try to find isProperty for boolean properties
    return findMethod("is" + capitalized, target);
  }

  private static Method findMethod(String name, Object target) {
    Class clazz = target.getClass();
    while (clazz != null) {
      try {
        return clazz.getDeclaredMethod(name);
      } catch (NoSuchMethodException | SecurityException ignored) {
      }
      clazz = clazz.getSuperclass();
    }
    return null;
  }

  private Introspection() {
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy