diff options
author | Josh Suereth <Joshua.Suereth@gmail.com> | 2012-08-06 13:34:28 -0700 |
---|---|---|
committer | Josh Suereth <Joshua.Suereth@gmail.com> | 2012-08-06 13:34:28 -0700 |
commit | 114367c0b2ce5f48186d4270c1724090fd77877b (patch) | |
tree | 94df12081ce214d5888a0a31c1b7faea8349749e /src/library | |
parent | f5b9ef4cd76d2c6ebf311bcee573fd43fc880326 (diff) | |
parent | 3442155d3582c02563b1a7b929b4bf9b098a148f (diff) | |
download | scala-114367c0b2ce5f48186d4270c1724090fd77877b.tar.gz scala-114367c0b2ce5f48186d4270c1724090fd77877b.tar.bz2 scala-114367c0b2ce5f48186d4270c1724090fd77877b.zip |
Merge pull request #1050 from scalamacros/topic/better-error-messages
better error messages for Symbol.asXXX methods
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/reflect/base/Symbols.scala | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/src/library/scala/reflect/base/Symbols.scala b/src/library/scala/reflect/base/Symbols.scala index 45f7c0c1bd..fe857c540f 100644 --- a/src/library/scala/reflect/base/Symbols.scala +++ b/src/library/scala/reflect/base/Symbols.scala @@ -116,9 +116,9 @@ trait Symbols { self: Universe => def isType: Boolean = false /** This symbol cast to a TypeSymbol. - * Returns ClassCastException if `isType` is false. + * @throws ScalaReflectionException if `isType` is false. */ - def asType: TypeSymbol = throw new ClassCastException(toString) + def asType: TypeSymbol = throw new ScalaReflectionException(s"$this is not a type") /** Does this symbol represent the definition of a term? * Note that every symbol is either a term or a term. @@ -128,9 +128,9 @@ trait Symbols { self: Universe => def isTerm: Boolean = false /** This symbol cast to a TermSymbol. - * Returns ClassCastException if `isTerm` is false. + * @throws ScalaReflectionException if `isTerm` is false. */ - def asTerm: TermSymbol = throw new ClassCastException(toString) + def asTerm: TermSymbol = throw new ScalaReflectionException(s"$this is not a term") /** Does this symbol represent the definition of a method? * If yes, `isTerm` is also guaranteed to be true. @@ -138,9 +138,19 @@ trait Symbols { self: Universe => def isMethod: Boolean = false /** This symbol cast to a MethodSymbol. - * Returns ClassCastException if `isMethod` is false. + * @throws ScalaReflectionException if `isMethod` is false. */ - def asMethod: MethodSymbol = throw new ClassCastException(toString) + def asMethod: MethodSymbol = { + def overloadedMsg = + "encapsulates multiple overloaded alternatives and cannot be treated as a method. "+ + "Consider invoking `<offending symbol>.asTerm.alternatives` and manually picking the required method" + def vanillaMsg = "is not a method" + val msg = if (isOverloadedMethod) overloadedMsg else vanillaMsg + throw new ScalaReflectionException(s"$this $msg") + } + + /** Used to provide a better error message for `asMethod` */ + protected def isOverloadedMethod = false /** Does this symbol represent the definition of a module (i.e. it * results from an object definition?). @@ -149,9 +159,9 @@ trait Symbols { self: Universe => def isModule: Boolean = false /** This symbol cast to a ModuleSymbol defined by an object definition. - * Returns ClassCastException if `isModule` is false. + * @throws ScalaReflectionException if `isModule` is false. */ - def asModule: ModuleSymbol = throw new ClassCastException(toString) + def asModule: ModuleSymbol = throw new ScalaReflectionException(s"$this is not a module") /** Does this symbol represent the definition of a class or trait? * If yes, `isType` is also guaranteed to be true. @@ -165,9 +175,9 @@ trait Symbols { self: Universe => def isModuleClass: Boolean = false /** This symbol cast to a ClassSymbol representing a class or trait. - * Returns ClassCastException if `isClass` is false. + * @throws ScalaReflectionException if `isClass` is false. */ - def asClass: ClassSymbol = throw new ClassCastException(toString) + def asClass: ClassSymbol = throw new ScalaReflectionException(s"$this is not a class") /** Does this symbol represent a free term captured by reification? * If yes, `isTerm` is also guaranteed to be true. @@ -175,9 +185,9 @@ trait Symbols { self: Universe => def isFreeTerm: Boolean = false /** This symbol cast to a free term symbol. - * Returns ClassCastException if `isFreeTerm` is false. + * @throws ScalaReflectionException if `isFreeTerm` is false. */ - def asFreeTerm: FreeTermSymbol = throw new ClassCastException(toString) + def asFreeTerm: FreeTermSymbol = throw new ScalaReflectionException(s"$this is not a free term") /** Does this symbol represent a free type captured by reification? * If yes, `isType` is also guaranteed to be true. @@ -185,9 +195,9 @@ trait Symbols { self: Universe => def isFreeType: Boolean = false /** This symbol cast to a free type symbol. - * Returns ClassCastException if `isFreeType` is false. + * @throws ScalaReflectionException if `isFreeType` is false. */ - def asFreeType: FreeTypeSymbol = throw new ClassCastException(toString) + def asFreeType: FreeTypeSymbol = throw new ScalaReflectionException(s"$this is not a free type") def newTermSymbol(name: TermName, pos: Position = NoPosition, flags: FlagSet = NoFlags): TermSymbol def newModuleAndClassSymbol(name: Name, pos: Position = NoPosition, flags: FlagSet = NoFlags): (ModuleSymbol, ClassSymbol) |