Skip to content

Commit

Permalink
WW-5411 Misc code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kusalk committed Jul 25, 2024
1 parent 4e4cf46 commit aec78dd
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serial;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
Expand All @@ -41,6 +42,7 @@
@Service
public class TestDataProvider implements Serializable, InitializingBean {

@Serial
private static final long serialVersionUID = 1L;
private static final Logger log = LogManager.getLogger(TestDataProvider.class);

Expand All @@ -64,9 +66,9 @@ public class TestDataProvider implements Serializable, InitializingBean {
};

public static final Employee[] TEST_EMPLOYEES = {
new Employee(new Long(1), "Alan", "Smithee", new Date(), new Float(2000f), true, POSITIONS[0],
new Employee(1L, "Alan", "Smithee", new Date(), 2000f, true, POSITIONS[0],
TEST_SKILLS[0], null, "alan", LEVELS[0], "Nice guy"),
new Employee(new Long(2), "Robert", "Robson", new Date(), new Float(10000f), false, POSITIONS[1],
new Employee(2L, "Robert", "Robson", new Date(), 10000f, false, POSITIONS[1],
TEST_SKILLS[1], Arrays.asList(TEST_SKILLS).subList(1, TEST_SKILLS.length), "rob", LEVELS[1], "Smart guy")
};

Expand All @@ -78,27 +80,27 @@ public class TestDataProvider implements Serializable, InitializingBean {

protected void addTestSkills() {
try {
for (int i = 0, j = TEST_SKILLS.length; i < j; i++) {
skillDao.merge(TEST_SKILLS[i]);
}
for (Skill testSkill : TEST_SKILLS) {
skillDao.merge(testSkill);
}
if (log.isInfoEnabled()) {
log.info("TestDataProvider - [addTestSkills]: Added test skill data.");
}
} catch (StorageException e) {
log.error("TestDataProvider - [addTestSkills]: Exception catched: " + e.getMessage());
log.error("TestDataProvider - [addTestSkills]: Exception caught: {}", e.getMessage());
}
}

protected void addTestEmployees() {
try {
for (int i = 0, j = TEST_EMPLOYEES.length; i < j; i++) {
employeeDao.merge(TEST_EMPLOYEES[i]);
}
for (Employee testEmployee : TEST_EMPLOYEES) {
employeeDao.merge(testEmployee);
}
if (log.isInfoEnabled()) {
log.info("TestDataProvider - [addTestEmployees]: Added test employee data.");
}
} catch (StorageException e) {
log.error("TestDataProvider - [addTestEmployees]: Exception catched: " + e.getMessage());
log.error("TestDataProvider - [addTestEmployees]: Exception caught: {}", e.getMessage());
}
}

Expand All @@ -107,6 +109,7 @@ protected void addTestData() {
addTestEmployees();
}

@Override
public void afterPropertiesSet() throws Exception {
addTestData();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,29 @@
*/
package org.apache.struts2.showcase.hangman;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Hangman implements Serializable {

@Serial
private static final long serialVersionUID = 8566954355839652509L;

private Vocab vocab;
private final Vocab vocab;

private Boolean win = false;

private int guessLeft = 5;
public List<Character> charactersAvailable;
public final List<Character> charactersAvailable;
public List<Character> charactersGuessed;

public Hangman(Vocab vocab) {
// Arrays.asList(...) returns List that doesn't support remove(), hence
// we wrap it with an ArrayList to avoid UnsupportedOperationException
// when doing a remove()
charactersAvailable = new ArrayList<Character>(Arrays.asList(
new Character[]{
Character.valueOf('A'), Character.valueOf('B'), Character.valueOf('C'),
Character.valueOf('D'), Character.valueOf('E'), Character.valueOf('F'),
Character.valueOf('G'), Character.valueOf('H'), Character.valueOf('I'),
Character.valueOf('J'), Character.valueOf('K'), Character.valueOf('L'),
Character.valueOf('M'), Character.valueOf('N'), Character.valueOf('O'),
Character.valueOf('P'), Character.valueOf('Q'), Character.valueOf('R'),
Character.valueOf('S'), Character.valueOf('T'), Character.valueOf('U'),
Character.valueOf('V'), Character.valueOf('W'), Character.valueOf('X'),
Character.valueOf('Y'), Character.valueOf('Z')
}));
charactersGuessed = new ArrayList<Character>();
charactersAvailable = new ArrayList<>(List.of(
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'));
charactersGuessed = new ArrayList<>();
this.vocab = vocab;
}

Expand Down Expand Up @@ -98,7 +87,7 @@ public Integer guessLeft() {

public List<Character> getCharactersAvailable() {
synchronized (charactersAvailable) {
return new ArrayList<Character>(charactersAvailable);
return new ArrayList<>(charactersAvailable);
//return charactersAvailable;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@
*/
package org.apache.struts2.showcase.hangman;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class Vocab implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

private String vocab;
private String hint;
private final String vocab;
private final String hint;
private Character[] characters; // character this vocab is made up of

public Vocab(String vocab, String hint) {
Expand All @@ -52,40 +54,29 @@ public String getHint() {
public Boolean containCharacter(Character character) {
assert (character != null);

return (vocab.contains(character.toString())) ? true : false;
return vocab.contains(character.toString());
}

public Character[] inCharacters() {
if (characters == null) {
char[] c = vocab.toCharArray();
characters = new Character[c.length];
for (int a = 0; a < c.length; a++) {
characters[a] = Character.valueOf(c[a]);
characters[a] = c[a];
}
}
return characters;
}

public boolean containsAllCharacter(List<Character> charactersGuessed) {
Character[] chars = inCharacters();
List<Character> tmpChars = Arrays.asList(chars);
return charactersGuessed.containsAll(tmpChars);
return new HashSet<>(charactersGuessed).containsAll(Arrays.asList(inCharacters()));
}

public static void main(String args[]) throws Exception {
public static void main(String[] args) throws Exception {
Vocab v = new Vocab("JAVA", "a java word");

List<Character> list1 = new ArrayList<Character>();
list1.add(new Character('J'));
list1.add(new Character('V'));

List<Character> list2 = new ArrayList<Character>();
list2.add(new Character('J'));
list2.add(new Character('V'));
list2.add(new Character('A'));

System.out.println(v.containsAllCharacter(list1));
System.out.println(v.containsAllCharacter(list2));
System.out.println(v.containsAllCharacter(List.of('J', 'V')));
System.out.println(v.containsAllCharacter(List.of('J', 'V', 'A')));

}
}
8 changes: 6 additions & 2 deletions core/src/main/java/com/opensymphony/xwork2/ActionSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
import org.apache.struts2.StrutsConstants;

import java.io.Serializable;
import java.util.*;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

/**
* Provides a default implementation for the most common actions.
Expand Down Expand Up @@ -160,7 +164,7 @@ public String getFormatted(String key, String expr) {
} else {
final ValueStack valueStack = ActionContext.getContext().getValueStack();
final Object val = valueStack.findValue(expr);
return getText(key, Arrays.asList(val));
return getText(key, List.of(val));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public String getText(String key, String defaultValue, List<?> args) {

return format.format(params);
}
return text;
return text;
}

public String getText(String key, String defaultValue, String[] args) {
Expand Down Expand Up @@ -136,7 +136,7 @@ public String getText(String key, String defaultValue, List<?> args, ValueStack

public String getText(String key, String defaultValue, String[] args, ValueStack stack) {
//we're not using the value stack here
List<Object> values = new ArrayList<Object>(Arrays.asList(args));
List<Object> values = new ArrayList<>(Arrays.asList(args));
return getText(key, defaultValue, values);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void injectStatics(List<Class<?>> staticInjections) {
}

void addInjectorsForMethods(Method[] methods, boolean statics, List<Injector> injectors) {
addInjectorsForMembers(Arrays.asList(methods), statics, injectors, MethodInjector::new);
addInjectorsForMembers(List.of(methods), statics, injectors, MethodInjector::new);
}

void addInjectorsForFields(Field[] fields, boolean statics, List<Injector> injectors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.StrutsException;

import java.io.Serial;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -61,6 +62,7 @@ public class OgnlValueStack implements Serializable, ValueStack, ClearableValueS

private static final Logger LOG = LogManager.getLogger(OgnlValueStack.class);

@Serial
private static final long serialVersionUID = 370737852934925530L;

private static final String MAP_IDENTIFIER_KEY = "com.opensymphony.xwork2.util.OgnlValueStack.MAP_IDENTIFIER_KEY";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

import static com.opensymphony.xwork2.util.ConfigParseUtil.toClassObjectsSet;
import static com.opensymphony.xwork2.util.ConfigParseUtil.toClassesSet;
Expand All @@ -49,8 +48,6 @@
import static com.opensymphony.xwork2.util.ConfigParseUtil.toPackageNamesSet;
import static java.text.MessageFormat.format;
import static java.util.Collections.emptySet;
import static java.util.Collections.singletonList;
import static java.util.Collections.unmodifiableSet;
import static org.apache.struts2.StrutsConstants.STRUTS_ALLOWLIST_CLASSES;
import static org.apache.struts2.StrutsConstants.STRUTS_ALLOWLIST_PACKAGE_NAMES;

Expand All @@ -62,20 +59,20 @@ public class SecurityMemberAccess implements MemberAccess {

private static final Logger LOG = LogManager.getLogger(SecurityMemberAccess.class);

private static final Set<String> ALLOWLIST_REQUIRED_PACKAGES = unmodifiableSet(new HashSet<>(Arrays.asList(
private static final Set<String> ALLOWLIST_REQUIRED_PACKAGES = Set.of(
"com.opensymphony.xwork2.validator.validators",
"org.apache.struts2.components",
"org.apache.struts2.views.jsp"
)));
);

private static final Set<Class<?>> ALLOWLIST_REQUIRED_CLASSES = unmodifiableSet(new HashSet<>(Arrays.asList(
private static final Set<Class<?>> ALLOWLIST_REQUIRED_CLASSES = Set.of(
java.lang.Enum.class,
java.lang.String.class,
java.util.Date.class,
java.util.HashMap.class,
java.util.Map.class,
java.util.Map.Entry.class
)));
);

private final ProviderAllowlist providerAllowlist;
private final ThreadAllowlist threadAllowlist;
Expand All @@ -85,15 +82,15 @@ public class SecurityMemberAccess implements MemberAccess {
private Set<Pattern> excludeProperties = emptySet();
private Set<Pattern> acceptProperties = emptySet();

private Set<String> excludedClasses = unmodifiableSet(new HashSet<>(singletonList(Object.class.getName())));
private Set<String> excludedClasses = Set.of(Object.class.getName());
private Set<Pattern> excludedPackageNamePatterns = emptySet();
private Set<String> excludedPackageNames = emptySet();
private Set<String> excludedPackageExemptClasses = emptySet();

private static volatile boolean isDevModeLogged = false;
private volatile boolean isDevModeInit;
private boolean isDevMode;
private Set<String> devModeExcludedClasses = unmodifiableSet(new HashSet<>(singletonList(Object.class.getName())));
private Set<String> devModeExcludedClasses = Set.of(Object.class.getName());
private Set<Pattern> devModeExcludedPackageNamePatterns = emptySet();
private Set<String> devModeExcludedPackageNames = emptySet();
private Set<String> devModeExcludedPackageExemptClasses = emptySet();
Expand Down Expand Up @@ -395,14 +392,10 @@ protected boolean isExcludedPackageNames(Class<?> clazz) {
}

public static boolean isClassBelongsToPackages(Class<?> clazz, Set<String> matchingPackages) {
List<String> packageParts = Arrays.asList(toPackageName(clazz).split("\\."));
for (int i = 0; i < packageParts.size(); i++) {
String parentPackage = String.join(".", packageParts.subList(0, i + 1));
if (matchingPackages.contains(parentPackage)) {
return true;
}
}
return false;
List<String> packageParts = List.of(toPackageName(clazz).split("\\."));
return IntStream.range(0, packageParts.size())
.mapToObj(i -> String.join(".", packageParts.subList(0, i + 1)))
.anyMatch(matchingPackages::contains);
}

protected boolean isClassExcluded(Class<?> clazz) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -158,7 +163,7 @@ public boolean internal() {
}

public Collection<? extends URL> getAllPhysicalUrls(URL url) throws IOException {
return Arrays.asList(url);
return List.of(url);
}

}
Loading

0 comments on commit aec78dd

Please sign in to comment.