Skip to content

Commit

Permalink
Add back getArgument(int, Class) to circumvent compilation issues
Browse files Browse the repository at this point in the history
Also add `@NotExtensible` to several of our interfaces to document they
are not intended to be subclassed.

Fixes #1609
  • Loading branch information
TimvdLippe committed Mar 4, 2019
1 parent 73f1d17 commit 2d886e6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/mockito/MockSettings.java
Expand Up @@ -44,6 +44,7 @@
* Firstly, to make it easy to add another mock setting when the demand comes.
* Secondly, to enable combining together different mock settings without introducing zillions of overloaded mock() methods.
*/
@NotExtensible
public interface MockSettings extends Serializable {

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/mockito/MockitoFramework.java
Expand Up @@ -20,6 +20,7 @@
* @since 2.1.0
*/
@Incubating
@NotExtensible
public interface MockitoFramework {

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/mockito/MockitoSession.java
Expand Up @@ -88,6 +88,7 @@
* @since 2.7.0
*/
@Incubating
@NotExtensible
public interface MockitoSession {

/**
Expand Down
Expand Up @@ -119,6 +119,11 @@ public <T> T getArgument(int index) {
return (T) arguments[index];
}

@Override
public <T> T getArgument(int index, Class<T> clazz) {
return clazz.cast(arguments[index]);
}

@Override
public Object callRealMethod() throws Throwable {
if (!realMethod.isInvokable()) {
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/mockito/invocation/InvocationOnMock.java
Expand Up @@ -7,13 +7,15 @@

import java.io.Serializable;
import java.lang.reflect.Method;
import org.mockito.NotExtensible;

/**
* An invocation on a mock.
*
* <p>
* A placeholder for mock, the method that was called and the arguments that were passed.
*/
@NotExtensible
public interface InvocationOnMock extends Serializable {

/**
Expand Down Expand Up @@ -44,12 +46,35 @@ public interface InvocationOnMock extends Serializable {
*
* Can lookup in expanded arguments form {@link #getArguments()}.
*
* This method is preferred over {@link #getArgument(int, Class)} for readability. Please read
* the documentation of {@link #getArgument(int, Class)} for an overview of situations when
* that method is preferred over this one.
*
* @param index argument index
* @return casted argument at the given index
* @since 2.1.0
*/
<T> T getArgument(int index);

/**
* Returns casted argument at the given index. This method is analogous to
* {@link #getArgument(int)}, but is necessary to circumvent issues when dealing with generics.
*
* In general, {@link #getArgument(int)} is the appropriate function to use. This particular
* function is only necessary if you are doing one of the following things:
*
* 1. You want to directly invoke a method on the result of {@link #getArgument(int)}.
* 2. You want to directly pas the result of the invocation into a function that accepts a generic parameter.
*
* If you prefer to use {@link #getArgument(int)} instead, you can circumvent the compilation
* issues by storing the intermediate result into a local variable with the correct type.
*
* @param index argument index
* @param clazz clazz to cast the argument to
* @return casted argument at the given index
*/
<T> T getArgument(int index, Class<T> clazz);

/**
* calls real method
* <p>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/mockito/session/MockitoSessionBuilder.java
Expand Up @@ -7,6 +7,7 @@
import org.mockito.Incubating;
import org.mockito.MockitoAnnotations;
import org.mockito.MockitoSession;
import org.mockito.NotExtensible;
import org.mockito.exceptions.misusing.UnfinishedMockingSessionException;
import org.mockito.quality.Strictness;

Expand All @@ -17,6 +18,7 @@
* @since 2.7.0
*/
@Incubating
@NotExtensible
public interface MockitoSessionBuilder {

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/mockito/session/MockitoSessionLogger.java
Expand Up @@ -6,6 +6,7 @@

import org.mockito.Incubating;
import org.mockito.MockitoSession;
import org.mockito.NotExtensible;

/**
* Logger for {@linkplain org.mockito.quality.MockitoHint hints} emitted when
Expand All @@ -17,6 +18,7 @@
* @since 2.15.0
*/
@Incubating
@NotExtensible
public interface MockitoSessionLogger {

/**
Expand Down

0 comments on commit 2d886e6

Please sign in to comment.