Skip to content

Commit

Permalink
Fix powermock#676: Do not mock class annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Saip authored and Sebastian Saip committed Jun 9, 2016
1 parent a2a6dbb commit 7d3e462
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions core/src/main/java/org/powermock/core/MockGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public class MockGateway {
*/
public static boolean MOCK_GET_CLASS_METHOD = false;

/**
* Tells PowerMock whether or not to mock
* {@link java.lang.Class#isAnnotationPresent(Class)} ()} and
* {@link java.lang.Class#getAnnotation(Class)}.
*/
public static boolean MOCK_ANNOTATION_METHODS = false;

// used for static methods
@SuppressWarnings("UnusedDeclaration")
public static Object methodCall(Class<?> type, String methodName, Object[] args, Class<?>[] sig,
Expand Down Expand Up @@ -161,6 +168,8 @@ private static boolean shouldMockMethod(String methodName, Class<?>[] sig) {
return false;
} else if (isGetClassMethod(methodName, sig) && !MOCK_GET_CLASS_METHOD) {
return false;
} else if (isAnnotationMethod(methodName, sig) && !MOCK_ANNOTATION_METHODS){
return false;
} else {
return true;
}
Expand All @@ -174,6 +183,10 @@ private static boolean isJavaStandardMethod(String methodName, Class<?>[] sig) {
private static boolean isGetClassMethod(String methodName, Class<?>[] sig) {
return methodName.equals("getClass") && sig.length == 0;
}

private static boolean isAnnotationMethod(String methodName, Class<?>[] sig) {
return (methodName.equals("isAnnotationPresent") && sig.length == 1) || (methodName.equals("getAnnotation") && sig.length == 1);
}

private static boolean shouldMockThisCall() {
Object shouldSkipMockingOfNextCall = MockRepository.getAdditionalState(DONT_MOCK_NEXT_CALL);
Expand Down

0 comments on commit 7d3e462

Please sign in to comment.