Skip to content

Commit

Permalink
Issue checkstyle#14625: fix inspection violations OptionalGetWithoutI…
Browse files Browse the repository at this point in the history
…sPresent
  • Loading branch information
MANISH-K-07 committed Apr 24, 2024
1 parent d6d4a5d commit 8230832
Show file tree
Hide file tree
Showing 18 changed files with 42 additions and 136 deletions.
24 changes: 3 additions & 21 deletions src/test/java/com/puppycrawl/tools/checkstyle/JavaParserTest.java
Expand Up @@ -57,12 +57,6 @@ public void testNullRootWithComments() {
.isNull();
}

/**
* Temporary java doc.
*
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
public void testAppendHiddenBlockCommentNodes() throws Exception {
final DetailAST root =
Expand All @@ -76,7 +70,7 @@ public void testAppendHiddenBlockCommentNodes() throws Exception {
.that(blockComment.isPresent())
.isTrue();

final DetailAST comment = blockComment.get();
final DetailAST comment = blockComment.orElseThrow();

assertWithMessage("Unexpected line number")
.that(comment.getLineNo())
Expand Down Expand Up @@ -145,12 +139,6 @@ public void testAppendHiddenSingleLineCommentNodes() throws Exception {
.startsWith(" inline comment");
}

/**
* Temporary java doc.
*
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
public void testAppendHiddenSingleLineCommentNodes2() throws Exception {
final DetailAST root =
Expand All @@ -163,7 +151,7 @@ public void testAppendHiddenSingleLineCommentNodes2() throws Exception {
.that(singleLineComment.isPresent())
.isTrue();

final DetailAST comment = singleLineComment.get();
final DetailAST comment = singleLineComment.orElseThrow();

assertWithMessage("Unexpected line number")
.that(comment.getLineNo())
Expand Down Expand Up @@ -242,12 +230,6 @@ public void testComments() throws Exception {
.isEqualTo(Arrays.asList("5,4", "8,0"));
}

/**
* Temporary java doc.
*
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
public void testJava14TextBlocks() throws Exception {
final DetailAST root =
Expand All @@ -262,7 +244,7 @@ public void testJava14TextBlocks() throws Exception {
.that(textBlockContent.isPresent())
.isTrue();

final DetailAST content = textBlockContent.get();
final DetailAST content = textBlockContent.orElseThrow();
final String expectedContents = "\n string";

assertWithMessage("Unexpected line number")
Expand Down
Expand Up @@ -858,12 +858,6 @@ public void testLoggedTime() throws IOException {
assertLoggedTime(loggedMessages, testingTime, "To process the files");
}

/**
* Temporary java doc.
*
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
private static void assertLoggedTime(List<MessageLevelPair> loggedMessages,
long testingTime, String expectedMsg) {

Expand All @@ -875,7 +869,7 @@ private static void assertLoggedTime(List<MessageLevelPair> loggedMessages,
.that(optionalMessageLevelPair.isPresent())
.isTrue();

final long actualTime = getNumberFromLine(optionalMessageLevelPair.get().getMsg());
final long actualTime = getNumberFromLine(optionalMessageLevelPair.orElseThrow().getMsg());

assertWithMessage("Logged time in '" + expectedMsg + "' "
+ "must be less than the testing time")
Expand Down
Expand Up @@ -406,12 +406,6 @@ public void testSuppressWarningsAsAnnotationProperty() throws Exception {
verifyWithInlineConfigParser(getPath("InputSuppressWarningsHolder7.java"), expected);
}

/**
* Temporary java doc.
*
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
@SuppressWarnings("unchecked")
public void testClearState() throws Exception {
Expand All @@ -427,8 +421,8 @@ public void testClearState() throws Exception {
.that(annotationDef.isPresent())
.isTrue();
assertWithMessage("State is not cleared on beginTree")
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, annotationDef.get(),
"ENTRIES",
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check,
annotationDef.orElseThrow(), "ENTRIES",
entries -> ((ThreadLocal<List<Object>>) entries).get().isEmpty()))
.isTrue();
}
Expand Down
Expand Up @@ -500,8 +500,6 @@ public void testHiddenFieldRecordsImplicitlyStaticClassComparison() throws Excep
* state of the field will be cleared.
*
* @throws Exception when code tested throws exception
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
public void testClearState() throws Exception {
Expand All @@ -516,8 +514,8 @@ public void testClearState() throws Exception {
.isTrue();
assertWithMessage("State is not cleared on beginTree")
.that(
TestUtil.isStatefulFieldClearedDuringBeginTree(check, classDef.get(), "frame",
new CheckIfStatefulFieldCleared()))
TestUtil.isStatefulFieldClearedDuringBeginTree(check, classDef.orElseThrow(),
"frame", new CheckIfStatefulFieldCleared()))
.isTrue();
}

Expand Down
Expand Up @@ -166,8 +166,6 @@ public void testImproperToken() {
* state of the field will be cleared.
*
* @throws Exception when code tested throws exception
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
@SuppressWarnings("unchecked")
Expand All @@ -183,9 +181,8 @@ public void testClearStateClassNames() throws Exception {
.that(classDef.isPresent())
.isTrue();
assertWithMessage("State is not cleared on beginTree")
.that(
TestUtil.isStatefulFieldClearedDuringBeginTree(check, classDef.get(), "classNames",
classNames -> ((Collection<String>) classNames).isEmpty()))
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, classDef.orElseThrow(),
"classNames", classNames -> ((Collection<String>) classNames).isEmpty()))
.isTrue();
}

Expand All @@ -195,8 +192,6 @@ public void testClearStateClassNames() throws Exception {
* state of the field will be cleared.
*
* @throws Exception when code tested throws exception
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
public void testClearStateImports() throws Exception {
Expand All @@ -211,9 +206,8 @@ public void testClearStateImports() throws Exception {
.that(importDef.isPresent())
.isTrue();
assertWithMessage("State is not cleared on beginTree")
.that(
TestUtil.isStatefulFieldClearedDuringBeginTree(check, importDef.get(), "imports",
imports -> ((Collection<?>) imports).isEmpty()))
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, importDef.orElseThrow(),
"imports", imports -> ((Collection<?>) imports).isEmpty()))
.isTrue();
}

Expand All @@ -223,8 +217,6 @@ public void testClearStateImports() throws Exception {
* state of the field will be cleared.
*
* @throws Exception when code tested throws exception
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
@SuppressWarnings("unchecked")
Expand All @@ -240,8 +232,8 @@ public void testClearStateInstantiations() throws Exception {
.that(literalNew.isPresent())
.isTrue();
assertWithMessage("State is not cleared on beginTree")
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, literalNew.get(),
"instantiations",
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check,
literalNew.orElseThrow(), "instantiations",
instantiations -> ((Collection<DetailAST>) instantiations).isEmpty()))
.isTrue();
}
Expand Down
Expand Up @@ -178,8 +178,6 @@ public void testRecordDecompositionInEnhancedForLoop() throws Exception {
* state of the field will be cleared.
*
* @throws Exception when code tested throws exception
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
@SuppressWarnings("unchecked")
Expand All @@ -195,8 +193,8 @@ public void testClearState() throws Exception {
.that(methodDef.isPresent())
.isTrue();
assertWithMessage("State is not cleared on beginTree")
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, methodDef.get(),
"variableStack",
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check,
methodDef.orElseThrow(), "variableStack",
variableStack -> ((Collection<Set<String>>) variableStack).isEmpty()))
.isTrue();
}
Expand Down
Expand Up @@ -103,8 +103,6 @@ public void testTokensNotNull() {
* state of the field will be cleared.
*
* @throws Exception when code tested throws exception
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
@SuppressWarnings("unchecked")
Expand All @@ -119,7 +117,7 @@ public void testClearState() throws Exception {
.that(methodDef.isPresent())
.isTrue();
assertWithMessage("State is not cleared on beginTree")
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, methodDef.get(),
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, methodDef.orElseThrow(),
"parameterNamesStack",
parameterNamesStack -> ((Collection<Set<String>>) parameterNamesStack).isEmpty()))
.isTrue();
Expand Down
Expand Up @@ -562,8 +562,6 @@ public void testUnusedMethodFor() throws Exception {
* state of the field will be cleared.
*
* @throws Exception when code tested throws exception
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
public void testClearState() throws Exception {
Expand All @@ -578,7 +576,7 @@ public void testClearState() throws Exception {
.that(classDef.isPresent())
.isTrue();
assertWithMessage("State is not cleared on beginTree")
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, classDef.get(),
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, classDef.orElseThrow(),
"current", current -> ((Collection<?>) current).isEmpty()))
.isTrue();
}
Expand Down
Expand Up @@ -155,8 +155,6 @@ public void testMaxForVoid() throws Exception {
* state of the field will be cleared.
*
* @throws Exception when code tested throws exception
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
@SuppressWarnings("unchecked")
Expand All @@ -171,8 +169,8 @@ public void testClearState() throws Exception {
.that(methodDef.isPresent())
.isTrue();
assertWithMessage("State is not cleared on beginTree")
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, methodDef.get(),
"contextStack",
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check,
methodDef.orElseThrow(), "contextStack",
contextStack -> ((Collection<Set<String>>) contextStack).isEmpty()))
.isTrue();
}
Expand Down
Expand Up @@ -92,8 +92,6 @@ public void testTokensNotNull() {
* state of the field will be cleared.
*
* @throws Exception when code tested throws exception
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
@SuppressWarnings("unchecked")
Expand All @@ -108,8 +106,8 @@ public void testClearState() throws Exception {
.that(methodDef.isPresent())
.isTrue();
assertWithMessage("State is not cleared on beginTree")
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, methodDef.get(),
"methodStack",
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check,
methodDef.orElseThrow(), "methodStack",
methodStack -> ((Collection<Set<String>>) methodStack).isEmpty()))
.isTrue();
}
Expand Down
Expand Up @@ -360,12 +360,6 @@ public void testUnusedLocalVariableTernaryAndExpressions() throws Exception {
expected);
}

/**
* Temporary java doc.
*
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
public void testClearStateVariables() throws Exception {
final UnusedLocalVariableCheck check = new UnusedLocalVariableCheck();
Expand All @@ -377,7 +371,7 @@ public void testClearStateVariables() throws Exception {
assertWithMessage("Ast should contain METHOD_DEF")
.that(methodDef.isPresent())
.isTrue();
final DetailAST variableDef = methodDef.get().getLastChild()
final DetailAST variableDef = methodDef.orElseThrow().getLastChild()
.findFirstToken(TokenTypes.VARIABLE_DEF);
assertWithMessage("State is not cleared on beginTree")
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, variableDef,
Expand All @@ -388,12 +382,6 @@ public void testClearStateVariables() throws Exception {
.isTrue();
}

/**
* Temporary java doc.
*
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
public void testClearStateClasses() throws Exception {
final UnusedLocalVariableCheck check = new UnusedLocalVariableCheck();
Expand All @@ -405,7 +393,7 @@ public void testClearStateClasses() throws Exception {
assertWithMessage("Ast should contain CLASS_DEF")
.that(classDef.isPresent())
.isTrue();
final DetailAST classDefToken = classDef.get();
final DetailAST classDefToken = classDef.orElseThrow();
assertWithMessage("State is not cleared on beginTree")
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, classDefToken,
"typeDeclarations",
Expand All @@ -429,12 +417,6 @@ public void testClearStateClasses() throws Exception {
.isTrue();
}

/**
* Temporary java doc.
*
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
public void testClearStateAnonInnerClass() throws Exception {
final UnusedLocalVariableCheck check = new UnusedLocalVariableCheck();
Expand All @@ -451,7 +433,7 @@ public void testClearStateAnonInnerClass() throws Exception {
.isTrue();
check.beginTree(root);
check.visitToken(classDefAst);
check.visitToken(literalNew.get());
check.visitToken(literalNew.orElseThrow());
check.beginTree(null);
final Predicate<Object> isClear = anonInnerAstToTypeDesc -> {
return ((Map<?, ?>) anonInnerAstToTypeDesc).isEmpty();
Expand All @@ -469,12 +451,6 @@ public void testClearStateAnonInnerClass() throws Exception {
.isTrue();
}

/**
* Temporary java doc.
*
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
public void testClearStatePackageDef() throws Exception {
final UnusedLocalVariableCheck check = new UnusedLocalVariableCheck();
Expand All @@ -486,7 +462,7 @@ public void testClearStatePackageDef() throws Exception {
assertWithMessage("Ast should contain PACKAGE_DEF")
.that(packageDef.isPresent())
.isTrue();
final DetailAST packageDefToken = packageDef.get();
final DetailAST packageDefToken = packageDef.orElseThrow();
assertWithMessage("State is not cleared on beginTree")
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, packageDefToken,
"packageName",
Expand Down
Expand Up @@ -256,8 +256,6 @@ public void testFinalClassNestedInRecord() throws Exception {
* state of the field will be cleared.
*
* @throws Exception when code tested throws exception
* @noinspection OptionalGetWithoutIsPresent
* @noinspectionreason OptionalGetWithoutIsPresent - until issue #14625
*/
@Test
public void testClearState() throws Exception {
Expand All @@ -271,8 +269,9 @@ public void testClearState() throws Exception {
.that(packageDef.isPresent())
.isTrue();
assertWithMessage("State is not cleared on beginTree")
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, packageDef.get(),
"packageName", packageName -> ((String) packageName).isEmpty()))
.that(TestUtil.isStatefulFieldClearedDuringBeginTree(check,
packageDef.orElseThrow(), "packageName",
packageName -> ((String) packageName).isEmpty()))
.isTrue();
}

Expand Down

0 comments on commit 8230832

Please sign in to comment.