Skip to content

Commit dee9858

Browse files
committed
Refactor null checks to use Objects.requireNonNull
- Replaced instances of SpecsCheck.checkNotNull with Objects.requireNonNull in various classes to standardize null checks. - Updated methods in AsmFieldData, DeployUtils, EsprimaComment, EsprimaNode, SpecsCollections, SpecsIo, SpecsStrings, and several others. - Improved code readability and consistency by utilizing Java's built-in null-checking mechanism.
1 parent 0eef3ad commit dee9858

34 files changed

Lines changed: 83 additions & 90 deletions

File tree

AsmParser/src/pt/up/fe/specs/binarytranslation/asm/parsing/AsmFieldData.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515

1616
import java.util.LinkedHashMap;
1717
import java.util.Map;
18+
import java.util.Objects;
1819

1920
import org.suikasoft.jOptions.DataStore.ADataClass;
2021
import org.suikasoft.jOptions.Datakey.DataKey;
2122
import org.suikasoft.jOptions.Datakey.KeyFactory;
2223

23-
import pt.up.fe.specs.util.SpecsCheck;
24-
2524
/**
2625
* Raw field data as extracted by an {@link IsaParser}
2726
*
@@ -98,7 +97,7 @@ public int getReducedOpcode() {
9897

9998
public int getFieldAsBinaryInteger(String fieldName) {
10099
var valueString = get(AsmFieldData.FIELDS).get(fieldName);
101-
SpecsCheck.checkNotNull(valueString, () -> "No value found for field " + fieldName);
100+
Objects.requireNonNull(valueString, () -> "No value found for field " + fieldName);
102101
return Integer.parseInt(valueString, 2);
103102
}
104103

EclipseUtils/src/pt/up/fe/specs/eclipse/Utilities/DeployUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Collection;
1919
import java.util.HashSet;
2020
import java.util.List;
21+
import java.util.Objects;
2122
import java.util.Set;
2223
import java.util.stream.Collectors;
2324

@@ -33,7 +34,6 @@
3334
import pt.up.fe.specs.eclipse.Classpath.Dependency;
3435
import pt.up.fe.specs.eclipse.builder.BuildResource;
3536
import pt.up.fe.specs.eclipse.builder.BuildUtils;
36-
import pt.up.fe.specs.util.SpecsCheck;
3737
import pt.up.fe.specs.util.SpecsIo;
3838
import pt.up.fe.specs.util.SpecsLogs;
3939
import pt.up.fe.specs.util.SpecsStrings;
@@ -636,13 +636,13 @@ public static String buildMavenRepoPom(EclipseDeploymentData data, ClasspathPars
636636
String groupId = data.pomInfo.get(() -> "groupId");
637637
String artifactId = data.pomInfo.get(() -> "artifactId");
638638

639-
SpecsCheck.checkNotNull(data.version,
639+
Objects.requireNonNull(data.version,
640640
() -> "No version supplied, use for instance %BUILD% in name of output JAR");
641641

642642
Set<License> licenses = parser.getLicenses(data.projetName);
643643
String licensesXml = licenses.stream().map(License::getXmlInfo).collect(Collectors.joining("\n"));
644644

645-
SpecsCheck.checkNotNull(data.developersXml,
645+
Objects.requireNonNull(data.developersXml,
646646
() -> "No developers XML file supplied");
647647
String developers = SpecsIo.read(data.developersXml);
648648

JsEngine/src/pt/up/fe/specs/jsengine/libs/EsprimaComment.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515

1616
import java.util.HashMap;
1717
import java.util.Map;
18+
import java.util.Objects;
1819

1920
import org.apache.commons.lang3.NotImplementedException;
2021

21-
import pt.up.fe.specs.util.SpecsCheck;
22-
2322
/**
2423
* Represents a comment node in an Esprima AST.
2524
*/
@@ -94,7 +93,7 @@ public String getContents() {
9493
*/
9594
public String getType() {
9695
var type = (String) comment.get("type");
97-
SpecsCheck.checkNotNull(type, () -> "Comment should have type");
96+
Objects.requireNonNull(type, () -> "Comment should have type");
9897
return type;
9998
}
10099

JsEngine/src/pt/up/fe/specs/jsengine/libs/EsprimaNode.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
import java.util.HashSet;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.Objects;
2223
import java.util.Set;
2324
import java.util.stream.Collectors;
2425
import java.util.stream.Stream;
2526

26-
import pt.up.fe.specs.util.SpecsCheck;
27-
2827
/**
2928
* Represents a node in an Esprima AST.
3029
*/
@@ -245,7 +244,7 @@ public List<EsprimaNode> getAsNodes(String key) {
245244
*/
246245
private <T> T getExistingValue(String key, Class<T> valueClass) {
247246
var value = node.get(key);
248-
SpecsCheck.checkNotNull(value, () -> "Expected value with key '" + key + "' to exist");
247+
Objects.requireNonNull(value, () -> "Expected value with key '" + key + "' to exist");
249248
return valueClass.cast(value);
250249
}
251250

@@ -268,7 +267,7 @@ public String toString() {
268267
public EsprimaLoc getLoc() {
269268
@SuppressWarnings("unchecked")
270269
var loc = (Map<String, Object>) node.get("loc");
271-
SpecsCheck.checkNotNull(loc, () -> "Loc is null");
270+
Objects.requireNonNull(loc, () -> "Loc is null");
272271
return EsprimaLoc.newInstance(loc);
273272
}
274273

SpecsUtils/src/pt/up/fe/specs/util/SpecsCollections.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ public static <T> void addOptional(Collection<T> includes, Optional<T> element)
630630
* Returns the first non-empty element of the stream.
631631
*/
632632
public static <T> Optional<T> findFirstNonEmpty(Stream<Optional<T>> stream) {
633-
Preconditions.checkArgument(stream != null, "stream must not be null");
633+
Objects.requireNonNull(stream, () -> "stream must not be null");
634634

635635
final Iterator<Optional<T>> iterator = stream.iterator();
636636

SpecsUtils/src/pt/up/fe/specs/util/SpecsIo.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.util.LinkedHashMap;
5656
import java.util.List;
5757
import java.util.Map;
58+
import java.util.Objects;
5859
import java.util.Optional;
5960
import java.util.Set;
6061
import java.util.UUID;
@@ -1224,7 +1225,7 @@ public static File resourceCopy(String resource) {
12241225
public static <T extends Enum<T> & ResourceProvider> void resourceCopy(Class<T> resources, File destinationFolder,
12251226
boolean useResourcePath) {
12261227

1227-
Preconditions.checkArgument(destinationFolder != null, "destinationFolder must not be null");
1228+
Objects.requireNonNull(destinationFolder, () -> "destinationFolder must not be null");
12281229

12291230
if (resources == null) {
12301231
throw new RuntimeException("resources must not be null");
@@ -1334,8 +1335,8 @@ public static ResourceCopyData resourceCopyVersioned(ResourceProvider resource,
13341335
public static File resourceCopy(String resource, File destinationFolder, boolean useResourcePath,
13351336
boolean overwrite) {
13361337

1337-
Preconditions.checkArgument(resource != null, "resource must not be null");
1338-
Preconditions.checkArgument(destinationFolder != null, "destinationFolder must not be null");
1338+
Objects.requireNonNull(resource, () -> "resource must not be null");
1339+
Objects.requireNonNull(destinationFolder, () -> "destinationFolder must not be null");
13391340

13401341
// Disabled option, is not good idea not to overwrite
13411342
// overwrite = true;

SpecsUtils/src/pt/up/fe/specs/util/SpecsStrings.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.List;
3131
import java.util.Locale;
3232
import java.util.Map;
33+
import java.util.Objects;
3334
import java.util.Optional;
3435
import java.util.StringJoiner;
3536
import java.util.concurrent.TimeUnit;
@@ -1220,7 +1221,7 @@ public static String packageNameToResource(String packageName) {
12201221
}
12211222

12221223
public static int parseIntegerRelaxed(String constant) {
1223-
Preconditions.checkArgument(constant != null);
1224+
Objects.requireNonNull(constant);
12241225

12251226
double doubleConstant = Double.parseDouble(constant);
12261227

@@ -1470,7 +1471,7 @@ public static String escapeJson(String string) {
14701471

14711472
public static String escapeJson(String string, boolean ignoreNewlines) {
14721473

1473-
SpecsCheck.checkNotNull(string, () -> "Cannot escape a null string");
1474+
Objects.requireNonNull(string, () -> "Cannot escape a null string");
14741475

14751476
StringBuilder escapedString = new StringBuilder();
14761477

@@ -1607,7 +1608,7 @@ public static String normalizeFileContents(String fileContents) {
16071608
* @return The parsed integer, or empty if the string is not an integer.
16081609
*/
16091610
public static Optional<Integer> tryGetDecimalInteger(String value) {
1610-
Preconditions.checkArgument(value != null, "value must not be null");
1611+
Objects.requireNonNull(value, () -> "value must not be null");
16111612

16121613
if (INTEGER_PATTERN.matcher(value).matches()) {
16131614
try {

SpecsUtils/src/pt/up/fe/specs/util/classmap/BiConsumerClassMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
import java.util.HashMap;
1717
import java.util.Map;
18+
import java.util.Objects;
1819
import java.util.function.BiConsumer;
1920

20-
import pt.up.fe.specs.util.SpecsCheck;
2121
import pt.up.fe.specs.util.exceptions.NotImplementedException;
2222
import pt.up.fe.specs.util.utilities.ClassMapper;
2323

@@ -80,7 +80,7 @@ private <TK extends T> BiConsumer<T, U> get(Class<TK> key) {
8080

8181
var function = this.map.get(mappedClass.get());
8282

83-
SpecsCheck.checkNotNull(function, () -> "There should be a mapping for " + mappedClass.get() + ", verify");
83+
Objects.requireNonNull(function, () -> "There should be a mapping for " + mappedClass.get() + ", verify");
8484

8585
return (BiConsumer<T, U>) function;
8686
}

SpecsUtils/src/pt/up/fe/specs/util/classmap/BiFunctionClassMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
import java.util.HashMap;
1717
import java.util.Map;
18+
import java.util.Objects;
1819
import java.util.function.BiFunction;
1920

20-
import pt.up.fe.specs.util.SpecsCheck;
2121
import pt.up.fe.specs.util.exceptions.NotImplementedException;
2222
import pt.up.fe.specs.util.utilities.ClassMapper;
2323

@@ -71,7 +71,7 @@ private <TK extends T> BiFunction<T, U, R> get(Class<TK> key) {
7171

7272
var function = this.map.get(mappedClass.get());
7373

74-
SpecsCheck.checkNotNull(function, () -> "There should be a mapping for " + mappedClass.get() + ", verify");
74+
Objects.requireNonNull(function, () -> "There should be a mapping for " + mappedClass.get() + ", verify");
7575

7676
return (BiFunction<T, U, R>) function;
7777
}

SpecsUtils/src/pt/up/fe/specs/util/classmap/ConsumerClassMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
package pt.up.fe.specs.util.classmap;
1515

16-
import pt.up.fe.specs.util.SpecsCheck;
1716
import pt.up.fe.specs.util.exceptions.NotImplementedException;
1817
import pt.up.fe.specs.util.utilities.ClassMapper;
1918

2019
import java.util.HashMap;
2120
import java.util.Map;
21+
import java.util.Objects;
2222
import java.util.function.Consumer;
2323

2424
/**
@@ -78,7 +78,7 @@ private <TK extends T> Consumer<T> get(Class<TK> key) {
7878

7979
var function = this.map.get(mappedClass.get());
8080

81-
SpecsCheck.checkNotNull(function, () -> "There should be a mapping for " + mappedClass.get() + ", verify");
81+
Objects.requireNonNull(function, () -> "There should be a mapping for " + mappedClass.get() + ", verify");
8282

8383
return (Consumer<T>) function;
8484
}

0 commit comments

Comments
 (0)