Skip to content

Commit 331831f

Browse files
author
Micha Kiener
committed
SPR-6423, generics added to attribute map methods of the conversation object
1 parent d0c7c0b commit 331831f

File tree

4 files changed

+59
-34
lines changed

4 files changed

+59
-34
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/factory/config/AbstractMapScope.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,23 @@ public final Map<String, Object> getMap() {
4444
* org.springframework.beans.factory.ObjectFactory)
4545
*/
4646
public Object get(String name, ObjectFactory<?> objectFactory) {
47-
Object scopedObject = super.get(name);
47+
Object scopedObject = super.getAttribute(name);
4848
if (scopedObject == null) {
4949
scopedObject = objectFactory.getObject();
50-
super.put(name, scopedObject);
50+
super.setAttribute(name, scopedObject);
5151
}
5252

5353
return scopedObject;
5454
}
5555

56+
/**
57+
* @see org.springframework.beans.factory.config.Scope#remove(java.lang.String)
58+
*/
59+
@Override
60+
public Object remove(String name) {
61+
return super.removeAttribute(name);
62+
}
63+
5664
/**
5765
* Closes this scope by removing any scoped object stored within it and
5866
* invoking destruction callbacks accordingly.

org.springframework.beans/src/main/java/org/springframework/beans/factory/config/DestructionAwareAttributeMap.java

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,33 @@ public Map<String, Object> getAttributeMap() {
5757
/**
5858
* Returns the attribute having the specified name, if available,
5959
* <code>null</code> otherwise.
60-
* @param name the name of the attribute to be returned
60+
*
61+
* @param name
62+
* the name of the attribute to be returned
6163
* @return the attribute value or <code>null</code> if not available
6264
*/
63-
public Object get(String name) {
65+
@SuppressWarnings("unchecked")
66+
public <T> T getAttribute(String name) {
6467
synchronized (attributes) {
65-
return attributes.get(name);
68+
return (T) attributes.get(name);
6669
}
6770
}
6871

6972
/**
7073
* Puts the given object with the specified name as an attribute to the
7174
* underlying map.
7275
*
73-
* @param name the name of the attribute
74-
* @param value the value to be stored
76+
* @param name
77+
* the name of the attribute
78+
* @param value
79+
* the value to be stored
7580
* @return any previously object stored under the same name, if any,
76-
* <code>null</code> otherwise
81+
* <code>null</code> otherwise
7782
*/
78-
public Object put(String name, Object value) {
83+
@SuppressWarnings("unchecked")
84+
public <T> T setAttribute(String name, T value) {
7985
synchronized (attributes) {
80-
return attributes.put(name, value);
86+
return (T) attributes.put(name, value);
8187
}
8288
}
8389

@@ -96,14 +102,17 @@ public Object put(String name, Object value) {
96102
* <b>Note: This is an optional operation.</b> Implementations may throw
97103
* {@link UnsupportedOperationException} if they do not support explicitly
98104
* removing an object.
99-
* @param name the name of the object to remove
105+
*
106+
* @param name
107+
* the name of the object to remove
100108
* @return the removed object, or <code>null</code> if no object was present
101109
* @see #registerDestructionCallback
102110
*/
103-
public Object remove(String name) {
104-
Object value = null;
111+
@SuppressWarnings("unchecked")
112+
public <T> T removeAttribute(String name) {
113+
T value = null;
105114
synchronized (attributes) {
106-
value = attributes.remove(name);
115+
value = (T) attributes.remove(name);
107116
}
108117

109118
// check for a destruction callback to be invoked
@@ -124,7 +133,8 @@ public void clear() {
124133
// step through the attribute map and invoke destruction callbacks,
125134
// if any
126135
if (registeredDescructionCallbacks != null) {
127-
Iterator<Runnable> it = registeredDescructionCallbacks.values().iterator();
136+
Iterator<Runnable> it = registeredDescructionCallbacks.values()
137+
.iterator();
128138
while (it.hasNext()) {
129139
it.next().run();
130140
}
@@ -157,13 +167,15 @@ public void clear() {
157167
* gets removed via this facade's {@link #remove(String)} method, any
158168
* registered destruction callback should be removed as well, assuming that
159169
* the removed object will be reused or manually destroyed.
160-
* @param name the name of the object to execute the destruction callback
161-
* for
162-
* @param callback the destruction callback to be executed. Note that the
163-
* passed-in Runnable will never throw an exception, so it can safely be
164-
* executed without an enclosing try-catch block. Furthermore, the Runnable
165-
* will usually be serializable, provided that its target object is
166-
* serializable as well.
170+
*
171+
* @param name
172+
* the name of the object to execute the destruction callback for
173+
* @param callback
174+
* the destruction callback to be executed. Note that the
175+
* passed-in Runnable will never throw an exception, so it can
176+
* safely be executed without an enclosing try-catch block.
177+
* Furthermore, the Runnable will usually be serializable,
178+
* provided that its target object is serializable as well.
167179
* @see org.springframework.beans.factory.DisposableBean
168180
* @see org.springframework.beans.factory.support.AbstractBeanDefinition#getDestroyMethodName()
169181
* @see DestructionAwareBeanPostProcessor
@@ -183,9 +195,11 @@ public void registerDestructionCallback(String name, Runnable callback) {
183195
* with the given name or <code>null</code> if no such callback was
184196
* registered.
185197
*
186-
* @param name the name of the registered callback requested
187-
* @param remove <code>true</code>, if the callback should be removed after
188-
* this call, <code>false</code>, if it stays
198+
* @param name
199+
* the name of the registered callback requested
200+
* @param remove
201+
* <code>true</code>, if the callback should be removed after
202+
* this call, <code>false</code>, if it stays
189203
* @return the callback, if found, <code>null</code> otherwise
190204
*/
191205
public Runnable getDesctructionCallback(String name, boolean remove) {

org.springframework.context/src/main/java/org/springframework/conversation/Conversation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public interface Conversation {
7676
* @return the old value attached to the same name, if any,
7777
* <code>null</code> otherwise
7878
*/
79-
Object setAttribute(String name, Object value);
79+
<T> T setAttribute(String name, T value);
8080

8181
/**
8282
* Returns the value attached to the given name, if any previously
@@ -90,7 +90,7 @@ public interface Conversation {
9090
* @return the value, if available in the current state, <code>null</code>
9191
* otherwise
9292
*/
93-
Object getAttribute(String name);
93+
<T> T getAttribute(String name);
9494

9595
/**
9696
* Removes the value in the current conversation having the given name and
@@ -101,7 +101,7 @@ public interface Conversation {
101101
* @param name the name of the value to be removed from this conversation
102102
* @return the removed value, if found, <code>null</code> otherwise
103103
*/
104-
Object removeAttribute(String name);
104+
<T> T removeAttribute(String name);
105105

106106
/**
107107
* Returns the map holding the attributes of this conversation. The map will

org.springframework.context/src/main/java/org/springframework/conversation/manager/ConversationImpl.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ public ConversationImpl() {
101101
*
102102
* @see org.springframework.conversation.Conversation#getAttribute(java.lang.String)
103103
*/
104-
public Object getAttribute(String name) {
104+
@Override
105+
public <T> T getAttribute(String name) {
105106
touch();
106107

107108
// first try to get the attribute from this conversation state
108-
Object value = super.get(name);
109+
T value = super.getAttribute(name);
109110
if (value != null) {
110111
return value;
111112
}
@@ -125,7 +126,8 @@ public Object getAttribute(String name) {
125126
* @see org.springframework.conversation.Conversation#setAttribute(java.lang.String,
126127
* java.lang.Object)
127128
*/
128-
public Object setAttribute(String name, Object value) {
129+
@Override
130+
public <T> T setAttribute(String name, T value) {
129131
touch();
130132

131133
// check for implementing the ConversationListener interface and
@@ -134,15 +136,16 @@ public Object setAttribute(String name, Object value) {
134136
addListener((ConversationListener) value);
135137
}
136138

137-
return super.put(name, value);
139+
return super.setAttribute(name, value);
138140
}
139141

140142
/**
141143
* @see org.springframework.conversation.Conversation#removeAttribute(java.lang.String)
142144
*/
143-
public Object removeAttribute(String name) {
145+
@Override
146+
public <T> T removeAttribute(String name) {
144147
touch();
145-
Object value = super.remove(name);
148+
T value = super.removeAttribute(name);
146149

147150
// if the attribute implements the listener interface, remove it from
148151
// the registered listeners

0 commit comments

Comments
 (0)