Declare isStatic and releaseTarget as default methods on TargetSource

Closes gh-31820
This commit is contained in:
Juergen Hoeller
2023-12-12 12:39:52 +01:00
parent eae53560e4
commit 6bb9775309
11 changed files with 14 additions and 98 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -49,10 +49,13 @@ public interface TargetSource extends TargetClassAware {
* Will all calls to {@link #getTarget()} return the same object?
* <p>In that case, there will be no need to invoke {@link #releaseTarget(Object)},
* and the AOP framework can cache the return value of {@link #getTarget()}.
* <p>The default implementation returns {@code false}.
* @return {@code true} if the target is immutable
* @see #getTarget
*/
boolean isStatic();
default boolean isStatic() {
return false;
}
/**
* Return a target instance. Invoked immediately before the
@@ -67,9 +70,11 @@ public interface TargetSource extends TargetClassAware {
/**
* Release the given target object obtained from the
* {@link #getTarget()} method, if any.
* <p>The default implementation is empty.
* @param target object obtained from a call to {@link #getTarget()}
* @throws Exception if the object can't be released
*/
void releaseTarget(Object target) throws Exception;
default void releaseTarget(Object target) throws Exception {
}
}

View File

@@ -153,16 +153,6 @@ public abstract class AbstractBeanFactoryBasedTargetSource implements TargetSour
}
}
@Override
public boolean isStatic() {
return false;
}
@Override
public void releaseTarget(Object target) throws Exception {
// Nothing to do here.
}
/**
* Copy configuration from the other AbstractBeanFactoryBasedTargetSource object.

View File

@@ -72,11 +72,6 @@ public abstract class AbstractLazyCreationTargetSource implements TargetSource {
return (this.lazyTarget != null ? this.lazyTarget.getClass() : null);
}
@Override
public boolean isStatic() {
return false;
}
/**
* Returns the lazy-initialized target object,
* creating it on-the-fly if it doesn't exist already.
@@ -91,11 +86,6 @@ public abstract class AbstractLazyCreationTargetSource implements TargetSource {
return this.lazyTarget;
}
@Override
public void releaseTarget(Object target) throws Exception {
// nothing to do
}
/**
* Subclasses should implement this method to return the lazy initialized object.

View File

@@ -116,13 +116,6 @@ public final class EmptyTargetSource implements TargetSource, Serializable {
return null;
}
/**
* Nothing to release.
*/
@Override
public void releaseTarget(Object target) {
}
/**
* Returns the canonical instance on deserialization in case

View File

@@ -66,21 +66,11 @@ public class HotSwappableTargetSource implements TargetSource, Serializable {
return this.target.getClass();
}
@Override
public final boolean isStatic() {
return false;
}
@Override
public synchronized Object getTarget() {
return this.target;
}
@Override
public void releaseTarget(Object target) {
// nothing to do
}
/**
* Swap the target, returning the old target object.

View File

@@ -67,11 +67,6 @@ public class SingletonTargetSource implements TargetSource, Serializable {
return this.target;
}
@Override
public void releaseTarget(Object target) {
// nothing to do
}
@Override
public boolean isStatic() {
return true;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -73,14 +73,6 @@ public abstract class AbstractRefreshableTargetSource implements TargetSource, R
return this.targetObject.getClass();
}
/**
* Not static.
*/
@Override
public boolean isStatic() {
return false;
}
@Override
@Nullable
public final synchronized Object getTarget() {
@@ -90,13 +82,6 @@ public abstract class AbstractRefreshableTargetSource implements TargetSource, R
return this.targetObject;
}
/**
* No need to release target.
*/
@Override
public void releaseTarget(Object object) {
}
@Override
public final synchronized void refresh() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -75,14 +75,9 @@ public class PrototypeBasedTargetSourceTests {
private TestBean thisFieldIsNotSerializable = new TestBean();
@Override
public Object getTarget() throws Exception {
public Object getTarget() {
return newPrototypeInstance();
}
@Override
public void releaseTarget(Object target) throws Exception {
// Do nothing
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -85,7 +85,7 @@ public class ContextAnnotationAutowireCandidateResolver extends QualifierAnnotat
}
private Object buildLazyResolutionProxy(
final DependencyDescriptor descriptor, final @Nullable String beanName, boolean classOnly) {
final DependencyDescriptor descriptor, @Nullable final String beanName, boolean classOnly) {
BeanFactory beanFactory = getBeanFactory();
Assert.state(beanFactory instanceof DefaultListableBeanFactory,
@@ -98,10 +98,6 @@ public class ContextAnnotationAutowireCandidateResolver extends QualifierAnnotat
return descriptor.getDependencyType();
}
@Override
public boolean isStatic() {
return false;
}
@Override
public Object getTarget() {
Set<String> autowiredBeanNames = (beanName != null ? new LinkedHashSet<>(1) : null);
Object target = dlbf.doResolveDependency(descriptor, beanName, autowiredBeanNames, null);
@@ -128,9 +124,6 @@ public class ContextAnnotationAutowireCandidateResolver extends QualifierAnnotat
}
return target;
}
@Override
public void releaseTarget(Object target) {
}
};
ProxyFactory pf = new ProxyFactory();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -148,8 +148,4 @@ public class JndiObjectTargetSource extends JndiObjectLocator implements TargetS
}
}
@Override
public void releaseTarget(Object target) {
}
}

View File

@@ -1143,17 +1143,10 @@ public abstract class AbstractAopProxyTests {
return TestBean.class;
}
@Override
public boolean isStatic() {
return false;
}
@Override
public Object getTarget() throws Exception {
assertThat(AopContext.currentProxy()).isEqualTo(proxy);
return target;
}
@Override
public void releaseTarget(Object target) throws Exception {
}
});
// Just test anything: it will fail if context wasn't found
@@ -1903,15 +1896,6 @@ public abstract class AbstractAopProxyTests {
throw new RuntimeException("Expectation failed: " + gets + " gets and " + releases + " releases");
}
}
/**
* @see org.springframework.aop.TargetSource#isStatic()
*/
@Override
public boolean isStatic() {
return false;
}
}