refined use of generics

This commit is contained in:
Juergen Hoeller 2010-03-29 23:08:32 +00:00
parent fba8bcc7dc
commit 8c8eca7e05
6 changed files with 21 additions and 34 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2006 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.springframework.web.servlet.view.jasperreports;
import net.sf.jasperreports.engine.JRExporter; import net.sf.jasperreports.engine.JRExporter;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
/** /**
* Configurable JasperReports View, allowing to specify the JasperReports exporter * Configurable JasperReports View, allowing to specify the JasperReports exporter
@ -33,7 +34,7 @@ import org.springframework.beans.BeanUtils;
*/ */
public class ConfigurableJasperReportsView extends AbstractJasperReportsSingleFormatView { public class ConfigurableJasperReportsView extends AbstractJasperReportsSingleFormatView {
private Class exporterClass; private Class<? extends JRExporter> exporterClass;
private boolean useWriter = true; private boolean useWriter = true;
@ -43,11 +44,8 @@ public class ConfigurableJasperReportsView extends AbstractJasperReportsSingleFo
* {@link IllegalArgumentException} if the <code>Class</code> doesn't implement * {@link IllegalArgumentException} if the <code>Class</code> doesn't implement
* {@link JRExporter}. Required setting, as it does not have a default. * {@link JRExporter}. Required setting, as it does not have a default.
*/ */
public void setExporterClass(Class exporterClass) { public void setExporterClass(Class<? extends JRExporter> exporterClass) {
if (!(JRExporter.class.isAssignableFrom(exporterClass))) { Assert.isAssignable(JRExporter.class, exporterClass);
throw new IllegalArgumentException(
"Exporter class [" + exporterClass.getName() + "] does not implement JRExporter");
}
this.exporterClass = exporterClass; this.exporterClass = exporterClass;
} }
@ -78,7 +76,7 @@ public class ConfigurableJasperReportsView extends AbstractJasperReportsSingleFo
*/ */
@Override @Override
protected JRExporter createExporter() { protected JRExporter createExporter() {
return (JRExporter) BeanUtils.instantiateClass(this.exporterClass); return BeanUtils.instantiateClass(this.exporterClass);
} }
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -82,7 +82,7 @@ public class JasperReportsMultiFormatView extends AbstractJasperReportsView {
* Stores the format mappings, with the format discriminator * Stores the format mappings, with the format discriminator
* as key and the corresponding view class as value. * as key and the corresponding view class as value.
*/ */
private Map<String, Class> formatMappings; private Map<String, Class<? extends AbstractJasperReportsView>> formatMappings;
/** /**
* Stores the mappings of mapping keys to Content-Disposition header values. * Stores the mappings of mapping keys to Content-Disposition header values.
@ -95,7 +95,7 @@ public class JasperReportsMultiFormatView extends AbstractJasperReportsView {
* with a default set of mappings. * with a default set of mappings.
*/ */
public JasperReportsMultiFormatView() { public JasperReportsMultiFormatView() {
this.formatMappings = new HashMap<String, Class>(4); this.formatMappings = new HashMap<String, Class<? extends AbstractJasperReportsView>>(4);
this.formatMappings.put("csv", JasperReportsCsvView.class); this.formatMappings.put("csv", JasperReportsCsvView.class);
this.formatMappings.put("html", JasperReportsHtmlView.class); this.formatMappings.put("html", JasperReportsHtmlView.class);
this.formatMappings.put("pdf", JasperReportsPdfView.class); this.formatMappings.put("pdf", JasperReportsPdfView.class);
@ -120,7 +120,7 @@ public class JasperReportsMultiFormatView extends AbstractJasperReportsView {
* <li><code>xls</code> - <code>JasperReportsXlsView</code></li> * <li><code>xls</code> - <code>JasperReportsXlsView</code></li>
* </ul> * </ul>
*/ */
public void setFormatMappings(Map<String, Class> formatMappings) { public void setFormatMappings(Map<String, Class<? extends AbstractJasperReportsView>> formatMappings) {
if (CollectionUtils.isEmpty(formatMappings)) { if (CollectionUtils.isEmpty(formatMappings)) {
throw new IllegalArgumentException("'formatMappings' must not be empty"); throw new IllegalArgumentException("'formatMappings' must not be empty");
} }
@ -173,7 +173,7 @@ public class JasperReportsMultiFormatView extends AbstractJasperReportsView {
logger.debug("Rendering report using format mapping key [" + format + "]"); logger.debug("Rendering report using format mapping key [" + format + "]");
} }
Class viewClass = this.formatMappings.get(format); Class<? extends AbstractJasperReportsView> viewClass = this.formatMappings.get(format);
if (viewClass == null) { if (viewClass == null) {
throw new IllegalArgumentException("Format discriminator [" + format + "] is not a configured mapping"); throw new IllegalArgumentException("Format discriminator [" + format + "] is not a configured mapping");
} }
@ -182,7 +182,7 @@ public class JasperReportsMultiFormatView extends AbstractJasperReportsView {
logger.debug("Rendering report using view class [" + viewClass.getName() + "]"); logger.debug("Rendering report using view class [" + viewClass.getName() + "]");
} }
AbstractJasperReportsView view = (AbstractJasperReportsView) BeanUtils.instantiateClass(viewClass); AbstractJasperReportsView view = BeanUtils.instantiateClass(viewClass);
// Can skip most initialization since all relevant URL processing // Can skip most initialization since all relevant URL processing
// has been done - just need to convert parameters on the sub view. // has been done - just need to convert parameters on the sub view.
view.setExporterParameters(getExporterParameters()); view.setExporterParameters(getExporterParameters());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,8 +21,6 @@ import java.util.Map;
import java.util.Properties; import java.util.Properties;
import javax.sql.DataSource; import javax.sql.DataSource;
import net.sf.jasperreports.engine.design.JRCompiler;
import org.springframework.web.servlet.view.AbstractUrlBasedView; import org.springframework.web.servlet.view.AbstractUrlBasedView;
import org.springframework.web.servlet.view.UrlBasedViewResolver; import org.springframework.web.servlet.view.UrlBasedViewResolver;
@ -48,8 +46,6 @@ public class JasperReportsViewResolver extends UrlBasedViewResolver {
private DataSource jdbcDataSource; private DataSource jdbcDataSource;
private JRCompiler reportCompiler;
/** /**
* Requires the view class to be a subclass of {@link AbstractJasperReportsView}. * Requires the view class to be a subclass of {@link AbstractJasperReportsView}.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,16 +23,6 @@ import org.springframework.context.support.StaticApplicationContext;
*/ */
public abstract class AbstractConfigurableJasperReportsViewTests extends AbstractJasperReportsViewTests { public abstract class AbstractConfigurableJasperReportsViewTests extends AbstractJasperReportsViewTests {
public void testSetInvalidExporterClass() throws Exception {
try {
new ConfigurableJasperReportsView().setExporterClass(String.class);
fail("Should not be able to set invalid view class.");
}
catch (IllegalArgumentException ex) {
// success
}
}
public void testNoConfiguredExporter() throws Exception { public void testNoConfiguredExporter() throws Exception {
ConfigurableJasperReportsView view = new ConfigurableJasperReportsView(); ConfigurableJasperReportsView view = new ConfigurableJasperReportsView();
view.setUrl(COMPILED_REPORT); view.setUrl(COMPILED_REPORT);
@ -44,4 +34,5 @@ public abstract class AbstractConfigurableJasperReportsViewTests extends Abstrac
// success // success
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -78,7 +78,8 @@ public class JasperReportsMultiFormatViewTests extends AbstractJasperReportsView
JasperReportsMultiFormatView view = (JasperReportsMultiFormatView) getView(UNCOMPILED_REPORT); JasperReportsMultiFormatView view = (JasperReportsMultiFormatView) getView(UNCOMPILED_REPORT);
Map<String, Class> mappings = new HashMap<String, Class>(); Map<String, Class<? extends AbstractJasperReportsView>> mappings =
new HashMap<String, Class<? extends AbstractJasperReportsView>>();
mappings.put("test", ExporterParameterTestView.class); mappings.put("test", ExporterParameterTestView.class);
Map<String, String> exporterParameters = new HashMap<String, String>(); Map<String, String> exporterParameters = new HashMap<String, String>();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,7 +28,8 @@ public class JasperReportsMultiFormatViewWithCustomMappingsTests extends JasperR
protected AbstractJasperReportsView getViewImplementation() { protected AbstractJasperReportsView getViewImplementation() {
JasperReportsMultiFormatView view = new JasperReportsMultiFormatView(); JasperReportsMultiFormatView view = new JasperReportsMultiFormatView();
view.setFormatKey("fmt"); view.setFormatKey("fmt");
Map<String, Class> mappings = new HashMap<String, Class>(); Map<String, Class<? extends AbstractJasperReportsView>> mappings =
new HashMap<String, Class<? extends AbstractJasperReportsView>>();
mappings.put("csv", JasperReportsCsvView.class); mappings.put("csv", JasperReportsCsvView.class);
mappings.put("comma-separated", JasperReportsCsvView.class); mappings.put("comma-separated", JasperReportsCsvView.class);
mappings.put("html", JasperReportsHtmlView.class); mappings.put("html", JasperReportsHtmlView.class);