Skip to content

Commit 6bb9775

Browse files
committed
Declare isStatic and releaseTarget as default methods on TargetSource
Closes gh-31820
1 parent eae5356 commit 6bb9775

File tree

11 files changed

+14
-98
lines changed

11 files changed

+14
-98
lines changed

spring-aop/src/main/java/org/springframework/aop/TargetSource.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,10 +49,13 @@ public interface TargetSource extends TargetClassAware {
4949
* Will all calls to {@link #getTarget()} return the same object?
5050
* <p>In that case, there will be no need to invoke {@link #releaseTarget(Object)},
5151
* and the AOP framework can cache the return value of {@link #getTarget()}.
52+
* <p>The default implementation returns {@code false}.
5253
* @return {@code true} if the target is immutable
5354
* @see #getTarget
5455
*/
55-
boolean isStatic();
56+
default boolean isStatic() {
57+
return false;
58+
}
5659

5760
/**
5861
* Return a target instance. Invoked immediately before the
@@ -67,9 +70,11 @@ public interface TargetSource extends TargetClassAware {
6770
/**
6871
* Release the given target object obtained from the
6972
* {@link #getTarget()} method, if any.
73+
* <p>The default implementation is empty.
7074
* @param target object obtained from a call to {@link #getTarget()}
7175
* @throws Exception if the object can't be released
7276
*/
73-
void releaseTarget(Object target) throws Exception;
77+
default void releaseTarget(Object target) throws Exception {
78+
}
7479

7580
}

spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,6 @@ public Class<?> getTargetClass() {
153153
}
154154
}
155155

156-
@Override
157-
public boolean isStatic() {
158-
return false;
159-
}
160-
161-
@Override
162-
public void releaseTarget(Object target) throws Exception {
163-
// Nothing to do here.
164-
}
165-
166156

167157
/**
168158
* Copy configuration from the other AbstractBeanFactoryBasedTargetSource object.

spring-aop/src/main/java/org/springframework/aop/target/AbstractLazyCreationTargetSource.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ public synchronized Class<?> getTargetClass() {
7272
return (this.lazyTarget != null ? this.lazyTarget.getClass() : null);
7373
}
7474

75-
@Override
76-
public boolean isStatic() {
77-
return false;
78-
}
79-
8075
/**
8176
* Returns the lazy-initialized target object,
8277
* creating it on-the-fly if it doesn't exist already.
@@ -91,11 +86,6 @@ public synchronized Object getTarget() throws Exception {
9186
return this.lazyTarget;
9287
}
9388

94-
@Override
95-
public void releaseTarget(Object target) throws Exception {
96-
// nothing to do
97-
}
98-
9989

10090
/**
10191
* Subclasses should implement this method to return the lazy initialized object.

spring-aop/src/main/java/org/springframework/aop/target/EmptyTargetSource.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,6 @@ public Object getTarget() {
116116
return null;
117117
}
118118

119-
/**
120-
* Nothing to release.
121-
*/
122-
@Override
123-
public void releaseTarget(Object target) {
124-
}
125-
126119

127120
/**
128121
* Returns the canonical instance on deserialization in case

spring-aop/src/main/java/org/springframework/aop/target/HotSwappableTargetSource.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,11 @@ public synchronized Class<?> getTargetClass() {
6666
return this.target.getClass();
6767
}
6868

69-
@Override
70-
public final boolean isStatic() {
71-
return false;
72-
}
73-
7469
@Override
7570
public synchronized Object getTarget() {
7671
return this.target;
7772
}
7873

79-
@Override
80-
public void releaseTarget(Object target) {
81-
// nothing to do
82-
}
83-
8474

8575
/**
8676
* Swap the target, returning the old target object.

spring-aop/src/main/java/org/springframework/aop/target/SingletonTargetSource.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ public Object getTarget() {
6767
return this.target;
6868
}
6969

70-
@Override
71-
public void releaseTarget(Object target) {
72-
// nothing to do
73-
}
74-
7570
@Override
7671
public boolean isStatic() {
7772
return true;

spring-aop/src/main/java/org/springframework/aop/target/dynamic/AbstractRefreshableTargetSource.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -73,14 +73,6 @@ public synchronized Class<?> getTargetClass() {
7373
return this.targetObject.getClass();
7474
}
7575

76-
/**
77-
* Not static.
78-
*/
79-
@Override
80-
public boolean isStatic() {
81-
return false;
82-
}
83-
8476
@Override
8577
@Nullable
8678
public final synchronized Object getTarget() {
@@ -90,13 +82,6 @@ public final synchronized Object getTarget() {
9082
return this.targetObject;
9183
}
9284

93-
/**
94-
* No need to release target.
95-
*/
96-
@Override
97-
public void releaseTarget(Object object) {
98-
}
99-
10085

10186
@Override
10287
public final synchronized void refresh() {

spring-aop/src/test/java/org/springframework/aop/target/PrototypeBasedTargetSourceTests.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -75,14 +75,9 @@ private static class TestTargetSource extends AbstractPrototypeBasedTargetSource
7575
private TestBean thisFieldIsNotSerializable = new TestBean();
7676

7777
@Override
78-
public Object getTarget() throws Exception {
78+
public Object getTarget() {
7979
return newPrototypeInstance();
8080
}
81-
82-
@Override
83-
public void releaseTarget(Object target) throws Exception {
84-
// Do nothing
85-
}
8681
}
8782

8883
}

spring-context/src/main/java/org/springframework/context/annotation/ContextAnnotationAutowireCandidateResolver.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -85,7 +85,7 @@ protected Object buildLazyResolutionProxy(DependencyDescriptor descriptor, @Null
8585
}
8686

8787
private Object buildLazyResolutionProxy(
88-
final DependencyDescriptor descriptor, final @Nullable String beanName, boolean classOnly) {
88+
final DependencyDescriptor descriptor, @Nullable final String beanName, boolean classOnly) {
8989

9090
BeanFactory beanFactory = getBeanFactory();
9191
Assert.state(beanFactory instanceof DefaultListableBeanFactory,
@@ -98,10 +98,6 @@ public Class<?> getTargetClass() {
9898
return descriptor.getDependencyType();
9999
}
100100
@Override
101-
public boolean isStatic() {
102-
return false;
103-
}
104-
@Override
105101
public Object getTarget() {
106102
Set<String> autowiredBeanNames = (beanName != null ? new LinkedHashSet<>(1) : null);
107103
Object target = dlbf.doResolveDependency(descriptor, beanName, autowiredBeanNames, null);
@@ -128,9 +124,6 @@ else if (Set.class == type || Collection.class == type) {
128124
}
129125
return target;
130126
}
131-
@Override
132-
public void releaseTarget(Object target) {
133-
}
134127
};
135128

136129
ProxyFactory pf = new ProxyFactory();

spring-context/src/main/java/org/springframework/jndi/JndiObjectTargetSource.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -148,8 +148,4 @@ public Object getTarget() {
148148
}
149149
}
150150

151-
@Override
152-
public void releaseTarget(Object target) {
153-
}
154-
155151
}

0 commit comments

Comments
 (0)