@Test public void shouldReturnNullIfNoClassAnnotated() throws NoSuchMethodException { final String value = ContainerObjectUtil.getTopCubeAttribute(EmptyClassAnnotation.class, "value", Cube.class, ""); assertThat(value, is(nullValue())); }
public static <T> T getTopCubeAttribute(final Class<?> source, final String nameField, final Class<? extends Annotation> annotationClass, final T defaultValue) { return AccessController.doPrivileged((PrivilegedAction<T>) () -> { Method field = null; try { field = annotationClass.getMethod(nameField); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(e); } Class<?> nextSource = source; boolean foundAnnotation = false; while (nextSource != Object.class) { if (nextSource.isAnnotationPresent(annotationClass)) { foundAnnotation = true; final Annotation annotation = nextSource.getAnnotation(annotationClass); final T value = getValue(annotation, field); if (defaultValue.getClass().isArray()) { if (defaultValue.getClass().getComponentType().isPrimitive()) { if (!Arrays.equals((int[]) value, (int[]) defaultValue)) { return value; } } else { if (!Arrays.equals((Object[]) value, (Object[]) defaultValue)) { return value; } } } else { if (!value.equals(defaultValue)) { return value; } } } nextSource = nextSource.getSuperclass(); } return foundAnnotation ? defaultValue : null; }); }
ContainerObjectUtil { public static <T> T getTopCubeAttribute(final Class<?> source, final String nameField, final Class<? extends Annotation> annotationClass, final T defaultValue) { return AccessController.doPrivileged((PrivilegedAction<T>) () -> { Method field = null; try { field = annotationClass.getMethod(nameField); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(e); } Class<?> nextSource = source; boolean foundAnnotation = false; while (nextSource != Object.class) { if (nextSource.isAnnotationPresent(annotationClass)) { foundAnnotation = true; final Annotation annotation = nextSource.getAnnotation(annotationClass); final T value = getValue(annotation, field); if (defaultValue.getClass().isArray()) { if (defaultValue.getClass().getComponentType().isPrimitive()) { if (!Arrays.equals((int[]) value, (int[]) defaultValue)) { return value; } } else { if (!Arrays.equals((Object[]) value, (Object[]) defaultValue)) { return value; } } } else { if (!value.equals(defaultValue)) { return value; } } } nextSource = nextSource.getSuperclass(); } return foundAnnotation ? defaultValue : null; }); } }
ContainerObjectUtil { public static <T> T getTopCubeAttribute(final Class<?> source, final String nameField, final Class<? extends Annotation> annotationClass, final T defaultValue) { return AccessController.doPrivileged((PrivilegedAction<T>) () -> { Method field = null; try { field = annotationClass.getMethod(nameField); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(e); } Class<?> nextSource = source; boolean foundAnnotation = false; while (nextSource != Object.class) { if (nextSource.isAnnotationPresent(annotationClass)) { foundAnnotation = true; final Annotation annotation = nextSource.getAnnotation(annotationClass); final T value = getValue(annotation, field); if (defaultValue.getClass().isArray()) { if (defaultValue.getClass().getComponentType().isPrimitive()) { if (!Arrays.equals((int[]) value, (int[]) defaultValue)) { return value; } } else { if (!Arrays.equals((Object[]) value, (Object[]) defaultValue)) { return value; } } } else { if (!value.equals(defaultValue)) { return value; } } } nextSource = nextSource.getSuperclass(); } return foundAnnotation ? defaultValue : null; }); } private  ContainerObjectUtil(); }
ContainerObjectUtil { public static <T> T getTopCubeAttribute(final Class<?> source, final String nameField, final Class<? extends Annotation> annotationClass, final T defaultValue) { return AccessController.doPrivileged((PrivilegedAction<T>) () -> { Method field = null; try { field = annotationClass.getMethod(nameField); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(e); } Class<?> nextSource = source; boolean foundAnnotation = false; while (nextSource != Object.class) { if (nextSource.isAnnotationPresent(annotationClass)) { foundAnnotation = true; final Annotation annotation = nextSource.getAnnotation(annotationClass); final T value = getValue(annotation, field); if (defaultValue.getClass().isArray()) { if (defaultValue.getClass().getComponentType().isPrimitive()) { if (!Arrays.equals((int[]) value, (int[]) defaultValue)) { return value; } } else { if (!Arrays.equals((Object[]) value, (Object[]) defaultValue)) { return value; } } } else { if (!value.equals(defaultValue)) { return value; } } } nextSource = nextSource.getSuperclass(); } return foundAnnotation ? defaultValue : null; }); } private  ContainerObjectUtil(); static List<T> getAllAnnotations(final Class<?> source,
        final Class<T> annotationClass); static T getTopCubeAttribute(final Class<?> source, final String nameField,
        final Class<? extends Annotation> annotationClass, final T defaultValue); }
ContainerObjectUtil { public static <T> T getTopCubeAttribute(final Class<?> source, final String nameField, final Class<? extends Annotation> annotationClass, final T defaultValue) { return AccessController.doPrivileged((PrivilegedAction<T>) () -> { Method field = null; try { field = annotationClass.getMethod(nameField); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(e); } Class<?> nextSource = source; boolean foundAnnotation = false; while (nextSource != Object.class) { if (nextSource.isAnnotationPresent(annotationClass)) { foundAnnotation = true; final Annotation annotation = nextSource.getAnnotation(annotationClass); final T value = getValue(annotation, field); if (defaultValue.getClass().isArray()) { if (defaultValue.getClass().getComponentType().isPrimitive()) { if (!Arrays.equals((int[]) value, (int[]) defaultValue)) { return value; } } else { if (!Arrays.equals((Object[]) value, (Object[]) defaultValue)) { return value; } } } else { if (!value.equals(defaultValue)) { return value; } } } nextSource = nextSource.getSuperclass(); } return foundAnnotation ? defaultValue : null; }); } private  ContainerObjectUtil(); static List<T> getAllAnnotations(final Class<?> source,
        final Class<T> annotationClass); static T getTopCubeAttribute(final Class<?> source, final String nameField,
        final Class<? extends Annotation> annotationClass, final T defaultValue);  }