Skip to content

Commit

Permalink
Merge pull request #5252 from pizzi80/4.1.0_copiers
Browse files Browse the repository at this point in the history
Copiers optimizations
  • Loading branch information
BalusC authored Jun 24, 2023
2 parents 8fe7bae + 6fe03e7 commit a3a2dae
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Object copy(Object object) {

Method cloneMethod = getMethod(object, "clone");

if (!cloneMethod.isAccessible()) {
if (!cloneMethod.canAccess(object)) {
cloneMethod.setAccessible(true);
}

Expand Down
23 changes: 8 additions & 15 deletions impl/src/main/java/com/sun/faces/util/copier/CopierUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> keywords;

static {
Set<String> 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<String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class CopyCtorCopier implements Copier {
public Object copy(Object object) {

try {
Constructor<? extends Object> copyConstructor = object.getClass().getConstructor(object.getClass());
Constructor<?> copyConstructor = object.getClass().getConstructor(object.getClass());

return copyConstructor.newInstance(object);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*/
public class MultiStrategyCopier implements Copier {

private static final List<Copier> COPIERS = asList( // Note: copier instances used here must be thread-safe!
private static final List<Copier> COPIERS = List.of( // Note: copier instances used here must be thread-safe!
new SerializationCopier(), new CloneCopier(), new CopyCtorCopier(), new NewInstanceCopier());

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
Expand All @@ -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);
}
}
Expand Down

0 comments on commit a3a2dae

Please sign in to comment.