Explicit nullability declarations for all AOP Alliance methods

Includes consistent declarations in AOP Alliance related Spring AOP code.

Closes gh-24117
This commit is contained in:
Juergen Hoeller
2020-05-13 23:33:47 +02:00
parent 60fac67884
commit 3c1ee64b7f
42 changed files with 202 additions and 77 deletions

View File

@@ -16,6 +16,8 @@
package org.aopalliance.intercept;
import javax.annotation.Nonnull;
/**
* Intercepts the construction of a new object.
*
@@ -54,6 +56,7 @@ public interface ConstructorInterceptor extends Interceptor {
* @throws Throwable if the interceptors or the target object
* throws an exception
*/
@Nonnull
Object construct(ConstructorInvocation invocation) throws Throwable;
}

View File

@@ -18,6 +18,8 @@ package org.aopalliance.intercept;
import java.lang.reflect.Constructor;
import javax.annotation.Nonnull;
/**
* Description of an invocation to a constructor, given to an
* interceptor upon constructor-call.
@@ -36,6 +38,7 @@ public interface ConstructorInvocation extends Invocation {
* {@link Joinpoint#getStaticPart()} method (same result).
* @return the constructor being called
*/
@Nonnull
Constructor<?> getConstructor();
}

View File

@@ -16,6 +16,8 @@
package org.aopalliance.intercept;
import javax.annotation.Nonnull;
/**
* This interface represents an invocation in the program.
*
@@ -32,6 +34,7 @@ public interface Invocation extends Joinpoint {
* array to change the arguments.
* @return the argument of the invocation
*/
@Nonnull
Object[] getArguments();
}

View File

@@ -18,6 +18,9 @@ package org.aopalliance.intercept;
import java.lang.reflect.AccessibleObject;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* This interface represents a generic runtime joinpoint (in the AOP
* terminology).
@@ -46,6 +49,7 @@ public interface Joinpoint {
* @return see the children interfaces' proceed definition
* @throws Throwable if the joinpoint throws an exception
*/
@Nullable
Object proceed() throws Throwable;
/**
@@ -53,6 +57,7 @@ public interface Joinpoint {
* <p>For instance, the target object for an invocation.
* @return the object (can be null if the accessible object is static)
*/
@Nullable
Object getThis();
/**
@@ -60,6 +65,7 @@ public interface Joinpoint {
* <p>The static part is an accessible object on which a chain of
* interceptors are installed.
*/
@Nonnull
AccessibleObject getStaticPart();
}

View File

@@ -16,6 +16,9 @@
package org.aopalliance.intercept;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Intercepts calls on an interface on its way to the target. These
* are nested "on top" of the target.
@@ -52,6 +55,7 @@ public interface MethodInterceptor extends Interceptor {
* @throws Throwable if the interceptors or the target object
* throws an exception
*/
Object invoke(MethodInvocation invocation) throws Throwable;
@Nullable
Object invoke(@Nonnull MethodInvocation invocation) throws Throwable;
}

View File

@@ -18,6 +18,8 @@ package org.aopalliance.intercept;
import java.lang.reflect.Method;
import javax.annotation.Nonnull;
/**
* Description of an invocation to a method, given to an interceptor
* upon method-call.
@@ -36,6 +38,7 @@ public interface MethodInvocation extends Invocation {
* {@link Joinpoint#getStaticPart()} method (same result).
* @return the method being called
*/
@Nonnull
Method getMethod();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2020 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.
@@ -23,6 +23,7 @@ import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterAdvice;
import org.springframework.lang.Nullable;
/**
* Spring AOP advice wrapping an AspectJ after advice method.
@@ -42,6 +43,7 @@ public class AspectJAfterAdvice extends AbstractAspectJAdvice
@Override
@Nullable
public Object invoke(MethodInvocation mi) throws Throwable {
try {
return mi.proceed();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2020 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.
@@ -23,6 +23,7 @@ import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterAdvice;
import org.springframework.lang.Nullable;
/**
* Spring AOP advice wrapping an AspectJ after-throwing advice method.
@@ -57,6 +58,7 @@ public class AspectJAfterThrowingAdvice extends AbstractAspectJAdvice
}
@Override
@Nullable
public Object invoke(MethodInvocation mi) throws Throwable {
try {
return mi.proceed();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2020 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.
@@ -25,6 +25,7 @@ import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.weaver.tools.JoinPointMatch;
import org.springframework.aop.ProxyMethodInvocation;
import org.springframework.lang.Nullable;
/**
* Spring AOP around advice (MethodInterceptor) that wraps
@@ -60,6 +61,7 @@ public class AspectJAroundAdvice extends AbstractAspectJAdvice implements Method
}
@Override
@Nullable
public Object invoke(MethodInvocation mi) throws Throwable {
if (!(mi instanceof ProxyMethodInvocation)) {
throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -84,11 +84,13 @@ public class MethodInvocationProceedingJoinPoint implements ProceedingJoinPoint,
}
@Override
@Nullable
public Object proceed() throws Throwable {
return this.methodInvocation.invocableClone().proceed();
}
@Override
@Nullable
public Object proceed(Object[] arguments) throws Throwable {
Assert.notNull(arguments, "Argument array passed to proceed cannot be null");
if (arguments.length != this.methodInvocation.getArguments().length) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -23,6 +23,7 @@ import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
@@ -51,6 +52,7 @@ public class AfterReturningAdviceInterceptor implements MethodInterceptor, After
@Override
@Nullable
public Object invoke(MethodInvocation mi) throws Throwable {
Object retVal = mi.proceed();
this.advice.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -23,6 +23,7 @@ import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.BeforeAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
@@ -51,6 +52,7 @@ public class MethodBeforeAdviceInterceptor implements MethodInterceptor, BeforeA
@Override
@Nullable
public Object invoke(MethodInvocation mi) throws Throwable {
this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis());
return mi.proceed();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -107,6 +107,7 @@ public class ThrowsAdviceInterceptor implements MethodInterceptor, AfterAdvice {
@Override
@Nullable
public Object invoke(MethodInvocation mi) throws Throwable {
try {
return mi.proceed();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2020 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.
@@ -25,6 +25,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.aop.support.AopUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Base {@code MethodInterceptor} implementation for tracing.
@@ -151,6 +152,7 @@ public abstract class AbstractTraceInterceptor implements MethodInterceptor, Ser
}
else {
Object target = invocation.getThis();
Assert.state(target != null, "Target must not be null");
return LogFactory.getLog(getClassForLogging(target));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2020 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.
@@ -21,6 +21,7 @@ import java.io.Serializable;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.lang.Nullable;
import org.springframework.util.ConcurrencyThrottleSupport;
/**
@@ -48,6 +49,7 @@ public class ConcurrencyThrottleInterceptor extends ConcurrencyThrottleSupport
}
@Override
@Nullable
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
beforeAccess();
try {

View File

@@ -296,6 +296,8 @@ public class CustomizableTraceInterceptor extends AbstractTraceInterceptor {
@Nullable Object returnValue, @Nullable Throwable throwable, long invocationTime) {
Matcher matcher = PATTERN.matcher(message);
Object target = methodInvocation.getThis();
Assert.state(target != null, "Target must not be null");
StringBuffer output = new StringBuffer();
while (matcher.find()) {
@@ -304,11 +306,11 @@ public class CustomizableTraceInterceptor extends AbstractTraceInterceptor {
matcher.appendReplacement(output, Matcher.quoteReplacement(methodInvocation.getMethod().getName()));
}
else if (PLACEHOLDER_TARGET_CLASS_NAME.equals(match)) {
String className = getClassForLogging(methodInvocation.getThis()).getName();
String className = getClassForLogging(target).getName();
matcher.appendReplacement(output, Matcher.quoteReplacement(className));
}
else if (PLACEHOLDER_TARGET_CLASS_SHORT_NAME.equals(match)) {
String shortName = ClassUtils.getShortName(getClassForLogging(methodInvocation.getThis()));
String shortName = ClassUtils.getShortName(getClassForLogging(target));
matcher.appendReplacement(output, Matcher.quoteReplacement(shortName));
}
else if (PLACEHOLDER_ARGUMENTS.equals(match)) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2020 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.
@@ -18,6 +18,8 @@ package org.springframework.aop.interceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.lang.Nullable;
/**
* AOP Alliance {@code MethodInterceptor} that can be introduced in a chain
* to display verbose information about intercepted invocations to the logger.
@@ -56,6 +58,7 @@ public class DebugInterceptor extends SimpleTraceInterceptor {
@Override
@Nullable
public Object invoke(MethodInvocation invocation) throws Throwable {
synchronized (this) {
this.count++;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -25,6 +25,7 @@ import org.springframework.aop.support.DefaultIntroductionAdvisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;
import org.springframework.beans.factory.NamedBean;
import org.springframework.lang.Nullable;
/**
* Convenient methods for creating advisors that may be used when autoproxying beans
@@ -110,6 +111,7 @@ public abstract class ExposeBeanNameAdvisors {
}
@Override
@Nullable
public Object invoke(MethodInvocation mi) throws Throwable {
if (!(mi instanceof ProxyMethodInvocation)) {
throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);
@@ -134,6 +136,7 @@ public abstract class ExposeBeanNameAdvisors {
}
@Override
@Nullable
public Object invoke(MethodInvocation mi) throws Throwable {
if (!(mi instanceof ProxyMethodInvocation)) {
throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);

View File

@@ -25,6 +25,7 @@ import org.springframework.aop.Advisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.core.NamedThreadLocal;
import org.springframework.core.PriorityOrdered;
import org.springframework.lang.Nullable;
/**
* Interceptor that exposes the current {@link org.aopalliance.intercept.MethodInvocation}
@@ -88,6 +89,7 @@ public final class ExposeInvocationInterceptor implements MethodInterceptor, Pri
}
@Override
@Nullable
public Object invoke(MethodInvocation mi) throws Throwable {
MethodInvocation oldInvocation = invocation.get();
invocation.set(mi);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2020 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.
@@ -19,6 +19,8 @@ package org.springframework.aop.interceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.springframework.util.Assert;
/**
* Simple AOP Alliance {@code MethodInterceptor} that can be introduced
* in a chain to display verbose trace information about intercepted method
@@ -73,7 +75,9 @@ public class SimpleTraceInterceptor extends AbstractTraceInterceptor {
* @return the description
*/
protected String getInvocationDescription(MethodInvocation invocation) {
String className = invocation.getThis().getClass().getName();
Object target = invocation.getThis();
Assert.state(target != null, "Target must not be null");
String className = target.getClass().getName();
return "method '" + invocation.getMethod().getName() + "' of class [" + className + "]";
}

View File

@@ -114,12 +114,13 @@ public class DelegatePerTargetObjectIntroductionInterceptor extends Introduction
* that it is introduced into. This method is <strong>never</strong> called for
* {@link MethodInvocation MethodInvocations} on the introduced interfaces.
*/
@Nullable
protected Object doProceed(MethodInvocation mi) throws Throwable {
// If we get here, just pass the invocation on.
return mi.proceed();
}
private Object getIntroductionDelegateFor(Object targetObject) {
private Object getIntroductionDelegateFor(@Nullable Object targetObject) {
synchronized (this.delegateMap) {
if (this.delegateMap.containsKey(targetObject)) {
return this.delegateMap.get(targetObject);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2020 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.
@@ -131,6 +131,7 @@ public class DelegatingIntroductionInterceptor extends IntroductionInfoSupport
* that it is introduced into. This method is <strong>never</strong> called for
* {@link MethodInvocation MethodInvocations} on the introduced interfaces.
*/
@Nullable
protected Object doProceed(MethodInvocation mi) throws Throwable {
// If we get here, just pass the invocation on.
return mi.proceed();