From 6fe03e7e6b7c5625b3bce6491e0cf4bfec37a144 Mon Sep 17 00:00:00 2001 From: pizzi80 Date: Tue, 20 Jun 2023 13:07:51 +0200 Subject: [PATCH] Copiers optimizations Signed-off-by: pizzi80 --- .../sun/faces/util/copier/CloneCopier.java | 2 +- .../sun/faces/util/copier/CopierUtils.java | 23 +++++++------------ .../sun/faces/util/copier/CopyCtorCopier.java | 2 +- .../util/copier/MultiStrategyCopier.java | 2 +- .../faces/util/copier/NewInstanceCopier.java | 6 +++-- 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/impl/src/main/java/com/sun/faces/util/copier/CloneCopier.java b/impl/src/main/java/com/sun/faces/util/copier/CloneCopier.java index aec695515c..787b82bce3 100644 --- a/impl/src/main/java/com/sun/faces/util/copier/CloneCopier.java +++ b/impl/src/main/java/com/sun/faces/util/copier/CloneCopier.java @@ -43,7 +43,7 @@ public Object copy(Object object) { Method cloneMethod = getMethod(object, "clone"); - if (!cloneMethod.isAccessible()) { + if (!cloneMethod.canAccess(object)) { cloneMethod.setAccessible(true); } diff --git a/impl/src/main/java/com/sun/faces/util/copier/CopierUtils.java b/impl/src/main/java/com/sun/faces/util/copier/CopierUtils.java index 5ad9b01e7a..25bcd0b728 100644 --- a/impl/src/main/java/com/sun/faces/util/copier/CopierUtils.java +++ b/impl/src/main/java/com/sun/faces/util/copier/CopierUtils.java @@ -29,21 +29,14 @@ public class CopierUtils { private static final String ERROR_COPIER_NAME = "The copier name should be a Java valid simple/qualified name."; private static final String COPIER_PREFIX = "com.sun.faces.util.copier."; - private final static Set keywords; - - static { - Set s = new HashSet<>(); - String[] kws = { "abstract", "continue", "for", "new", "switch", "assert", "default", "if", "package", "synchronized", "boolean", "do", "goto", - "private", "this", "break", "double", "implements", "protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", - "instanceof", "return", "transient", "catch", "extends", "int", "short", "try", "char", "final", "interface", "static", "void", "class", - "finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while", - // literals - "null", "true", "false" }; - for (String kw : kws) { - s.add(kw); - } - keywords = Collections.unmodifiableSet(s); - } + private final static Set keywords = Set.of( + "abstract", "continue", "for", "new", "switch", "assert", "default", "if", "package", "synchronized", "boolean", "do", "goto", + "private", "this", "break", "double", "implements", "protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", + "instanceof", "return", "transient", "catch", "extends", "int", "short", "try", "char", "final", "interface", "static", "void", "class", + "finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while", + // literals + "null", "true", "false" + ); public static Copier getCopier(FacesContext context, String copierType) { Copier copier = null; diff --git a/impl/src/main/java/com/sun/faces/util/copier/CopyCtorCopier.java b/impl/src/main/java/com/sun/faces/util/copier/CopyCtorCopier.java index a4ee60b6eb..c50a57ed08 100644 --- a/impl/src/main/java/com/sun/faces/util/copier/CopyCtorCopier.java +++ b/impl/src/main/java/com/sun/faces/util/copier/CopyCtorCopier.java @@ -35,7 +35,7 @@ public class CopyCtorCopier implements Copier { public Object copy(Object object) { try { - Constructor copyConstructor = object.getClass().getConstructor(object.getClass()); + Constructor copyConstructor = object.getClass().getConstructor(object.getClass()); return copyConstructor.newInstance(object); diff --git a/impl/src/main/java/com/sun/faces/util/copier/MultiStrategyCopier.java b/impl/src/main/java/com/sun/faces/util/copier/MultiStrategyCopier.java index 82bfa27632..067b27b743 100644 --- a/impl/src/main/java/com/sun/faces/util/copier/MultiStrategyCopier.java +++ b/impl/src/main/java/com/sun/faces/util/copier/MultiStrategyCopier.java @@ -37,7 +37,7 @@ */ public class MultiStrategyCopier implements Copier { - private static final List COPIERS = asList( // Note: copier instances used here must be thread-safe! + private static final List COPIERS = List.of( // Note: copier instances used here must be thread-safe! new SerializationCopier(), new CloneCopier(), new CopyCtorCopier(), new NewInstanceCopier()); @Override diff --git a/impl/src/main/java/com/sun/faces/util/copier/NewInstanceCopier.java b/impl/src/main/java/com/sun/faces/util/copier/NewInstanceCopier.java index 684ab024e5..1c44bd6cbb 100644 --- a/impl/src/main/java/com/sun/faces/util/copier/NewInstanceCopier.java +++ b/impl/src/main/java/com/sun/faces/util/copier/NewInstanceCopier.java @@ -16,6 +16,8 @@ package com.sun.faces.util.copier; +import java.lang.reflect.InvocationTargetException; + /** * Copier that doesn't actually copy an object fully, but just returns a new instance of the same type. *

@@ -30,8 +32,8 @@ public class NewInstanceCopier implements Copier { @Override public Object copy(Object object) { try { - return object.getClass().newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + return object.getClass().getDeclaredConstructor().newInstance(); + } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { throw new IllegalStateException(e); } }