Skip to content

Commit

Permalink
Make setter's parameter name value by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ting-yuan committed Sep 13, 2024
1 parent 20ec7f7 commit f47c2b6
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class KSValueParameterImpl private constructor(

override val name: KSName? by lazy {
if (origin == Origin.SYNTHETIC && parent is KSPropertySetter) {
KSNameImpl.getCached("<set-?>")
KSNameImpl.getCached("value")
} else {
KSNameImpl.getCached(ktValueParameterSymbol.name.asString())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class KSPAATest : AbstractKSPAATest() {
@TestMetadata("annotationInDependencies.kt")
@Test
fun testAnnotationsInDependencies() {
runTest("../test-utils/testData/api/annotationInDependencies.kt")
runTest("../kotlin-analysis-api/testData/annotationInDependencies.kt")
}

@TestMetadata("annotationOnConstructorParameter.kt")
Expand Down Expand Up @@ -318,7 +318,7 @@ class KSPAATest : AbstractKSPAATest() {
@TestMetadata("implicitPropertyAccessors.kt")
@Test
fun testImplicitPropertyAccessors() {
runTest("../test-utils/testData/api/implicitPropertyAccessors.kt")
runTest("../kotlin-analysis-api/testData/implicitPropertyAccessors.kt")
}

@TestMetadata("inheritedTypeAlias.kt")
Expand Down
202 changes: 202 additions & 0 deletions kotlin-analysis-api/testData/annotationInDependencies.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
* Copyright 2020 Google LLC
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
*
* 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.
*/

// WITH_RUNTIME
// TEST PROCESSOR: AnnotationsInDependenciesProcessor
// EXPECTED:
// main.KotlinClass ->
// class main.KotlinClass : annotations.ClassTarget{[value = onClass]}
// class main.KotlinClass : annotations.NoTargetAnnotation{[value = onClass]}
// function myFun : annotations.FunctionTarget{[value = onMyFun]}
// function myFun : annotations.NoTargetAnnotation{[value = onMyFun]}
// getter of property prop : annotations.PropertyGetterTarget{[value = get:]}
// parameter param1 : annotations.NoTargetAnnotation{[value = onParam1]}
// parameter param1 : annotations.ValueParameterTarget{[value = onParam1]}
// parameter param2 : annotations.NoTargetAnnotation{[value = onParam2]}
// parameter param2 : annotations.ValueParameterTarget{[value = onParam2]}
// parameter value : annotations.ValueParameterTarget{[value = onPropSetter]}
// parameter value : annotations.ValueParameterTarget{[value = onProp]}
// property prop : annotations.FieldTarget2{[value = field:]}
// property prop : annotations.FieldTarget{[value = onProp]}
// property prop : annotations.NoTargetAnnotation{[value = onProp]}
// property prop : annotations.PropertyTarget{[value = onProp]}
// setter of property prop : annotations.PropertySetterTarget{[value = set:]}
// lib.KotlinClass ->
// class lib.KotlinClass : annotations.ClassTarget{[value = onClass]}
// class lib.KotlinClass : annotations.NoTargetAnnotation{[value = onClass]}
// function myFun : annotations.FunctionTarget{[value = onMyFun]}
// function myFun : annotations.NoTargetAnnotation{[value = onMyFun]}
// getter of property prop : annotations.PropertyGetterTarget{[value = get:]}
// parameter param1 : annotations.NoTargetAnnotation{[value = onParam1]}
// parameter param1 : annotations.ValueParameterTarget{[value = onParam1]}
// parameter param2 : annotations.NoTargetAnnotation{[value = onParam2]}
// parameter param2 : annotations.ValueParameterTarget{[value = onParam2]}
// parameter propInConstructor : annotations.ValueParameterTarget{[value = propInConstructor]}
// property prop : annotations.FieldTarget2{[value = field:]}
// property prop : annotations.FieldTarget{[value = onProp]}
// property prop : annotations.NoTargetAnnotation{[value = onProp]}
// property prop : annotations.PropertyTarget{[value = onProp]}
// setter of property prop : annotations.PropertySetterTarget{[value = set:]}
// main.DataClass ->
// class main.DataClass : annotations.ClassTarget{[value = onDataClass]}
// class main.DataClass : annotations.NoTargetAnnotation{[value = onDataClass]}
// getter of property constructorParam : annotations.PropertyGetterTarget{[value = get:]}
// parameter constructorParam : annotations.NoTargetAnnotation{[value = onConstructorParam]}
// parameter constructorParam : annotations.ValueParameterTarget{[value = onConstructorParam]}
// parameter value : annotations.ValueParameterTarget{[value = onConstructorParam]}
// property constructorParam : annotations.FieldTarget2{[value = field:]}
// property constructorParam : annotations.FieldTarget{[value = onConstructorParam]}
// property constructorParam : annotations.NoTargetAnnotation{[value = onConstructorParam]}
// property constructorParam : annotations.PropertyTarget{[value = onConstructorParam]}
// property constructorParam : annotations.ValueParameterAndFieldTarget{[value = valueParameterAndField]}
// setter of property constructorParam : annotations.PropertySetterTarget{[value = set:]}
// lib.DataClass ->
// class lib.DataClass : annotations.ClassTarget{[value = onDataClass]}
// class lib.DataClass : annotations.NoTargetAnnotation{[value = onDataClass]}
// getter of property constructorParam : annotations.PropertyGetterTarget{[value = get:]}
// parameter constructorParam : annotations.NoTargetAnnotation{[value = onConstructorParam]}
// property constructorParam : annotations.FieldTarget2{[value = field:]}
// property constructorParam : annotations.FieldTarget{[value = onConstructorParam]}
// property constructorParam : annotations.PropertyTarget{[value = onConstructorParam]}
// setter of property constructorParam : annotations.PropertySetterTarget{[value = set:]}
// END
// MODULE: annotations
// FILE: Annotations.kt
package annotations;
annotation class NoTargetAnnotation(val value:String)

@Target(AnnotationTarget.FIELD)
annotation class FieldTarget(val value:String)

@Target(AnnotationTarget.FIELD)
annotation class FieldTarget2(val value:String)

@Target(AnnotationTarget.PROPERTY)
annotation class PropertyTarget(val value:String)

@Target(AnnotationTarget.PROPERTY_SETTER)
annotation class PropertySetterTarget(val value:String)

@Target(AnnotationTarget.PROPERTY_GETTER)
annotation class PropertyGetterTarget(val value:String)

@Target(AnnotationTarget.CLASS)
annotation class ClassTarget(val value:String)

@Target(AnnotationTarget.FUNCTION)
annotation class FunctionTarget(val value:String)

@Target(AnnotationTarget.VALUE_PARAMETER)
annotation class ValueParameterTarget(val value:String)

@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.FIELD)
annotation class ValueParameterAndFieldTarget(val value: String)

// MODULE: lib(annotations)
// FILE: ClassInLib.kt
package lib;
import annotations.*;
@NoTargetAnnotation("onClass")
@ClassTarget("onClass")
class KotlinClass(@ValueParameterTarget("propInConstructor") val propInConstructor: String ) {
@NoTargetAnnotation("onProp")
@FieldTarget("onProp")
@PropertyTarget("onProp")
@set:PropertySetterTarget("set:")
@get:PropertyGetterTarget("get:")
@field:FieldTarget2("field:")
var prop : String = ""

@NoTargetAnnotation("onMyFun")
@FunctionTarget("onMyFun")
fun myFun(
@NoTargetAnnotation("onParam1")
@ValueParameterTarget("onParam1")
param1: String,
@NoTargetAnnotation("onParam2")
@ValueParameterTarget("onParam2")
param2: Int
) {
}
}

@NoTargetAnnotation("onDataClass")
@ClassTarget("onDataClass")
class DataClass(
@NoTargetAnnotation("onConstructorParam")
@FieldTarget("onConstructorParam")
@PropertyTarget("onConstructorParam")
@set:PropertySetterTarget("set:")
@get:PropertyGetterTarget("get:")
@field:FieldTarget2("field:")
var constructorParam : String = ""
)
// FILE: lib/JavaClass.java
package lib;
import annotations.*;
public class JavaClass {}
// MODULE: main(lib, annotations)
// FILE: ClassInModule2.kt
package main;
import annotations.*;
@NoTargetAnnotation("onClass")
@ClassTarget("onClass")
class KotlinClass {
@NoTargetAnnotation("onProp")
@FieldTarget("onProp")
@PropertyTarget("onProp")
@set:PropertySetterTarget("set:")
@get:PropertyGetterTarget("get:")
@field:FieldTarget2("field:")
@setparam:ValueParameterTarget("onProp")
var prop : String = ""
@setparam:ValueParameterTarget("onPropSetter")
set

@NoTargetAnnotation("onMyFun")
@FunctionTarget("onMyFun")
fun myFun(
@NoTargetAnnotation("onParam1")
@ValueParameterTarget("onParam1")
param1: String,
@NoTargetAnnotation("onParam2")
@ValueParameterTarget("onParam2")
param2: Int
) {
}
}

@NoTargetAnnotation("onDataClass")
@ClassTarget("onDataClass")
class DataClass(
@NoTargetAnnotation("onConstructorParam")
@FieldTarget("onConstructorParam")
@PropertyTarget("onConstructorParam")
@set:PropertySetterTarget("set:")
@get:PropertyGetterTarget("get:")
@field:FieldTarget2("field:")
@field:ValueParameterAndFieldTarget("valueParameterAndField")
@setparam:ValueParameterTarget("onConstructorParam")
@ValueParameterTarget("onConstructorParam")
var constructorParam : String = ""
)
// FILE: main/JavaClassInModule2.java
pakage main;
import annotations.*;
@NoTargetAnnotation
class JavaClassInMain {
}
2 changes: 1 addition & 1 deletion kotlin-analysis-api/testData/getSymbolsFromAnnotation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
// RGB.B:KSClassDeclaration
// ==== Cnno in depth ====
// constructorParameterFoo:KSPropertyDeclaration
// <set-?>:KSValueParameter
// value:KSValueParameter
// constructorParameterFoo:KSValueParameter
// x:KSPropertyDeclaration
// x:KSValueParameter
Expand Down
45 changes: 45 additions & 0 deletions kotlin-analysis-api/testData/implicitPropertyAccessors.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2022 Google LLC
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
*
* 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.
*/

// TEST PROCESSOR: ImplicitPropertyAccessorProcessor
// EXPECTED:
// privateGetterVal.getter(): Int
// privateGetterVar.getter(): String
// privateGetterVar.setter()(value: String)
// val1.getter(): Int
// var2.getter(): String
// var2.setter()(value: String)
// END
// MODULE: lib
// FILE: lib/Bar.kt
package lib

class Bar {
val val1: Int = 0
var var2: String = ""
}
// MODULE: main(lib)
// FILE: Foo.kt

class Foo {
val privateGetterVal: Int
private get

var privateGetterVar: String
set
private get
}
14 changes: 7 additions & 7 deletions kotlin-analysis-api/testData/javaWildcards2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@
// R : Any?
// propWithFinalType : String
// propWithFinalType.getter() : String
// <set-?> : String
// value : String
// propWithOpenType : Number
// propWithOpenType.getter() : Number
// <set-?> : Number
// value : Number
// propWithFinalGeneric : List<String>
// propWithFinalGeneric.getter() : List<String>
// <set-?> : List<String>
// value : List<String>
// propWithOpenGeneric : List<Number>
// propWithOpenGeneric.getter() : List<Number>
// <set-?> : List<Number>
// value : List<Number>
// propWithTypeArg : R
// propWithTypeArg.getter() : R
// <set-?> : R
// value : R
// propWithTypeArgGeneric : List<R>
// propWithTypeArgGeneric.getter() : List<R>
// <set-?> : List<R>
// value : List<R>
// propWithOpenTypeButSuppressAnnotation : Number
// propWithOpenTypeButSuppressAnnotation.getter() : Number
// <set-?> : Number
// value : Number
// list2 : List<Any?>
// list1 : List<Any?>
// list3 : List<R>
Expand Down
4 changes: 2 additions & 2 deletions kotlin-analysis-api/testData/parent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@
// parent of String: String
// parent of String: b.getter()
// parent of b.getter(): b
// parent of String: <set-?>
// parent of <set-?>: b.setter()
// parent of String: value
// parent of value: b.setter()
// parent of b.setter(): b
// parent of b: topClass
// parent of topClass: synthetic constructor for topClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ class ImplicitPropertyAccessorProcessor : AbstractTestProcessor() {
}

override fun process(resolver: Resolver): List<KSAnnotated> {
val foo = resolver.getClassDeclarationByName("Foo")!!
foo.declarations.filterIsInstance<KSPropertyDeclaration>().forEach { prop ->
result.add(prop.getter?.returnType.toString())
prop.setter?.parameter?.let {
result.add(it.toString())
result.add(it.type.toString())
listOf("Foo", "lib.Bar").forEach { clsName ->
val cls = resolver.getClassDeclarationByName(clsName)!!
cls.declarations.filterIsInstance<KSPropertyDeclaration>().forEach { prop ->
prop.getter?.let { getter ->
result.add("$getter: ${getter.returnType}")
}
prop.setter?.let { setter ->
val param = setter.parameter
val paramName = param.name?.getShortName()
result.add("$setter($paramName: ${setter.parameter.type})")
}
}
}
return emptyList()
Expand Down
21 changes: 16 additions & 5 deletions test-utils/testData/api/implicitPropertyAccessors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@

// TEST PROCESSOR: ImplicitPropertyAccessorProcessor
// EXPECTED:
// Int
// String
// <set-?>
// String
// privateGetterVal.getter(): Int
// privateGetterVar.getter(): String
// privateGetterVar.setter()(<set-?>: String)
// val1.getter(): Int
// var2.getter(): String
// var2.setter()(<set-?>: String)
// END
// FILE: a.kt
// MODULE: lib
// FILE: lib/Bar.kt
package lib

class Bar {
val val1: Int = 0
var var2: String = ""
}
// MODULE: main(lib)
// FILE: Foo.kt

class Foo {
val privateGetterVal: Int
Expand Down

0 comments on commit f47c2b6

Please sign in to comment.