From 3f9337e34c7bf8fcdecffb5bf2aa1ab45dabdb79 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 10 Jan 2025 22:09:50 +0100 Subject: [PATCH 1/2] Remove Utils class from maven-di --- .../main/java/org/apache/maven/di/Key.java | 19 +++--- .../apache/maven/di/impl/ReflectionUtils.java | 62 ++++++++++++++----- .../java/org/apache/maven/di/impl/Utils.java | 52 ---------------- 3 files changed, 57 insertions(+), 76 deletions(-) delete mode 100644 impl/maven-di/src/main/java/org/apache/maven/di/impl/Utils.java diff --git a/impl/maven-di/src/main/java/org/apache/maven/di/Key.java b/impl/maven-di/src/main/java/org/apache/maven/di/Key.java index 4326d95de86..6ea17f8e0b5 100644 --- a/impl/maven-di/src/main/java/org/apache/maven/di/Key.java +++ b/impl/maven-di/src/main/java/org/apache/maven/di/Key.java @@ -25,7 +25,6 @@ import org.apache.maven.api.annotations.Nullable; import org.apache.maven.di.impl.ReflectionUtils; import org.apache.maven.di.impl.Types; -import org.apache.maven.di.impl.Utils; /** * The key defines an identity of a binding. In any DI, a key is usually a type of the object along @@ -128,15 +127,19 @@ public Key getTypeParameter(int index) { * and prepended qualifier display string if this key has a qualifier. */ public String getDisplayString() { - return (qualifier != null ? getQualifierDisplayString() + " " : "") + ReflectionUtils.getDisplayName(type); - } - - private String getQualifierDisplayString() { + StringBuilder result = new StringBuilder(); if (qualifier instanceof String s) { - return s.isEmpty() ? "@Named" : "@Named(\"" + s + "\")"; + if (s.isEmpty()) { + result.append("@Named "); + } else { + result.append("@Named(\"").append(s).append("\") "); + } + } else if (qualifier != null) { + ReflectionUtils.getDisplayString(result, qualifier); + result.append(" "); } - String s = Utils.getDisplayString(qualifier); - return s; + result.append(ReflectionUtils.getDisplayName(type)); + return result.toString(); } @Override diff --git a/impl/maven-di/src/main/java/org/apache/maven/di/impl/ReflectionUtils.java b/impl/maven-di/src/main/java/org/apache/maven/di/impl/ReflectionUtils.java index 08337041a7b..4566f65dc21 100644 --- a/impl/maven-di/src/main/java/org/apache/maven/di/impl/ReflectionUtils.java +++ b/impl/maven-di/src/main/java/org/apache/maven/di/impl/ReflectionUtils.java @@ -56,21 +56,6 @@ public final class ReflectionUtils { private static final Pattern PACKAGE_AND_PARENT = Pattern.compile(PACKAGE.pattern() + "(?:" + IDENT + "\\$\\d*)?"); private static final Pattern ARRAY_SIGNATURE = Pattern.compile("\\[L(.*?);"); - public static String getDisplayName(Type type) { - Class raw = Types.getRawType(type); - String typeName; - if (raw.isAnonymousClass()) { - Type superclass = raw.getGenericSuperclass(); - typeName = "? extends " + superclass.getTypeName(); - } else { - typeName = type.getTypeName(); - } - - return PACKAGE_AND_PARENT - .matcher(ARRAY_SIGNATURE.matcher(typeName).replaceAll("$1[]")) - .replaceAll(""); - } - public static @Nullable Object getOuterClassInstance(Object innerClassInstance) { if (innerClassInstance == null) { return null; @@ -105,7 +90,7 @@ public static String getDisplayName(Type type) { qualifier = named.value(); } else { Class annotationType = annotation.annotationType(); - qualifier = Utils.isMarker(annotationType) ? annotationType : annotation; + qualifier = isMarker(annotationType) ? annotationType : annotation; } } } @@ -382,4 +367,49 @@ public static Binding bindingFromConstructor(Key key, Constructor c return binding.withKey(key); } + + public static void getDisplayString(StringBuilder sb, Object object) { + if (object instanceof Class clazz && clazz.isAnnotation()) { + //noinspection unchecked + getDisplayString(sb, (Class) object, null); + } else if (object instanceof Annotation annotation) { + getDisplayString(sb, annotation.annotationType(), annotation); + } else { + sb.append(object.toString()); + } + } + + public static void getDisplayString( + StringBuilder sb, Class annotationType, @Nullable Annotation annotation) { + if (annotation == null) { + sb.append("@").append(ReflectionUtils.getDisplayName(annotationType)); + } else { + String typeName = annotationType.getName(); + String str = annotation.toString(); + if (str.startsWith("@" + typeName)) { + sb.append("@").append(getDisplayName(annotationType)).append(str.substring(typeName.length() + 1)); + } else { + sb.append(str); + } + } + } + + public static String getDisplayName(Type type) { + Class raw = Types.getRawType(type); + String typeName; + if (raw.isAnonymousClass()) { + Type superclass = raw.getGenericSuperclass(); + typeName = "? extends " + superclass.getTypeName(); + } else { + typeName = type.getTypeName(); + } + + return PACKAGE_AND_PARENT + .matcher(ARRAY_SIGNATURE.matcher(typeName).replaceAll("$1[]")) + .replaceAll(""); + } + + public static boolean isMarker(Class annotationType) { + return annotationType.getDeclaredMethods().length == 0; + } } diff --git a/impl/maven-di/src/main/java/org/apache/maven/di/impl/Utils.java b/impl/maven-di/src/main/java/org/apache/maven/di/impl/Utils.java deleted file mode 100644 index cc1dd3bb120..00000000000 --- a/impl/maven-di/src/main/java/org/apache/maven/di/impl/Utils.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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.maven.di.impl; - -import java.lang.annotation.Annotation; - -import org.apache.maven.api.annotations.Nullable; - -public final class Utils { - - public static String getDisplayString(Class annotationType, @Nullable Annotation annotation) { - if (annotation == null) { - return "@" + ReflectionUtils.getDisplayName(annotationType); - } - String typeName = annotationType.getName(); - String str = annotation.toString(); - return str.startsWith("@" + typeName) - ? "@" + ReflectionUtils.getDisplayName(annotationType) + str.substring(typeName.length() + 1) - : str; - } - - public static String getDisplayString(Object object) { - if (object instanceof Class clazz && clazz.isAnnotation()) { - //noinspection unchecked - return getDisplayString((Class) object, null); - } - if (object instanceof Annotation annotation) { - return getDisplayString(annotation.annotationType(), annotation); - } - return object.toString(); - } - - public static boolean isMarker(Class annotationType) { - return annotationType.getDeclaredMethods().length == 0; - } -} From a043a97e1abe5546c7601d49aa570e9327cde67c Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Sat, 11 Jan 2025 16:40:45 +0100 Subject: [PATCH 2/2] Inline one method, make another one private --- .../apache/maven/di/impl/ReflectionUtils.java | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/impl/maven-di/src/main/java/org/apache/maven/di/impl/ReflectionUtils.java b/impl/maven-di/src/main/java/org/apache/maven/di/impl/ReflectionUtils.java index 4566f65dc21..809e39e9552 100644 --- a/impl/maven-di/src/main/java/org/apache/maven/di/impl/ReflectionUtils.java +++ b/impl/maven-di/src/main/java/org/apache/maven/di/impl/ReflectionUtils.java @@ -90,7 +90,7 @@ public final class ReflectionUtils { qualifier = named.value(); } else { Class annotationType = annotation.annotationType(); - qualifier = isMarker(annotationType) ? annotationType : annotation; + qualifier = annotationType.getDeclaredMethods().length == 0 ? annotationType : annotation; } } } @@ -379,21 +379,6 @@ public static void getDisplayString(StringBuilder sb, Object object) { } } - public static void getDisplayString( - StringBuilder sb, Class annotationType, @Nullable Annotation annotation) { - if (annotation == null) { - sb.append("@").append(ReflectionUtils.getDisplayName(annotationType)); - } else { - String typeName = annotationType.getName(); - String str = annotation.toString(); - if (str.startsWith("@" + typeName)) { - sb.append("@").append(getDisplayName(annotationType)).append(str.substring(typeName.length() + 1)); - } else { - sb.append(str); - } - } - } - public static String getDisplayName(Type type) { Class raw = Types.getRawType(type); String typeName; @@ -409,7 +394,18 @@ public static String getDisplayName(Type type) { .replaceAll(""); } - public static boolean isMarker(Class annotationType) { - return annotationType.getDeclaredMethods().length == 0; + private static void getDisplayString( + StringBuilder sb, Class annotationType, @Nullable Annotation annotation) { + if (annotation == null) { + sb.append("@").append(ReflectionUtils.getDisplayName(annotationType)); + } else { + String typeName = annotationType.getName(); + String str = annotation.toString(); + if (str.startsWith("@" + typeName)) { + sb.append("@").append(getDisplayName(annotationType)).append(str.substring(typeName.length() + 1)); + } else { + sb.append(str); + } + } } }