Skip to content

Commit c2b6c11

Browse files
authored
Merge pull request #1076 from mrglavas/1075#reduce-code-duplication
Reduce code duplication in JDTUtils.getFieldAccessors()
2 parents 001047d + efc840f commit c2b6c11

File tree

1 file changed

+7
-6
lines changed
  • src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij

1 file changed

+7
-6
lines changed

src/main/java/io/openliberty/tools/intellij/lsp4jakarta/lsp4ij/JDTUtils.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2016-2017 Red Hat, Inc.
2+
* Copyright (c) 2016, 2024 Red Hat, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -28,11 +28,15 @@
2828
import java.util.Collections;
2929
import java.util.List;
3030
import java.util.Map;
31+
import java.util.stream.Collectors;
3132

3233
public class JDTUtils {
3334
// Percent encoding obtained from: https://en.wikipedia.org/wiki/Percent-encoding#Reserved_characters
3435
private static final String LEVEL1_URI_REGEX = "(?:\\/(?:(?:\\{(\\w|-|%20|%21|%23|%24|%25|%26|%27|%28|%29|%2A|%2B|%2C|%2F|%3A|%3B|%3D|%3F|%40|%5B|%5D)+\\})|(?:(\\w|%20|%21|%23|%24|%25|%26|%27|%28|%29|%2A|%2B|%2C|%2F|%3A|%3B|%3D|%3F|%40|%5B|%5D)+)))*\\/?";
3536

37+
// Unmodifiable list of accessor prefixes
38+
private static final List<String> ACCESSOR_PREFIXES = List.of("get", "set", "is");
39+
3640
/**
3741
* Check if a URI starts with a leading slash.
3842
*
@@ -66,11 +70,8 @@ public static boolean isValidLevel1URI(String uriString) {
6670
public static List<PsiMethod> getFieldAccessors(PsiJavaFile unit, PsiField field) {
6771
List<PsiMethod> accessors = new ArrayList<PsiMethod>();
6872
String fieldName = field.getName();
69-
fieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
70-
List<String> accessorNames = new ArrayList<String>();
71-
accessorNames.add("get" + fieldName);
72-
accessorNames.add("set" + fieldName);
73-
accessorNames.add("is" + fieldName);
73+
String accessorSuffix = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
74+
List<String> accessorNames = ACCESSOR_PREFIXES.stream().map(s -> s + accessorSuffix).collect(Collectors.toList());
7475

7576
for (PsiClass type : unit.getClasses()) {
7677
for (PsiMethod method : type.getMethods()) {

0 commit comments

Comments
 (0)