diff options
author | Eugene Burmako <xeno.by@gmail.com> | 2012-07-30 22:35:36 +0200 |
---|---|---|
committer | Eugene Burmako <xeno.by@gmail.com> | 2012-08-02 15:36:57 +0200 |
commit | 7112c66d6951ac83ae3591426291ec2797824258 (patch) | |
tree | f8da05ea01df38b658c4b0cc75e96900c6dd4f32 | |
parent | 37d4bfe77c1d8443e522a951cd404459a409035b (diff) | |
download | scala-7112c66d6951ac83ae3591426291ec2797824258.tar.gz scala-7112c66d6951ac83ae3591426291ec2797824258.tar.bz2 scala-7112c66d6951ac83ae3591426291ec2797824258.zip |
navigation between fields and accessors
This works around SI-5736 that's been deemed too risky to be fixed in 2.10.0.
A reflection newbie will be unlikely to acquire a field symbol from its name,
but the `accessed` method provides an easy way to navigate to it from a getter.
-rw-r--r-- | src/reflect/scala/reflect/api/Mirrors.scala | 2 | ||||
-rw-r--r-- | src/reflect/scala/reflect/api/Symbols.scala | 12 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/Symbols.scala | 2 | ||||
-rw-r--r-- | test/files/run/reflection-fieldsymbol-navigation.check | 6 | ||||
-rw-r--r-- | test/files/run/reflection-fieldsymbol-navigation.scala | 15 |
5 files changed, 36 insertions, 1 deletions
diff --git a/src/reflect/scala/reflect/api/Mirrors.scala b/src/reflect/scala/reflect/api/Mirrors.scala index 27176a2a2d..fe4348d568 100644 --- a/src/reflect/scala/reflect/api/Mirrors.scala +++ b/src/reflect/scala/reflect/api/Mirrors.scala @@ -34,7 +34,7 @@ trait Mirrors { self: Universe => * that can be used to get and, if appropriate, set the value of the field. * * To get a field symbol by the name of the field you would like to reflect, - * use `<this mirror>.symbol.typeSignature.member(newTermName(<name of the field>)).asTermSymbol`. + * use `<this mirror>.symbol.typeSignature.member(newTermName(<name of the field>)).asTermSymbol.accessed`. * For further information about member lookup refer to `Symbol.typeSignature`. * * The input symbol can be either private or non-private (Scala reflection transparently deals with visibility). diff --git a/src/reflect/scala/reflect/api/Symbols.scala b/src/reflect/scala/reflect/api/Symbols.scala index 7a5db4a3f5..7ca1d9c7c6 100644 --- a/src/reflect/scala/reflect/api/Symbols.scala +++ b/src/reflect/scala/reflect/api/Symbols.scala @@ -185,6 +185,18 @@ trait Symbols extends base.Symbols { self: Universe => /** The overloaded alternatives of this symbol */ def alternatives: List[Symbol] + + /** Backing field for an accessor method, NoSymbol for all other term symbols. + */ + def accessed: Symbol + + /** Getter method for a backing field of a val or a val, NoSymbol for all other term symbols. + */ + def getter: Symbol + + /** Setter method for a backing field of a val or a val, NoSymbol for all other term symbols. + */ + def setter: Symbol } /** The API of type symbols */ diff --git a/src/reflect/scala/reflect/internal/Symbols.scala b/src/reflect/scala/reflect/internal/Symbols.scala index 05ead8d1ac..8cf20ba062 100644 --- a/src/reflect/scala/reflect/internal/Symbols.scala +++ b/src/reflect/scala/reflect/internal/Symbols.scala @@ -84,6 +84,8 @@ trait Symbols extends api.Symbols { self: SymbolTable => def getAnnotations: List[AnnotationInfo] = { initialize; annotations } def setAnnotations(annots: AnnotationInfo*): this.type = { setAnnotations(annots.toList); this } + def getter: Symbol = getter(owner) + def setter: Symbol = setter(owner) } /** The class for all symbols */ diff --git a/test/files/run/reflection-fieldsymbol-navigation.check b/test/files/run/reflection-fieldsymbol-navigation.check new file mode 100644 index 0000000000..79f0928ea5 --- /dev/null +++ b/test/files/run/reflection-fieldsymbol-navigation.check @@ -0,0 +1,6 @@ +method x
+false
+variable x
+true
+method x
+method x_=
diff --git a/test/files/run/reflection-fieldsymbol-navigation.scala b/test/files/run/reflection-fieldsymbol-navigation.scala new file mode 100644 index 0000000000..da4612a564 --- /dev/null +++ b/test/files/run/reflection-fieldsymbol-navigation.scala @@ -0,0 +1,15 @@ +import scala.reflect.runtime.universe._ + +class C { + var x = 2 +} + +object Test extends App { + val x = typeOf[C].member(newTermName("x")).asTerm + println(x) + println(x.isVariable) + println(x.accessed) + println(x.accessed.asTerm.isVariable) + println(x.getter) + println(x.setter) +}
\ No newline at end of file |