Skip to content

Commit

Permalink
NOJIRA: Replace instanceof checks with pattern matching (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoayyed authored Nov 27, 2024
1 parent d62fd6d commit 94918b4
Show file tree
Hide file tree
Showing 26 changed files with 43 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,12 @@ public AuthProfileItemDirectoryPanel(
protected abstract String sortProperty();

@Override
@SuppressWarnings("unchecked")
public void onEvent(final IEvent<?> event) {
if (event.getPayload() instanceof ExitEvent) {
AjaxRequestTarget target = ExitEvent.class.cast(event.getPayload()).getTarget();
authProfileModal.close(target);
} else if (event.getPayload() instanceof AjaxWizard.EditItemActionEvent) {
@SuppressWarnings("unchecked")
AjaxWizard.EditItemActionEvent<?> payload = (AjaxWizard.EditItemActionEvent<?>) event.getPayload();
} else if (event.getPayload() instanceof final AjaxWizard.EditItemActionEvent<?> payload) {
payload.getTarget().ifPresent(actionTogglePanel::close);
}
super.onEvent(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ protected ConnectorDirectoryPanel(final String id, final ConnectorDirectoryPanel

@Override
public void onEvent(final IEvent<?> event) {
if (event.getPayload() instanceof ConnectorSearchEvent) {
ConnectorSearchEvent payload = (ConnectorSearchEvent) event.getPayload();
if (event.getPayload() instanceof final ConnectorSearchEvent payload) {
AjaxRequestTarget target = payload.getTarget();
if (StringUtils.isNotBlank(payload.getKeyword())) {
keyword = payload.getKeyword().toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ protected void onConfigure() {

@Override
public void onEvent(final IEvent<?> event) {
if (event.getPayload() instanceof ResourceSearchEvent) {
ResourceSearchEvent payload = (ResourceSearchEvent) event.getPayload();
if (event.getPayload() instanceof final ResourceSearchEvent payload) {
AjaxRequestTarget target = payload.getTarget();
if (StringUtils.isNotEmpty(payload.getKeyword())) {
keyword = payload.getKeyword().toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ protected void onUpdate(final AjaxRequestTarget target) {

@Override
public void onEvent(final IEvent<?> event) {
if (event.getPayload() instanceof EventSelectionChanged) {
EventSelectionChanged eventSelectionChanged = (EventSelectionChanged) event.getPayload();
if (event.getPayload() instanceof final EventSelectionChanged eventSelectionChanged) {

eventSelectionChanged.getToBeRemoved().
forEach(toBeRemoved -> model.getObject().remove(toBeRemoved.toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,7 @@ public DirectoryPanel<T, W, DP, E> disableCheckBoxes() {

@Override
public void onEvent(final IEvent<?> event) {
if (event.getPayload() instanceof EventDataWrapper) {
EventDataWrapper data = (EventDataWrapper) event.getPayload();
if (event.getPayload() instanceof final EventDataWrapper data) {

if (data.getRows() < 1) {
updateResultTable(data.isCreate());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,7 @@ public void onEvent(final IEvent<?> event) {

target.ifPresent(t -> t.add(ListViewPanel.this));
super.onEvent(event);
} else if (event.getPayload() instanceof ListViewPanel.ListViewReload) {
final ListViewPanel.ListViewReload<?> payload = (ListViewPanel.ListViewReload<?>) event.getPayload();
} else if (event.getPayload() instanceof final ListViewPanel.ListViewReload<?> payload) {
if (payload.getItems() != null) {
ListViewPanel.this.listOfItems.clear();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ private static SearchClause getPrimitiveSearchClause(final SearchCondition<Searc
}

ConditionType ct = sc.getConditionType();
if (sc instanceof SyncopeFiqlSearchCondition && sc.getConditionType() == ConditionType.CUSTOM) {
SyncopeFiqlSearchCondition<SearchBean> sfsc = (SyncopeFiqlSearchCondition<SearchBean>) sc;
if (sc instanceof final SyncopeFiqlSearchCondition<SearchBean> sfsc
&& sc.getConditionType() == ConditionType.CUSTOM) {
if (SyncopeFiqlParser.IEQ.equals(sfsc.getOperator())) {
ct = ConditionType.EQUALS;
} else if (SyncopeFiqlParser.NIEQ.equals(sfsc.getOperator())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.syncope.client.console.wizards.any;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.apache.syncope.client.console.commons.RealmsUtils;
import org.apache.syncope.client.console.layout.UserFormLayoutInfo;
Expand Down Expand Up @@ -48,11 +49,7 @@ public UserTemplateWizardBuilder(
super(anyTypeClasses, formLayoutInfo, userRestClient, pageRef);
templatable = null;

if (template == null) {
setItem(new UserWrapper(new UserTO()));
} else {
setItem(new UserWrapper(template));
}
setItem(new UserWrapper(Objects.requireNonNullElseGet(template, UserTO::new)));
}

public UserTemplateWizardBuilder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ public void onException(final Exception e) {
Throwable root = ExceptionUtils.getRootCause(e);
String message = root.getMessage();

if (root instanceof SyncopeClientException) {
SyncopeClientException sce = (SyncopeClientException) root;
if (root instanceof final SyncopeClientException sce) {
message = sce.isComposite()
? sce.asComposite().getExceptions().stream().map(this::message).collect(Collectors.joining("; "))
: message(sce);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ public static <A extends AnyTO, C extends AnyCR> void toAnyCR(final A anyTO, fin
anyCR.getVirAttrs().addAll(anyTO.getVirAttrs());
anyCR.getResources().addAll(anyTO.getResources());

if (anyCR instanceof UserCR && anyTO instanceof UserTO) {
UserCR userCR = (UserCR) anyCR;
UserTO userTO = (UserTO) anyTO;
if (anyCR instanceof final UserCR userCR && anyTO instanceof final UserTO userTO) {

userCR.setUsername(userTO.getUsername());
userCR.setPassword(userTO.getPassword());
Expand All @@ -82,19 +80,15 @@ public static <A extends AnyTO, C extends AnyCR> void toAnyCR(final A anyTO, fin
userCR.getRelationships().addAll(userTO.getRelationships());
userCR.getMemberships().addAll(userTO.getMemberships());
userCR.getRoles().addAll(userTO.getRoles());
} else if (anyCR instanceof GroupCR && anyTO instanceof GroupTO) {
GroupCR groupCR = (GroupCR) anyCR;
GroupTO groupTO = (GroupTO) anyTO;
} else if (anyCR instanceof final GroupCR groupCR && anyTO instanceof final GroupTO groupTO) {

groupCR.setName(groupTO.getName());
groupCR.setUserOwner(groupTO.getUserOwner());
groupCR.setGroupOwner(groupTO.getGroupOwner());
groupCR.setUDynMembershipCond(groupTO.getUDynMembershipCond());
groupCR.getADynMembershipConds().putAll(groupTO.getADynMembershipConds());
groupCR.getTypeExtensions().addAll(groupTO.getTypeExtensions());
} else if (anyCR instanceof AnyObjectCR && anyTO instanceof AnyObjectTO) {
AnyObjectCR anyObjectCR = (AnyObjectCR) anyCR;
AnyObjectTO anyObjectTO = (AnyObjectTO) anyTO;
} else if (anyCR instanceof final AnyObjectCR anyObjectCR && anyTO instanceof final AnyObjectTO anyObjectTO) {

anyObjectCR.setType(anyObjectTO.getType());
anyObjectCR.setName(anyObjectTO.getName());
Expand All @@ -110,9 +104,7 @@ public static <C extends AnyCR, A extends AnyTO> void toAnyTO(final C anyCR, fin
anyTO.getVirAttrs().addAll(anyCR.getVirAttrs());
anyTO.getResources().addAll(anyCR.getResources());

if (anyTO instanceof UserTO && anyCR instanceof UserCR) {
UserTO userTO = (UserTO) anyTO;
UserCR userCR = (UserCR) anyCR;
if (anyTO instanceof final UserTO userTO && anyCR instanceof final UserCR userCR) {

userTO.setUsername(userCR.getUsername());
userTO.setPassword(userCR.getPassword());
Expand All @@ -122,19 +114,15 @@ public static <C extends AnyCR, A extends AnyTO> void toAnyTO(final C anyCR, fin
userTO.getRelationships().addAll(userCR.getRelationships());
userTO.getMemberships().addAll(userCR.getMemberships());
userTO.getRoles().addAll(userCR.getRoles());
} else if (anyTO instanceof GroupTO && anyCR instanceof GroupCR) {
GroupTO groupTO = (GroupTO) anyTO;
GroupCR groupCR = (GroupCR) anyCR;
} else if (anyTO instanceof final GroupTO groupTO && anyCR instanceof final GroupCR groupCR) {

groupTO.setName(groupCR.getName());
groupTO.setUserOwner(groupCR.getUserOwner());
groupTO.setGroupOwner(groupCR.getGroupOwner());
groupTO.setUDynMembershipCond(groupCR.getUDynMembershipCond());
groupTO.getADynMembershipConds().putAll(groupCR.getADynMembershipConds());
groupTO.getTypeExtensions().addAll(groupCR.getTypeExtensions());
} else if (anyTO instanceof AnyObjectTO && anyCR instanceof AnyObjectCR) {
AnyObjectTO anyObjectTO = (AnyObjectTO) anyTO;
AnyObjectCR anyObjectCR = (AnyObjectCR) anyCR;
} else if (anyTO instanceof final AnyObjectTO anyObjectTO && anyCR instanceof final AnyObjectCR anyObjectCR) {

anyObjectTO.setType(anyObjectCR.getType());
anyObjectTO.setName(anyObjectCR.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public SyncopeJsonMapper() {
* @return the unwrapped map or the original value
*/
protected Object unwrapMap(final Object value) {
if (value instanceof Map) {
Map<?, ?> map = (Map<?, ?>) value;
if (value instanceof final Map<?, ?> map) {
if (map.size() == 1) {
return map.values().iterator().next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ private Filter visitPrimitive(final SearchCondition<SearchBean> sc) {
Optional<SpecialAttr> specialAttrValue = SpecialAttr.fromString(value);

ConditionType ct = sc.getConditionType();
if (sc instanceof SyncopeFiqlSearchCondition && sc.getConditionType() == ConditionType.CUSTOM) {
SyncopeFiqlSearchCondition<SearchBean> sfsc = (SyncopeFiqlSearchCondition<SearchBean>) sc;
if (sc instanceof final SyncopeFiqlSearchCondition<SearchBean> sfsc
&& sc.getConditionType() == ConditionType.CUSTOM) {
switch (sfsc.getOperator()) {
case SyncopeFiqlParser.IEQ:
ct = ConditionType.EQUALS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ protected static String getValue(final SearchCondition<SearchBean> sc) {

protected static ConditionType getConditionType(final SearchCondition<SearchBean> sc) {
ConditionType ct = sc.getConditionType();
if (sc instanceof SyncopeFiqlSearchCondition && sc.getConditionType() == ConditionType.CUSTOM) {
SyncopeFiqlSearchCondition<SearchBean> sfsc = (SyncopeFiqlSearchCondition<SearchBean>) sc;
if (sc instanceof final SyncopeFiqlSearchCondition<SearchBean> sfsc
&& sc.getConditionType() == ConditionType.CUSTOM) {
switch (sfsc.getOperator()) {
case SyncopeFiqlParser.IEQ:
ct = ConditionType.EQUALS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof AbstractEntity)) {
if (!(obj instanceof final AbstractEntity entity)) {
return false;
}
AbstractEntity entity = (AbstractEntity) obj;
return Objects.equals(getKey(), entity.getKey());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof AbstractNode)) {
if (!(obj instanceof final AbstractNode entity)) {
return false;
}
AbstractNode entity = (AbstractNode) obj;
return Objects.equals(getKey(), entity.getKey());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ public TaskDataBinderImpl(
}

protected void fill(final ProvisioningTask<?> provisioningTask, final ProvisioningTaskTO provisioningTaskTO) {
if (provisioningTask instanceof PushTask && provisioningTaskTO instanceof PushTaskTO) {
PushTask pushTask = (PushTask) provisioningTask;
PushTaskTO pushTaskTO = (PushTaskTO) provisioningTaskTO;
if (provisioningTask instanceof final PushTask pushTask
&& provisioningTaskTO instanceof final PushTaskTO pushTaskTO) {

Implementation jobDelegate = pushTaskTO.getJobDelegate() == null
? implementationDAO.findByType(IdRepoImplementationType.TASKJOB_DELEGATE).stream().
Expand Down Expand Up @@ -160,9 +159,8 @@ protected void fill(final ProvisioningTask<?> provisioningTask, final Provisioni
// remove all filters not contained in the TO
pushTask.getFilters().entrySet().
removeIf(filter -> !pushTaskTO.getFilters().containsKey(filter.getKey()));
} else if (provisioningTask instanceof PullTask && provisioningTaskTO instanceof PullTaskTO) {
PullTask pullTask = (PullTask) provisioningTask;
PullTaskTO pullTaskTO = (PullTaskTO) provisioningTaskTO;
} else if (provisioningTask instanceof final PullTask pullTask
&& provisioningTaskTO instanceof final PullTaskTO pullTaskTO) {

Implementation jobDelegate = pullTaskTO.getJobDelegate() == null
? implementationDAO.findByType(IdRepoImplementationType.TASKJOB_DELEGATE).stream().
Expand Down Expand Up @@ -418,9 +416,8 @@ protected void fill(final SchedTaskTO schedTaskTO, final SchedTask schedTask) {
scheduler.getNextTrigger(AuthContextUtils.getDomain(), JobNamer.getJobName(schedTask)).
ifPresent(schedTaskTO::setNextExec);

if (schedTaskTO instanceof ProvisioningTaskTO && schedTask instanceof ProvisioningTask) {
ProvisioningTaskTO provisioningTaskTO = (ProvisioningTaskTO) schedTaskTO;
ProvisioningTask<?> provisioningTask = (ProvisioningTask<?>) schedTask;
if (schedTaskTO instanceof final ProvisioningTaskTO provisioningTaskTO
&& schedTask instanceof final ProvisioningTask<?> provisioningTask) {

provisioningTaskTO.setResource(provisioningTask.getResource().getKey());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,7 @@ protected List<PropagationTaskInfo> createTasks(
}
});

if (any instanceof User && propByLinkedAccount != null) {
User user = (User) any;
if (any instanceof final User user && propByLinkedAccount != null) {
propByLinkedAccount.asMap().forEach((accountInfo, operation) -> {
LinkedAccount account = user.getLinkedAccount(accountInfo.getLeft(), accountInfo.getRight()).
orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,10 @@ public <C extends AnyCR> C getAnyCR(
EntityTOUtils.toAnyCR(anyTO, anyCR);

// (for users) if password was not set above, generate if possible
if (anyCR instanceof UserCR
if (anyCR instanceof final UserCR userCR
&& StringUtils.isBlank(((UserCR) anyCR).getPassword())
&& generatePassword) {

UserCR userCR = (UserCR) anyCR;
List<PasswordPolicy> passwordPolicies = new ArrayList<>();

// add resource policies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ public void removeAuditIndex(final String domain) throws IOException {
public void entity(final EntityLifecycleEvent<Entity> event) throws IOException {
LOG.debug("About to {} index for {}", event.getType().name(), event.getEntity());

if (event.getEntity() instanceof Any) {
Any<?> any = (Any<?>) event.getEntity();
if (event.getEntity() instanceof final Any<?> any) {

if (event.getType() == SyncDeltaType.DELETE) {
DeleteRequest request = new DeleteRequest.Builder().index(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,8 @@ public void cancelByProcessDefinition(final String processDefinitionId) {
public void cancelByUser(final EntityLifecycleEvent<Entity> event) {
if (AuthContextUtils.getDomain().equals(event.getDomain())
&& event.getType() == SyncDeltaType.DELETE
&& event.getEntity() instanceof User) {
&& event.getEntity() instanceof final User user) {

User user = (User) event.getEntity();
engine.getRuntimeService().createNativeProcessInstanceQuery().
sql(createProcessInstanceQuery(user.getKey()).toString()).
list().forEach(procInst -> engine.getRuntimeService().deleteProcessInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public class SyncopeFormHandlerHelper extends FormHandlerHelper {
public TaskFormHandler getTaskFormHandlder(final String procDefId, final String taskId) {
Process process = ProcessDefinitionUtil.getProcess(procDefId);
FlowElement flowElement = process.getFlowElement(taskId, true);
if (flowElement instanceof UserTask) {
UserTask userTask = (UserTask) flowElement;
if (flowElement instanceof final UserTask userTask) {

ProcessDefinition processDefinitionEntity = ProcessDefinitionUtil.getProcessDefinition(procDefId);
DeploymentEntity deploymentEntity = CommandContextUtil.getProcessEngineConfiguration().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ public void removeAuditIndex(final String domain) throws IOException {
public void entity(final EntityLifecycleEvent<Entity> event) throws IOException {
LOG.debug("About to {} index for {}", event.getType().name(), event.getEntity());

if (event.getEntity() instanceof Any) {
Any<?> any = (Any<?>) event.getEntity();
if (event.getEntity() instanceof final Any<?> any) {

if (event.getType() == SyncDeltaType.DELETE) {
DeleteRequest request = new DeleteRequest.Builder().index(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -800,9 +800,7 @@ public Pair<UserUR, StatusR> toUserUpdate(
StatusR statusR = null;

if (op.getPath() == null && op.getOp() != PatchOp.remove
&& !CollectionUtils.isEmpty(op.getValue()) && op.getValue().get(0) instanceof SCIMUser) {

SCIMUser after = (SCIMUser) op.getValue().get(0);
&& !CollectionUtils.isEmpty(op.getValue()) && op.getValue().get(0) instanceof final SCIMUser after) {

if (after.getActive() != null && before.isSuspended() == after.isActive()) {
statusR = new StatusR.Builder(
Expand Down Expand Up @@ -932,8 +930,7 @@ public Pair<UserUR, StatusR> toUserUpdate(
}

case "addresses" -> {
if (!CollectionUtils.isEmpty(op.getValue()) && op.getValue().get(0) instanceof SCIMUser) {
SCIMUser after = (SCIMUser) op.getValue().get(0);
if (!CollectionUtils.isEmpty(op.getValue()) && op.getValue().get(0) instanceof final SCIMUser after) {
after.getAddresses().stream().filter(address -> address.getType() != null).forEach(
address -> conf.getUserConf().getAddresses().stream()
.filter(object -> address.getType().equals(object.getType().name())).findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public Response toResponse(final Exception ex) {
return Response.status(Response.Status.NOT_FOUND).entity(new SCIMError(null,
Response.Status.NOT_FOUND.getStatusCode(), ExceptionUtils.getRootCauseMessage(ex))).
build();
} else if (ex instanceof SyncopeClientException) {
SyncopeClientException sce = (SyncopeClientException) ex;
} else if (ex instanceof final SyncopeClientException sce) {
builder = builder(sce.getType(), ExceptionUtils.getRootCauseMessage(ex));
} else if (ex instanceof DelegatedAdministrationException
|| ExceptionUtils.getRootCause(ex) instanceof DelegatedAdministrationException) {
Expand Down
Loading

0 comments on commit 94918b4

Please sign in to comment.