Moved FindMainMethod into runtime, to avoid the need for hacks (to avoid NoClassDefFoundErrors).

This commit is contained in:
jfrijters 2009-11-25 10:18:29 +00:00
Родитель e6d72c9a8d
Коммит d6f78332b0
2 изменённых файлов: 12 добавлений и 51 удалений

Просмотреть файл

@ -349,7 +349,7 @@ public class Starter
java.lang.Class clazz = java.lang.Class.forName(mainClass, true, java.lang.ClassLoader.getSystemClassLoader());
try
{
Method method = FindMainMethod(clazz);
Method method = IKVM.Internal.Starter.FindMainMethod(clazz);
if(method == null)
{
throw new java.lang.NoSuchMethodError("main");
@ -425,34 +425,4 @@ public class Starter
}
return mainClass.Replace('/', '.');
}
private static Method FindMainMethod(java.lang.Class clazz)
{
// HACK without this hack, clazz.getDeclaredMethods would throw a NoClassDefFoundError if any
// of the methods in the class had an unloadable parameter type, but we don't want that.
IKVM.Internal.Starter.EnableReflectionOnMethodsWithUnloadableTypeParameters = true;
try
{
while(clazz != null)
{
foreach(Method m in clazz.getDeclaredMethods())
{
if(m.getName() == "main" && m.getReturnType() == java.lang.Void.TYPE)
{
java.lang.Class[] parameters = m.getParameterTypes();
if(parameters.Length == 1 && parameters[0] == java.lang.Class.forName("[Ljava.lang.String;"))
{
return m;
}
}
}
clazz = clazz.getSuperclass();
}
return null;
}
finally
{
IKVM.Internal.Starter.EnableReflectionOnMethodsWithUnloadableTypeParameters = false;
}
}
}

Просмотреть файл

@ -46,17 +46,21 @@ namespace IKVM.Internal
DynamicClassLoader.SaveDebugImages();
}
public static bool EnableReflectionOnMethodsWithUnloadableTypeParameters
#if !FIRST_PASS
public static java.lang.reflect.Method FindMainMethod(java.lang.Class clazz)
{
get
// This method exists because we don't use Class.getDeclaredMethods(),
// since that could cause us to run into NoClassDefFoundError if any of the
// method signatures references a missing class.
TypeWrapper tw = TypeWrapper.FromClass(clazz);
MethodWrapper mw = tw.GetMethodWrapper("main", "([Ljava.lang.String;)V", true);
if (mw != null && mw.IsStatic)
{
return JVM.EnableReflectionOnMethodsWithUnloadableTypeParameters;
}
set
{
JVM.EnableReflectionOnMethodsWithUnloadableTypeParameters = value;
return (java.lang.reflect.Method)mw.ToMethodOrConstructor(true);
}
return null;
}
#endif
public static bool ClassUnloading
{
@ -82,7 +86,6 @@ namespace IKVM.Internal
internal const bool IsSaveDebugImage = false;
#else
internal const bool IsStaticCompiler = false;
private static bool enableReflectionOnMethodsWithUnloadableTypeParameters;
private static bool finishingForDebugSave;
private static int emitSymbols;
internal static bool IsSaveDebugImage;
@ -145,18 +148,6 @@ namespace IKVM.Internal
}
#if !STATIC_COMPILER
public static bool EnableReflectionOnMethodsWithUnloadableTypeParameters
{
get
{
return enableReflectionOnMethodsWithUnloadableTypeParameters;
}
set
{
enableReflectionOnMethodsWithUnloadableTypeParameters = value;
}
}
internal static bool FinishingForDebugSave
{
get