Skip to content

Commit

Permalink
Comments are addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
solomax committed Aug 15, 2024
1 parent 2a12a19 commit 37773f1
Showing 1 changed file with 7 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,35 @@
*/
package org.apache.openjpa.util;

import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

public class BlacklistClassResolver {
private static final String MATCH_ANY = "*";
private static final Set<String> PRIMITIVES = Set.of(
boolean.class.getName(),
byte.class.getName(),
char.class.getName(),
double.class.getName(),
float.class.getName(),
int.class.getName(),
long.class.getName(),
short.class.getName()
);
private static final List<String> PRIMITIVE_ARRAY = List.of(
(new boolean[0]).getClass().getName(),
(new byte[0]).getClass().getName(),
(new char[0]).getClass().getName(),
(new double[0]).getClass().getName(),
(new float[0]).getClass().getName(),
(new int[0]).getClass().getName(),
(new long[0]).getClass().getName(),
(new short[0]).getClass().getName()
);

public static final BlacklistClassResolver DEFAULT = new BlacklistClassResolver(
toArray(System.getProperty(
"openjpa.serialization.class.blacklist",
"org.codehaus.groovy.runtime.,org.apache.commons.collections4.functors.,org.apache.xalan")),
toArray(System.getProperty("openjpa.serialization.class.whitelist")));

private final boolean allWhite;
private final boolean allBlack;
private final String[] blacklist;
private final String[] whitelist;

protected BlacklistClassResolver(final String[] blacklist, final String[] whitelist) {
allWhite = Stream.of(whitelist).anyMatch(white -> MATCH_ANY.equals(white));
allBlack = Stream.of(blacklist).anyMatch(black -> MATCH_ANY.equals(black));

this.whitelist = whitelist;
this.blacklist = blacklist;
}

protected boolean isBlacklisted(final String name) {
if (contains(whitelist, name) || isPrimitive(name)) {
if (allWhite || contains(whitelist, name)) {
return false;
}
return contains(blacklist, name);
return allBlack || contains(blacklist, name);
}

public final String check(final String name) {
Expand All @@ -73,18 +56,6 @@ public final String check(final String name) {
return name;
}

private static boolean isPrimitive(final String name) {
if (PRIMITIVES.contains(name)) {
return true;
}
for (String arr : PRIMITIVE_ARRAY) {
if (name.endsWith(arr)) { // array can be [[[[B for ex.
return true;
}
}
return false;
}

private static String[] toArray(final String property) {
return property == null
? new String[] {}
Expand All @@ -95,9 +66,6 @@ private static String[] toArray(final String property) {

private static boolean contains(final String[] list, String name) {
for (final String white : list) {
if (MATCH_ANY.equals(white)) {
return true;
}
if (name.startsWith(white)) {
return true;
}
Expand Down

0 comments on commit 37773f1

Please sign in to comment.