[
https://issues.apache.org/jira/browse/LANG-1691?focusedWorklogId=809175&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-809175
]
ASF GitHub Bot logged work on LANG-1691:
----------------------------------------
Author: ASF GitHub Bot
Created on: 15/Sep/22 16:08
Start Date: 15/Sep/22 16:08
Worklog Time Spent: 10m
Work Description: thiyagu-7 commented on code in PR #949:
URL: https://github.com/apache/commons-lang/pull/949#discussion_r972177609
##########
src/test/java/org/apache/commons/lang3/ClassUtilsTest.java:
##########
@@ -540,12 +539,11 @@ public void test_getShortCanonicalName_Object() {
class Named {
// empty
}
- // WARNING: this is fragile, implementation may change, naming is not
guaranteed
- assertEquals("ClassUtilsTest.9", ClassUtils.getShortCanonicalName(new
Object() {
+ assertEquals("", ClassUtils.getShortCanonicalName(new Object() {
// empty
}, "<null>"));
- assertEquals("ClassUtilsTest.9Named",
ClassUtils.getShortCanonicalName(new Named(), "<null>"));
- assertEquals("ClassUtilsTest.Inner",
ClassUtils.getShortCanonicalName(new Inner(), "<null>"));
+ assertEquals("", ClassUtils.getShortCanonicalName(new Named(),
"<null>"));
Review Comment:
I felt the same. It is because it is a _local class_. Local classes don't
have canonical name
https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getCanonicalName--
> Returns null if the underlying class does not have a canonical name (i.e.,
if it is a local or anonymous class or an array whose component type does not
have a canonical name).
Issue Time Tracking
-------------------
Worklog Id: (was: 809175)
Time Spent: 50m (was: 40m)
> ClassUtils.getShortCanonicalName doesn't use the canonicalName
> --------------------------------------------------------------
>
> Key: LANG-1691
> URL: https://issues.apache.org/jira/browse/LANG-1691
> Project: Commons Lang
> Issue Type: Bug
> Components: lang.*
> Affects Versions: 3.12.0
> Reporter: Thiyagarajan
> Priority: Major
> Time Spent: 50m
> Remaining Estimate: 0h
>
> The
> [getShortCanonicalName|https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/ClassUtils.html#getShortCanonicalName-java.lang.Class-]
> should return the canonical name minus the package name from a Class.
> Also, the javadoc of an overloaded _getShortCanonicalName_ states,
> _If the class is an inner class then the result value will not contain the
> outer classes. This way the behavior of this method is different from
> getShortClassName(String). The argument in that case is class name and not
> canonical name and the return value retains the outer classes._
> But for inner class, its behaviour is similar to _getShortClassName._
> {code:java}
> Class<?> clazz = Map.Entry.class;
> System.out.println(ClassUtils.getShortCanonicalName(clazz)); {code}
> *Actual:* Map.Entry
> *Expected:* Entry
> Looking into the implementation, it calls
> *getShortCanonicalName(cls.getName()).* I believe it should have been
> *getShortCanonicalName(cls.getCanonicalName())*
> {code:java}
> public static String getShortCanonicalName(final Class<?> cls) {
> if (cls == null) {
> return StringUtils.EMPTY;
> }
> return getShortCanonicalName(cls.getName()); //<---
> }
> public static String getShortCanonicalName(final String canonicalName) {
> return getShortClassName(getCanonicalName(canonicalName));
> } {code}
>
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)