Skip to content

Commit

Permalink
Bad practice - Method with Boolean return type returns explicit null
Browse files Browse the repository at this point in the history
Use Optional instead, where Optional.empty() corresponds to null.
  • Loading branch information
ptziegler committed Feb 1, 2024
1 parent d642a75 commit 75380b1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2023 Google, Inc.
* Copyright (c) 2011, 2024 Google, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -680,7 +680,7 @@ private static void addExposedJavaInfo(JavaInfo host, JavaInfo exposed) throws E
private static boolean isEnabledExposeMethod(JavaInfo host, Method getMethod) {
// check filters
for (ExposingRule rule : host.getDescription().getExposingRules()) {
Boolean filter = rule.filter(getMethod);
Boolean filter = rule.filter(getMethod).orElse(null);
if (filter != null) {
if (filter.booleanValue()) {
return true;
Expand All @@ -707,7 +707,7 @@ private static boolean isEnabledExposeMethod(JavaInfo host, Method getMethod) {
private static boolean isEnabledExposeField(JavaInfo host, Field field) {
// check filters
for (ExposingRule rule : host.getDescription().getExposingRules()) {
Boolean filter = rule.filter(field);
Boolean filter = rule.filter(field).orElse(null);
if (filter != null) {
if (filter.booleanValue()) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011 Google, Inc.
* Copyright (c) 2011, 2024 Google, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -13,6 +13,7 @@
import org.eclipse.wb.internal.core.utils.check.Assert;

import java.lang.reflect.Method;
import java.util.Optional;

/**
* Implementation of {@link ExposingRule} that name of given {@link Method}.
Expand Down Expand Up @@ -40,13 +41,13 @@ public ExposingMethodRule(boolean include, String methodName) {
//
////////////////////////////////////////////////////////////////////////////
@Override
public Boolean filter(Method method) {
public Optional<Boolean> filter(Method method) {
Assert.isLegal(method.getParameterTypes().length == 0, method.toString());
// check method name
if (!method.getName().equals(m_methodName)) {
return null;
return Optional.empty();
}
// OK, method satisfies to filter, so include/exclude it
return m_include;
return Optional.of(m_include);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011 Google, Inc.
* Copyright (c) 2011, 2024 Google, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -14,6 +14,7 @@

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Optional;

/**
* Implementation of {@link ExposingRule} that checks package of {@link Class} declaring given
Expand Down Expand Up @@ -42,20 +43,20 @@ public ExposingPackageRule(boolean include, String packageName) {
//
////////////////////////////////////////////////////////////////////////////
@Override
public Boolean filter(Method method) {
public Optional<Boolean> filter(Method method) {
String packageName = CodeUtils.getPackage(method.getDeclaringClass().getName());
if (packageName.equals(m_packageName)) {
return m_include;
return Optional.of(m_include);
}
return null;
return Optional.empty();
}

@Override
public Boolean filter(Field field) {
public Optional<Boolean> filter(Field field) {
String packageName = CodeUtils.getPackage(field.getDeclaringClass().getName());
if (packageName.equals(m_packageName)) {
return m_include;
return Optional.of(m_include);
}
return null;
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011 Google, Inc.
* Copyright (c) 2011, 2024 Google, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -12,6 +12,7 @@

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Optional;

/**
* Filter for checking that some {@link Method} can be used to expose child.
Expand All @@ -25,22 +26,22 @@ public abstract class ExposingRule {
* the {@link Method} to filter.
*
* @return <code>true</code> if given {@link Method} can be used to expose child,
* <code>false</code> - if can not be exposed, or <code>null</code> if given
* <code>false</code> - if can not be exposed, or <code>empty</code> if given
* {@link Method} does not fall into this rule.
*/
public Boolean filter(Method method) {
return null;
public Optional<Boolean> filter(Method method) {
return Optional.empty();
}

/**
* @param method
* the {@link Field} to filter.
*
* @return <code>true</code> if given {@link Field} can be used to expose child,
* <code>false</code> - if can not be exposed, or <code>null</code> if given {@link Field}
* <code>false</code> - if can not be exposed, or <code>empty</code> if given {@link Field}
* does not fall into this rule.
*/
public Boolean filter(Field field) {
return null;
public Optional<Boolean> filter(Field field) {
return Optional.empty();
}
}

0 comments on commit 75380b1

Please sign in to comment.