Skip to content

Commit

Permalink
Throw IllegalArgumentException instead of AssertionError
Browse files Browse the repository at this point in the history
  • Loading branch information
amuniz committed Aug 18, 2015
1 parent 5e9ee2c commit 858a1fd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
19 changes: 11 additions & 8 deletions core/src/main/java/hudson/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,29 @@
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;

import edu.umd.cs.findbugs.annotations.SuppressWarnings;
import hudson.Proc.LocalProc;
import hudson.model.TaskListener;
import hudson.os.PosixAPI;
import hudson.util.QuotedStringTokenizer;
import hudson.util.VariableResolver;
import hudson.util.jna.WinIOException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Chmod;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.types.FileSet;

import jnr.posix.FileStat;
import jnr.posix.POSIX;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import java.io.*;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -72,12 +76,14 @@
import java.util.regex.Pattern;

import hudson.util.jna.Kernel32Utils;

import static hudson.util.jna.GNUCLibrary.LIBC;

import java.security.DigestInputStream;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.apache.commons.codec.digest.DigestUtils;

/**
Expand Down Expand Up @@ -1421,14 +1427,10 @@ public static Number tryParseNumber(@CheckForNull String numberStr, @CheckForNul
* is overridden in the given derived type.
*/
public static boolean isOverridden(@Nonnull Class base, @Nonnull Class derived, @Nonnull String methodName, @Nonnull Class... types) {
try {
return !getMethod(base, methodName, types).equals(getMethod(derived, methodName, types));
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
return !getMethod(base, methodName, types).equals(getMethod(derived, methodName, types));
}

private static Method getMethod(@Nonnull Class clazz, @Nonnull String methodName, @Nonnull Class... types) throws NoSuchMethodException {
private static Method getMethod(@Nonnull Class clazz, @Nonnull String methodName, @Nonnull Class... types) {
Method res = null;
try {
res = clazz.getDeclaredMethod(methodName, types);
Expand All @@ -1447,7 +1449,8 @@ private static Method getMethod(@Nonnull Class clazz, @Nonnull String methodName
throw new AssertionError(e);
}
if (res == null) {
throw new NoSuchMethodException("Method " + methodName + " not found in " + clazz.getName());
throw new IllegalArgumentException(
String.format("Method %s not found in %s (or it is private, final or static)", methodName, clazz.getName()));
}
return res;
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/java/hudson/util/IsOverriddenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public void isOverriddenTest() {
* Negative test.
* Trying to check for a method which does not exist in the hierarchy,
*/
@Test(expected = AssertionError.class)
@Test(expected = IllegalArgumentException.class)
public void isOverriddenNegativeTest() {
Util.isOverridden(Base.class, Derived.class, "method2");
}

/**
* Do not inspect private methods.
*/
@Test(expected = AssertionError.class)
@Test(expected = IllegalArgumentException.class)
public void avoidPrivateMethodsInspection() {
Util.isOverridden(Base.class, Intermediate.class, "aPrivateMethod");
}
Expand Down

0 comments on commit 858a1fd

Please sign in to comment.