Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,12 @@ private Replacements junitReplacement(
", %s".formatted(contentFor(argument))
).orElse("");

return isTryBlock ?
new Replacements(
"assertThrows(%s, () -> ".formatted(typeClass(firstCaughtTypeInTry(tryStatement))),
"%s);".formatted(argumentsSuffix)
) :
new Replacements(
"assertDoesNotThrow(() -> ",
"%s);".formatted(argumentsSuffix)
);
return new Replacements(
isTryBlock
? "assertThrows(%s, () -> ".formatted(typeClass(firstCaughtTypeInTry(tryStatement)))
: "assertDoesNotThrow(() -> ",
"%s);".formatted(argumentsSuffix)
);
}

private Replacements assertJReplacement(
Expand All @@ -188,15 +185,12 @@ private Replacements assertJReplacement(
var failureMessagePart = failArguments.isEmpty() ?
"" :
".withFailMessage(%s)".formatted(contentFor(failArguments.get(0)));
return isTryBlock ?
new Replacements(
"assertThatCode(() -> ",
")%s.isInstanceOf(%s);".formatted(failureMessagePart, typeClass(firstCaughtTypeInTry(tryStatement)))
) :
new Replacements(
"assertThatCode(() -> ",
")%s.doesNotThrowAnyException();".formatted(failureMessagePart)
);
return new Replacements(
"assertThatCode(() -> ",
isTryBlock
? ")%s.isInstanceOf(%s);".formatted(failureMessagePart, typeClass(firstCaughtTypeInTry(tryStatement)))
: ")%s.doesNotThrowAnyException();".formatted(failureMessagePart)
);
}

private String contentFor(Tree tree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void visitNode(Tree tree) {

private void reportIssueIfHardCoded(MethodInvocationTree mit, String argName) {
Arguments arguments = mit.arguments();
ExpressionTree passwordArg = arguments.size() == 1 ? arguments.get(0) : arguments.get(1);
ExpressionTree passwordArg = arguments.get(arguments.size() == 1 ? 0 : 1);
reportIssueIfHardCoded(passwordArg, argName);
}

Expand Down
16 changes: 8 additions & 8 deletions java-frontend/src/main/java/org/sonar/java/model/JParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -976,10 +976,11 @@ private EnumConstantTreeImpl processEnumConstantDeclaration(EnumConstantDeclarat
final InternalSyntaxToken closeParToken;
if (tokenManager.get(openParTokenIndex).tokenType == TerminalToken.TokenNameLPAREN) {
openParToken = createSyntaxToken(openParTokenIndex);
ASTNode closeParAnchor = e.arguments().isEmpty()
? e.getName()
: (ASTNode) e.arguments().get(e.arguments().size() - 1);
closeParToken = firstTokenAfter(closeParAnchor, TerminalToken.TokenNameRPAREN);
closeParToken = firstTokenAfter(
e.arguments().isEmpty()
? e.getName()
: (ASTNode) e.arguments().get(e.arguments().size() - 1),
TerminalToken.TokenNameRPAREN);
} else {
openParToken = null;
closeParToken = null;
Expand Down Expand Up @@ -2738,11 +2739,10 @@ private JavaTree.WildcardTreeImpl convertWildcardType(WildcardType e) {
if (bound == null) {
t = new JavaTree.WildcardTreeImpl(questionToken);
} else {
Tree.Kind wildcardKind = e.isUpperBound() ? Tree.Kind.EXTENDS_WILDCARD : Tree.Kind.SUPER_WILDCARD;
TerminalToken boundTokenType = e.isUpperBound() ? TerminalToken.TokenNameextends : TerminalToken.TokenNamesuper;
boolean isUpperBound = e.isUpperBound();
t = new JavaTree.WildcardTreeImpl(
wildcardKind,
firstTokenBefore(bound, boundTokenType),
isUpperBound ? Tree.Kind.EXTENDS_WILDCARD : Tree.Kind.SUPER_WILDCARD,
firstTokenBefore(bound, isUpperBound ? TerminalToken.TokenNameextends : TerminalToken.TokenNamesuper),
convertType(bound)
).complete(questionToken);
}
Expand Down
Loading