From 75380b1be391432c04b40dec70b587272beed4a7 Mon Sep 17 00:00:00 2001 From: Patrick Ziegler Date: Thu, 1 Feb 2024 20:40:03 +0100 Subject: [PATCH] Bad practice - Method with Boolean return type returns explicit null Use Optional instead, where Optional.empty() corresponds to null. --- .../wb/internal/core/model/JavaInfoUtils.java | 6 +++--- .../model/description/ExposingMethodRule.java | 9 +++++---- .../model/description/ExposingPackageRule.java | 15 ++++++++------- .../core/model/description/ExposingRule.java | 15 ++++++++------- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/JavaInfoUtils.java b/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/JavaInfoUtils.java index 9bd50f24b..baa9d0aed 100644 --- a/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/JavaInfoUtils.java +++ b/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/JavaInfoUtils.java @@ -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 @@ -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; @@ -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; diff --git a/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/description/ExposingMethodRule.java b/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/description/ExposingMethodRule.java index 4e94bcc23..569dbcf0f 100644 --- a/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/description/ExposingMethodRule.java +++ b/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/description/ExposingMethodRule.java @@ -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 @@ -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}. @@ -40,13 +41,13 @@ public ExposingMethodRule(boolean include, String methodName) { // //////////////////////////////////////////////////////////////////////////// @Override - public Boolean filter(Method method) { + public Optional 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); } } diff --git a/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/description/ExposingPackageRule.java b/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/description/ExposingPackageRule.java index ea9c58891..592cf47b9 100644 --- a/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/description/ExposingPackageRule.java +++ b/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/description/ExposingPackageRule.java @@ -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 @@ -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 @@ -42,20 +43,20 @@ public ExposingPackageRule(boolean include, String packageName) { // //////////////////////////////////////////////////////////////////////////// @Override - public Boolean filter(Method method) { + public Optional 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 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(); } } diff --git a/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/description/ExposingRule.java b/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/description/ExposingRule.java index 0d82c98cd..212d9f3f7 100644 --- a/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/description/ExposingRule.java +++ b/org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/description/ExposingRule.java @@ -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 @@ -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. @@ -25,11 +26,11 @@ public abstract class ExposingRule { * the {@link Method} to filter. * * @return true if given {@link Method} can be used to expose child, - * false - if can not be exposed, or null if given + * false - if can not be exposed, or empty if given * {@link Method} does not fall into this rule. */ - public Boolean filter(Method method) { - return null; + public Optional filter(Method method) { + return Optional.empty(); } /** @@ -37,10 +38,10 @@ public Boolean filter(Method method) { * the {@link Field} to filter. * * @return true if given {@link Field} can be used to expose child, - * false - if can not be exposed, or null if given {@link Field} + * false - if can not be exposed, or empty if given {@link Field} * does not fall into this rule. */ - public Boolean filter(Field field) { - return null; + public Optional filter(Field field) { + return Optional.empty(); } }