org.mockito.internal.util.reflection.Fields Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockito-all Show documentation
Show all versions of mockito-all Show documentation
Mock objects library for java
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util.reflection;
import org.mockito.Incubating;
import org.mockito.internal.util.Checks;
import org.mockito.internal.util.collections.ListUtil.Filter;
import org.mockito.internal.util.collections.ListUtil;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* Small fluent reflection tools to work with fields.
*
* Code is very new and might need rework.
*/
@Incubating
public abstract class Fields {
/**
* Instance fields declared in the class and superclasses of the given instance.
*
* @param instance Instance from which declared fields will be retrieved.
* @return InstanceFields of this object instance.
*/
public static InstanceFields allDeclaredFieldsOf(Object instance) {
List instanceFields = new ArrayList();
for (Class> clazz = instance.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
instanceFields.addAll(instanceFieldsIn(instance, clazz.getDeclaredFields()));
}
return new InstanceFields(instance, instanceFields);
}
/**
* Instance fields declared in the class of the given instance.
*
* @param instance Instance from which declared fields will be retrieved.
* @return InstanceFields of this object instance.
*/
public static InstanceFields declaredFieldsOf(Object instance) {
List instanceFields = new ArrayList();
instanceFields.addAll(instanceFieldsIn(instance, instance.getClass().getDeclaredFields()));
return new InstanceFields(instance, instanceFields);
}
private static List instanceFieldsIn(Object instance, Field[] fields) {
List instanceDeclaredFields = new ArrayList();
for (Field field : fields) {
InstanceField instanceField = new InstanceField(field, instance);
instanceDeclaredFields.add(instanceField);
}
return instanceDeclaredFields;
}
/**
* Accept fields annotated by the given annotations.
*
* @param annotations Annotation types to check.
* @return The filter.
*/
public static Filter annotatedBy(final Class extends Annotation>... annotations) {
return new Filter() {
public boolean isOut(InstanceField instanceField) {
Checks.checkNotNull(annotations, "Provide at least one annotation class");
for (Class extends Annotation> annotation : annotations) {
if(instanceField.isAnnotatedBy(annotation)) {
return false;
}
}
return true;
}
};
}
/**
* Accept fields with non null value.
*
* @return The filter.
*/
private static Filter nullField() {
return new Filter() {
public boolean isOut(InstanceField instanceField) {
return instanceField.isNull();
}
};
}
public static class InstanceFields {
private final Object instance;
private final List instanceFields;
public InstanceFields(Object instance, List instanceFields) {
this.instance = instance;
this.instanceFields = instanceFields;
}
public InstanceFields filter(Filter withFilter) {
return new InstanceFields(instance, ListUtil.filter(instanceFields, withFilter));
}
public InstanceFields notNull() {
return filter(nullField());
}
public List instanceFields() {
return new ArrayList(instanceFields);
}
public List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy