Last active
August 29, 2015 14:07
-
-
Save piyo7/11704b373d5ea9082daf to your computer and use it in GitHub Desktop.
Scalaのリフレクションでメソッド一覧をテキスト出力 ref: http://qiita.com/piyo7/items/0218c01b6cd524326908
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sbt.version=0.13.6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name := "hello_universe" | |
version := "1.0" | |
scalaVersion := "2.11.2" | |
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.reflect.runtime.universe._ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
printMethods[List[Int]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
addString(b: StringBuilder): StringBuilder | |
addString(b: StringBuilder, sep: String): StringBuilder | |
addString(b: StringBuilder, start: String, sep: String, end: String): StringBuilder | |
aggregate[B](z: => B)(seqop: (B, A) => B, combop: (B, B) => B): B | |
andThen[C](k: B => C): PartialFunction[A,C] | |
apply(n: Int): A | |
applyOrElse[A1][B1](x: A1, default: A1 => B1): B1 | |
asInstanceOf[T0]: T0 | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object HelloUniverse extends App { | |
implicit class RichList[T](self: List[T]) { | |
def mkStringIfNonEmpty[T](start: String, | |
seq: String, | |
end: String): String = | |
if (self.nonEmpty) self.mkString(start, seq, end) | |
else "" | |
} | |
import scala.reflect.runtime.universe._ | |
def paramToString(param: Symbol): String = | |
(if (param.isImplicit) "implicit " else "") + | |
param.name + | |
": " + | |
param.typeSignature | |
def methodToString(method: MethodSymbol): String = | |
method.name.decodedName + | |
method.typeParams.map( | |
_.name | |
).mkStringIfNonEmpty("[", "][", "]") + | |
method.paramLists.map( | |
_.map(paramToString).mkString(", ") | |
).mkStringIfNonEmpty("(", ")(", ")") + | |
": " + | |
method.returnType | |
def printMethods[T: TypeTag] = | |
typeOf[T]. | |
members. | |
filter(_.isMethod). | |
map(_.asMethod). | |
map(methodToString). | |
toArray. | |
sorted. | |
foreach(println) | |
printMethods[List[Int]] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment