Skip to content

Commit 3abe69f

Browse files
Try to disambiguate class vs method name
There is no syntactic way to distinguish a method from a class name. Therefore, we try loading the class and if it fails we assume it was a method name and try again with what we think the class name is.
1 parent c498c78 commit 3abe69f

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

jbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,16 +318,46 @@ void java_bytecode_languaget::initialize_class_loader()
318318
}
319319
}
320320

321+
static void throwMainClassLoadingError(const std::string &main_class)
322+
{
323+
throw invalid_source_file_exceptiont(
324+
"Error: Could not find or load main class " + main_class);
325+
}
326+
321327
void java_bytecode_languaget::parse_from_main_class()
322328
{
323329
if(!main_class.empty())
324330
{
325-
status() << "Java main class: " << main_class << eom;
331+
// Try to load class
332+
status() << "Trying to load Java main class: " << main_class << eom;
333+
if(!java_class_loader.can_load_class(main_class))
334+
{
335+
// Try to extract class name and load class
336+
const auto maybe_class_name =
337+
class_name_from_method_name(id2string(main_class));
338+
if(!maybe_class_name)
339+
{
340+
throwMainClassLoadingError(id2string(main_class));
341+
return;
342+
}
343+
status() << "Trying to load Java main class: " << maybe_class_name.value()
344+
<< eom;
345+
if(!java_class_loader.can_load_class(maybe_class_name.value()))
346+
{
347+
throwMainClassLoadingError(id2string(main_class));
348+
return;
349+
}
350+
// Everything went well. We have a loadable main class.
351+
// The entry point ('main') will be checked later.
352+
config.main = id2string(main_class);
353+
main_class = maybe_class_name.value();
354+
}
355+
status() << "Found Java main class: " << main_class << eom;
356+
// Now really load it.
326357
const auto &parse_trees = java_class_loader(main_class);
327358
if(parse_trees.empty() || !parse_trees.front().loading_successful)
328359
{
329-
throw invalid_source_file_exceptiont(
330-
"Error: Could not find or load main class " + id2string(main_class));
360+
throwMainClassLoadingError(id2string(main_class));
331361
}
332362
}
333363
}

0 commit comments

Comments
 (0)