Reputation: 16541
I have package :
com.google.*.*.*
There may be many subpackages.
I have a Class foo.java
How to check if its inside the main package com.google
When I use Class.getPackage()
, it gives only the whole package like this:
com.google.util.domain.sup
Must I really use functions like : substring() , split()
. Or is there a fancy way to check if it belongs to certain parent packages?
Upvotes: 0
Views: 413
Reputation: 533492
Do you mean like...
if (getClass().getName().startsWith("com.google."))
if you need an exact match you can use
if (getClass().getPackage().getName().equals("com.google.util.domain.sup"))
Upvotes: 5
Reputation: 691685
Packages are not hierarchical in Java. com.google.foo
has nothing to do with package com.google
. So if your goal is to check that a class is in a package, or one of its "sub-packages", then yes, String manipulation is your best bet.
Upvotes: 7