Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
import static com.fitbur.assertj.util.ArrayWrapperList.wrap;
import static com.fitbur.assertj.util.IterableUtil.isNullOrEmpty;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* Utility methods for fields access.
*
* @author Joel Costigliola
*/
public enum FieldSupport {
EXTRACTION(true),
EXTRACTION_OF_PUBLIC_FIELD_ONLY(false),
COMPARISON(true);
private static final String SEPARATOR = ".";
private boolean allowUsingPrivateFields;
/**
* Returns the instance dedicated to extraction of fields.
*
* @return the instance dedicated to extraction of fields.
*/
public static FieldSupport extraction() {
return EXTRACTION;
}
/**
* Returns the instance dedicated to comparison of fields.
*
* @return the instance dedicated to comparison of fields.
*/
public static FieldSupport comparison() {
return COMPARISON;
}
/**
* Build a new {@link FieldSupport}
*
* @param allowUsingPrivateFields whether to read private fields or not.
*/
FieldSupport(boolean allowUsingPrivateFields) {
this.allowUsingPrivateFields = allowUsingPrivateFields;
}
public boolean isAllowedToUsePrivateFields() {
return allowUsingPrivateFields;
}
/**
* Sets whether the use of private fields is allowed.
* If a method tries to extract/compare private fields and is not allowed to, it will fail with an exception.
*
* @param allowUsingPrivateFields allow private fields extraction and comparison. Default {@code true}.
*/
public void setAllowUsingPrivateFields(boolean allowUsingPrivateFields) {
this.allowUsingPrivateFields = allowUsingPrivateFields;
}
/**
* Returns a {@link List} containing the values of the given field name, from the elements of the given
* {@link Iterable}. If the given {@code Iterable} is empty or {@code null}, this method will return an
* empty {@code List}. This method supports nested fields (e.g. "address.street.number").
*
* @param fieldName the name of the field. It may be a nested field. It is left to the clients to validate for
* {@code null} or empty.
* @param fieldClass the expected type of the given field.
* @param target the given {@code Iterable}.
* @return an {@code Iterable} containing the values of the given field name, from the elements of the given
* {@code Iterable}.
* @throws IntrospectionError if an element in the given {@code Iterable} does not have a field with a matching name.
*/
public List fieldValues(String fieldName, Class fieldClass, Iterable> target) {
if (isNullOrEmpty(target)) return emptyList();
if (isNestedField(fieldName)) {
String firstFieldName = popFieldNameFrom(fieldName);
Iterable