/** * Returns the adapter corresponding to a specific type. * * @param type * @return */ public Class<? extends IAdapter> getAdapter(Class<?> type) throws AdapterInitializationException { // match adapter if (!this.adapterMap.containsKey(type)) { for (Map.Entry<Class<?>, Class<? extends IAdapter>> adapterEntry : this.adapterMap.entrySet()) { if (!this.isWildcardAdapter(adapterEntry.getValue())) continue; // get definition AdapterDefinition definition = adapterEntry.getValue().getAnnotation(AdapterDefinition.class); // check input if (definition.input().isAssignableFrom(type)) return adapterEntry.getValue(); } } // get fallback adapter if (!type.isPrimitive() && !this.adapterMap.containsKey(type)) return ObjectAdapter.class; // verify if (!this.adapterMap.containsKey(type)) throw new AdapterInitializationException( "Could not find the appropriate adapter for type " + type.getName()); // return explicit adapter return this.adapterMap.get(type); }
/** * Verifies an adapter class. * * @param adapter * @throws AdapterInitializationException */ public void verifyTypeAdapter(Class<? extends ITypeAdapter> adapter) throws AdapterInitializationException { if (!adapter.isAnnotationPresent(TypeAdapterDefinition.class)) throw new AdapterInitializationException( "Cannot initialize adapter of type " + adapter.getName() + ": Missing @TypeAdapterDefinition annotation"); }
/** * Verifies an adapter class. * * @param adapter * @throws AdapterInitializationException */ public void verifyAdapter(Class<? extends IAdapter> adapter) throws AdapterInitializationException { Preconditions.checkNotNull(adapter, "adapter"); if (!adapter.isAnnotationPresent(AdapterDefinition.class)) throw new AdapterInitializationException( "Cannot initialize adapter of type " + adapter.getName() + ": Missing @AdapterDefinition annotation"); }