From 4e9ab06658c57c8482fb60b471146a1bffe48742 Mon Sep 17 00:00:00 2001 From: pizzi80 Date: Wed, 10 Apr 2024 17:59:16 +0200 Subject: [PATCH 1/7] Backport some Faces Util optimizations Signed-off-by: pizzi80 --- .../main/java/com/sun/faces/util/Util.java | 261 +++++++++--------- 1 file changed, 130 insertions(+), 131 deletions(-) diff --git a/impl/src/main/java/com/sun/faces/util/Util.java b/impl/src/main/java/com/sun/faces/util/Util.java index ca481492aa..f5afe6d66d 100644 --- a/impl/src/main/java/com/sun/faces/util/Util.java +++ b/impl/src/main/java/com/sun/faces/util/Util.java @@ -46,16 +46,19 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; +import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Enumeration; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.logging.Level; @@ -107,6 +110,7 @@ import jakarta.faces.webapp.FacesServlet; import jakarta.servlet.ServletContext; import jakarta.servlet.ServletRegistration; +import jakarta.servlet.ServletResponse; import jakarta.servlet.http.HttpServletMapping; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.MappingMatch; @@ -145,7 +149,7 @@ private Util() { } private static Map getPatternCache(Map appMap) { - @SuppressWarnings("unchecked") + @SuppressWarnings("unchecked") Map result = (Map) appMap.get(PATTERN_CACHE_KEY); if (result == null) { result = Collections.synchronizedMap(new LRUMap<>(15)); @@ -223,7 +227,7 @@ public static String generateCreatedBy(FacesContext facesContext) { // ignore } - return applicationContextPath + " " + Thread.currentThread().toString() + " " + System.currentTimeMillis(); + return applicationContextPath + " " + Thread.currentThread() + " " + System.currentTimeMillis(); } @@ -253,7 +257,7 @@ public static Object getListenerInstance(ValueExpression type, ValueExpression b } if (instance == null && type != null) { try { - instance = ReflectionUtils.newInstance((String) type.getValue(faces.getELContext())); + instance = ReflectionUtils.newInstance(type.getValue(faces.getELContext())); } catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) { throw new AbortProcessingException(e.getMessage(), e); } @@ -357,18 +361,25 @@ public static SchemaFactory createSchemaFactory(String uri) { return factory; } - public static Class loadClass(String name, Object fallbackClass) throws ClassNotFoundException { - ClassLoader loader = Util.getCurrentLoader(fallbackClass); - String[] primitiveNames = { "byte", "short", "int", "long", "float", "double", "boolean", "char" }; - Class[] primitiveClasses = { byte.class, short.class, int.class, long.class, float.class, double.class, boolean.class, char.class }; + public static final Map> primitiveTypes = Map.of( + "byte", byte.class, + "short", short.class, + "int", int.class, + "long", long.class, + "float", float.class, + "double", double.class, + "boolean", boolean.class, + "char", char.class + ); - for (int i = 0; i < primitiveNames.length; i++) { - if (primitiveNames[i].equals(name)) { - return primitiveClasses[i]; - } - } + public static Class loadClass(String name, Object fallbackClass) throws ClassNotFoundException { + // Primitive Type + Class primitiveType = primitiveTypes.get(name); + if (primitiveType != null) return primitiveType; + // Class.forName + ClassLoader loader = getCurrentLoader(fallbackClass); return Class.forName(name, true, loader); } @@ -403,11 +414,8 @@ public static ClassLoader getCurrentLoader(Object fallbackClass) { } private static ClassLoader getContextClassLoader() { - if (System.getSecurityManager() == null) { - return Thread.currentThread().getContextClassLoader(); - } else { - return (ClassLoader) java.security.AccessController.doPrivileged((PrivilegedAction) () -> Thread.currentThread().getContextClassLoader()); - } + // todo: remove this check when on Java 17 + return System.getSecurityManager() == null ? Thread.currentThread().getContextClassLoader() : AccessController.doPrivileged((PrivilegedAction) () -> Thread.currentThread().getContextClassLoader()); } /** @@ -549,6 +557,31 @@ public static boolean isEmpty(String string) { return string == null || string.isEmpty(); } + /** + * Returns true if the given string is not null and not empty. + * + * @param string The string to be checked on emptiness. + * @return True if the given string is not null and not empty. + */ + public static boolean isNotEmpty(String string) { return !isEmpty(string); } + + /** + * Returns true if the given string is null or is blank. + * + * @param string The string to be checked. + * @return True if the given string is null or is blank. + */ + public static boolean isBlank(String string) { + return string == null || string.isBlank(); + } + + /** + * @return null if the passed String is null or blank, s otherwise + */ + public static String nullIfBlank(String s) { + return isBlank(s) ? null : s; + } + /** * Returns true if the given array is null or is empty. * @@ -569,6 +602,8 @@ public static boolean isEmpty(Collection collection) { return collection == null || collection.isEmpty(); } + public static boolean isNotEmpty(Collection collection) { return !isEmpty(collection); } + /** * Returns true if the given value is null or is empty. Types of String, Collection, Map, Optional and * Array are recognized. If none is recognized, then examine the emptiness of the toString() representation instead. @@ -586,7 +621,7 @@ public static boolean isEmpty(Object value) { } else if (value instanceof Map) { return ((Map) value).isEmpty(); } else if (value instanceof Optional) { - return !((Optional) value).isPresent(); + return ((Optional) value).isEmpty(); } else if (value.getClass().isArray()) { return Array.getLength(value) == 0; } else { @@ -657,7 +692,7 @@ public static boolean isAnyNull(Object... values) { @SafeVarargs public static boolean isOneOf(T object, T... objects) { for (Object other : objects) { - if (object == null ? other == null : object.equals(other)) { + if (Objects.equals(object, other)) { return true; } } @@ -744,7 +779,7 @@ public static Converter getConverterForClass(Class converterClass, FacesContext } } - public static Converter getConverterForIdentifer(String converterId, FacesContext context) { + public static Converter getConverterForIdentifier(String converterId, FacesContext context) { if (converterId == null) { return null; } @@ -760,44 +795,17 @@ public static StateManager getStateManager(FacesContext context) throws FacesExc return context.getApplication().getStateManager(); } - public static Class getTypeFromString(String type) throws ClassNotFoundException { - Class result; - switch (type) { - case "byte": - result = Byte.TYPE; - break; - case "short": - result = Short.TYPE; - break; - case "int": - result = Integer.TYPE; - break; - case "long": - result = Long.TYPE; - break; - case "float": - result = Float.TYPE; - break; - case "double": - result = Double.TYPE; - break; - case "boolean": - result = Boolean.TYPE; - break; - case "char": - result = Character.TYPE; - break; - case "void": - result = Void.TYPE; - break; - default: - if (type.indexOf('.') == -1) { - type = "java.lang." + type; - } - result = Util.loadClass(type, Void.TYPE); - break; + public static Class getTypeFromString(String type) throws ClassNotFoundException { + if ( type == null ) throw new ClassNotFoundException("Type is required"); + + Class primitiveClass = primitiveTypes.get(type); + if ( primitiveClass != null ) return primitiveClass; + + if (type.indexOf('.') == -1) { + type = "java.lang." + type; } + Class result = loadClass(type, Void.TYPE); return result; } @@ -814,12 +822,12 @@ public static ViewHandler getViewHandler(FacesContext context) throws FacesExcep } public static boolean componentIsDisabled(UIComponent component) { - return Boolean.valueOf(String.valueOf(component.getAttributes().get("disabled"))); + return Boolean.parseBoolean(String.valueOf(component.getAttributes().get("disabled"))); } public static boolean componentIsDisabledOrReadonly(UIComponent component) { - return Boolean.valueOf(String.valueOf(component.getAttributes().get("disabled"))) - || Boolean.valueOf(String.valueOf(component.getAttributes().get("readonly"))); + return Boolean.parseBoolean(String.valueOf(component.getAttributes().get("disabled"))) + || Boolean.parseBoolean(String.valueOf(component.getAttributes().get("readonly"))); } // W3C XML specification refers to IETF RFC 1766 for language code @@ -832,19 +840,10 @@ public static Locale getLocaleFromString(String localeStr) throws IllegalArgumen if (null == localeStr || localeStr.length() < 2) { throw new IllegalArgumentException("Illegal locale String: " + localeStr); } - Locale result = null; - try { - Method method = Locale.class.getMethod("forLanguageTag", String.class); - if (method != null) { - result = (Locale) method.invoke(null, localeStr); - } - } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException throwable) { - // if we are NOT running JavaSE 7 we end up here and we will - // default to the previous way of determining the Locale below. - } + Locale result = Locale.forLanguageTag(localeStr); - if (result == null || result.getLanguage().equals("")) { + if (result == null || result.getLanguage().isEmpty()) { String lang = null; String country = null; String variant = null; @@ -907,8 +906,8 @@ public static Locale getLocaleFromString(String localeStr) throws IllegalArgumen public static int indexOfSet(String str, char[] set, int fromIndex) { int result = -1; for (int i = fromIndex, len = str.length(); i < len; i++) { - for (int j = 0, innerLen = set.length; j < innerLen; j++) { - if (str.charAt(i) == set[j]) { + for (char c : set) { + if (str.charAt(i) == c) { result = i; break; } @@ -928,7 +927,7 @@ public static int indexOfSet(String str, char[] set, int fromIndex) { * * @param e the Throwable to obtain the stacktrace from * - * @return the String representation ofthe stack trace obtained by calling getStackTrace() on the passed in exception. + * @return the String representation of the stack trace obtained by calling getStackTrace() on the passed in exception. * If null is passed in, we return the empty String. */ public static String getStackTraceString(Throwable e) { @@ -937,11 +936,11 @@ public static String getStackTraceString(Throwable e) { } StackTraceElement[] stacks = e.getStackTrace(); - StringBuffer sb = new StringBuffer(); + StringBuilder builder = new StringBuilder(1024); for (StackTraceElement stack : stacks) { - sb.append(stack.toString()).append('\n'); + builder.append(stack).append('\n'); } - return sb.toString(); + return builder.toString(); } /** @@ -959,26 +958,26 @@ public static String getStackTraceString(Throwable e) { * @return the content type of the response */ public static String getContentTypeFromResponse(Object response) { - String result = null; - if (null != response) { + if ( response == null ) return null; + if ( response instanceof ServletResponse ) return ((ServletResponse)response).getContentType(); - try { - Method method = ReflectionUtils.lookupMethod(response.getClass(), "getContentType", RIConstants.EMPTY_CLASS_ARGS); - if (null != method) { - Object obj = method.invoke(response, RIConstants.EMPTY_METH_ARGS); - if (null != obj) { - result = obj.toString(); - } + String result = null; + try { + Method method = ReflectionUtils.lookupMethod(response.getClass(), "getContentType", RIConstants.EMPTY_CLASS_ARGS); + if (null != method) { + Object obj = method.invoke(response, RIConstants.EMPTY_METH_ARGS); + if (null != obj) { + result = obj.toString(); } - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - throw new FacesException(e); } + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + throw new FacesException(e); } return result; } public static FeatureDescriptor getFeatureDescriptor(String name, String displayName, String desc, boolean expert, boolean hidden, boolean preferred, - Object type, Boolean designTime) { + Object type, Boolean designTime) { FeatureDescriptor fd = new FeatureDescriptor(); fd.setName(name); @@ -1049,9 +1048,9 @@ public static String[] split(ServletContext sc, String toSplit, String regex) { * @throws NullPointerException if context is null */ public static HttpServletMapping getFacesMapping(FacesContext context) { - notNull("context", context); + notNull("context", context); - return ((HttpServletRequest) context.getExternalContext().getRequest()).getHttpServletMapping(); + return ((HttpServletRequest) context.getExternalContext().getRequest()).getHttpServletMapping(); } /** @@ -1208,20 +1207,16 @@ static public boolean classHasAnnotations(Class clazz) { while (clazz != Object.class) { try { Field[] fields = clazz.getDeclaredFields(); - if (fields != null) { - for (Field field : fields) { - if (field.getAnnotations().length > 0) { - return true; - } + for (Field field : fields) { + if (field.getAnnotations().length > 0) { + return true; } } Method[] methods = clazz.getDeclaredMethods(); - if (methods != null) { - for (Method method : methods) { - if (method.getDeclaredAnnotations().length > 0) { - return true; - } + for (Method method : methods) { + if (method.getDeclaredAnnotations().length > 0) { + return true; } } } @@ -1262,29 +1257,29 @@ public static String getNamingContainerPrefix(FacesContext context) { } public static String getViewStateId(FacesContext context) { - String result = null; + final String result; final String viewStateCounterKey = "com.sun.faces.util.ViewStateCounterKey"; Map contextAttrs = context.getAttributes(); Integer counter = (Integer) contextAttrs.get(viewStateCounterKey); if (null == counter) { - counter = Integer.valueOf(0); + counter = 0; } char sep = UINamingContainer.getSeparatorChar(context); UIViewRoot root = context.getViewRoot(); - result = root.getContainerClientId(context) + sep + ResponseStateManager.VIEW_STATE_PARAM + sep + +counter; + result = root.getContainerClientId(context) + sep + ResponseStateManager.VIEW_STATE_PARAM + sep + counter; contextAttrs.put(viewStateCounterKey, ++counter); return result; } public static String getClientWindowId(FacesContext context) { - String result = null; + final String result; final String clientWindowIdCounterKey = "com.sun.faces.util.ClientWindowCounterKey"; Map contextAttrs = context.getAttributes(); Integer counter = (Integer) contextAttrs.get(clientWindowIdCounterKey); if (null == counter) { - counter = Integer.valueOf(0); + counter = 0; } char sep = UINamingContainer.getSeparatorChar(context); @@ -1393,7 +1388,7 @@ public static String getFacesConfigXmlVersion(FacesContext facesContext) { dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); - } catch (ParserConfigurationException pce) { + } catch (ParserConfigurationException ignored) { } dbf.setNamespaceAware(true); dbf.setValidating(false); @@ -1409,8 +1404,7 @@ public static String getFacesConfigXmlVersion(FacesContext facesContext) { if (stream != null) { try { stream.close(); - } catch (IOException ioe) { - } + } catch (IOException ignored) {} } } return result; @@ -1437,9 +1431,7 @@ public static String getWebXmlVersion(FacesContext facesContext) { dbf.setFeature("http://xml.org/sax/features/external-general-entities", false); dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); - - } catch (ParserConfigurationException e) { - } + } catch (ParserConfigurationException ignored) {} dbf.setNamespaceAware(true); dbf.setValidating(false); dbf.setXIncludeAware(false); @@ -1453,8 +1445,7 @@ public static String getWebXmlVersion(FacesContext facesContext) { if (stream != null) { try { stream.close(); - } catch (IOException ioe) { - } + } catch (IOException ignored) {} } } return result; @@ -1475,7 +1466,7 @@ public String getPrefix(String namespaceURI) { } @Override - public Iterator getPrefixes(String namespaceURI) { + public Iterator getPrefixes(String namespaceURI) { return null; } } @@ -1533,9 +1524,17 @@ public static BeanManager getCdiBeanManager(FacesContext facesContext) { public static Stream stream(Object object) { if (object == null) { return Stream.empty(); - } else if (object instanceof Stream) { + } + else if (object instanceof Stream) { return (Stream) object; - } else if (object instanceof Iterable) { + } + else if (object instanceof Collection) { + return ((Collection)object).stream(); // little bonus with sized spliterator... + } + else if ( object instanceof Enumeration ) { // recursive call wrapping in an Iterator (Java 9+) + return stream( ((Enumeration)object).asIterator() ); + } + else if (object instanceof Iterable) { return (Stream) StreamSupport.stream(((Iterable) object).spliterator(), false); } else if (object instanceof Map) { return (Stream) ((Map) object).entrySet().stream(); @@ -1608,12 +1607,12 @@ public static String ensureLeadingSlash(String s) { public static int extractFirstNumericSegment(String clientId, char separatorChar) { int nextSeparatorChar = clientId.indexOf(separatorChar); - while (clientId.length() > 0 && !isDigit(clientId.charAt(0)) && nextSeparatorChar >= 0) { + while ( !clientId.isEmpty() && !isDigit(clientId.charAt(0)) && nextSeparatorChar >= 0 ) { clientId = clientId.substring(nextSeparatorChar + 1); nextSeparatorChar = clientId.indexOf(separatorChar); } - if (clientId.length() > 0 && isDigit(clientId.charAt(0))) { + if ( !clientId.isEmpty() && isDigit(clientId.charAt(0)) ) { String firstNumericSegment = nextSeparatorChar >= 0 ? clientId.substring(0, nextSeparatorChar) : clientId; return Integer.parseInt(firstNumericSegment); } @@ -1656,8 +1655,8 @@ public static String getResponseEncoding(FacesContext context, Optional } if (encoding == null) { - // 3. If none found then get it from request (could happen when the view isn't built yet). - // See also ViewHandler#initView() and ViewHandler#calculateCharacterEncoding(). + // 3. If none found then get it from request (could happen when the view isn't built yet). + // See also ViewHandler#initView() and ViewHandler#calculateCharacterEncoding(). encoding = context.getExternalContext().getRequestCharacterEncoding(); if (encoding != null && LOGGER.isLoggable(FINEST)) { @@ -1666,8 +1665,8 @@ public static String getResponseEncoding(FacesContext context, Optional } if (encoding == null && context.getExternalContext().getSession(false) != null) { - // 4. If still none found then get previously known request encoding from session. - // See also ViewHandler#initView(). + // 4. If still none found then get previously known request encoding from session. + // See also ViewHandler#initView(). encoding = (String) context.getExternalContext().getSessionMap().get(CHARACTER_ENCODING_KEY); if (encoding != null && LOGGER.isLoggable(FINEST)) { @@ -1679,19 +1678,19 @@ public static String getResponseEncoding(FacesContext context, Optional // 5. If still none found then fall back to specified default. if (defaultEncoding.isPresent()) { encoding = defaultEncoding.get(); - } + } - if (encoding != null && !encoding.isBlank()) { - if (LOGGER.isLoggable(FINEST)) { - LOGGER.log(FINEST, "Using specified default encoding {0}", encoding); - } + if ( encoding != null && !encoding.isBlank() ) { + if (LOGGER.isLoggable(FINEST)) { + LOGGER.log(FINEST, "Using specified default encoding {0}", encoding); } - else { + } + else { // 6. If specified default is null or blank then finally fall back to hardcoded default. - encoding = RIConstants.CHAR_ENCODING; + encoding = RIConstants.CHAR_ENCODING; - if (LOGGER.isLoggable(FINEST)) { - LOGGER.log(FINEST, "No encoding found, defaulting to {0}", encoding); + if (LOGGER.isLoggable(FINEST)) { + LOGGER.log(FINEST, "No encoding found, defaulting to {0}", encoding); } } } From e66991953fd5315bd1227da1732bfc5dba86e66c Mon Sep 17 00:00:00 2001 From: pizzi80 Date: Wed, 10 Apr 2024 18:02:49 +0200 Subject: [PATCH 2/7] Backport some Faces Util optimizations Signed-off-by: pizzi80 --- impl/src/main/java/com/sun/faces/util/Util.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/impl/src/main/java/com/sun/faces/util/Util.java b/impl/src/main/java/com/sun/faces/util/Util.java index f5afe6d66d..5940915158 100644 --- a/impl/src/main/java/com/sun/faces/util/Util.java +++ b/impl/src/main/java/com/sun/faces/util/Util.java @@ -1655,8 +1655,8 @@ public static String getResponseEncoding(FacesContext context, Optional } if (encoding == null) { - // 3. If none found then get it from request (could happen when the view isn't built yet). - // See also ViewHandler#initView() and ViewHandler#calculateCharacterEncoding(). + // 3. If none found then get it from request (could happen when the view isn't built yet). + // See also ViewHandler#initView() and ViewHandler#calculateCharacterEncoding(). encoding = context.getExternalContext().getRequestCharacterEncoding(); if (encoding != null && LOGGER.isLoggable(FINEST)) { @@ -1665,8 +1665,8 @@ public static String getResponseEncoding(FacesContext context, Optional } if (encoding == null && context.getExternalContext().getSession(false) != null) { - // 4. If still none found then get previously known request encoding from session. - // See also ViewHandler#initView(). + // 4. If still none found then get previously known request encoding from session. + // See also ViewHandler#initView(). encoding = (String) context.getExternalContext().getSessionMap().get(CHARACTER_ENCODING_KEY); if (encoding != null && LOGGER.isLoggable(FINEST)) { @@ -1686,7 +1686,7 @@ public static String getResponseEncoding(FacesContext context, Optional } } else { - // 6. If specified default is null or blank then finally fall back to hardcoded default. + // 6. If specified default is null or blank then finally fall back to hardcoded default. encoding = RIConstants.CHAR_ENCODING; if (LOGGER.isLoggable(FINEST)) { From fe52d6e4117fcfdc05c6f2f6f785eec4dbcddf79 Mon Sep 17 00:00:00 2001 From: pizzi80 Date: Wed, 10 Apr 2024 18:04:45 +0200 Subject: [PATCH 3/7] Backport some Faces Util optimizations Signed-off-by: pizzi80 --- .../main/java/com/sun/faces/util/Util.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/impl/src/main/java/com/sun/faces/util/Util.java b/impl/src/main/java/com/sun/faces/util/Util.java index 5940915158..5e6e45aec6 100644 --- a/impl/src/main/java/com/sun/faces/util/Util.java +++ b/impl/src/main/java/com/sun/faces/util/Util.java @@ -1678,19 +1678,19 @@ public static String getResponseEncoding(FacesContext context, Optional // 5. If still none found then fall back to specified default. if (defaultEncoding.isPresent()) { encoding = defaultEncoding.get(); - } + } - if ( encoding != null && !encoding.isBlank() ) { - if (LOGGER.isLoggable(FINEST)) { - LOGGER.log(FINEST, "Using specified default encoding {0}", encoding); + if ( encoding != null && !encoding.isBlank() ) { + if (LOGGER.isLoggable(FINEST)) { + LOGGER.log(FINEST, "Using specified default encoding {0}", encoding); + } } - } - else { - // 6. If specified default is null or blank then finally fall back to hardcoded default. - encoding = RIConstants.CHAR_ENCODING; + else { + // 6. If specified default is null or blank then finally fall back to hardcoded default. + encoding = RIConstants.CHAR_ENCODING; - if (LOGGER.isLoggable(FINEST)) { - LOGGER.log(FINEST, "No encoding found, defaulting to {0}", encoding); + if (LOGGER.isLoggable(FINEST)) { + LOGGER.log(FINEST, "No encoding found, defaulting to {0}", encoding); } } } From e4c300a37d68430ca24134239e350b4d29fdddb9 Mon Sep 17 00:00:00 2001 From: pizzi80 Date: Wed, 10 Apr 2024 18:05:49 +0200 Subject: [PATCH 4/7] Backport some Faces Util optimizations Signed-off-by: pizzi80 --- impl/src/main/java/com/sun/faces/util/Util.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/impl/src/main/java/com/sun/faces/util/Util.java b/impl/src/main/java/com/sun/faces/util/Util.java index 5e6e45aec6..1dce4ac3db 100644 --- a/impl/src/main/java/com/sun/faces/util/Util.java +++ b/impl/src/main/java/com/sun/faces/util/Util.java @@ -1680,7 +1680,7 @@ public static String getResponseEncoding(FacesContext context, Optional encoding = defaultEncoding.get(); } - if ( encoding != null && !encoding.isBlank() ) { + if (encoding != null && !encoding.isBlank()) { if (LOGGER.isLoggable(FINEST)) { LOGGER.log(FINEST, "Using specified default encoding {0}", encoding); } From 65fb89cba356811b800e2d50c0bfe2e2783fffb1 Mon Sep 17 00:00:00 2001 From: pizzi80 Date: Wed, 10 Apr 2024 18:07:40 +0200 Subject: [PATCH 5/7] Backport some Faces Util optimizations Signed-off-by: pizzi80 --- impl/src/main/java/com/sun/faces/util/Util.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/impl/src/main/java/com/sun/faces/util/Util.java b/impl/src/main/java/com/sun/faces/util/Util.java index 1dce4ac3db..ea90586cda 100644 --- a/impl/src/main/java/com/sun/faces/util/Util.java +++ b/impl/src/main/java/com/sun/faces/util/Util.java @@ -798,8 +798,8 @@ public static StateManager getStateManager(FacesContext context) throws FacesExc public static Class getTypeFromString(String type) throws ClassNotFoundException { if ( type == null ) throw new ClassNotFoundException("Type is required"); - Class primitiveClass = primitiveTypes.get(type); - if ( primitiveClass != null ) return primitiveClass; + Class primitiveType = primitiveTypes.get(type); + if ( primitiveType != null ) return primitiveType; if (type.indexOf('.') == -1) { type = "java.lang." + type; From ba093136b4d92131661a14a531f608fee382b3bb Mon Sep 17 00:00:00 2001 From: pizzi80 Date: Wed, 10 Apr 2024 18:42:34 +0200 Subject: [PATCH 6/7] Util.stream optimized Signed-off-by: pizzi80 --- .../main/java/com/sun/faces/util/Util.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/impl/src/main/java/com/sun/faces/util/Util.java b/impl/src/main/java/com/sun/faces/util/Util.java index ea90586cda..f4b0774ba6 100644 --- a/impl/src/main/java/com/sun/faces/util/Util.java +++ b/impl/src/main/java/com/sun/faces/util/Util.java @@ -61,6 +61,8 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.Spliterator; +import java.util.Spliterators; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Pattern; @@ -1524,18 +1526,12 @@ public static BeanManager getCdiBeanManager(FacesContext facesContext) { public static Stream stream(Object object) { if (object == null) { return Stream.empty(); - } - else if (object instanceof Stream) { + } else if (object instanceof Stream) { return (Stream) object; - } - else if (object instanceof Collection) { - return ((Collection)object).stream(); // little bonus with sized spliterator... - } - else if ( object instanceof Enumeration ) { // recursive call wrapping in an Iterator (Java 9+) - return stream( ((Enumeration)object).asIterator() ); - } - else if (object instanceof Iterable) { - return (Stream) StreamSupport.stream(((Iterable) object).spliterator(), false); + } else if (object instanceof Collection) { + return ((Collection)object).stream(); // little bonus with sized spliterator... + } else if (object instanceof Iterable) { + return StreamSupport.stream(((Iterable) object).spliterator(), false); } else if (object instanceof Map) { return (Stream) ((Map) object).entrySet().stream(); } else if (object instanceof int[]) { @@ -1546,6 +1542,10 @@ else if (object instanceof Iterable) { return (Stream) Arrays.stream((double[]) object).boxed(); } else if (object instanceof Object[]) { return (Stream) Arrays.stream((Object[]) object); + } else if ( object instanceof Enumeration) { // recursive call using Enumeration.asIterator() (Java 9+) + return stream( ((Enumeration)object).asIterator() ); + } else if ( object instanceof Iterator) { // Iterator => Stream + return StreamSupport.stream( Spliterators.spliteratorUnknownSize((Iterator)object, Spliterator.ORDERED), false); } else { return (Stream) Stream.of(object); } From 3d606d416657517737c9fa64df11a2cc7ae2376b Mon Sep 17 00:00:00 2001 From: pizzi80 Date: Mon, 2 Sep 2024 13:29:54 +0200 Subject: [PATCH 7/7] changes required to approve Signed-off-by: pizzi80 --- .../main/java/com/sun/faces/util/Util.java | 27 +------------------ 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/impl/src/main/java/com/sun/faces/util/Util.java b/impl/src/main/java/com/sun/faces/util/Util.java index f4b0774ba6..238090f187 100644 --- a/impl/src/main/java/com/sun/faces/util/Util.java +++ b/impl/src/main/java/com/sun/faces/util/Util.java @@ -559,31 +559,6 @@ public static boolean isEmpty(String string) { return string == null || string.isEmpty(); } - /** - * Returns true if the given string is not null and not empty. - * - * @param string The string to be checked on emptiness. - * @return True if the given string is not null and not empty. - */ - public static boolean isNotEmpty(String string) { return !isEmpty(string); } - - /** - * Returns true if the given string is null or is blank. - * - * @param string The string to be checked. - * @return True if the given string is null or is blank. - */ - public static boolean isBlank(String string) { - return string == null || string.isBlank(); - } - - /** - * @return null if the passed String is null or blank, s otherwise - */ - public static String nullIfBlank(String s) { - return isBlank(s) ? null : s; - } - /** * Returns true if the given array is null or is empty. * @@ -798,7 +773,7 @@ public static StateManager getStateManager(FacesContext context) throws FacesExc } public static Class getTypeFromString(String type) throws ClassNotFoundException { - if ( type == null ) throw new ClassNotFoundException("Type is required"); + Objects.requireNonNull(type); Class primitiveType = primitiveTypes.get(type); if ( primitiveType != null ) return primitiveType;