WW-2537 Fix generics in all codebase

Starting with Java5 enhancements in S2. 
This patch applies required changes to S2 related to the "generified xwork"

XWork Java5 enhancements 
o added generics where possible
o use parameterizes Collection classes where possible
o converted for to for-each loops
o optimized imports
o updated tests
o aligned parameter types with Struts2
o fixed some minor bugs

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@670170 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Rainer Hermanns
2008-06-21 09:40:34 +00:00
parent 59d65a0aa7
commit 0a23cd4ec5
20 changed files with 95 additions and 54 deletions
@@ -44,7 +44,7 @@ public class ConfigTest extends StrutsTestCase {
ActionSupport.INPUT.equals(result));
}
protected Map assertFieldErrors(ActionSupport action) throws Exception {
protected Map<String, List<String>> assertFieldErrors(ActionSupport action) throws Exception {
assertTrue(action.hasFieldErrors());
return action.getFieldErrors();
}
@@ -191,7 +191,7 @@ public class ActionComponent extends ContextBean {
Map application = ctx.getApplication();
Dispatcher du = Dispatcher.getInstance();
Map extraContext = du.createContextMap(new RequestMap(req),
Map<String, Object> extraContext = du.createContextMap(new RequestMap(req),
newParams,
session,
application,
@@ -87,7 +87,7 @@ import com.opensymphony.xwork2.util.ValueStack;
"or partial depending on param tag nested)if they exists")
public class FieldError extends UIBean implements UnnamedParametric {
private List errorFieldNames = new ArrayList();
private List<String> errorFieldNames = new ArrayList<String>();
public FieldError(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
super(stack, request, response);
@@ -105,7 +105,7 @@ public class FieldError extends UIBean implements UnnamedParametric {
}
}
public List getFieldErrorFieldNames() {
public List<String> getFieldErrorFieldNames() {
return errorFieldNames;
}
}
@@ -39,12 +39,12 @@ import javax.servlet.http.HttpSession;
* Note, this will occur lazily - only when the entry set is asked for.
*
*/
public class SessionMap extends AbstractMap implements Serializable {
public class SessionMap<K, V> extends AbstractMap<K,V> implements Serializable {
private static final long serialVersionUID = 4678843241638046854L;
protected HttpSession session;
protected Set<Object> entries;
protected Set<Map.Entry<K,V>> entries;
protected HttpServletRequest request;
@@ -101,23 +101,23 @@ public class SessionMap extends AbstractMap implements Serializable {
*
* @return a Set of attributes from the http session.
*/
public Set entrySet() {
public Set<java.util.Map.Entry<K,V>> entrySet() {
if (session == null) {
return Collections.EMPTY_SET;
return Collections.emptySet();
}
synchronized (session) {
if (entries == null) {
entries = new HashSet<Object>();
entries = new HashSet<Map.Entry<K,V>>();
Enumeration enumeration = session.getAttributeNames();
Enumeration<? extends Object> enumeration = session.getAttributeNames();
while (enumeration.hasMoreElements()) {
final String key = enumeration.nextElement().toString();
final Object value = session.getAttribute(key);
entries.add(new Map.Entry() {
entries.add(new Map.Entry<K,V>() {
public boolean equals(Object obj) {
Map.Entry entry = (Map.Entry) obj;
Map.Entry<K, V> entry = (Map.Entry<K, V>) obj;
return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
}
@@ -126,18 +126,18 @@ public class SessionMap extends AbstractMap implements Serializable {
return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
}
public Object getKey() {
return key;
public K getKey() {
return (K) key;
}
public Object getValue() {
return value;
public V getValue() {
return (V) value;
}
public Object setValue(Object obj) {
public V setValue(Object obj) {
session.setAttribute(key, obj);
return value;
return (V) value;
}
});
}
@@ -153,13 +153,13 @@ public class SessionMap extends AbstractMap implements Serializable {
* @param key the name of the session attribute.
* @return the session attribute or <tt>null</tt> if it doesn't exist.
*/
public Object get(Object key) {
public V get(Object key) {
if (session == null) {
return null;
}
synchronized (session) {
return session.getAttribute(key.toString());
return (V) session.getAttribute(key.toString());
}
}
@@ -170,7 +170,7 @@ public class SessionMap extends AbstractMap implements Serializable {
* @param value the value to set.
* @return the object that was just set.
*/
public Object put(Object key, Object value) {
public V put(K key, V value) {
synchronized (this) {
if (session == null) {
session = request.getSession(true);
@@ -191,7 +191,7 @@ public class SessionMap extends AbstractMap implements Serializable {
* @param key the name of the attribute to remove.
* @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
*/
public Object remove(Object key) {
public V remove(Object key) {
if (session == null) {
return null;
}
@@ -199,7 +199,7 @@ public class SessionMap extends AbstractMap implements Serializable {
synchronized (session) {
entries = null;
Object value = get(key);
V value = get(key);
session.removeAttribute(key.toString());
return value;
@@ -42,5 +42,5 @@ public interface ParameterAware {
*
* @param parameters a Map of parameters (name/value Strings).
*/
public void setParameters(Map<String,String[]> parameters);
public void setParameters(Map<String, Object> parameters);
}
@@ -29,6 +29,7 @@ import com.opensymphony.xwork2.TextProvider;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
/**
* Helper methods to access text from TextProviders
@@ -50,7 +51,7 @@ public class TextProviderHelper {
*
* @return the message if found, otherwise the defaultMessage
*/
public static String getText(String key, String defaultMessage, List<String> args, ValueStack stack) {
public static String getText(String key, String defaultMessage, List<Object> args, ValueStack stack) {
String msg = null;
TextProvider tp = null;
@@ -101,7 +102,7 @@ public class TextProviderHelper {
*
* @return the message if found, otherwise the defaultMessage
*/
public static String getText(String key, String defaultMessage,ValueStack stack) {
return getText(key, defaultMessage, new LinkedList<String>(), stack);
public static String getText(String key, String defaultMessage, ValueStack stack) {
return getText(key, defaultMessage, Collections.emptyList(), stack);
}
}
@@ -62,7 +62,7 @@ public class TagUtils {
"has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag.");
}
stack = du.getContainer().getInstance(ValueStackFactory.class).createValueStack();
Map extraContext = du.createContextMap(new RequestMap(req),
Map<String, Object> extraContext = du.createContextMap(new RequestMap(req),
req.getParameterMap(),
new SessionMap(req),
new ApplicationMap(pageContext.getServletContext()),
@@ -77,7 +77,7 @@ public class TagUtils {
ActionContext.setContext(new ActionContext(stack.getContext()));
} else {
// let's make sure that the current page context is in the action context
Map context = stack.getContext();
Map<String, Object> context = stack.getContext();
context.put(ServletActionContext.PAGE_CONTEXT, pageContext);
AttributeMap attrMap = new AttributeMap(context);
+7 -1
View File
@@ -19,6 +19,12 @@ Please do not edit it directly.
<h2>Attributes</h2>
<!-- START SNIPPET: tagattributes -->
<table width="100%">
<tr>
<td colspan="6"><h4>Dynamic Attributes Allowed:</h4> false</td>
</tr>
<tr>
<td colspan="6">&nbsp;</td>
</tr>
<tr>
<th align="left" valign="top"><h4>Name</h4></th>
<th align="left" valign="top"><h4>Required</h4></th>
@@ -209,7 +215,7 @@ Please do not edit it directly.
<td align="left" valign="top"></td>
<td align="left" valign="top">false</td>
<td align="left" valign="top">String</td>
<td align="left" valign="top">Label expression used for rendering a element specific label</td>
<td align="left" valign="top">Label expression used for rendering an element specific label</td>
</tr>
<tr>
<td align="left" valign="top">labelSeparator</td>
@@ -19,6 +19,12 @@ Please do not edit it directly.
<h2>Attributes</h2>
<!-- START SNIPPET: tagattributes -->
<table width="100%">
<tr>
<td colspan="6"><h4>Dynamic Attributes Allowed:</h4> false</td>
</tr>
<tr>
<td colspan="6">&nbsp;</td>
</tr>
<tr>
<th align="left" valign="top"><h4>Name</h4></th>
<th align="left" valign="top"><h4>Required</h4></th>
@@ -257,7 +263,7 @@ Please do not edit it directly.
<td align="left" valign="top"></td>
<td align="left" valign="top">false</td>
<td align="left" valign="top">String</td>
<td align="left" valign="top">Label expression used for rendering a element specific label</td>
<td align="left" valign="top">Label expression used for rendering an element specific label</td>
</tr>
<tr>
<td align="left" valign="top">labelSeparator</td>
@@ -19,6 +19,12 @@ Please do not edit it directly.
<h2>Attributes</h2>
<!-- START SNIPPET: tagattributes -->
<table width="100%">
<tr>
<td colspan="6"><h4>Dynamic Attributes Allowed:</h4> false</td>
</tr>
<tr>
<td colspan="6">&nbsp;</td>
</tr>
<tr>
<th align="left" valign="top"><h4>Name</h4></th>
<th align="left" valign="top"><h4>Required</h4></th>
@@ -161,7 +167,7 @@ Please do not edit it directly.
<td align="left" valign="top"></td>
<td align="left" valign="top">false</td>
<td align="left" valign="top">String</td>
<td align="left" valign="top">Label expression used for rendering a element specific label</td>
<td align="left" valign="top">Label expression used for rendering an element specific label</td>
</tr>
<tr>
<td align="left" valign="top">labelSeparator</td>
+7 -1
View File
@@ -19,6 +19,12 @@ Please do not edit it directly.
<h2>Attributes</h2>
<!-- START SNIPPET: tagattributes -->
<table width="100%">
<tr>
<td colspan="6"><h4>Dynamic Attributes Allowed:</h4> false</td>
</tr>
<tr>
<td colspan="6">&nbsp;</td>
</tr>
<tr>
<th align="left" valign="top"><h4>Name</h4></th>
<th align="left" valign="top"><h4>Required</h4></th>
@@ -225,7 +231,7 @@ Please do not edit it directly.
<td align="left" valign="top"></td>
<td align="left" valign="top">false</td>
<td align="left" valign="top">String</td>
<td align="left" valign="top">Label expression used for rendering a element specific label</td>
<td align="left" valign="top">Label expression used for rendering an element specific label</td>
</tr>
<tr>
<td align="left" valign="top">labelSeparator</td>
@@ -19,6 +19,12 @@ Please do not edit it directly.
<h2>Attributes</h2>
<!-- START SNIPPET: tagattributes -->
<table width="100%">
<tr>
<td colspan="6"><h4>Dynamic Attributes Allowed:</h4> false</td>
</tr>
<tr>
<td colspan="6">&nbsp;</td>
</tr>
<tr>
<th align="left" valign="top"><h4>Name</h4></th>
<th align="left" valign="top"><h4>Required</h4></th>
@@ -145,7 +151,7 @@ Please do not edit it directly.
<td align="left" valign="top"></td>
<td align="left" valign="top">false</td>
<td align="left" valign="top">String</td>
<td align="left" valign="top">Label expression used for rendering a element specific label</td>
<td align="left" valign="top">Label expression used for rendering an element specific label</td>
</tr>
<tr>
<td align="left" valign="top">labelSeparator</td>
@@ -19,6 +19,12 @@ Please do not edit it directly.
<h2>Attributes</h2>
<!-- START SNIPPET: tagattributes -->
<table width="100%">
<tr>
<td colspan="6"><h4>Dynamic Attributes Allowed:</h4> false</td>
</tr>
<tr>
<td colspan="6">&nbsp;</td>
</tr>
<tr>
<th align="left" valign="top"><h4>Name</h4></th>
<th align="left" valign="top"><h4>Required</h4></th>
@@ -113,7 +119,7 @@ Please do not edit it directly.
<td align="left" valign="top"></td>
<td align="left" valign="top">false</td>
<td align="left" valign="top">String</td>
<td align="left" valign="top">Label expression used for rendering a element specific label</td>
<td align="left" valign="top">Label expression used for rendering an element specific label</td>
</tr>
<tr>
<td align="left" valign="top">labelSeparator</td>
+7 -1
View File
@@ -19,6 +19,12 @@ Please do not edit it directly.
<h2>Attributes</h2>
<!-- START SNIPPET: tagattributes -->
<table width="100%">
<tr>
<td colspan="6"><h4>Dynamic Attributes Allowed:</h4> false</td>
</tr>
<tr>
<td colspan="6">&nbsp;</td>
</tr>
<tr>
<th align="left" valign="top"><h4>Name</h4></th>
<th align="left" valign="top"><h4>Required</h4></th>
@@ -233,7 +239,7 @@ Please do not edit it directly.
<td align="left" valign="top"></td>
<td align="left" valign="top">false</td>
<td align="left" valign="top">String</td>
<td align="left" valign="top">Label expression used for rendering a element specific label</td>
<td align="left" valign="top">Label expression used for rendering an element specific label</td>
</tr>
<tr>
<td align="left" valign="top">labelSeparator</td>
@@ -205,7 +205,7 @@ public class FileUploadInterceptorTest extends StrutsTestCase {
mai.setAction(action);
mai.setResultCode("success");
mai.setInvocationContext(ActionContext.getContext());
Map<String, Object[]> param = new HashMap<String, Object[]>();
Map<String, Object> param = new HashMap<String, Object>();
ActionContext.getContext().setParameters(param);
ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, createMultipartRequest((HttpServletRequest) req, 2000));
@@ -214,7 +214,6 @@ public class FileUploadInterceptorTest extends StrutsTestCase {
assertTrue(!action.hasErrors());
assertTrue(param.size() == 3);
System.err.println("param.get(\"file\"): " + param.get("file").getClass());
File[] files = (File[]) param.get("file");
String[] fileContentTypes = (String[]) param.get("fileContentType");
String[] fileRealFilenames = (String[]) param.get("fileFileName");
@@ -266,7 +265,7 @@ public class FileUploadInterceptorTest extends StrutsTestCase {
mai.setAction(action);
mai.setResultCode("success");
mai.setInvocationContext(ActionContext.getContext());
Map<String, Object[]> param = new HashMap<String, Object[]>();
Map<String, Object> param = new HashMap<String, Object>();
ActionContext.getContext().setParameters(param);
ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, createMultipartRequest(req, 2000));
@@ -88,7 +88,7 @@ public class ServletConfigInterceptorTest extends StrutsTestCase {
MockActionInvocation mai = createActionInvocation(mock);
Map param = new HashMap();
Map<String, Object> param = new HashMap<String, Object>();
mai.getInvocationContext().setParameters(param);
mock.setParameters(param);
@@ -105,7 +105,7 @@ public class ServletConfigInterceptorTest extends StrutsTestCase {
MockActionInvocation mai = createActionInvocation(mock);
Map session = new HashMap();
Map<String, Object> session = new HashMap<String, Object>();
mai.getInvocationContext().setSession(session);
mock.setSession(session);
@@ -122,7 +122,7 @@ public class ServletConfigInterceptorTest extends StrutsTestCase {
MockActionInvocation mai = createActionInvocation(mock);
Map app = new HashMap();
Map<String, Object> app = new HashMap<String, Object>();
mai.getInvocationContext().setApplication(app);
mock.setApplication(app);
@@ -113,7 +113,7 @@ public abstract class AbstractTagTest extends StrutsTestCase {
Dispatcher.setInstance(du);
du.setConfigurationManager(configurationManager);
session = new SessionMap(request);
Map extraContext = du.createContextMap(new RequestMap(request),
Map<String, Object> extraContext = du.createContextMap(new RequestMap(request),
request.getParameterMap(),
session,
new ApplicationMap(pageContext.getServletContext()),
@@ -216,7 +216,7 @@ public class FieldErrorTagTest extends AbstractUITagTest {
this.returnNullForFieldErrors = returnNullForFieldErrors;
}
public Map getFieldErrors() {
public Map<String, List<String>> getFieldErrors() {
if (haveFieldErrors) {
List err1 = new ArrayList();
err1.add("field error message number 1");
@@ -234,7 +234,7 @@ public class FieldErrorTagTest extends AbstractUITagTest {
return null;
}
else {
return Collections.EMPTY_MAP;
return Collections.emptyMap();
}
}
@@ -43,7 +43,6 @@ import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
/**
@@ -52,8 +51,8 @@ import com.opensymphony.xwork2.util.ValueStackFactory;
*/
public abstract class AbstractTagTest extends StrutsTestCase {
protected Action action;
protected Map context;
protected Map session;
protected Map<String, Object> context;
protected Map<String, Object> session;
protected ValueStack stack;
/**
@@ -110,12 +109,12 @@ public abstract class AbstractTagTest extends StrutsTestCase {
pageContext.setServletContext(servletContext);
mockContainer = new Mock(Container.class);
Dispatcher du = new Dispatcher(pageContext.getServletContext(), new HashMap());
Dispatcher du = new Dispatcher(pageContext.getServletContext(), new HashMap<String, String>());
du.init();
Dispatcher.setInstance(du);
du.setConfigurationManager(configurationManager);
session = new SessionMap(request);
Map extraContext = du.createContextMap(new RequestMap(request),
session = new SessionMap<String, Object>(request);
Map<String, Object> extraContext = du.createContextMap(new RequestMap(request),
request.getParameterMap(),
session,
new ApplicationMap(pageContext.getServletContext()),
@@ -75,7 +75,7 @@ public class DWRValidator {
ServletContext servletContext = WebContextFactory.get().getServletContext();
HttpServletResponse res = WebContextFactory.get().getHttpServletResponse();
Map requestParams = new HashMap(req.getParameterMap());
Map<String, Object> requestParams = new HashMap<String, Object>(req.getParameterMap());
if (params != null) {
requestParams.putAll(params);
} else {
@@ -85,7 +85,7 @@ public class DWRValidator {
Map session = new SessionMap(req);
Map application = new ApplicationMap(servletContext);
Dispatcher du = Dispatcher.getInstance();
HashMap ctx = du.createContextMap(requestMap,
HashMap<String, Object> ctx = du.createContextMap(requestMap,
params,
session,
application,
@@ -122,7 +122,7 @@ public class DWRValidator {
public static class ValidatorActionInvocation extends DefaultActionInvocation {
private static final long serialVersionUID = -7645433725470191275L;
protected ValidatorActionInvocation(Map extraContext, boolean pushAction) throws Exception {
protected ValidatorActionInvocation(Map<String, Object> extraContext, boolean pushAction) throws Exception {
super(extraContext, pushAction);
}