Skip to content

Commit

Permalink
Add one more exception for DiamondOperatorCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
pnatashap committed Mar 27, 2024
1 parent 77142e9 commit 03b211b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ public void visitToken(final DetailAST node) {
instance = assign.getFirstChild().getFirstChild();
}
if (instance != null && instance.getType() == TokenTypes.LITERAL_NEW
&& DiamondOperatorCheck.isNotObjectBlock(instance)
&& DiamondOperatorCheck.isNotArray(instance)) {
&& DiamondOperatorCheck.validUsage(instance)) {
final DetailAST type =
DiamondOperatorCheck.findFirstChildNodeOfType(
instance, TokenTypes.TYPE_ARGUMENTS
Expand All @@ -113,6 +112,18 @@ public void visitToken(final DetailAST node) {
}
}

/**
* Checks if diamond is not required.
*
* @param node Node
* @return True if not array
*/
private static boolean validUsage(final DetailAST node) {
return DiamondOperatorCheck.isNotObjectBlock(node)
&& DiamondOperatorCheck.isNotArray(node)
&& !DiamondOperatorCheck.isInitUsingDiamond(node);
}

/**
* Checks if node is not array.
*
Expand All @@ -133,6 +144,44 @@ private static boolean isNotObjectBlock(final DetailAST node) {
return node.getLastChild().getType() != TokenTypes.OBJBLOCK;
}

/**
* Checks if node has initialization with diamond operator.
*
* @param node Node
* @return True if not object block
*/
private static boolean isInitUsingDiamond(final DetailAST node) {
final DetailAST init = node.findFirstToken(TokenTypes.ELIST);
boolean typed = false;
if (init != null) {
final DetailAST inst = DiamondOperatorCheck.secondChild(init);
if (inst != null && inst.getType() == TokenTypes.LITERAL_NEW) {
typed =
DiamondOperatorCheck.isDiamondOperatorUsed(
inst.findFirstToken(TokenTypes.TYPE_ARGUMENTS)
);
}
}
return typed;
}

/**
* Checks if node has initialization with diamond operator.
*
* @param node Node
* @return True if not object block
*/
private static DetailAST secondChild(final DetailAST node) {
DetailAST result = null;
if (node != null) {
final DetailAST first = node.getFirstChild();
if (first != null) {
result = first.getFirstChild();
}
}
return result;
}

/**
* Checks if node contains empty set of type parameters and
* comprises angle brackets only (<>).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapOf;

/**
* Better to use diamond operator where possible.
Expand Down Expand Up @@ -33,6 +36,16 @@ public static void innerClassUsage() {
new SimpleInterface.InnerClass<>();
}

/**
* Correct non-diamonds, types required for inner entity.
*/
public static void foo() {
final Map<String, String> params = new MapOf<String, String>(
new MapEntry<>("a", "foo"),
new MapEntry<>("b", "foo")
);
}

/**
* Simple interface, used as wrapper.
* @since 1.0
Expand Down

0 comments on commit 03b211b

Please sign in to comment.