This is an automated email from the ASF dual-hosted git repository.

mattsicker pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 418459f6b22b8b7d3336ea60af271f571c88cde1
Author: Matt Sicker <[email protected]>
AuthorDate: Sat Mar 18 19:01:29 2023 -0500

    Add more specific injection-related exceptions
    
    Signed-off-by: Matt Sicker <[email protected]>
---
 .../di/AmbiguousInjectConstructorException.java    | 28 ++++++++++++
 .../plugins/di/CircularDependencyException.java    | 34 ++++++++++++++
 .../log4j/plugins/di/NotInjectableException.java   | 53 ++++++++++++++++++++++
 3 files changed, 115 insertions(+)

diff --git 
a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/AmbiguousInjectConstructorException.java
 
b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/AmbiguousInjectConstructorException.java
new file mode 100644
index 0000000000..1b88c8ba50
--- /dev/null
+++ 
b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/AmbiguousInjectConstructorException.java
@@ -0,0 +1,28 @@
+/*
+ * 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.logging.log4j.plugins.di;
+
+import org.apache.logging.log4j.plugins.Inject;
+
+/**
+ * Exception thrown when a class has multiple constructors annotated with 
{@link Inject}.
+ */
+public class AmbiguousInjectConstructorException extends InjectException {
+    public AmbiguousInjectConstructorException(final Class<?> injectClass) {
+        super("Multiple @Inject constructors found in " + injectClass);
+    }
+}
diff --git 
a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/CircularDependencyException.java
 
b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/CircularDependencyException.java
new file mode 100644
index 0000000000..591e3b61d9
--- /dev/null
+++ 
b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/CircularDependencyException.java
@@ -0,0 +1,34 @@
+/*
+ * 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.logging.log4j.plugins.di;
+
+/**
+ * Exception thrown when a circular dependency is encountered.
+ */
+public class CircularDependencyException extends InjectException {
+    public CircularDependencyException(final Key<?> key, final DependencyChain 
dependencies) {
+        super(formatMessage(key, dependencies));
+    }
+
+    private static String formatMessage(final Key<?> key, final 
DependencyChain dependencies) {
+        final StringBuilder sb = new StringBuilder("Circular dependency 
encountered: ");
+        for (final Key<?> dependency : dependencies) {
+            sb.append(dependency).append(" -> ");
+        }
+        return sb.append(key).toString();
+    }
+}
diff --git 
a/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/NotInjectableException.java
 
b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/NotInjectableException.java
new file mode 100644
index 0000000000..53835d3a87
--- /dev/null
+++ 
b/log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/di/NotInjectableException.java
@@ -0,0 +1,53 @@
+/*
+ * 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.logging.log4j.plugins.di;
+
+import org.apache.logging.log4j.util.StringBuilders;
+
+/**
+ * Exception thrown when an instance of a type is not injectable.
+ */
+public class NotInjectableException extends InjectException {
+    public NotInjectableException(final Class<?> injectClass) {
+        this(DependencyChain.empty(), injectClass);
+    }
+
+    public NotInjectableException(final Key<?> key) {
+        this(DependencyChain.empty(), key);
+    }
+
+    public NotInjectableException(final Key<?> key, final DependencyChain 
dependencies) {
+        this(dependencies, key);
+    }
+
+    private NotInjectableException(final DependencyChain chain, final Object 
target) {
+        super(formatMessage(target, chain));
+    }
+
+    private static String formatMessage(final Object target, final 
DependencyChain dependencies) {
+        final StringBuilder sb = new StringBuilder("No @Inject constructor or 
default constructor found for ");
+        if (!dependencies.isEmpty()) {
+            sb.append("chain ");
+            for (final Key<?> dependency : dependencies) {
+                dependency.formatTo(sb);
+                sb.append(" -> ");
+            }
+        }
+        StringBuilders.appendValue(sb, target);
+        return sb.toString();
+    }
+}

Reply via email to