/** * Convenience method that looks for a specific method on the subclass handler for the given * type * * @param type user-friendly type name * @param methodName * @param argumentTypes * @return the indicated method if it exists, null otherwise */ protected Method findSubclassHandlerMethod(String type, String methodName, Class<?>... argumentTypes) { DelegatingSubclassHandler<T, ? extends T> handler = getSubclassHandler(type); if (handler == null) return null; try { Method method = handler.getClass().getMethod(methodName, argumentTypes); return method; } catch (Exception e) { return null; } }
/** * @param clazz * @return a subclass handler if any is suitable for the given class, or this resource itself if * no subclass handler works */ protected DelegatingResourceHandler<? extends T> getResourceHandler(Class<? extends T> clazz) { if (!hasTypesDefined()) return this; DelegatingResourceHandler<? extends T> handler = getSubclassHandler(clazz); if (handler != null) return handler; return this; }
/** * @param type user-friendly type name * @return the actual java class for this type */ protected Class<? extends T> getActualSubclass(String type) { DelegatingSubclassHandler<T, ? extends T> handler = getSubclassHandler(type); if (handler != null) return handler.getSubclassHandled(); // otherwise we need to return our own declared class return ReflectionUtil.getParameterizedTypeFromInterface(getClass(), DelegatingResourceHandler.class, 0); }
/** * @param type user-friendly type name * @return a subclass handler if any is suitable for type, or this resource itself if it is * suitable */ protected DelegatingResourceHandler<? extends T> getResourceHandler(String type) { if (type == null || !hasTypesDefined()) return this; DelegatingSubclassHandler<T, ? extends T> handler = getSubclassHandler(type); if (handler != null) return handler; if (getResourceName().endsWith(type)) return this; throw new IllegalArgumentException("type=" + type + " is not handled by this resource (" + getClass() + ") or any subclass"); }
/** * If this resources supports subclasses, this method gets the user-friendly type name for the * given subclass * * @param subclass * @return */ protected String getTypeName(Class<? extends T> subclass) { if (hasTypesDefined()) { DelegatingSubclassHandler<T, ? extends T> handler = getSubclassHandler(subclass); if (handler != null) return handler.getTypeName(); if (newDelegate().getClass().equals(subclass)) { String resourceName = getResourceName(); int lastSlash = resourceName.lastIndexOf("/"); resourceName = resourceName.substring(lastSlash + 1); return resourceName; } } return null; }
return null; try { DelegatingSubclassHandler<T, ? extends T> handler = getSubclassHandler(type); return method.invoke(handler, arguments);