Oak
Oak

Reputation: 26858

Get the package of a Java source file

My goal is to find the package (as string) of a Java source file, given as plaintext and not already sorted in folders.

I can't just locate the first instance of the keyword package in the file, because it may appear inside a comment. So I was thinking about two alternatives:

Another difference between the two approaches is that when scanning manually I can stop the scan when I can be certain the package keyword can no longer appear, saving some time... and I'm not sure I can do something similar with regexes. On the other hand, the decision "when it can no longer appear" is not necessarily simple, though I could use some heuristic for that.

I would like to hear any input on this problem, and would welcome any help with the regex. My solution is written in Java as well.

EDIT: to those suggesting actually parsing the file - it's definitely a viable option, thank you, but it feels a bit of an overkill for me to parse the whole file for just the package. I'll do it if there's no simpler alternative.

Upvotes: 3

Views: 1249

Answers (2)

mdma
mdma

Reputation: 57757

You could use an actual java source parser, like javaparser. It gives the correctly parsed java file without needing to reinvent a java parser or using a poor man's parser (regex.)

The only downside I see is that perhaps you want to stop parsing as soon as you've found the package, and avoid parsing the remainder of the file. There are various, somewhat hacky, ways you could achieve this, but I recommend that you meausre whole-file performance before thinking about this.

Upvotes: 1

tangens
tangens

Reputation: 39733

I solved this problem by using a java parser. For my purpose javaparser was the best fit.

CompilationUnit cu = JavaParser.parse( file );
String packageName = cu.getPackage().getName().toString();

Upvotes: 6

Related Questions