-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support use-site meta-annotations #16445
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
Changes from 14 commits
6d3ee4a
6d5ccbb
1c644d7
78a433f
48a7d11
ad520f3
c2c937e
caf73bf
fa6326a
e580aaa
f847ef6
ceabdbd
8c512cd
e35dc2a
0bd5087
ff68dc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
package dotty.tools.dotc | ||
package dotty.tools | ||
package dotc | ||
dwijnand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
package transform | ||
|
||
import dotty.tools.dotc.ast.{Trees, tpd, untpd, desugar} | ||
|
@@ -186,12 +187,8 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase | |
private def removeUnwantedAnnotations(sym: Symbol, metaAnnotSym: Symbol, | ||
metaAnnotSymBackup: Symbol, keepIfNoRelevantAnnot: Boolean)(using Context): Unit = | ||
def shouldKeep(annot: Annotation): Boolean = | ||
val annotSym = annot.symbol | ||
annotSym.hasAnnotation(metaAnnotSym) | ||
|| annotSym.hasAnnotation(metaAnnotSymBackup) | ||
|| (keepIfNoRelevantAnnot && { | ||
!annotSym.annotations.exists(metaAnnot => defn.FieldAccessorMetaAnnots.contains(metaAnnot.symbol)) | ||
}) | ||
annot.hasOneOfMetaAnnotation(metaAnnotSym, metaAnnotSymBackup) | ||
|| keepIfNoRelevantAnnot && !annot.hasOneOfMetaAnnotation(defn.NonBeanMetaAnnots.toList*) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why exclude "bean" meta annotations here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This runs before bean getters and setters are created. So if we lose the @(JsonProperty @beanGetter) @BeanProperty val beanGet: String = "" Then they'll never be copied over onto the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Memoize, where we split vals into fields and re-written getters/setters, we now also drop annotations that are There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 thanks |
||
if sym.annotations.nonEmpty then | ||
sym.filterAnnotations(shouldKeep(_)) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
true | ||
10 | ||
[@beans.LibraryAnnotation_1()] | ||
[] | ||
some text | ||
other text |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
inspecting constructor MyTable | ||
inspecting param aaaParam1 @MyColumnBase | ||
inspecting param fldParam1 | ||
inspecting param getParam1 | ||
inspecting param parParam1 @MyColumnBase | ||
inspecting field aaaField1 @MyColumnBase | ||
inspecting field aaaParam1 | ||
inspecting field fldField1 @MyColumnBase | ||
inspecting field fldParam1 @MyColumnBase | ||
inspecting field getField1 | ||
inspecting field getParam1 | ||
inspecting field parField1 | ||
inspecting field parParam1 | ||
inspecting method aaaField1 | ||
inspecting method aaaParam1 | ||
inspecting method fldField1 | ||
inspecting method fldParam1 | ||
inspecting method getField1 @MyColumnBase | ||
inspecting method getParam1 @MyColumnBase | ||
inspecting method parField1 | ||
inspecting method parParam1 | ||
inspecting constructor MyTable2 | ||
inspecting param fldParam2 | ||
inspecting param getParam2 | ||
inspecting param parParam2 @MyColumnBase | ||
inspecting field fldField2 @MyColumnBase | ||
inspecting field fldParam2 @MyColumnBase | ||
inspecting field getField2 | ||
inspecting field getParam2 | ||
inspecting field parField2 | ||
inspecting field parParam2 | ||
inspecting method fldField2 | ||
inspecting method fldParam2 | ||
inspecting method getField2 @MyColumnBase | ||
inspecting method getParam2 @MyColumnBase | ||
inspecting method parField2 | ||
inspecting method parParam2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface MyColumnBase {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import scala.annotation.meta.{ field as fld, getter as get, param as par } | ||
|
||
type FldColumn = MyColumnBase @fld | ||
type GetColumn = MyColumnBase @get | ||
type ParColumn = MyColumnBase @par | ||
|
||
class MyTable( | ||
@(MyColumnBase ) val aaaParam1: String, | ||
@(MyColumnBase @fld) val fldParam1: String, | ||
@(MyColumnBase @get) val getParam1: String, | ||
@(MyColumnBase @par) val parParam1: String, | ||
) { | ||
@(MyColumnBase ) val aaaField1: String = "" | ||
@(MyColumnBase @fld) val fldField1: String = "" | ||
@(MyColumnBase @get) val getField1: String = "" | ||
@(MyColumnBase @par) val parField1: String = "" | ||
} | ||
|
||
class MyTable2( | ||
@FldColumn val fldParam2: String, | ||
@GetColumn val getParam2: String, | ||
@ParColumn val parParam2: String, | ||
) { | ||
@FldColumn val fldField2: String = "" | ||
@GetColumn val getField2: String = "" | ||
@ParColumn val parField2: String = "" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// scalajs: --skip | ||
object Test: | ||
def main(args: Array[String]): Unit = | ||
go(classOf[MyTable]) | ||
go(classOf[MyTable2]) | ||
|
||
def go(cls: Class[?]): Unit = | ||
for c <- cls.getDeclaredConstructors.sortBy(_.getName) do | ||
c.setAccessible(true) | ||
println(s"inspecting constructor ${c.getName}") | ||
for p <- c.getParameters.sortBy(_.getName) do | ||
print(s"inspecting param ${p.getName}") | ||
for a <- p.getAnnotations.sortBy(_.annotationType.toString) do | ||
print(s" @${a.annotationType.getName}") | ||
println() | ||
|
||
for (m <- cls.getDeclaredFields.sortBy(_.getName)) { | ||
m.setAccessible(true) | ||
print(s"inspecting field ${m.getName}") | ||
for a <- m.getAnnotations().sortBy(_.annotationType.toString) do | ||
print(s" @${a.annotationType.getName}") | ||
println() | ||
} | ||
|
||
for (m <- cls.getDeclaredMethods.sortBy(_.getName)) { | ||
m.setAccessible(true) | ||
print(s"inspecting method ${m.getName}") | ||
for a <- m.getAnnotations().sortBy(_.annotationType.toString) do | ||
print(s" @${a.annotationType.getName}") | ||
println() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
inspecting constructor Bean | ||
inspecting field beanAaa @JsonProperty | ||
inspecting field beanGet | ||
inspecting field beanSet | ||
inspecting field normAaa @JsonProperty | ||
inspecting field normFld @JsonProperty | ||
inspecting field normGet | ||
inspecting field normSet | ||
inspecting method beanAaa | ||
inspecting method beanGet | ||
inspecting method beanSet | ||
inspecting method beanSet_$eq | ||
inspecting method getBeanAaa | ||
inspecting method getBeanGet @JsonProperty | ||
inspecting method getBeanSet | ||
inspecting method normAaa | ||
inspecting method normFld | ||
inspecting method normGet @JsonProperty | ||
inspecting method normSet | ||
inspecting method normSet_$eq @JsonProperty | ||
inspecting method setBeanSet @JsonProperty | ||
inspecting constructor Bean2 | ||
inspecting field beanGet | ||
inspecting field beanSet | ||
inspecting method beanGet | ||
inspecting method beanSet | ||
inspecting method beanSet_$eq | ||
inspecting method getBeanGet @JsonProperty | ||
inspecting method getBeanSet | ||
inspecting method setBeanSet @JsonProperty |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import scala.annotation.meta.{ field as fld, getter as get, setter as set, * } | ||
import scala.beans.BeanProperty | ||
|
||
type BeanGetJsonProperty = JsonProperty @beanGetter | ||
type BeanSetJsonProperty = JsonProperty @beanSetter | ||
|
||
class Bean { | ||
@(JsonProperty ) val normAaa: String = "" | ||
@(JsonProperty @fld) val normFld: String = "" | ||
@(JsonProperty @get) val normGet: String = "" | ||
@(JsonProperty @set) var normSet: String = "" | ||
|
||
@(JsonProperty ) @BeanProperty val beanAaa: String = "" | ||
@(JsonProperty @beanGetter) @BeanProperty val beanGet: String = "" | ||
@(JsonProperty @beanSetter) @BeanProperty var beanSet: String = "" | ||
} | ||
|
||
class Bean2 { | ||
@BeanGetJsonProperty @BeanProperty val beanGet: String = "" | ||
@BeanSetJsonProperty @BeanProperty var beanSet: String = "" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface JsonProperty {} |
Uh oh!
There was an error while loading. Please reload this page.