Skip to content

Commit

Permalink
Declare isStatic and releaseTarget as default methods on TargetSource
Browse files Browse the repository at this point in the history
Closes gh-31820
  • Loading branch information
jhoeller committed Dec 12, 2023
1 parent eae5356 commit 6bb9775
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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 {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,6 @@ public Class<?> getTargetClass() {
}
}

@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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ public synchronized Class<?> getTargetClass() {
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.
Expand All @@ -91,11 +86,6 @@ public synchronized Object getTarget() throws Exception {
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,6 @@ public Object getTarget() {
return null;
}

/**
* Nothing to release.
*/
@Override
public void releaseTarget(Object target) {
}


/**
* Returns the canonical instance on deserialization in case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,11 @@ public synchronized Class<?> getTargetClass() {
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ public Object getTarget() {
return this.target;
}

@Override
public void releaseTarget(Object target) {
// nothing to do
}

@Override
public boolean isStatic() {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -73,14 +73,6 @@ public synchronized Class<?> getTargetClass() {
return this.targetObject.getClass();
}

/**
* Not static.
*/
@Override
public boolean isStatic() {
return false;
}

@Override
@Nullable
public final synchronized Object getTarget() {
Expand All @@ -90,13 +82,6 @@ public final synchronized Object getTarget() {
return this.targetObject;
}

/**
* No need to release target.
*/
@Override
public void releaseTarget(Object object) {
}


@Override
public final synchronized void refresh() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -75,14 +75,9 @@ private static class TestTargetSource extends AbstractPrototypeBasedTargetSource
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
}
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -85,7 +85,7 @@ protected Object buildLazyResolutionProxy(DependencyDescriptor descriptor, @Null
}

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,
Expand All @@ -98,10 +98,6 @@ public Class<?> getTargetClass() {
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);
Expand All @@ -128,9 +124,6 @@ else if (Set.class == type || Collection.class == type) {
}
return target;
}
@Override
public void releaseTarget(Object target) {
}
};

ProxyFactory pf = new ProxyFactory();
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -148,8 +148,4 @@ public Object getTarget() {
}
}

@Override
public void releaseTarget(Object target) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1143,17 +1143,10 @@ public Class<?> getTargetClass() {
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
Expand Down Expand Up @@ -1903,15 +1896,6 @@ public void verify() {
throw new RuntimeException("Expectation failed: " + gets + " gets and " + releases + " releases");
}
}

/**
* @see org.springframework.aop.TargetSource#isStatic()
*/
@Override
public boolean isStatic() {
return false;
}

}


Expand Down

0 comments on commit 6bb9775

Please sign in to comment.