All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.stickycode.coercion.extension.ClassCoercion Maven / Gradle / Ivy

There is a newer version: 6.6
Show newest version
package net.stickycode.coercion.extension;

import net.stickycode.coercion.AbstractNoDefaultCoercion;
import net.stickycode.coercion.CoercionTarget;

public class ClassCoercion
    extends AbstractNoDefaultCoercion> {

  @Override
  public Class coerce(CoercionTarget type, String value) {
    try {
      Class loaded = type.getOwner().getClassLoader().loadClass(value);
      Class expected = type.getComponentCoercionTypes()[0].getType();
      if (expected.isAssignableFrom(loaded))
        return loaded;
      
      throw new ConfiguredClassIsNotOfTheCorrectTypeException(expected, loaded);
    }
    catch (ClassNotFoundException e) {
      throw new ConfiguredClassNotFoundException(e, value);
    }
  }

  @Override
  public boolean isApplicableTo(CoercionTarget target) {
    return Class.class.isAssignableFrom(target.getType());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy