Example usage for org.springframework.util Assert isAssignable

List of usage examples for org.springframework.util Assert isAssignable

Introduction

In this page you can find the example usage for org.springframework.util Assert isAssignable.

Prototype

public static void isAssignable(Class<?> superType, @Nullable Class<?> subType,
        Supplier<String> messageSupplier) 

Source Link

Document

Assert that superType.isAssignableFrom(subType) is true .

Usage

From source file:org.spring.guice.annotation.GuiceModuleRegistrar.java

private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) {

    List<TypeFilter> typeFilters = new ArrayList<TypeFilter>();
    FilterType filterType = filterAttributes.getEnum("type");

    for (Class<?> filterClass : filterAttributes.getClassArray("value")) {
        switch (filterType) {
        case ANNOTATION:
            Assert.isAssignable(Annotation.class, filterClass,
                    "An error occured when processing a @ComponentScan " + "ANNOTATION type filter: ");
            @SuppressWarnings("unchecked")
            Class<Annotation> annoClass = (Class<Annotation>) filterClass;
            typeFilters.add(new AnnotationTypeFilter(annoClass));
            break;
        case ASSIGNABLE_TYPE:
            typeFilters.add(new AssignableTypeFilter(filterClass));
            break;
        case CUSTOM:
            Assert.isAssignable(TypeFilter.class, filterClass,
                    "An error occured when processing a @ComponentScan " + "CUSTOM type filter: ");
            typeFilters.add(BeanUtils.instantiateClass(filterClass, TypeFilter.class));
            break;
        default:/*from   w ww  . ja  v a  2  s .c  o m*/
            throw new IllegalArgumentException("Unknown filter type " + filterType);
        }
    }

    for (String expression : getPatterns(filterAttributes)) {

        String rawName = filterType.toString();

        if ("REGEX".equals(rawName)) {
            typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression)));
        } else if ("ASPECTJ".equals(rawName)) {
            typeFilters.add(new AspectJTypeFilter(expression, this.resourceLoader.getClassLoader()));
        } else {
            throw new IllegalArgumentException("Unknown filter type " + filterType);
        }
    }

    return typeFilters;
}

From source file:org.codehaus.grepo.core.repository.GenericRepositoryFactoryBean.java

/**
 * Validates the {@link #proxyInterface} property.
 *//*  w w  w .j  av  a  2 s .  co m*/
protected void validateProxyInterface() {
    Assert.notNull(proxyInterface, "proxyInterface must not be null");
    Assert.isTrue(proxyInterface.isInterface(), "proxyInterface is not an interface");
    Assert.isAssignable(getRequiredGenericRepositoryType(), proxyInterface,
            "proxyInterface is not of required type '" + getRequiredGenericRepositoryType().getName() + "'");
}

From source file:com.vaadin.spring.navigator.SpringViewProvider.java

private boolean isViewBeanNameValidForCurrentUI(String beanName) {
    try {/*from  w w w  . j  ava  2  s . c o  m*/
        final Class<?> type = applicationContext.getType(beanName);

        Assert.isAssignable(View.class, type, "bean did not implement View interface");

        final UI currentUI = UI.getCurrent();
        final SpringView annotation = applicationContext.findAnnotationOnBean(beanName, SpringView.class);

        Assert.notNull(annotation, "class did not have a SpringView annotation");

        if (annotation.ui().length == 0) {
            LOGGER.trace("View class [{}] with view name [{}] is available for all UI subclasses",
                    type.getCanonicalName(), getViewNameFromAnnotation(type, annotation));
        } else {
            Class<? extends UI> validUI = getValidUIClass(currentUI, annotation.ui());
            if (validUI != null) {
                LOGGER.trace("View class [%s] with view name [{}] is available for UI subclass [{}]",
                        type.getCanonicalName(), getViewNameFromAnnotation(type, annotation),
                        validUI.getCanonicalName());
            } else {
                return false;
            }
        }

        return true;
    } catch (NoSuchBeanDefinitionException ex) {
        return false;
    }
}

From source file:org.opennms.ng.dao.support.DefaultRrdDao.java

/** {@inheritDoc} */
@Override//from   w  w w.  jav  a  2s.  com
public Double getLastFetchValue(OnmsAttribute attribute, int interval)
        throws DataAccessResourceFailureException {
    Assert.notNull(attribute, "attribute argument must not be null");
    Assert.isTrue(interval > 0, "interval argument must be greater than zero");
    Assert.isAssignable(attribute.getClass(), RrdGraphAttribute.class,
            "attribute argument must be assignable to RrdGraphAttribute");

    RrdGraphAttribute rrdAttribute = (RrdGraphAttribute) attribute;

    File rrdFile = new File(m_rrdBaseDirectory, rrdAttribute.getRrdRelativePath());
    try {
        return m_rrdStrategy.fetchLastValue(rrdFile.getAbsolutePath(), attribute.getName(), interval);
    } catch (Throwable e) {
        throw new DataAccessResourceFailureException(
                "Failure to fetch last value from file '" + rrdFile + "' with interval " + interval, e);
    }
}

From source file:org.opennms.ng.dao.support.DefaultRrdDao.java

/** {@inheritDoc} */
@Override/*from  w ww .j av a 2s  .c o m*/
public Double getLastFetchValue(OnmsAttribute attribute, int interval, int range)
        throws DataAccessResourceFailureException {
    Assert.notNull(attribute, "attribute argument must not be null");
    Assert.isTrue(interval > 0, "interval argument must be greater than zero");
    Assert.isTrue(range > 0, "range argument must be greater than zero");
    Assert.isAssignable(attribute.getClass(), RrdGraphAttribute.class,
            "attribute argument must be assignable to RrdGraphAttribute");

    RrdGraphAttribute rrdAttribute = (RrdGraphAttribute) attribute;

    File rrdFile = new File(m_rrdBaseDirectory, rrdAttribute.getRrdRelativePath());
    try {
        return m_rrdStrategy.fetchLastValueInRange(rrdFile.getAbsolutePath(), attribute.getName(), interval,
                range);
    } catch (Throwable e) {
        throw new DataAccessResourceFailureException("Failure to fetch last value from file '" + rrdFile
                + "' with interval " + interval + " and range " + range, e);
    }
}

From source file:de.extra.client.core.plugin.dummies.DummyQueryDataResponceOutputPlugin.java

/**
 * Liefert QueryArguments als List//from   w w w.ja v a2 s . c o m
 * 
 * @return
 */
private List<String> getQueryArguments(final RequestTransport requestXml) {
    final RequestTransportBody transportBody = requestXml.getTransportBody();
    Assert.notNull(transportBody, "ResponseTransportBody is null");
    final DataType data = transportBody.getData();
    Assert.notNull(data, "TransportData is null");
    final ElementSequenceType elementSequence = data.getElementSequence();
    Assert.notNull(elementSequence, "TransportData.elementSequence is null");
    final List<Object> any = elementSequence.getAny();
    Assert.notNull(any, "ElementSequence is empty is null");
    Assert.isTrue(any.size() == 1, "ElementSequense beinhaltet mehr als ein Element");
    final Object dataRequestObject = any.get(0);
    Assert.isAssignable(DataRequest.class, dataRequestObject.getClass(),
            "Unexpectede ElementSequence entry" + dataRequestObject.getClass() + " Expected DataRequest.");
    final DataRequest dataRequest = (DataRequest) dataRequestObject;
    final DataRequestQuery query = dataRequest.getQuery();
    Assert.notNull(query, "Query is null");
    final List<DataRequestArgument> argument = query.getArgument();
    Assert.notNull(argument, "DataRequestArgument List is null");
    // Assert.isTrue(argument.size() == 1,
    // "DataRequestArgument List beinhaltet mehr als ein Element");
    final DataRequestArgument dataRequestArgument = argument.get(0);
    Assert.notNull(dataRequestArgument, "DataRequestArgument is null");
    final List<JAXBElement<?>> dataRequestArgumentContent = dataRequestArgument.getContent();
    Assert.notNull(dataRequestArgumentContent, "DataRequestArgument.content is null");
    Assert.isTrue(dataRequestArgumentContent.size() == 1,
            "DataRequestArgument.content  beinhaltet mehr als ein Element");
    final JAXBElement<?> jaxbElement = dataRequestArgumentContent.get(0);

    // TODO MAXRESP (06.11.12)
    boolean assignableOperand = false;
    final Object operandSetObject = jaxbElement.getValue();
    final List<String> queryArgumentList = new ArrayList<String>();
    // OperandSet oder Operand?
    if (operandSetObject.getClass().isAssignableFrom(OperandSet.class)) {
        assignableOperand = true;
        final OperandSet operandSet = (OperandSet) operandSetObject;
        final List<Operand> operandEQ = operandSet.getEQ();
        Assert.notNull(operandEQ, "operandEQ is null");
        Assert.notEmpty(operandEQ, "operandEQ is empty");
        for (final Operand operand : operandEQ) {
            final String operandValue = operand.getValue();
            queryArgumentList.add(operandValue);
        }
    } else if (operandSetObject.getClass().isAssignableFrom(Operand.class)) {
        // Fr Fachverfahren 'Sterbedaten Abgleich'!
        // Als OperandValue wird die maximale ResponseId erwartet (z.B. 31)
        // Als Dummy Server Antwort werden drei aufeinanderfolgende ID'S
        // (z.B. 32,33,34) generiert
        assignableOperand = true;
        final Operand operand = (Operand) operandSetObject;
        final String operandValue = operand.getValue();
        try {
            final long operandValueAsLong = Long.parseLong(operandValue);
            for (long respId = operandValueAsLong + 1; respId <= operandValueAsLong + 3; respId++) {
                queryArgumentList.add(String.valueOf(respId));
            }
        } catch (final NumberFormatException ex) {
            // Anderes Fachverfahren?
            queryArgumentList.add(operandValue);
        }
    }

    Assert.isTrue(assignableOperand, "Unexpected dataRequestArgumentContent entry"
            + dataRequestObject.getClass() + " Expected OperandSet or Operand.");
    return queryArgumentList;
}

From source file:org.jasig.springframework.web.portlet.context.PortletContextLoader.java

/**
 * Return the {@link ApplicationContextInitializer} implementation classes to use
 * if any have been specified by {@link #CONTEXT_INITIALIZER_CLASSES_PARAM}.
 * @param portletContext current portlet context
 * @see #CONTEXT_INITIALIZER_CLASSES_PARAM
 *///from w w  w . ja  va  2  s.  c o m
@SuppressWarnings("unchecked")
protected List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> determineContextInitializerClasses(
        PortletContext portletContext) {
    String classNames = portletContext.getInitParameter(CONTEXT_INITIALIZER_CLASSES_PARAM);
    List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> classes = new ArrayList<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>>();
    if (classNames != null) {
        for (String className : StringUtils.tokenizeToStringArray(classNames, ",")) {
            try {
                Class<?> clazz = ClassUtils.forName(className, ClassUtils.getDefaultClassLoader());
                Assert.isAssignable(ApplicationContextInitializer.class, clazz,
                        "class [" + className + "] must implement ApplicationContextInitializer");
                classes.add((Class<ApplicationContextInitializer<ConfigurableApplicationContext>>) clazz);
            } catch (ClassNotFoundException ex) {
                throw new ApplicationContextException(
                        "Failed to load context initializer class [" + className + "]", ex);
            }
        }
    }
    return classes;
}

From source file:org.jasig.springframework.web.portlet.context.PortletContextLoader.java

/**
 * Customize the {@link ConfigurablePortletApplicationContext} created by this
 * PortletContextLoader after config locations have been supplied to the context
 * but before the context is <em>refreshed</em>.
 * <p>The default implementation {@linkplain #determineContextInitializerClasses(PortletContext)
 * determines} what (if any) context initializer classes have been specified through
 * {@linkplain #CONTEXT_INITIALIZER_CLASSES_PARAM context init parameters} and
 * {@linkplain ApplicationContextInitializer#initialize invokes each} with the
 * given web application context.//from ww  w .  ja  va  2  s  .co  m
 * <p>Any {@code ApplicationContextInitializers} implementing
 * {@link org.springframework.core.Ordered Ordered} or marked with @{@link
 * org.springframework.core.annotation.Order Order} will be sorted appropriately.
 * @param portletContext the current portlet context
 * @param applicationContext the newly created application context
 * @see #createPortletApplicationContext(PortletContext)
 * @see #CONTEXT_INITIALIZER_CLASSES_PARAM
 * @see ApplicationContextInitializer#initialize(ConfigurableApplicationContext)
 */
protected void customizeContext(PortletContext portletContext,
        ConfigurablePortletApplicationContext applicationContext) {
    List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> initializerClasses = determineContextInitializerClasses(
            portletContext);

    if (initializerClasses.size() == 0) {
        // no ApplicationContextInitializers have been declared -> nothing to do
        return;
    }

    ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerInstances = new ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>>();

    for (Class<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerClass : initializerClasses) {
        Class<?> contextClass = applicationContext.getClass();
        Class<?> initializerContextClass = GenericTypeResolver.resolveTypeArgument(initializerClass,
                ApplicationContextInitializer.class);
        Assert.isAssignable(initializerContextClass, contextClass,
                String.format(
                        "Could not add context initializer [%s] as its generic parameter [%s] "
                                + "is not assignable from the type of application context used by this "
                                + "context loader [%s]",
                        initializerClass.getName(), initializerContextClass, contextClass));
        initializerInstances.add(BeanUtils.instantiateClass(initializerClass));
    }

    //TODO remove cast when ContribXmlPortletApplicationContext is merged into super classes
    ((ConfigurablePortletEnvironment) applicationContext.getEnvironment())
            .initPropertySources(this.servletContext, portletContext, null);

    Collections.sort(initializerInstances, new AnnotationAwareOrderComparator());
    for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : initializerInstances) {
        initializer.initialize(applicationContext);
    }
}

From source file:com.dhcc.framework.web.context.DhccContextLoader.java

/**
 * Return the {@link ApplicationContextInitializer} implementation classes to use
 * if any have been specified by {@link #CONTEXT_INITIALIZER_CLASSES_PARAM}.
 * @param servletContext current servlet context
 * @see #CONTEXT_INITIALIZER_CLASSES_PARAM
 *///from   ww  w  .ja v  a2s. c o  m
@SuppressWarnings("unchecked")
protected List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> determineContextInitializerClasses(
        ServletContext servletContext) {
    String classNames = servletContext.getInitParameter(CONTEXT_INITIALIZER_CLASSES_PARAM);
    List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> classes = new ArrayList<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>>();
    if (classNames != null) {
        for (String className : StringUtils.tokenizeToStringArray(classNames, ",")) {
            try {
                Class<?> clazz = ClassUtils.forName(className, ClassUtils.getDefaultClassLoader());
                Assert.isAssignable(ApplicationContextInitializer.class, clazz,
                        "class [" + className + "] must implement ApplicationContextInitializer");
                classes.add((Class<ApplicationContextInitializer<ConfigurableApplicationContext>>) clazz);
            } catch (ClassNotFoundException ex) {
                throw new ApplicationContextException(
                        "Failed to load context initializer class [" + className + "]", ex);
            }
        }
    }
    return classes;
}