diff --git a/util/src/main/java/de/muehlencord/shared/util/StringUtil.java b/util/src/main/java/de/muehlencord/shared/util/StringUtil.java index a208608..227165c 100644 --- a/util/src/main/java/de/muehlencord/shared/util/StringUtil.java +++ b/util/src/main/java/de/muehlencord/shared/util/StringUtil.java @@ -1,7 +1,10 @@ package de.muehlencord.shared.util; import java.io.UnsupportedEncodingException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.text.ParseException; +import java.util.HashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -84,8 +87,7 @@ public abstract class StringUtil { * @param keyWord1 the starting keyword * @param keyWord2 the end keywod * @return the string between keyword1 and keyword2 - * @throws ParseException if the value cannot be determined - e.g. if one of - * the keywords is not found + * @throws ParseException if the value cannot be determined - e.g. if one of the keywords is not found */ public static String getValueBetweenKeywords(String content, String keyWord1, String keyWord2) throws ParseException { int pos1 = content.indexOf(keyWord1); @@ -111,4 +113,25 @@ public abstract class StringUtil { public static boolean isEmpty(String s) { return (s == null) || (s.equals("")); } + + public static > String getEnumString(Class clazz, E[] enumValues, String method) { + try { + String returnValue = ""; + Method m = clazz.getMethod(method); + for (E e : enumValues) { + String value = (String) m.invoke(e); + returnValue += value + ", "; + } + if (returnValue.endsWith(", ")) { + returnValue = returnValue.substring(0, returnValue.length() - 2); + } + return returnValue; + } catch (ClassCastException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { + LOGGER.error(ex.getMessage()); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Detailed stacktrace", new Object[]{ex}); + } + return null; + } } +}