Skip to content

Commit 2a37284

Browse files
committed
Merge pull request #24649 from stsypanov
* pr/24649: Polish "Skip non-overridden methods of Object.class" Skip non-overridden methods of Object.class Closes gh-24649
2 parents 21613ea + 95c43bb commit 2a37284

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java

+19-15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.reflect.Method;
2121
import java.lang.reflect.Modifier;
2222
import java.lang.reflect.UndeclaredThrowableException;
23+
import java.util.ArrayList;
2324
import java.util.Collections;
2425
import java.util.List;
2526
import java.util.Map;
@@ -335,36 +336,39 @@ private Callback[] getCallbacks(Class<?> rootClass) throws Exception {
335336
new HashCodeInterceptor(this.advised)
336337
};
337338

338-
Callback[] callbacks;
339-
340339
// If the target is a static one and the advice chain is frozen,
341340
// then we can make some optimizations by sending the AOP calls
342341
// direct to the target using the fixed chain for that method.
343342
if (isStatic && isFrozen) {
344343
Method[] methods = rootClass.getMethods();
345-
Callback[] fixedCallbacks = new Callback[methods.length];
346-
this.fixedInterceptorMap = CollectionUtils.newHashMap(methods.length);
344+
int methodsCount = methods.length;
345+
List<Callback> fixedCallbacks = new ArrayList<>(methodsCount);
346+
this.fixedInterceptorMap = CollectionUtils.newHashMap(methodsCount);
347347

348-
// TODO: small memory optimization here (can skip creation for methods with no advice)
349-
for (int x = 0; x < methods.length; x++) {
348+
int advicedMethodCount = methodsCount;
349+
for (int x = 0; x < methodsCount; x++) {
350350
Method method = methods[x];
351+
//do not create advices for non-overridden methods of java.lang.Object
352+
if (method.getDeclaringClass() == Object.class) {
353+
advicedMethodCount--;
354+
continue;
355+
}
351356
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, rootClass);
352-
fixedCallbacks[x] = new FixedChainStaticTargetInterceptor(
353-
chain, this.advised.getTargetSource().getTarget(), this.advised.getTargetClass());
354-
this.fixedInterceptorMap.put(method, x);
357+
fixedCallbacks.add(new FixedChainStaticTargetInterceptor(
358+
chain, this.advised.getTargetSource().getTarget(), this.advised.getTargetClass()));
359+
this.fixedInterceptorMap.put(method, x - (methodsCount - advicedMethodCount) );
355360
}
356361

357362
// Now copy both the callbacks from mainCallbacks
358363
// and fixedCallbacks into the callbacks array.
359-
callbacks = new Callback[mainCallbacks.length + fixedCallbacks.length];
364+
Callback[] callbacks = new Callback[mainCallbacks.length + advicedMethodCount];
360365
System.arraycopy(mainCallbacks, 0, callbacks, 0, mainCallbacks.length);
361-
System.arraycopy(fixedCallbacks, 0, callbacks, mainCallbacks.length, fixedCallbacks.length);
366+
System.arraycopy(fixedCallbacks.toArray(Callback[]::new), 0, callbacks,
367+
mainCallbacks.length, advicedMethodCount);
362368
this.fixedInterceptorOffset = mainCallbacks.length;
369+
return callbacks;
363370
}
364-
else {
365-
callbacks = mainCallbacks;
366-
}
367-
return callbacks;
371+
return mainCallbacks;
368372
}
369373

370374

0 commit comments

Comments
 (0)