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

Improve the dependency path exception message #11405

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
36 changes: 36 additions & 0 deletions core/src/main/java/io/micronaut/core/naming/NameUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micronaut.core.naming;

import io.micronaut.core.annotation.AccessorsStyle;
import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.core.util.StringUtils;
Expand Down Expand Up @@ -214,6 +215,41 @@ public static String getSimpleName(String className) {
return className;
}

/**
* Returns the shortened fully-qualified name for a class represented as a string.
* Shortened name would have package names and owner objects reduced to a single letter.
* For example, {@code com.example.Owner$Inner} would become {@code c.e.O$Inner}.
* IDEs would still be able to recognize these types, but they would take less space
* visually.
*
* @since 4.8.x
* @param typeName The fully-qualified type name
* @return The shortened type name
*/
@Experimental
public static String getShortenedName(String typeName) {
int nameStart = typeName.lastIndexOf('$');
Copy link
Contributor

Choose a reason for hiding this comment

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

check type name doesn't == null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is not checked in other methods, so I assume it is not typical for typeName to be null. Added @NonNull though.

if (nameStart < 0) {
nameStart = typeName.lastIndexOf('.');
}
if (nameStart < 0) {
nameStart = 0;
}
StringBuilder shortened = new StringBuilder();
boolean segmentStart = true;
for (int i = 0; i < nameStart; i++) {
char c = typeName.charAt(i);
if (segmentStart) {
shortened.append(c);
segmentStart = false;
} else if (c == '.' || c == '$') {
shortened.append(c);
segmentStart = true;
}
}
return shortened.append(typeName.substring(nameStart)).toString();
}

/**
* Is the given method name a valid setter name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ConstructorCircularDependencyFailureSpec extends Specification {
ApplicationContext context = ApplicationContext.run()

when:"A bean is obtained that has a setter with @Inject"
MyClassB b = context.getBean(MyClassB)
context.getBean(MyClassB)

then:"The implementation is injected"
def e = thrown(CircularDependencyException)
Expand All @@ -40,10 +40,10 @@ Failed to inject value for field [propA] of class: io.micronaut.inject.failures.

Message: Circular dependency detected
Path Taken:
new MyClassB()
\\---> MyClassB.propA
^ \\---> new MyClassA([MyClassC propC])
| \\---> new MyClassC([MyClassB propB])
new i.m.i.f.C$MyClassB()
\\---> i.m.i.f.C$MyClassB#propA
^ \\---> new i.m.i.f.C$MyClassA([MyClassC propC])
| \\---> new i.m.i.f.C$MyClassC([MyClassB propB])
| |
+--------------+'''

Expand All @@ -65,11 +65,11 @@ Failed to inject value for field [propA] of class: io.micronaut.inject.failures.

Message: Circular dependency detected
Path Taken:
new MyClassD(MyClassB propB)
\\---> new MyClassD([MyClassB propB])
\\---> MyClassB.propA
^ \\---> new MyClassA([MyClassC propC])
| \\---> new MyClassC([MyClassB propB])
new i.m.i.f.C$MyClassD(MyClassB propB)
\\---> new i.m.i.f.C$MyClassD([MyClassB propB])
\\---> i.m.i.f.C$MyClassB#propA
^ \\---> new i.m.i.f.C$MyClassA([MyClassC propC])
| \\---> new i.m.i.f.C$MyClassC([MyClassB propB])
| |
+--------------+'''
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,35 @@ class ConstructorDependencyFailureSpec extends Specification {
void "test a useful exception is thrown when a dependency injection failure occurs"() {
given:
ApplicationContext context = ApplicationContext.run()
var space = " "

when:"A bean that defines a constructor dependency on a missing bean"
B b = context.getBean(B)
context.getBean(MyClassB)

then:"The correct error is thrown"
def e = thrown(DependencyInjectionException)
e.message.normalize().contains('''\
Failed to inject value for parameter [a] of class: io.micronaut.inject.failures.ConstructorDependencyFailureSpec$B
e.message.normalize() == """\
Failed to inject value for parameter [propA] of class: io.micronaut.inject.failures.ConstructorDependencyFailureSpec\$MyClassB

Message: No bean of type [io.micronaut.inject.failures.ConstructorDependencyFailureSpec$A] exists.''')

e.message.normalize().contains('Path Taken: new B(A a) --> new B([A a])')
Message: No bean of type [io.micronaut.inject.failures.ConstructorDependencyFailureSpec\$MyClassA] exists.$space
Path Taken:$space
new i.m.i.f.C\$MyClassB(MyClassA propA)
\\---> new i.m.i.f.C\$MyClassB([MyClassA propA])"""

cleanup:
context.close()
}

static interface A {
static interface MyClassA {

}

static class B {
private final A a
static class MyClassB {
private final MyClassA propA

@Inject
B(A a) {
this.a = a
MyClassB(MyClassA propA) {
this.propA = propA
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,43 @@ class ConstructorExceptionSpec extends Specification {
ApplicationContext context = ApplicationContext.run()

when:"A bean is obtained that has a setter with @Inject"
B b = context.getBean(B)
MyClassB b = context.getBean(MyClassB)

then:"The implementation is injected"
def e = thrown(BeanInstantiationException)
//e.cause.message == 'bad'
e.message.normalize() == '''\
Error instantiating bean of type [io.micronaut.inject.failures.ConstructorExceptionSpec$A]
Error instantiating bean of type [io.micronaut.inject.failures.ConstructorExceptionSpec$MyClassA]

Message: bad
Path Taken: new B() --> B.a --> new A([C c])'''
Path Taken:
new i.m.i.f.C$MyClassB()
\\---> i.m.i.f.C$MyClassB#propA
\\---> new i.m.i.f.C$MyClassA([MyClassC propC])'''

cleanup:
context.close()
}

@Singleton
static class C {
C() {
static class MyClassC {
MyClassC() {
throw new RuntimeException("bad")
}
}
@Singleton
static class A {
A(C c) {
static class MyClassA {
MyClassA(MyClassC propC) {

}
}

static class B {
static class MyClassB {
@Inject
private A a
private MyClassA propA

A getA() {
return this.a
MyClassA getA() {
return this.propA
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ Failed to inject value for parameter [stations] of class: io.micronaut.inject.fa

Message: Circular dependency detected
Path Taken:
new ElectricalGrid(List<ElectricStation E> stations)
\\---> new ElectricalGrid([List<ElectricStation E> stations])
^ \\---> ElectricStationFactory.nuclearStation([MeasuringEquipment equipment])
| \\---> MeasuringEquipment.grid
new i.m.i.f.F$ElectricalGrid(List<ElectricStation E> stations)
\\---> new i.m.i.f.F$ElectricalGrid([List<ElectricStation E> stations])
^ \\---> i.m.i.f.F$ElectricStationFactory#nuclearStation([MeasuringEquipment equipment])
| \\---> i.m.i.f.F$MeasuringEquipment#grid
| |
+--------------+'''

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2017-2019 original authors
*
* Licensed 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 io.micronaut.inject.failures

import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Factory
import io.micronaut.context.exceptions.BeanInstantiationException
import io.micronaut.context.exceptions.CircularDependencyException
import spock.lang.Specification

import javax.inject.Inject
import javax.inject.Singleton


class FactoryDependencyFailureSpec extends Specification {

void "test dependency with factory failure"() {
given:
ApplicationContext context = ApplicationContext.run()

when:"A bean is obtained that has a setter with @Inject"
context.getBean(ElectricalGrid)

then:"The implementation is injected"
def e = thrown(BeanInstantiationException)
e.message.normalize() == '''\
Error instantiating bean of type [io.micronaut.inject.failures.FactoryDependencyFailureSpec$ElectricStation]

Message: Outdated equipment
Path Taken:
new i.m.i.f.F$ElectricalGrid(List<ElectricStation E> stations)
\\---> new i.m.i.f.F$ElectricalGrid([List<ElectricStation E> stations])
\\---> i.m.i.f.F$ElectricStationFactory#nuclearStation([MeasuringEquipment equipment])'''

cleanup:
context.close()
}

static class ElectricalGrid {
@Inject
ElectricalGrid(List<ElectricStation> stations) {}
}

static class ElectricStation {
ElectricStation() {}
}

@Factory
static class ElectricStationFactory {

@Singleton
ElectricStation solarStation() {
return new ElectricStation()
}

@Singleton
ElectricStation nuclearStation(MeasuringEquipment equipment) {
return new ElectricStation()
}

}

@Singleton
static class MeasuringEquipment {
MeasuringEquipment() {
throw new RuntimeException("Outdated equipment")
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,35 @@ class FieldCircularDependencyFailureSpec extends Specification {
ApplicationContext context = ApplicationContext.run()

when:"A bean is obtained that has a setter with @Inject"
B b = context.getBean(B)
MyClassB b = context.getBean(MyClassB)

then:"The implementation is injected"
def e = thrown(CircularDependencyException)
e.message.normalize() == '''\
Failed to inject value for field [a] of class: io.micronaut.inject.failures.FieldCircularDependencyFailureSpec$B
Failed to inject value for field [propA] of class: io.micronaut.inject.failures.FieldCircularDependencyFailureSpec$MyClassB

Message: Circular dependency detected
Path Taken:
new B()
\\---> B.a
^ \\---> new A([C c])
| \\---> C.b
new i.m.i.f.F$MyClassB()
\\---> i.m.i.f.F$MyClassB#propA
^ \\---> new i.m.i.f.F$MyClassA([MyClassC propC])
| \\---> i.m.i.f.F$MyClassC#propB
| |
+--------------+'''
cleanup:
context.close()
}

static class C {
@Inject protected B b
static class MyClassC {
@Inject protected MyClassB propB
}
@Singleton
static class A {
A(C c) {}
static class MyClassA {
MyClassA(MyClassC propC) {}
}

@Singleton
static class B {
@Inject protected A a
static class MyClassB {
@Inject protected MyClassA propA
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,35 @@ class FieldDependencyMissingFailureSpec extends Specification {
void "test injection via setter with interface"() {
given:
ApplicationContext context = ApplicationContext.run()
var space = " "

when:"A bean is obtained that has a setter with @Inject"
B b = context.getBean(B)
context.getBean(MyClassB)

then:"The implementation is injected"
DependencyInjectionException e = thrown()
e.message.normalize().contains 'Failed to inject value for field [a] of class: io.micronaut.inject.failures.FieldDependencyMissingFailureSpec$B'
e.message.normalize().contains 'Path Taken: new B() --> B.a'
e.message.normalize() == """\
Failed to inject value for field [propA] of class: io.micronaut.inject.failures.FieldDependencyMissingFailureSpec\$MyClassB

Message: No bean of type [io.micronaut.inject.failures.FieldDependencyMissingFailureSpec\$MyClassA] exists.$space
Path Taken:$space
new i.m.i.f.F\$MyClassB()
\\---> i.m.i.f.F\$MyClassB#propA"""

cleanup:
context.close()
}

static interface A {
static interface MyClassA {

}

static class B {
static class MyClassB {
@Inject
private A a
private MyClassA propA

A getA() {
return this.a
MyClassA getPropA() {
return this.propA
}
}

Expand Down
Loading