Skip to content

Commit

Permalink
Merge pull request #1076 from mrglavas/1075#reduce-code-duplication
Browse files Browse the repository at this point in the history
Reduce code duplication in JDTUtils.getFieldAccessors()
  • Loading branch information
mrglavas authored Nov 7, 2024
2 parents 001047d + efc840f commit c2b6c11
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016-2017 Red Hat, Inc.
* Copyright (c) 2016, 2024 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -28,11 +28,15 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class JDTUtils {
// Percent encoding obtained from: https://en.wikipedia.org/wiki/Percent-encoding#Reserved_characters
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)+)))*\\/?";

// Unmodifiable list of accessor prefixes
private static final List<String> ACCESSOR_PREFIXES = List.of("get", "set", "is");

/**
* Check if a URI starts with a leading slash.
*
Expand Down Expand Up @@ -66,11 +70,8 @@ public static boolean isValidLevel1URI(String uriString) {
public static List<PsiMethod> getFieldAccessors(PsiJavaFile unit, PsiField field) {
List<PsiMethod> accessors = new ArrayList<PsiMethod>();
String fieldName = field.getName();
fieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
List<String> accessorNames = new ArrayList<String>();
accessorNames.add("get" + fieldName);
accessorNames.add("set" + fieldName);
accessorNames.add("is" + fieldName);
String accessorSuffix = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
List<String> accessorNames = ACCESSOR_PREFIXES.stream().map(s -> s + accessorSuffix).collect(Collectors.toList());

for (PsiClass type : unit.getClasses()) {
for (PsiMethod method : type.getMethods()) {
Expand Down

0 comments on commit c2b6c11

Please sign in to comment.