1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .beans .factory .support ;
18
18
19
19
import java .lang .reflect .Method ;
20
+ import java .util .Collections ;
20
21
import java .util .LinkedHashSet ;
21
22
import java .util .Set ;
22
23
34
35
*/
35
36
public class MethodOverrides {
36
37
37
- private final Set <MethodOverride > overrides = new LinkedHashSet <MethodOverride >(0 );
38
+ private final Set <MethodOverride > overrides =
39
+ Collections .synchronizedSet (new LinkedHashSet <MethodOverride >(0 ));
40
+
41
+ private volatile boolean modified = false ;
38
42
39
43
40
44
/**
@@ -56,14 +60,16 @@ public MethodOverrides(MethodOverrides other) {
56
60
*/
57
61
public void addOverrides (MethodOverrides other ) {
58
62
if (other != null ) {
59
- this .overrides .addAll (other .getOverrides ());
63
+ this .modified = true ;
64
+ this .overrides .addAll (other .overrides );
60
65
}
61
66
}
62
67
63
68
/**
64
69
* Add the given method override.
65
70
*/
66
71
public void addOverride (MethodOverride override ) {
72
+ this .modified = true ;
67
73
this .overrides .add (override );
68
74
}
69
75
@@ -73,14 +79,15 @@ public void addOverride(MethodOverride override) {
73
79
* @see MethodOverride
74
80
*/
75
81
public Set <MethodOverride > getOverrides () {
82
+ this .modified = true ;
76
83
return this .overrides ;
77
84
}
78
85
79
86
/**
80
87
* Return whether the set of method overrides is empty.
81
88
*/
82
89
public boolean isEmpty () {
83
- return this .overrides .isEmpty ();
90
+ return (! this .modified || this . overrides .isEmpty () );
84
91
}
85
92
86
93
/**
@@ -89,13 +96,18 @@ public boolean isEmpty() {
89
96
* @return the method override, or {@code null} if none
90
97
*/
91
98
public MethodOverride getOverride (Method method ) {
92
- MethodOverride match = null ;
93
- for (MethodOverride candidate : this .overrides ) {
94
- if (candidate .matches (method )) {
95
- match = candidate ;
99
+ if (!this .modified ) {
100
+ return null ;
101
+ }
102
+ synchronized (this .overrides ) {
103
+ MethodOverride match = null ;
104
+ for (MethodOverride candidate : this .overrides ) {
105
+ if (candidate .matches (method )) {
106
+ match = candidate ;
107
+ }
96
108
}
109
+ return match ;
97
110
}
98
- return match ;
99
111
}
100
112
101
113
0 commit comments