@Test public void shouldReturnTestString() { Object instance1 = new Sample(); Object instance2 = new Sample2(); assertEquals("testString", uiRepresentationString(instance1)); assertEquals("testString", uiRepresentationString(instance2)); }
public static String uiRepresentationString(Object instance) { if(instance == null) { return ""; } Class clazz = instance.getClass(); List<Method> methods = new ArrayList<>(); for (Method method : clazz.getDeclaredMethods()) { if (method.isAnnotationPresent(UIRepresentation.class)) { if (ArrayUtils.isEmpty(method.getParameterTypes()) && String.class.equals(method.getReturnType())){ methods.add(method); } else { LOGGER.error("Method {} is not a valid method for @UIRepresentation annotation." + " It should have String as return type and should not have any parameters.", method.getName()); } } } String uiRepresentation = null; if (methods.isEmpty()) { LOGGER.debug("No Method with @UIRepresentation annotation in class {}", clazz.getName()); } else if (methods.size() > 1 ) { LOGGER.error("Multiple Methods with @UIRepresentation annotation in class {}. " + "@UIRepresentation annotation won't be respected for this class." + "Only one method should be annotated with @UIRepresentation", clazz.getName()); } else { try { uiRepresentation = (String) methods.get(0).invoke(instance); } catch (IllegalAccessException | InvocationTargetException e) { LOGGER.error("Error while retrieving @UIRepresentation value for {}", clazz.getName(), e); } } return uiRepresentation; }
UIRepresentationUtil { public static String uiRepresentationString(Object instance) { if(instance == null) { return ""; } Class clazz = instance.getClass(); List<Method> methods = new ArrayList<>(); for (Method method : clazz.getDeclaredMethods()) { if (method.isAnnotationPresent(UIRepresentation.class)) { if (ArrayUtils.isEmpty(method.getParameterTypes()) && String.class.equals(method.getReturnType())){ methods.add(method); } else { LOGGER.error("Method {} is not a valid method for @UIRepresentation annotation." + " It should have String as return type and should not have any parameters.", method.getName()); } } } String uiRepresentation = null; if (methods.isEmpty()) { LOGGER.debug("No Method with @UIRepresentation annotation in class {}", clazz.getName()); } else if (methods.size() > 1 ) { LOGGER.error("Multiple Methods with @UIRepresentation annotation in class {}. " + "@UIRepresentation annotation won't be respected for this class." + "Only one method should be annotated with @UIRepresentation", clazz.getName()); } else { try { uiRepresentation = (String) methods.get(0).invoke(instance); } catch (IllegalAccessException | InvocationTargetException e) { LOGGER.error("Error while retrieving @UIRepresentation value for {}", clazz.getName(), e); } } return uiRepresentation; } }
UIRepresentationUtil { public static String uiRepresentationString(Object instance) { if(instance == null) { return ""; } Class clazz = instance.getClass(); List<Method> methods = new ArrayList<>(); for (Method method : clazz.getDeclaredMethods()) { if (method.isAnnotationPresent(UIRepresentation.class)) { if (ArrayUtils.isEmpty(method.getParameterTypes()) && String.class.equals(method.getReturnType())){ methods.add(method); } else { LOGGER.error("Method {} is not a valid method for @UIRepresentation annotation." + " It should have String as return type and should not have any parameters.", method.getName()); } } } String uiRepresentation = null; if (methods.isEmpty()) { LOGGER.debug("No Method with @UIRepresentation annotation in class {}", clazz.getName()); } else if (methods.size() > 1 ) { LOGGER.error("Multiple Methods with @UIRepresentation annotation in class {}. " + "@UIRepresentation annotation won't be respected for this class." + "Only one method should be annotated with @UIRepresentation", clazz.getName()); } else { try { uiRepresentation = (String) methods.get(0).invoke(instance); } catch (IllegalAccessException | InvocationTargetException e) { LOGGER.error("Error while retrieving @UIRepresentation value for {}", clazz.getName(), e); } } return uiRepresentation; } private  UIRepresentationUtil(); }
UIRepresentationUtil { public static String uiRepresentationString(Object instance) { if(instance == null) { return ""; } Class clazz = instance.getClass(); List<Method> methods = new ArrayList<>(); for (Method method : clazz.getDeclaredMethods()) { if (method.isAnnotationPresent(UIRepresentation.class)) { if (ArrayUtils.isEmpty(method.getParameterTypes()) && String.class.equals(method.getReturnType())){ methods.add(method); } else { LOGGER.error("Method {} is not a valid method for @UIRepresentation annotation." + " It should have String as return type and should not have any parameters.", method.getName()); } } } String uiRepresentation = null; if (methods.isEmpty()) { LOGGER.debug("No Method with @UIRepresentation annotation in class {}", clazz.getName()); } else if (methods.size() > 1 ) { LOGGER.error("Multiple Methods with @UIRepresentation annotation in class {}. " + "@UIRepresentation annotation won't be respected for this class." + "Only one method should be annotated with @UIRepresentation", clazz.getName()); } else { try { uiRepresentation = (String) methods.get(0).invoke(instance); } catch (IllegalAccessException | InvocationTargetException e) { LOGGER.error("Error while retrieving @UIRepresentation value for {}", clazz.getName(), e); } } return uiRepresentation; } private  UIRepresentationUtil(); static String uiRepresentationString(Object instance); }
UIRepresentationUtil { public static String uiRepresentationString(Object instance) { if(instance == null) { return ""; } Class clazz = instance.getClass(); List<Method> methods = new ArrayList<>(); for (Method method : clazz.getDeclaredMethods()) { if (method.isAnnotationPresent(UIRepresentation.class)) { if (ArrayUtils.isEmpty(method.getParameterTypes()) && String.class.equals(method.getReturnType())){ methods.add(method); } else { LOGGER.error("Method {} is not a valid method for @UIRepresentation annotation." + " It should have String as return type and should not have any parameters.", method.getName()); } } } String uiRepresentation = null; if (methods.isEmpty()) { LOGGER.debug("No Method with @UIRepresentation annotation in class {}", clazz.getName()); } else if (methods.size() > 1 ) { LOGGER.error("Multiple Methods with @UIRepresentation annotation in class {}. " + "@UIRepresentation annotation won't be respected for this class." + "Only one method should be annotated with @UIRepresentation", clazz.getName()); } else { try { uiRepresentation = (String) methods.get(0).invoke(instance); } catch (IllegalAccessException | InvocationTargetException e) { LOGGER.error("Error while retrieving @UIRepresentation value for {}", clazz.getName(), e); } } return uiRepresentation; } private  UIRepresentationUtil(); static String uiRepresentationString(Object instance);  }