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

net.thucydides.core.annotations.Fields Maven / Gradle / Ivy

The newest version!
package net.thucydides.core.annotations;


import com.google.common.base.Optional;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * Find the annotated fields in a given class.
 * Used as a utility class for the higher-level annotation processing.
 * Typical use:
 * 
 *     
 *         for (Field field : Fields.of(someClass).allFields()) {
 *             ...
 *         }
 *     
 * 
*/ public class Fields { private final Class clazz; public static Fields of(final Class testClass) { return new Fields(testClass); } private Fields(Class clazz) { this.clazz = clazz; } public Set allFields() { Set fields = new HashSet(); fields.addAll(Arrays.asList(clazz.getDeclaredFields())); fields.addAll(Arrays.asList(clazz.getFields())); if (clazz != Object.class) { fields.addAll(Fields.of(clazz.getSuperclass()).allFields()); } return fields; } public Set nonStaticFields() { Set fields = allFields(); Set nonStaticFields = new HashSet(); for(Field field : fields) { if (!Modifier.isStatic(field.getModifiers())) { nonStaticFields.add(field); } } return nonStaticFields; } public Optional withName(String pages) { for(Field field : allFields()) { if (field.getName().equals(pages)){ return Optional.of(field); } } return Optional.absent(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy