private static Map<String, Object> attrs(final Annotation annotation) { Map<String, Object> result = new LinkedHashMap<>(); Class<? extends Annotation> annotationType = annotation.annotationType(); if (!IGNORE.contains(annotationType)) { Method[] attrs = annotation.annotationType().getDeclaredMethods(); for (Method attr : attrs) { Try.apply(() -> attr.invoke(annotation)) .onSuccess(value -> { if (value.getClass().isArray() && Annotation.class .isAssignableFrom(value.getClass().getComponentType())) { List<Map<String, Object>> array = new ArrayList<>(); for(int i = 0; i < Array.getLength(value); i ++) { array.add(attrs((Annotation) Array.get(value, i))); } result.put(attrName(annotation, attr), array.toArray()); } else { result.put(attrName(annotation, attr), value); } }); } } return result; }
@SuppressWarnings("rawtypes") private static Injector proxyInjector(final ClassLoader loader, final Map<Key, Object> registry) { return Reflection.newProxy(Injector.class, (proxy, method, args) -> { if (method.getName().equals("getInstance")) { Key key = (Key) args[0]; Object value = registry.get(key); if (value == null) { Object type = key.getAnnotation() != null ? key : key.getTypeLiteral(); IllegalStateException iex = new IllegalStateException("Not found: " + type); // Skip proxy and some useless lines: Try.apply(() -> { StackTraceElement[] stacktrace = iex.getStackTrace(); return Lists.newArrayList(stacktrace).subList(CLEAN_STACK, stacktrace.length); }).onSuccess(stacktrace -> iex .setStackTrace(stacktrace.toArray(new StackTraceElement[stacktrace.size()]))); throw iex; } return value; } throw new UnsupportedOperationException(method.toString()); }); }
/** * Parse value as {@link Duration}. If the value is already a number then it uses as seconds. * Otherwise, it parse expressions like: 8m, 1h, 365d, etc... * * @param maxAge Set the cache header max-age value in seconds. * @return This handler. */ public AssetHandler maxAge(final String maxAge) { Try.apply(() -> Long.parseLong(maxAge)) .recover(x -> ConfigFactory.empty() .withValue("v", ConfigValueFactory.fromAnyRef(maxAge)) .getDuration("v") .getSeconds()) .onSuccess(this::maxAge); return this; }
private static Map<String, Object> attrs(final Annotation annotation) { Map<String, Object> result = new LinkedHashMap<>(); Class<? extends Annotation> annotationType = annotation.annotationType(); if (!IGNORE.contains(annotationType)) { Method[] attrs = annotation.annotationType().getDeclaredMethods(); for (Method attr : attrs) { Try.apply(() -> attr.invoke(annotation)) .onSuccess(value -> { if (value.getClass().isArray() && Annotation.class .isAssignableFrom(value.getClass().getComponentType())) { List<Map<String, Object>> array = new ArrayList<>(); for(int i = 0; i < Array.getLength(value); i ++) { array.add(attrs((Annotation) Array.get(value, i))); } result.put(attrName(annotation, attr), array.toArray()); } else { result.put(attrName(annotation, attr), value); } }); } } return result; }
@SuppressWarnings("rawtypes") private static Injector proxyInjector(final ClassLoader loader, final Map<Key, Object> registry) { return Reflection.newProxy(Injector.class, (proxy, method, args) -> { if (method.getName().equals("getInstance")) { Key key = (Key) args[0]; Object value = registry.get(key); if (value == null) { Object type = key.getAnnotation() != null ? key : key.getTypeLiteral(); IllegalStateException iex = new IllegalStateException("Not found: " + type); // Skip proxy and some useless lines: Try.apply(() -> { StackTraceElement[] stacktrace = iex.getStackTrace(); return Lists.newArrayList(stacktrace).subList(CLEAN_STACK, stacktrace.length); }).onSuccess(stacktrace -> iex .setStackTrace(stacktrace.toArray(new StackTraceElement[stacktrace.size()]))); throw iex; } return value; } throw new UnsupportedOperationException(method.toString()); }); }
/** * Parse value as {@link Duration}. If the value is already a number then it uses as seconds. * Otherwise, it parse expressions like: 8m, 1h, 365d, etc... * * @param maxAge Set the cache header max-age value in seconds. * @return This handler. */ public AssetHandler maxAge(final String maxAge) { Try.apply(() -> Long.parseLong(maxAge)) .recover(x -> ConfigFactory.empty() .withValue("v", ConfigValueFactory.fromAnyRef(maxAge)) .getDuration("v") .getSeconds()) .onSuccess(this::maxAge); return this; }