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

Fix introspection for abstract classes (#10889) #11268

Open
wants to merge 1 commit into
base: 4.7.x
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
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ default Optional<MethodElement> getDefaultConstructor() {
// only static inner classes can be constructed
return Optional.empty();
}
if (isAbstract()) {
// abstract classes constructors are accessible only from their subtypes
return Optional.empty();
}
List<ConstructorElement> constructors = getAccessibleConstructors()
.stream()
.filter(ctor -> ctor.getParameters().length == 0).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2216,6 +2216,14 @@ abstract class Test {
expect:
beanIntrospection != null
beanIntrospection.getBeanProperties().size() == 2
!beanIntrospection.isBuildable()

when:
beanIntrospection.instantiate()

then:
def e = thrown(InstantiationException)
e.message == 'No default constructor exists'
}

void "test targeting abstract class with @Introspected(classes = "() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4561,6 +4561,14 @@ abstract class Test {
expect:
beanIntrospection != null
beanIntrospection.getBeanProperties().size() == 2
!beanIntrospection.isBuildable()

when:
beanIntrospection.instantiate()

then:
def e = thrown(InstantiationException)
e.message == 'No default constructor exists'
}

void "test introspection on abstract class with custom getter"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,19 @@ internal open class KotlinClassElement(
if (defaultConstructor.isPresent) {
defaultConstructor
} else {
Optional.ofNullable(declaration.primaryConstructor)
.filter { !it.isPrivate() && it.parameters.isEmpty() }
.map {
visitorContext.elementFactory.newConstructorElement(
this,
it,
elementAnnotationMetadataFactory
)
}
if (isAbstract) {
Optional.empty()
} else {
Optional.ofNullable(declaration.primaryConstructor)
.filter { !it.isPrivate() && it.parameters.isEmpty() }
.map {
visitorContext.elementFactory.newConstructorElement(
this,
it,
elementAnnotationMetadataFactory
)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2077,6 +2077,14 @@ abstract class Test {
expect:
beanIntrospection != null
beanIntrospection.getBeanProperties().size() == 2
!beanIntrospection.isBuildable()

when:
beanIntrospection.instantiate()

then:
def e = thrown(InstantiationException)
e.message == 'No default constructor exists'
}

void "test targeting abstract class with @Introspected(classes = "() {
Expand Down
Loading