Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8510] Remove Utils class from maven-di #2040

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions impl/maven-di/src/main/java/org/apache/maven/di/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -128,15 +127,19 @@ public <U> Key<U> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,7 +90,7 @@ public static String getDisplayName(Type type) {
qualifier = named.value();
} else {
Class<? extends Annotation> annotationType = annotation.annotationType();
qualifier = Utils.isMarker(annotationType) ? annotationType : annotation;
qualifier = annotationType.getDeclaredMethods().length == 0 ? annotationType : annotation;
}
}
}
Expand Down Expand Up @@ -382,4 +367,45 @@ public static <T> Binding<T> bindingFromConstructor(Key<T> key, Constructor<T> c

return binding.withKey(key);
}

public static void getDisplayString(StringBuilder sb, Object object) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two methods can now be private.

if (object instanceof Class<?> clazz && clazz.isAnnotation()) {
//noinspection unchecked
getDisplayString(sb, (Class<? extends Annotation>) object, null);
} else if (object instanceof Annotation annotation) {
getDisplayString(sb, annotation.annotationType(), annotation);
} else {
sb.append(object.toString());
}
}

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("");
}

private static void getDisplayString(
StringBuilder sb, Class<? extends Annotation> 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);
}
}
}
}
52 changes: 0 additions & 52 deletions impl/maven-di/src/main/java/org/apache/maven/di/impl/Utils.java

This file was deleted.

Loading