garydgregory commented on code in PR #1591:
URL: https://github.com/apache/commons-lang/pull/1591#discussion_r2733455792
##########
src/main/java/org/apache/commons/lang3/ClassUtils.java:
##########
@@ -1024,17 +1021,37 @@ public static String getShortClassName(final Class<?>
cls) {
if (cls == null) {
return StringUtils.EMPTY;
}
- return getShortClassName(cls.getName());
+ int dim = 0;
+ Class<?> c = cls;
+ while (c.isArray()) {
+ dim++;
+ c = c.getComponentType();
+ }
+
Review Comment:
Remove the extra blank line. If you want to "phrase" a method, you can use a
`// comment` to explain what's happening.
##########
src/test/java/org/apache/commons/lang3/ClassUtilsShortClassNameTest.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class $trange {
+
+}
+
+class Pa$$word {
+
+}
+
+/**
+ * Tests for https://issues.apache.org/jira/browse/LANG-1818
+ */
+public class ClassUtilsShortClassNameTest {
+
+ class $Inner {
+
+ }
+
+ class Inner {
+ class Ne$ted {
+
+ }
+ }
+
+ @Test
+ void testDollarSignImmediatelyAfterPackage() {
+ String result = ClassUtils.getShortClassName($trange.class);
Review Comment:
Inline `result`.
##########
src/main/java/org/apache/commons/lang3/ClassUtils.java:
##########
@@ -1024,17 +1021,37 @@ public static String getShortClassName(final Class<?>
cls) {
if (cls == null) {
return StringUtils.EMPTY;
}
- return getShortClassName(cls.getName());
+ int dim = 0;
+ Class<?> c = cls;
+ while (c.isArray()) {
+ dim++;
+ c = c.getComponentType();
+ }
+
+ final String base;
+ // Preserve legacy behavior for anonymous/local classes (keeps
compiler ordinals: $13, $10Named, etc.)
+ if (c.isAnonymousClass() || c.isLocalClass()) {
+ base = getShortClassName(c.getName());
+ } else {
+ final Deque<String> parts = new ArrayDeque<>();
+ Class<?> x = c;
+ while (x != null) {
+ parts.push(x.getSimpleName());
+ x = x.getDeclaringClass();
+ }
+ base = String.join(String.valueOf(PACKAGE_SEPARATOR_CHAR), parts);
+ }
+
+ final StringBuilder sb = new StringBuilder(base);
+ for (int i = 0; i < dim; i++) {
Review Comment:
You could reuse `StringUtils.repeat()` or
`AppendableJoiner.join(StringBuilder, T...)` or at least pre-allocate the
`StringBuilder` since its final size is known.
##########
src/main/java/org/apache/commons/lang3/ClassUtils.java:
##########
@@ -1011,11 +1013,6 @@ public static String getShortCanonicalName(final String
canonicalName) {
/**
* Gets the class name minus the package name from a {@link Class}.
*
- * <p>
Review Comment:
We should have some examples here, especially since `$` in simple/short
class names is unusual. You might also want a link to the JLS.
We don't explain the subtle difference between our "short" class name and
Java's "simple" class name very well, IMO, but that might be an issue for a
different PR. I'll leave that up to you.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]