
org.sahagin.runlib.srctreegen.ASTUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sahagin Show documentation
Show all versions of sahagin Show documentation
Sahagin makes your Selenium script more readable and maintainable.
package org.sahagin.runlib.srctreegen;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.tuple.Pair;
import org.eclipse.jdt.core.dom.IAnnotationBinding;
import org.eclipse.jdt.core.dom.IMemberValuePairBinding;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.sahagin.runlib.external.CaptureStyle;
import org.sahagin.runlib.external.Locale;
import org.sahagin.runlib.external.Page;
import org.sahagin.runlib.external.Pages;
import org.sahagin.runlib.external.TestDoc;
import org.sahagin.runlib.external.TestDocs;
import org.sahagin.share.AcceptableLocales;
public class ASTUtils {
// Get the method annotation whose class name is equals to annotationClass canonical name.
// Return null if specified name annotation is not found.
public static IAnnotationBinding getAnnotationBinding(
IAnnotationBinding[] annotations, String annotationClassName) {
if (annotations == null) {
return null;
}
if (annotationClassName == null) {
throw new NullPointerException();
}
for (IAnnotationBinding annotation : annotations) {
String qName = annotation.getAnnotationType().getBinaryName();
assert qName != null;
if (qName.equals(annotationClassName)) {
return annotation;
}
}
return null;
}
// Get the method annotation whose class equals to annotationClass.
// Return null if specified name annotation is not found.
public static IAnnotationBinding getAnnotationBinding(
IAnnotationBinding[] annotations, Class> annotationClass) {
if (annotationClass == null) {
throw new NullPointerException();
}
return getAnnotationBinding(annotations, annotationClass.getCanonicalName());
}
// returns null if specified varName annotation is not found
public static Object getAnnotationValue(IAnnotationBinding annotation, String varName) {
if (annotation == null) {
throw new NullPointerException();
}
if (varName == null) {
throw new NullPointerException();
}
for (IMemberValuePairBinding value : annotation.getDeclaredMemberValuePairs()) {
if (value.getName() != null && value.getName().equals(varName)) {
assert value.getValue() != null; // annotation value cannot be null
assert !(value.getValue() instanceof IVariableBinding);
return value.getValue();
}
}
return null;
}
// - for example, returns string "STEP_IN" for CaptureStyle.STEP_IN
// returns null if specified varName annotation is not found
public static String getEnumAnnotationFieldName(IAnnotationBinding annotation, String varName) {
if (annotation == null) {
throw new NullPointerException();
}
if (varName == null) {
throw new NullPointerException();
}
for (IMemberValuePairBinding value : annotation.getDeclaredMemberValuePairs()) {
if (value.getName() != null && value.getName().equals(varName)) {
assert value.getValue() != null; // annotation value cannot be null
assert value.getValue() instanceof IVariableBinding;
IVariableBinding varBinding = (IVariableBinding) value.getValue();
assert varBinding.isEnumConstant();
return varBinding.getName();
}
}
return null;
}
// returns default value if varName value is not specified
public static CaptureStyle getAnnotationCaptureStyleValue(
IAnnotationBinding annotation, String varName) {
String fieldName = getEnumAnnotationFieldName(annotation, varName);
if (fieldName == null) {
return CaptureStyle.getDefault();
}
CaptureStyle resultCaptureStyle = CaptureStyle.valueOf(fieldName);
if (resultCaptureStyle == null) {
throw new RuntimeException("invalid captureStyle: " + fieldName);
}
return resultCaptureStyle;
}
// returns default value if varName value is not specified
public static Locale getAnnotationLocaleValue(
IAnnotationBinding annotation, String varName) {
String fieldName = getEnumAnnotationFieldName(annotation, varName);
if (fieldName == null) {
return Locale.getDefault();
}
Locale resultLocale = Locale.valueOf(fieldName);
if (resultLocale == null) {
throw new RuntimeException("invalid locale: " + fieldName);
}
return resultLocale;
}
// return empty list and null pair if no TestDoc is found
private static Pair
© 2015 - 2025 Weber Informatics LLC | Privacy Policy