@Nonnull private <T> Class<T> parseAttributeAsType( @Nonnull Element element, @Nonnull String attributeName, @Nonnull Class<T> expectedType) { requireNonNull("expectedType", expectedType); final String attributeValue = requireNonNull("element", element) .getAttribute(requireNonNull("attributeName", attributeName)); final Class<?> type; try { type = _classLoader.loadClass(attributeValue); } catch (ClassNotFoundException e) { throw new IllegalArgumentException( "The value of <" + element.getTagName() + " " + attributeName + "=\"..\" ../> is no valid class.", e); } if (!expectedType.isAssignableFrom(type)) { throw new IllegalArgumentException( "The value of <" + element.getTagName() + " " + attributeName + "=\"..\" ../> is no type of '" + expectedType.getName() + "'."); } // noinspection unchecked return (Class<T>) type; }
@Nonnull private <T> T newInstanceOf(@Nonnull Class<T> type) { requireNonNull("type", type); final Constructor<T> constructor; try { constructor = type.getConstructor(); } catch (Exception e) { throw new IllegalArgumentException( "Could not find a default constructor for " + type.getName() + ".", e); } if (!constructor.isAccessible()) { try { constructor.setAccessible(true); } catch (Exception e) { throw new RuntimeException( "Could not access the default constructor for " + type.getName() + ".", e); } } try { return constructor.newInstance(); } catch (Exception e) { throw new RuntimeException("Could not create an instance of " + type.getName() + ".", e); } }
@Nonnull private <T> Class<T> findDefaultType( @Nonnull Class<?> from, @Nonnull Class<? extends Annotation> atAnnotationDefinedWith, @Nonnull Class<T> ofType, @Nonnull String tagName) { requireNonNull("from", from); requireNonNull("atAnnotationDefinedWith", atAnnotationDefinedWith); requireNonNull("ofType", ofType); requireNonNull("tagName", tagName); final Annotation annotation = from.getAnnotation(atAnnotationDefinedWith); if (annotation == null) { throw new IllegalArgumentException( "Neither is @" + from.getName() + " annotated with @" + atAnnotationDefinedWith.getName() + " nor is in '" + SERVICES_LOCATION + "' the tag <" + tagName + " .../> defined."); } final Method valueMethod; try { valueMethod = annotation.annotationType().getMethod("value"); } catch (NoSuchMethodException e) { throw new IllegalArgumentException( "@" + atAnnotationDefinedWith.getName() + " annotated at @" + from.getName() + " has no value() method.", e); } final Object value; try { value = valueMethod.invoke(annotation); } catch (Exception e) { throw new IllegalArgumentException( "Could not get from @" + atAnnotationDefinedWith.getName() + ".value() annotated at @" + from.getName() + " the value.", e); } if (!(value instanceof Class)) { throw new IllegalArgumentException( "The value of @" + atAnnotationDefinedWith.getName() + ".value() annotated at @" + from.getName() + " is not of type " + Class.class.getName() + "."); } final Class<?> type = (Class<?>) value; if (!ofType.isAssignableFrom(type)) { throw new IllegalStateException(type + " is not of expected type " + ofType.getName() + "."); } // noinspection unchecked return (Class<T>) type; }