try {
GrapheneContext grapheneContext = ((GrapheneProxyInstance) root).getContext();
T pageFragment = null;
if (Modifier.isFinal(clazz.getModifiers())) {
throw new PageFragmentInitializationException("Page Fragment must not be final class. It is, your "
+ clazz + ", declared in: " + clazz);
}
if (isAboutToDelegateToWebElement(clazz)) {
pageFragment = createProxyDelegatingToRoot(root, clazz);
} else {
pageFragment = instantiate(clazz);
}
List<Field> roots = new LinkedList<Field>();
roots.addAll(ReflectionHelper.getFieldsWithAnnotation(clazz, Root.class));
if (roots.size() > 1) {
throw new PageFragmentInitializationException("The Page Fragment " + NEW_LINE + pageFragment.getClass()
+ NEW_LINE + " can not have more than one field annotated with Root annotation!"
+ "Your fields with @Root annotation: " + roots + NEW_LINE);
}
if (roots.size() == 1) {
setValue(roots.get(0), pageFragment, root);
}
enrichRecursively(root, pageFragment);
T proxy = GrapheneProxy.getProxyForHandler(GrapheneContextualHandler.forTarget(grapheneContext, pageFragment),
clazz);
enrichRecursively(root, proxy); // because of possibility of direct access to attributes from test class
return proxy;
} catch (NoSuchMethodException ex) {
throw new PageFragmentInitializationException(" Check whether declared Page Fragment has no argument constructor!",
ex);
} catch (IllegalAccessException ex) {
throw new PageFragmentInitializationException(
" Check whether declared Page Fragment has public no argument constructor!", ex);
} catch (InstantiationException ex) {
throw new PageFragmentInitializationException(
" Check whether you did not declare Page Fragment with abstract type!", ex);
} catch (Exception ex) {
throw new PageFragmentInitializationException(ex);
}
}