mirror of https://github.com/sbt/sbt.git
Allow main class to be non-public. Fixes #883.
This commit is contained in:
parent
72125e73ce
commit
b8b6426cf9
|
|
@ -14,7 +14,13 @@ class Discovery(baseClasses: Set[String], annotations: Set[String])
|
||||||
def apply(d: Definition): Discovered =
|
def apply(d: Definition): Discovered =
|
||||||
d match
|
d match
|
||||||
{
|
{
|
||||||
case c: ClassLike if isPublic(c) && isConcrete(c.modifiers) => discover(c)
|
case c: ClassLike if isConcrete(c.modifiers) =>
|
||||||
|
if(isPublic(c))
|
||||||
|
discover(c)
|
||||||
|
else if(isModule(c) && hasMainMethod(c)) // jvm does not require a main class to be public
|
||||||
|
new Discovered(Set.empty, Set.empty, true, true)
|
||||||
|
else
|
||||||
|
Discovered.empty
|
||||||
case _ => Discovered.empty
|
case _ => Discovered.empty
|
||||||
}
|
}
|
||||||
def discover(c: ClassLike): Discovered =
|
def discover(c: ClassLike): Discovered =
|
||||||
|
|
@ -24,6 +30,7 @@ class Discovery(baseClasses: Set[String], annotations: Set[String])
|
||||||
val module = isModule(c)
|
val module = isModule(c)
|
||||||
new Discovered( bases(c.name, c.structure.parents), onClass ++ onDefs, module && hasMainMethod(c), module )
|
new Discovered( bases(c.name, c.structure.parents), onClass ++ onDefs, module && hasMainMethod(c), module )
|
||||||
}
|
}
|
||||||
|
|
||||||
def bases(own: String, c: Seq[Type]): Set[String] =
|
def bases(own: String, c: Seq[Type]): Set[String] =
|
||||||
(own +: c.flatMap(simpleName)).filter(baseClasses).toSet
|
(own +: c.flatMap(simpleName)).filter(baseClasses).toSet
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -76,6 +76,9 @@ class Run(instance: ScalaInstance, trapExit: Boolean, nativeTmp: File) extends S
|
||||||
{
|
{
|
||||||
val mainClass = Class.forName(mainClassName, true, loader)
|
val mainClass = Class.forName(mainClassName, true, loader)
|
||||||
val method = mainClass.getMethod("main", classOf[Array[String]])
|
val method = mainClass.getMethod("main", classOf[Array[String]])
|
||||||
|
// jvm allows the actual main class to be non-public and to run a method in the non-public class,
|
||||||
|
// we need to make it accessible
|
||||||
|
method.setAccessible(true)
|
||||||
val modifiers = method.getModifiers
|
val modifiers = method.getModifiers
|
||||||
if(!isPublic(modifiers)) throw new NoSuchMethodException(mainClassName + ".main is not public")
|
if(!isPublic(modifiers)) throw new NoSuchMethodException(mainClassName + ".main is not public")
|
||||||
if(!isStatic(modifiers)) throw new NoSuchMethodException(mainClassName + ".main is not static")
|
if(!isStatic(modifiers)) throw new NoSuchMethodException(mainClassName + ".main is not static")
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
public class A{
|
// Java allows the main class to have default access, test that here
|
||||||
|
class A{
|
||||||
public static void main(String... args){
|
public static void main(String... args){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue