Search in sources :

Example 1 with ClassOption

use of org.sosy_lab.common.configuration.ClassOption in project java-common-lib by sosy-lab.

the class ClassTypeConverter method convert.

@Override
public Object convert(String optionName, String value, TypeToken<?> type, Annotation secondaryOption, Path pSource, LogManager logger) throws InvalidConfigurationException {
    // null means "no prefix"
    @Var Iterable<String> packagePrefixes = Collections.<String>singleton(null);
    // get optional package prefix
    if (secondaryOption != null) {
        if (!(secondaryOption instanceof ClassOption)) {
            throw new UnsupportedOperationException("Options of type Class may not be annotated with " + secondaryOption);
        }
        packagePrefixes = from(packagePrefixes).append(((ClassOption) secondaryOption).packagePrefix());
    }
    // get class object
    @Var Class<?> cls = null;
    for (String prefix : packagePrefixes) {
        try {
            cls = Classes.forName(value, prefix);
        } catch (ClassNotFoundException ignored) {
        // Ignore, we try next prefix and throw below if none is found.
        }
    }
    if (cls == null) {
        throw new InvalidConfigurationException("Class " + value + " specified in option " + optionName + " not found");
    }
    Object result;
    if (type.getRawType().equals(Class.class)) {
        // get value of type parameter
        TypeToken<?> targetType = Classes.getSingleTypeArgument(type);
        // check type
        if (!targetType.isSupertypeOf(cls)) {
            throw new InvalidConfigurationException(String.format("Class %s specified in option %s is not an instance of %s", value, optionName, targetType));
        }
        result = cls;
        Classes.produceClassLoadingWarning(logger, cls, targetType.getRawType());
    } else {
        // This should be a factory interface for which we create a proxy implementation.
        try {
            result = Classes.createFactory(type, cls);
        } catch (UnsuitedClassException e) {
            throw new InvalidConfigurationException(String.format("Class %s specified in option %s is invalid (%s)", value, optionName, e.getMessage()));
        }
        Classes.produceClassLoadingWarning(logger, cls, type.getRawType());
    }
    return result;
}
Also used : Var(com.google.errorprone.annotations.Var) ClassOption(org.sosy_lab.common.configuration.ClassOption) UnsuitedClassException(org.sosy_lab.common.Classes.UnsuitedClassException) InvalidConfigurationException(org.sosy_lab.common.configuration.InvalidConfigurationException)

Aggregations

Var (com.google.errorprone.annotations.Var)1 UnsuitedClassException (org.sosy_lab.common.Classes.UnsuitedClassException)1 ClassOption (org.sosy_lab.common.configuration.ClassOption)1 InvalidConfigurationException (org.sosy_lab.common.configuration.InvalidConfigurationException)1