aboutsummaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
authorChristopher Vogt <oss.nsp@cvogt.org>2017-03-28 09:35:24 -0400
committerChristopher Vogt <oss.nsp@cvogt.org>2017-03-28 10:09:15 -0400
commita3e5f304e99ff47165af6ed20182f06700e15b33 (patch)
treeecd5ab13a2a2c5a11415fa9d4f64f3634e02309a /libraries
parent497fff26705be6a21e6099a562ff14bfe1d90003 (diff)
downloadcbt-a3e5f304e99ff47165af6ed20182f06700e15b33.tar.gz
cbt-a3e5f304e99ff47165af6ed20182f06700e15b33.tar.bz2
cbt-a3e5f304e99ff47165af6ed20182f06700e15b33.zip
minor reflection related refactor
Diffstat (limited to 'libraries')
-rw-r--r--libraries/reflect/StaticMethod.scala3
-rw-r--r--libraries/reflect/build/build.scala2
-rw-r--r--libraries/reflect/reflect.scala47
3 files changed, 31 insertions, 21 deletions
diff --git a/libraries/reflect/StaticMethod.scala b/libraries/reflect/StaticMethod.scala
index e2a0d07..d7ab3c5 100644
--- a/libraries/reflect/StaticMethod.scala
+++ b/libraries/reflect/StaticMethod.scala
@@ -1,4 +1,5 @@
package cbt.reflect
-case class StaticMethod[Arg, Result]( function: Arg => Result, name: String ) extends ( Arg => Result ) {
+import java.lang.reflect.Method
+case class StaticMethod[Arg, Result]( function: Arg => Result, method: Method ) extends ( Arg => Result ) {
def apply( arg: Arg ): Result = function( arg )
}
diff --git a/libraries/reflect/build/build.scala b/libraries/reflect/build/build.scala
index 5c27090..3b6658a 100644
--- a/libraries/reflect/build/build.scala
+++ b/libraries/reflect/build/build.scala
@@ -4,5 +4,5 @@ import cbt_internal._
class Build(val context: Context) extends Library{
override def inceptionYear = 2017
override def description = "discover classes on your classpath and invoke methods reflectively, preventing System.exit"
- override def dependencies = super.dependencies :+ libraries.file
+ override def dependencies = super.dependencies :+ libraries.file :+ libraries.common_1
}
diff --git a/libraries/reflect/reflect.scala b/libraries/reflect/reflect.scala
index c18d926..bd7c245 100644
--- a/libraries/reflect/reflect.scala
+++ b/libraries/reflect/reflect.scala
@@ -7,6 +7,7 @@ import scala.reflect.ClassTag
import cbt.ExitCode
import cbt.file._
+import cbt.common_1._
object `package` extends Module {
implicit class CbtClassOps( val c: Class[_] ) extends AnyVal with ops.CbtClassOps
@@ -54,27 +55,37 @@ package ops {
def isSynchronized = Modifier.isSynchronized( m.getModifiers )
def isTransient = Modifier.isTransient( m.getModifiers )
def isVolatile = Modifier.isVolatile( m.getModifiers )
+
+ def show = (
+ m.name ~ "( "
+ ~ m.parameters.map( _.getType.name ).mkString( ", " )
+ ~ " )"
+ )
}
}
trait Module {
- def runMain( cls: Class[_], args: Seq[String] ): ExitCode =
- discoverStaticExitMethodForced[Array[String]]( cls, "main" ).apply( args.to )
+ def getMain( cls: Class[_] ): StaticMethod[Seq[String], ExitCode] = {
+ val f = findStaticExitMethodForced[Array[String]]( cls, "main" )
+ f.copy(
+ function = ( args: Seq[String] ) => f.function( args.to )
+ )
+ }
- def discoverMain( cls: Class[_] ): Option[StaticMethod[Seq[String], ExitCode]] = {
- discoverStaticExitMethod[Array[String]]( cls, "main" )
+ def findMain( cls: Class[_] ): Option[StaticMethod[Seq[String], ExitCode]] = {
+ findStaticExitMethod[Array[String]]( cls, "main" )
.map( f =>
f.copy(
- function = ( arg: Seq[String] ) => f.function( arg.to )
+ function = ( args: Seq[String] ) => f.function( args.to )
) )
}
/** ignoreMissingClasses allows ignoring other classes root directories which are subdirectories of this one */
- def iterateClasses(
+ def topLevelClasses(
classesRootDirectory: File,
classLoader: ClassLoader,
ignoreMissingClasses: Boolean
): Seq[Class[_]] =
- iterateClassNames( classesRootDirectory )
+ topLevelClassNames( classesRootDirectory )
.map { name =>
try {
classLoader.loadClass( name )
@@ -85,10 +96,10 @@ trait Module {
}
.filterNot( ignoreMissingClasses && _ == null )
- /** Given a directory corresponding to the root package, iterate
- * the names of all classes derived from the class files found
+ /** Given a directory corresponding to the root package, return
+ * the names of all top-level classes based on the class files found
*/
- def iterateClassNames( classesRootDirectory: File ): Seq[String] =
+ def topLevelClassNames( classesRootDirectory: File ): Seq[String] =
classesRootDirectory.listRecursive
.filter( _.isFile )
.map( _.getPath )
@@ -102,16 +113,16 @@ trait Module {
.replace( File.separator, "." )
}
- def discoverStaticExitMethodForced[Arg: ClassTag](
+ def findStaticExitMethodForced[Arg: ClassTag](
cls: Class[_], name: String
): StaticMethod[Arg, ExitCode] = {
- val f = discoverStaticMethodForced[Arg, Unit]( cls, name )
+ val f = findStaticMethodForced[Arg, Unit]( cls, name )
f.copy(
function = arg => trapExitCode { f.function( arg ); ExitCode.Success }
)
}
- def discoverStaticMethodForced[Arg, Result](
+ def findStaticMethodForced[Arg, Result](
cls: Class[_], name: String
)(
implicit
@@ -122,15 +133,15 @@ trait Module {
typeStaticMethod( m )
}
- def discoverStaticExitMethod[Arg: ClassTag](
+ def findStaticExitMethod[Arg: ClassTag](
cls: Class[_], name: String
): Option[StaticMethod[Arg, ExitCode]] =
- discoverStaticMethod[Arg, Unit]( cls, name ).map( f =>
+ findStaticMethod[Arg, Unit]( cls, name ).map( f =>
f.copy(
function = arg => trapExitCode { f.function( arg ); ExitCode.Success }
) )
- def discoverStaticMethod[Arg, Result](
+ def findStaticMethod[Arg, Result](
cls: Class[_], name: String
)(
implicit
@@ -157,9 +168,7 @@ trait Module {
else m.declaringClass.newInstance // Dottydoc needs this. It's main method is not static.
StaticMethod(
arg => m.invoke( instance, arg.asInstanceOf[AnyRef] ).asInstanceOf[Result],
- m.getClass.name.stripSuffix( "$" ) ++ "." ++ m.name ++ "( "
- ++ m.parameters.map( _.getType.name ).mkString( ", " )
- ++ " )"
+ m
)
}