Skip to content

Commit

Permalink
fix: KSP processing for package private properties (#10777)
Browse files Browse the repository at this point in the history
  • Loading branch information
timyates committed May 2, 2024
1 parent 821b5c7 commit 32fd3c4
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 3 deletions.
Expand Up @@ -2522,4 +2522,56 @@ class Holder<A extends Animal> {
animal.isTypeVariable()
}

void "test package private property introspection"() {
when:
def introspection = buildBeanIntrospection('test.Test', '''
package test
import io.micronaut.core.annotation.Introspected
import io.micronaut.inject.visitor.MySuperclass
@Introspected
class Test extends MySuperclass {
String name
}
''')
then: 'the property in this class is introspected'
introspection.getProperty("name").orElse(null)

and: 'the public property in the java superclass is introspected'
introspection.getProperty("publicProperty").orElse(null)

and: 'the private property in the java superclass is not introspected'
!introspection.getProperty("privateProperty").orElse(null)

and: 'the package private superclass property is not introspected'
!introspection.getProperty("packagePrivateProperty").orElse(null)
}

void "test package private property introspection in same package"() {
when:
def introspection = buildBeanIntrospection('io.micronaut.inject.visitor.Test', '''
package io.micronaut.inject.visitor
import io.micronaut.core.annotation.Introspected
@Introspected
class Test extends MySuperclass {
String name
}
''')
then: 'the property in this class is introspected'
introspection.getProperty("name").orElse(null)

and: 'the public property in the java superclass is introspected'
introspection.getProperty("publicProperty").orElse(null)

and: 'the private property in the java superclass is not introspected'
!introspection.getProperty("privateProperty").orElse(null)

and: 'the package private superclass property is introspected, as we are in the same package'
introspection.getProperty("packagePrivateProperty").orElse(null)
}
}
@@ -0,0 +1,4 @@
package io.micronaut.inject.visitor

class MySuperclass extends PackagePrivateSuperclass {
}
@@ -0,0 +1,22 @@
package io.micronaut.inject.visitor;

class PackagePrivateSuperclass {

String packagePrivateProperty;

String getPackagePrivateProperty() {
return packagePrivateProperty;
}

private String privateProperty;

private String getPrivateProperty() {
return privateProperty;
}

public String publicProperty;

public String getPublicProperty() {
return publicProperty;
}
}
Expand Up @@ -5230,6 +5230,75 @@ class Holder<A extends Animal> {
animal.isTypeVariable()
}

void "test package private property introspection"() {
when:
BeanIntrospection introspection = buildBeanIntrospection('test.Test', '''
package test;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.inject.visitor.beans.MySuperclass;
@Introspected
class Test extends MySuperclass {
private final String name;
Test(String name) {
this.name = name;
}
String getName() {
return name;
}
}
''')
then: 'the property in this class is introspected'
introspection.getProperty("name").orElse(null)

and: 'the public property in the java superclass is introspected'
introspection.getProperty("publicProperty").orElse(null)

and: 'the private property in the java superclass is not introspected'
!introspection.getProperty("privateProperty").orElse(null)

and: 'the package private superclass property is not introspected'
!introspection.getProperty("packagePrivateProperty").orElse(null)
}

void "test package private property introspection in same package"() {
when:
BeanIntrospection introspection = buildBeanIntrospection('io.micronaut.inject.visitor.beans.Test', '''
package io.micronaut.inject.visitor.beans;
import io.micronaut.core.annotation.Introspected;
@Introspected
class Test extends MySuperclass {
private final String name;
Test(String name) {
this.name = name;
}
String getName() {
return name;
}
}
''')
then: 'the property in this class is introspected'
introspection.getProperty("name").orElse(null)

and: 'the public property in the java superclass is introspected'
introspection.getProperty("publicProperty").orElse(null)

and: 'the private property in the java superclass is not introspected'
!introspection.getProperty("privateProperty").orElse(null)

and: 'the package private superclass property is introspected, as we are in the same package'
introspection.getProperty("packagePrivateProperty").orElse(null)
}

void "test private property 1"() {
given:
BeanIntrospection introspection = buildBeanIntrospection('test.OptionalDoubleHolder', '''
Expand All @@ -5255,6 +5324,7 @@ class OptionalDoubleHolder {
introspection.getProperty("optionalDouble").get().getType() == OptionalDouble.class
introspection.getProperty("optionalDouble").get().hasAnnotation(DecimalMin)
}

void "test private property 2"() {
given:
BeanIntrospection introspection = buildBeanIntrospection('test.OptionalStringHolder', '''
Expand Down
@@ -0,0 +1,4 @@
package io.micronaut.inject.visitor.beans;

public class MySuperclass extends PackagePrivateSuperclass {
}
@@ -0,0 +1,22 @@
package io.micronaut.inject.visitor.beans;

class PackagePrivateSuperclass {

String packagePrivateProperty;

String getPackagePrivateProperty() {
return packagePrivateProperty;
}

private String privateProperty;

private String getPrivateProperty() {
return privateProperty;
}

public String publicProperty;

public String getPublicProperty() {
return publicProperty;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 original authors
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@ import com.google.devtools.ksp.getDeclaredProperties
import com.google.devtools.ksp.getKotlinClassByName
import com.google.devtools.ksp.isAbstract
import com.google.devtools.ksp.isConstructor
import com.google.devtools.ksp.isJavaPackagePrivate
import com.google.devtools.ksp.isPrivate
import com.google.devtools.ksp.symbol.ClassKind
import com.google.devtools.ksp.symbol.KSAnnotated
Expand Down Expand Up @@ -64,7 +65,7 @@ import io.micronaut.inject.ast.utils.AstBeanPropertiesUtils
import io.micronaut.inject.ast.utils.EnclosedElementsQuery
import io.micronaut.inject.processing.ProcessingException
import io.micronaut.kotlin.processing.getBinaryName
import java.util.*
import java.util.Optional
import java.util.function.Function
import java.util.stream.Stream

Expand Down Expand Up @@ -815,7 +816,7 @@ internal open class KotlinClassElement(
}

PropertyElement::class.java -> {
classNode.getDeclaredProperties().toList()
classNode.getDeclaredProperties().filter { !it.isJavaPackagePrivate() }.toList()
}

ConstructorElement::class.java -> {
Expand Down
Expand Up @@ -2352,6 +2352,53 @@ class Holder<A : Animal>(
animal.isTypeVariable()
}

void "test package private property introspection"() {
when:
def introspection = buildBeanIntrospection('test.Test', '''
package test
import io.micronaut.core.annotation.Introspected
import io.micronaut.kotlin.processing.visitor.MySuperclass
@Introspected
class Test(val name: String) : MySuperclass()
''')
then: 'the property in this class is introspected'
introspection.getProperty("name").orElse(null)

and: 'the public property in the java superclass is introspected'
introspection.getProperty("publicProperty").orElse(null)

and: 'the private property in the java superclass is not introspected'
!introspection.getProperty("privateProperty").orElse(null)

and: 'the package private superclass property is not introspected'
!introspection.getProperty("packagePrivateProperty").orElse(null)
}

void "test package private property introspection in same package"() {
when:
def introspection = buildBeanIntrospection('io.micronaut.kotlin.processing.visitor.Test', '''
package io.micronaut.kotlin.processing.visitor
import io.micronaut.core.annotation.Introspected
@Introspected
class Test(val name: String) : MySuperclass()
''')
then: 'the property in this class is introspected'
introspection.getProperty("name").orElse(null)

and: 'the public property in the java superclass is introspected'
introspection.getProperty("publicProperty").orElse(null)

and: 'the private property in the java superclass is not introspected'
!introspection.getProperty("privateProperty").orElse(null)

and: 'the package private superclass property is introspected, as we are in the same package'
introspection.getProperty("packagePrivateProperty").orElse(null)
}

void "test list property"() {
given:
BeanIntrospection introspection = buildBeanIntrospection('test.Cart', '''
Expand Down
@@ -0,0 +1,5 @@
package io.micronaut.kotlin.processing.visitor;

public class MySuperclass extends PackagePrivateSuperclass {

}
@@ -0,0 +1,22 @@
package io.micronaut.kotlin.processing.visitor;

class PackagePrivateSuperclass {

String packagePrivateProperty;

String getPackagePrivateProperty() {
return packagePrivateProperty;
}

private String privateProperty;

private String getPrivateProperty() {
return privateProperty;
}

public String publicProperty;

public String getPublicProperty() {
return publicProperty;
}
}

0 comments on commit 32fd3c4

Please sign in to comment.