[
https://issues.apache.org/jira/browse/LANG-1649?focusedWorklogId=775695&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-775695
]
ASF GitHub Bot logged work on LANG-1649:
----------------------------------------
Author: ASF GitHub Bot
Created on: 29/May/22 07:56
Start Date: 29/May/22 07:56
Worklog Time Spent: 10m
Work Description: aherbert commented on code in PR #731:
URL: https://github.com/apache/commons-lang/pull/731#discussion_r884226270
##########
src/test/java/org/apache/commons/lang3/reflect/testbed/ABG2.java:
##########
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3.reflect.testbed;
+
+import org.apache.commons.lang3.ClassUtilsTest;
+
+/**
+ * Test class for {@link ClassUtilsTest#test_isOverridable()}}.
+ */
+public class ABG2 {
Review Comment:
Looking at the rest of the testbed package, this does not have a very
descriptive name. I would suggest `ClassWithPackagePrivateMethod` and the
method to be `packagePrivateMethod`. The method is not a default method (which
applies to interfaces). The purpose of the testbed class is then
self-documenting.
The test class that extends this can then be named whatever you want but I
would suggest `IsOverridableClass`.
##########
src/test/java/org/apache/commons/lang3/ClassUtilsTest.java:
##########
@@ -90,6 +91,15 @@ private class DeeplyNested {
}
}
+ private static class ABG extends ABG2 {
+ public void publicMethod() {}
+ private void privateMethod() {}
+ void defaultMethod() {}
Review Comment:
Can you put all the method modifiers in the method name:
```Java
void packagePrivateMethod() {}
public final void publicFinalMethod() {}
public static void publicStaticMethod() {}
```
##########
src/main/java/org/apache/commons/lang3/ClassUtils.java:
##########
@@ -982,6 +982,59 @@ public static String getShortCanonicalName(final String
canonicalName) {
return getShortClassName(getCanonicalName(canonicalName));
}
+ /**
+ * Check if the given {@code Method} is overridable in the given target
{@code Class}.
+ *
+ * <p>This method returns {@code false} for any {@code null} input or
method with the static or final modifiers.</p>
+ * <pre>
+ * <code>
+ * private static class ABG {
+ * private void privateMethod() {
+ * }
+ * void defaultMethod() {
+ * }
+ * public void publicMethod() {
+ * }
+ * public final void finalMethod() {
+ * }
+ * public static void staticMethod() {
+ * }
+ * }
+ * Method publicMethod = ABG.class.getDeclaredMethod("publicMethod",
null)
+ * Method privateMethod = ABG.class.getDeclaredMethod("privateMethod",
null)
+ * Method defaultMethod = ABG.class.getDeclaredMethod("defaultMethod",
null)
+ * </code>
+ *
+ * ClassUtils.isOverridable(null, null) = false
+ * ClassUtils.isOverridable(null, Class) = false
+ * ClassUtils.isOverridable(Class, null) = false
+ * ClassUtils.isOverridable(privateMethod, ABG.class)) = false
+ * ClassUtils.isOverridable(defaultMethod, ABG.class)) = true
Review Comment:
Update this to `packagePrivateMethod`
The limitations of the result for packagePrivateMethod should be documented.
For example:
```
<p>Note that the result of {@code isOverridable} when called on a
package-private method is
sensitive to the package of the target class. When invoked on the declaring
class or a child
class within the same package the result will be {@code true} as the method
is visible within
the package. When invoked on a child class within a different package the
result will be
{@code false}.
```
##########
src/main/java/org/apache/commons/lang3/ClassUtils.java:
##########
@@ -982,6 +982,59 @@ public static String getShortCanonicalName(final String
canonicalName) {
return getShortClassName(getCanonicalName(canonicalName));
}
+ /**
+ * Check if the given {@code Method} is overridable in the given target
{@code Class}.
+ *
+ * <p>This method returns {@code false} for any {@code null} input or
method with the static or final modifiers.</p>
+ * <pre>
+ * <code>
+ * private static class ABG {
+ * private void privateMethod() {
+ * }
+ * void defaultMethod() {
Review Comment:
Update this to `packagePrivateMethod`
##########
src/main/java/org/apache/commons/lang3/ClassUtils.java:
##########
@@ -982,6 +982,59 @@ public static String getShortCanonicalName(final String
canonicalName) {
return getShortClassName(getCanonicalName(canonicalName));
}
+ /**
+ * Check if the given {@code Method} is overridable in the given target
{@code Class}.
+ *
+ * <p>This method returns {@code false} for any {@code null} input or
method with the static or final modifiers.</p>
+ * <pre>
+ * <code>
+ * private static class ABG {
+ * private void privateMethod() {
+ * }
+ * void defaultMethod() {
+ * }
+ * public void publicMethod() {
+ * }
+ * public final void finalMethod() {
+ * }
+ * public static void staticMethod() {
+ * }
+ * }
+ * Method publicMethod = ABG.class.getDeclaredMethod("publicMethod",
null)
+ * Method privateMethod = ABG.class.getDeclaredMethod("privateMethod",
null)
+ * Method defaultMethod = ABG.class.getDeclaredMethod("defaultMethod",
null)
+ * </code>
+ *
+ * ClassUtils.isOverridable(null, null) = false
+ * ClassUtils.isOverridable(null, Class) = false
+ * ClassUtils.isOverridable(Class, null) = false
+ * ClassUtils.isOverridable(privateMethod, ABG.class)) = false
+ * ClassUtils.isOverridable(defaultMethod, ABG.class)) = true
+ * ClassUtils.isOverridable(publicMethod, ABG.class)) = true
+ * ClassUtils.isOverridable(finalMethod, ABG.class)) = false
+ * ClassUtils.isOverridable(staticMethod, ABG.class)) = false
+ * </pre>
+ *
+ * @param method the method to check, may be {@code null}
+ * @param cls the target class to check against, may be {@code null}
+ * @return {@code true} if the is overridable, false if not or any {@code
null} input
+ * @since 3.13.0
+ */
+ public static boolean isOverridable(final Method method, final Class<?>
cls) {
+ if (ObjectUtils.anyNull(method, cls)) {
+ return false;
+ }
+ final int mod = method.getModifiers();
+ if ((mod & (Modifier.STATIC | Modifier.FINAL | Modifier.PRIVATE)) !=
0) {
+ return false;
+ }
+ if ((mod & (Modifier.PUBLIC | Modifier.PROTECTED)) != 0) {
+ return true;
+ }
+ // default scope: the package must be the same to allow override
Review Comment:
package-private scope:
##########
src/main/java/org/apache/commons/lang3/ClassUtils.java:
##########
@@ -982,6 +982,59 @@ public static String getShortCanonicalName(final String
canonicalName) {
return getShortClassName(getCanonicalName(canonicalName));
}
+ /**
+ * Check if the given {@code Method} is overridable in the given target
{@code Class}.
+ *
+ * <p>This method returns {@code false} for any {@code null} input or
method with the static or final modifiers.</p>
+ * <pre>
+ * <code>
+ * private static class ABG {
+ * private void privateMethod() {
+ * }
+ * void defaultMethod() {
+ * }
+ * public void publicMethod() {
+ * }
+ * public final void finalMethod() {
+ * }
+ * public static void staticMethod() {
+ * }
+ * }
+ * Method publicMethod = ABG.class.getDeclaredMethod("publicMethod",
null)
+ * Method privateMethod = ABG.class.getDeclaredMethod("privateMethod",
null)
+ * Method defaultMethod = ABG.class.getDeclaredMethod("defaultMethod",
null)
Review Comment:
Update this to `packagePrivateMethod`
Issue Time Tracking
-------------------
Worklog Id: (was: 775695)
Time Spent: 4h 20m (was: 4h 10m)
> Check if method is Overridable
> ------------------------------
>
> Key: LANG-1649
> URL: https://issues.apache.org/jira/browse/LANG-1649
> Project: Commons Lang
> Issue Type: Improvement
> Components: lang.*
> Affects Versions: 3.12.0
> Reporter: Arturo Bernal
> Priority: Minor
> Time Spent: 4h 20m
> Remaining Estimate: 0h
>
> Create a method that Check if the given Method is overridable in the given a
> Class
>
>
--
This message was sent by Atlassian Jira
(v8.20.7#820007)