summaryrefslogtreecommitdiff
path: root/src/reflect/scala/reflect/internal/Symbols.scala
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2015-09-02 13:13:06 +1000
committerJason Zaugg <jzaugg@gmail.com>2015-09-02 13:43:17 +1000
commit9a3089bd4ac68db798ac006731ebd1b99e9aaaff (patch)
tree0ecb18e1b3e6945df81f02412af63868979be6c0 /src/reflect/scala/reflect/internal/Symbols.scala
parentf9b52c7086ad1541cb949dd4ff2412990bde792e (diff)
downloadscala-9a3089bd4ac68db798ac006731ebd1b99e9aaaff.tar.gz
scala-9a3089bd4ac68db798ac006731ebd1b99e9aaaff.tar.bz2
scala-9a3089bd4ac68db798ac006731ebd1b99e9aaaff.zip
Add a convenience method to Symbol to "resugar" fields
val and var members of classes are split into getter/setter and field symbols. However, we can't call `defString` on any of these and see what existed in source code. Example: ``` scala> class X { protected var x: Int = 0 } defined class X scala> val xField = typeOf[X].member(TermName("x ")) xField: $r.intp.global.Symbol = variable x scala> xField.defString res10: String = private[this] var x: Int scala> xField.getterIn(xField.owner).defString res11: String = protected def x: Int scala> xField.setterIn(xField.owner).defString res12: String = protected def x_=(x$1: Int): Unit ``` This commit introduces a new method on `Symbol` that pieces together the information from these symbols to create an artificial symbol that has the `defString` we're after: ``` scala> xField.sugaredSymbolOrSelf.defString res14: String = protected var x: Int ```
Diffstat (limited to 'src/reflect/scala/reflect/internal/Symbols.scala')
-rw-r--r--src/reflect/scala/reflect/internal/Symbols.scala13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/reflect/scala/reflect/internal/Symbols.scala b/src/reflect/scala/reflect/internal/Symbols.scala
index 2e3449588b..4bdb8e6da8 100644
--- a/src/reflect/scala/reflect/internal/Symbols.scala
+++ b/src/reflect/scala/reflect/internal/Symbols.scala
@@ -500,6 +500,19 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
case _ => new StubTermSymbol(this, name.toTermName, missingMessage)
}
+ /** Given a field, construct a term symbol that represents the source construct that gave rise the the field */
+ def sugaredSymbolOrSelf = {
+ val getter = getterIn(owner)
+ if (getter == NoSymbol) {
+ this
+ } else {
+ val result = owner.newValue(getter.name.toTermName, newFlags = getter.flags & ~Flags.METHOD).setPrivateWithin(getter.privateWithin).setInfo(getter.info.resultType)
+ val setter = setterIn(owner)
+ if (setter != NoSymbol) result.setFlag(Flags.MUTABLE)
+ result
+ }
+ }
+
// ----- locking and unlocking ------------------------------------------------------
// True if the symbol is unlocked.