Skip to content

Commit

Permalink
Avoiding NPE when processing execution command is missing from comman…
Browse files Browse the repository at this point in the history
…d line #39

Former-commit-id: a231c2b
  • Loading branch information
lfoppiano committed Oct 17, 2018
1 parent 91750c5 commit 861a2ae
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions grobid-core/src/main/java/org/grobid/core/utilities/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import org.grobid.core.exceptions.GrobidException;
import org.grobid.core.layout.LayoutToken;

import static shadedwipo.org.apache.commons.lang3.ArrayUtils.isEmpty;

/**
* Some utilities methods that I don't know where to put.
*
*
* @author Patrice Lopez
*/
public class Utilities {
Expand All @@ -56,8 +58,8 @@ public static boolean deleteDir(File dir) {
}

/**
* Deletes all files and subdirectories under dir if they are older than a given
* amount of seconds. Returns true if all deletions were successful. If a deletion
* Deletes all files and subdirectories under dir if they are older than a given
* amount of seconds. Returns true if all deletions were successful. If a deletion
* fails, the method stops attempting to delete and returns false.
*/
public static boolean deleteOldies(File dir, int maxLifeInSeconds) {
Expand All @@ -80,7 +82,7 @@ public static boolean deleteOldies(File dir, int maxLifeInSeconds, boolean root)
return false;
}
}
else
else
empty = false;
}
}
Expand Down Expand Up @@ -348,7 +350,7 @@ public static BiblioItem cleanZFNMetadata(BiblioItem item) {
* lin-32<br>
* lin-64<br>
* mac-64<br>
*
*
* @return name of the directory corresponding to the os name and
* architecture.
*/
Expand All @@ -361,7 +363,7 @@ public static String getOsNameAndArch() {

/**
* Convert a string to boolean.
*
*
* @param value
* the value to convert
* @return true if the string value is "true", false is it equals to
Expand All @@ -380,7 +382,7 @@ public static boolean stringToBoolean(String value) {

/**
* Call a java method using the method name given in string.
*
*
* @param obj
* Class in which the method is.
* @param args
Expand All @@ -400,12 +402,16 @@ public static Object launchMethod(Object obj, Object[] args,
paramTypes[i] = args[i].getClass();
}
}

if(StringUtils.isEmpty(methodName)) {
throw new GrobidException("Missing method in command line. To specify with -exe [methodName]. " + methodName);
}
return getMethod(obj, paramTypes, methodName).invoke(obj, args);
}
}

/**
* Call a java method using the method name given in string.
*
*
* @param obj
* Class in which the method is.
* @param args
Expand All @@ -426,15 +432,15 @@ public static Object launchMethod(Object obj, Object[] args,
/**
* Get the method given in string in input corresponding to the given
* arguments.
*
*
* @param obj
* Class in which the method is.
* @param paramTypes
* types of the arguments.
* @param methodName
* the name of the method.
* @return Methood
*
*
* @throws NoSuchMethodException
*/
@SuppressWarnings("rawtypes")
Expand All @@ -446,12 +452,12 @@ public static Method getMethod(Object obj, Class[] paramTypes,

/**
* Format a date in string using pFormat.
*
*
* @param pDate
* the date to parse.
* @param pFormat
* the format to use following SimpleDateFormat patterns.
*
*
* @return the formatted date.
*/
public static String dateToString(Date pDate, String pFormat){
Expand All @@ -469,17 +475,17 @@ public static boolean doubleEquals(double d1, double d2, double epsilon) {
}

/**
* Merge the offset positions of two lists, merging overlapping positions
* Merge the offset positions of two lists, merging overlapping positions
* into a spanning one.
*
*
* @param positions1
* the first offset position list to be merged
* @param positions2
* the second offset position list to be merged
*
*
* @return the merged list of (merged) offset positions
*/
public static List<OffsetPosition> mergePositions(List<OffsetPosition> positions1,
public static List<OffsetPosition> mergePositions(List<OffsetPosition> positions1,
List<OffsetPosition> positions2) {
if (CollectionUtils.isEmpty(positions1))
return positions2;
Expand Down Expand Up @@ -543,7 +549,7 @@ public static List<OffsetPosition> convertStringOffsetToTokenOffset(
found = true;
break;
}
indexToken++;
indexToken++;
}
if (found) {
indexToken++;
Expand All @@ -553,7 +559,7 @@ public static List<OffsetPosition> convertStringOffsetToTokenOffset(
result.add(currentPosition);
}
}
indexToken++;
indexToken++;
}
}
return result;
Expand Down

0 comments on commit 861a2ae

Please sign in to comment.