org.fluentlenium.core.inject.LabelAnnotations Maven / Gradle / Ivy
package org.fluentlenium.core.inject;
import org.fluentlenium.core.annotation.Label;
import org.fluentlenium.core.annotation.LabelHint;
import org.fluentlenium.core.label.FluentLabelProvider;
import java.lang.reflect.Field;
/**
* Parses and stores the values of the {@link Label} and {@link LabelHint} annotations of a given field.
*/
public class LabelAnnotations implements FluentLabelProvider {
private String label;
private String[] labelHints;
/**
* Creates a new label annotations object.
*
* If the {@code @Label} annotation is present than it either uses that value as the label,
* of if it's the default empty string value, then sets the label as the concatenation of the field's declaring
* class and the field's name, for example for:
*
* public class Homepage {
*
* @FindBy(css = ".teaser img")
* @Label
* private FluentWebElement teaserImage;
* }
*
*
* the label is set to {@code Homepage.teaserImage}.
*
* If the {@code @LabelHint} annotation is present then it simply sets its value in this object.
*
* @param field field to parse
*/
public LabelAnnotations(Field field) {
if (field.isAnnotationPresent(Label.class)) {
label = field.getAnnotation(Label.class).value();
if (label.isEmpty()) {
label = field.getDeclaringClass().getSimpleName() + "." + field.getName();
}
}
if (field.isAnnotationPresent(LabelHint.class)) {
labelHints = field.getAnnotation(LabelHint.class).value();
}
}
@Override
public String getLabel() {
return label;
}
@SuppressWarnings("PMD.MethodReturnsInternalArray")
@Override
public String[] getLabelHints() {
return labelHints;
}
}