简体   繁体   English

Java类在某些父包中吗?

[英]Is Java class in certain parent package?

I have package : 我有包裹:

com.google.*.*.*

There may be many subpackages. 可能有很多子包。

I have a Class foo.java 我有一个foo.java

How to check if its inside the main package com.google 如何检查它是否在主包com.google

When I use Class.getPackage() , it gives only the whole package like this: com.google.util.domain.sup 当我使用Class.getPackage() ,它仅给出整个包,如下所示: com.google.util.domain.sup

Must I really use functions like : substring() , split() . 我是否必须真正使用像substring() , split()这样的函数。 Or is there a fancy way to check if it belongs to certain parent packages? 还是有一种奇特的方法来检查它是否属于某些父包?

Packages are not hierarchical in Java. 包在Java中不是分层的。 com.google.foo has nothing to do with package com.google . com.google.foocom.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. 因此,如果您的目标是检查类是否在包中或其某个“子包”中,那么可以,String操作是您的最佳选择。

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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM