Search in sources :

Example 61 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project openj9 by eclipse.

the class TypeAnnotatedTestClassTest method checkAnnotations.

private void checkAnnotations(AnnotatedElement[] annotatees, String annotationName, String argumentName, String[] expectedAnnotations) {
    HashSet<String> actualAnnotations = new HashSet<>(annotatees.length);
    for (AnnotatedElement at : annotatees) {
        for (Annotation an : at.getAnnotations()) {
            actualAnnotations.add(an.toString());
        }
    }
    if (verbose) {
        logger.debug("=================================");
        for (String s : actualAnnotations) {
            logger.debug("Annotation: " + s);
        }
    }
    if (null != expectedAnnotations) {
        AssertJUnit.assertEquals("wrong number of annotations", expectedAnnotations.length, actualAnnotations.size());
        for (String s : expectedAnnotations) {
            String comparandJava8 = "@" + annotationName + "(" + argumentName + "=" + s + ")";
            String comparandJava9 = "@" + annotationName + "(" + argumentName + "=\"" + s + "\")";
            AssertJUnit.assertTrue(comparandJava8 + " and " + comparandJava9 + " missing", actualAnnotations.contains(comparandJava8) || actualAnnotations.contains(comparandJava9));
        }
    } else {
        AssertJUnit.assertTrue("unexpected annotations", actualAnnotations.size() == 0);
    }
}
Also used : AnnotatedElement(java.lang.reflect.AnnotatedElement) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet)

Example 62 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project java-client by appium.

the class WidgetByBuilder method getByFromDeclaredClass.

@SuppressWarnings("unchecked")
private By getByFromDeclaredClass(WhatIsNeeded whatIsNeeded) {
    AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
    Field field = Field.class.cast(annotatedElement);
    Class<?> declaredClass;
    By result = null;
    try {
        if (List.class.isAssignableFrom(field.getType())) {
            declaredClass = getClassFromAListField(field);
        } else {
            declaredClass = field.getType();
        }
        Class<?> convenientClass;
        if (whatIsNeeded.equals(WhatIsNeeded.DEFAULT_OR_HTML)) {
            convenientClass = getDefaultOrHTMLWidgetClass((Class<? extends Widget>) declaredClass, field);
        } else {
            convenientClass = getMobileNativeWidgetClass((Class<? extends Widget>) declaredClass, field, platform, automation);
        }
        while (result == null && !convenientClass.equals(Object.class)) {
            setAnnotated(convenientClass);
            if (whatIsNeeded.equals(WhatIsNeeded.DEFAULT_OR_HTML)) {
                result = super.buildDefaultBy();
            } else {
                result = super.buildMobileNativeBy();
            }
            convenientClass = convenientClass.getSuperclass();
        }
        return result;
    } finally {
        if (field != null) {
            setAnnotated(field);
        }
    }
}
Also used : Field(java.lang.reflect.Field) By(org.openqa.selenium.By) AnnotatedElement(java.lang.reflect.AnnotatedElement) OverrideWidgetReader.getDefaultOrHTMLWidgetClass(io.appium.java_client.pagefactory.OverrideWidgetReader.getDefaultOrHTMLWidgetClass) OverrideWidgetReader.getMobileNativeWidgetClass(io.appium.java_client.pagefactory.OverrideWidgetReader.getMobileNativeWidgetClass)

Example 63 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project java-client by appium.

the class DefaultElementByBuilder method buildMobileNativeBy.

@Override
protected By buildMobileNativeBy() {
    AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
    HowToUseLocators howToUseLocators = annotatedElement.getAnnotation(HowToUseLocators.class);
    Optional<HowToUseLocators> howToUseLocatorsOptional = ofNullable(howToUseLocators);
    By result = null;
    if (isSelendroidAutomation()) {
        result = buildMobileBy(howToUseLocatorsOptional.map(HowToUseLocators::selendroidAutomation).orElse(null), getBys(SelendroidFindBy.class, SelendroidFindBys.class, SelendroidFindAll.class));
    }
    if (isAndroid() && result == null) {
        return buildMobileBy(howToUseLocatorsOptional.map(HowToUseLocators::androidAutomation).orElse(null), getBys(AndroidFindBy.class, AndroidFindBys.class, AndroidFindAll.class));
    }
    if (isIOSXcuit()) {
        result = buildMobileBy(howToUseLocatorsOptional.map(HowToUseLocators::iOSXCUITAutomation).orElse(null), getBys(iOSXCUITFindBy.class, iOSXCUITFindBys.class, iOSXCUITFindAll.class));
    }
    if (isIOS() && result == null) {
        return buildMobileBy(howToUseLocatorsOptional.map(HowToUseLocators::iOSAutomation).orElse(null), getBys(iOSFindBy.class, iOSFindBys.class, iOSFindAll.class));
    }
    if (isWindows()) {
        return buildMobileBy(howToUseLocatorsOptional.map(HowToUseLocators::windowsAutomation).orElse(null), getBys(WindowsFindBy.class, WindowsFindBys.class, WindowsFindAll.class));
    }
    return ofNullable(result).orElse(null);
}
Also used : AnnotatedElement(java.lang.reflect.AnnotatedElement) FindBy(org.openqa.selenium.support.FindBy) By(org.openqa.selenium.By) ContentMappedBy(io.appium.java_client.pagefactory.bys.ContentMappedBy)

Example 64 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project java-client by appium.

the class DefaultElementByBuilder method getBys.

private By[] getBys(Class<? extends Annotation> singleLocator, Class<? extends Annotation> chainedLocator, Class<? extends Annotation> allLocator) {
    AnnotationComparator comparator = new AnnotationComparator();
    AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
    List<Annotation> annotations = new ArrayList<>(asList(annotatedElement.getAnnotationsByType(singleLocator)));
    annotations.addAll(asList(annotatedElement.getAnnotationsByType(chainedLocator)));
    annotations.addAll(asList(annotatedElement.getAnnotationsByType(allLocator)));
    annotations.sort(comparator);
    List<By> result = new ArrayList<>();
    for (Annotation a : annotations) {
        Class<?> annotationClass = a.annotationType();
        if (singleLocator.equals(annotationClass)) {
            result.add(createBy(new Annotation[] { a }, HowToUseSelectors.USE_ONE));
            continue;
        }
        Method value;
        Annotation[] subLocators;
        try {
            value = annotationClass.getMethod(VALUE, ANNOTATION_ARGUMENTS);
            subLocators = (Annotation[]) value.invoke(a, ANNOTATION_PARAMETERS);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            throw new ClassCastException(String.format("The annotation '%s' has no convenient '%s' method which " + "returns array of annotations", annotationClass.getName(), VALUE));
        }
        Arrays.sort(subLocators, comparator);
        if (chainedLocator.equals(annotationClass)) {
            result.add(createBy(subLocators, HowToUseSelectors.BUILD_CHAINED));
            continue;
        }
        if (allLocator.equals(annotationClass)) {
            result.add(createBy(subLocators, HowToUseSelectors.USE_ANY));
        }
    }
    return result.toArray(new By[result.size()]);
}
Also used : AnnotatedElement(java.lang.reflect.AnnotatedElement) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException) FindBy(org.openqa.selenium.support.FindBy) By(org.openqa.selenium.By) ContentMappedBy(io.appium.java_client.pagefactory.bys.ContentMappedBy)

Example 65 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project candlepin by candlepin.

the class HibernateValidationAnnotationTest method runMap.

private void runMap(Map<Field, Matcher<AnnotatedElement>> fm) {
    for (Map.Entry<Field, Matcher<AnnotatedElement>> entry : fm.entrySet()) {
        Matcher<AnnotatedElement> matcher = entry.getValue();
        Field field = entry.getKey();
        assertTrue(matcher.matches(field));
    }
}
Also used : Field(java.lang.reflect.Field) Matcher(com.google.inject.matcher.Matcher) AnnotatedElement(java.lang.reflect.AnnotatedElement) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

AnnotatedElement (java.lang.reflect.AnnotatedElement)106 Method (java.lang.reflect.Method)23 Annotation (java.lang.annotation.Annotation)17 Field (java.lang.reflect.Field)17 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)11 HashMap (java.util.HashMap)9 Test (org.junit.jupiter.api.Test)8 Member (java.lang.reflect.Member)7 LinkedHashSet (java.util.LinkedHashSet)7 List (java.util.List)7 Constructor (java.lang.reflect.Constructor)6 Type (java.lang.reflect.Type)6 Map (java.util.Map)6 HashSet (java.util.HashSet)5 By (org.openqa.selenium.By)5 Statement (org.junit.runners.model.Statement)4 FindBy (org.openqa.selenium.support.FindBy)4 EntityType (com.querydsl.codegen.EntityType)3 Collection (java.util.Collection)3