Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if CtPackage contains a package-info.java #2955

Closed
msteinbeck opened this issue May 3, 2019 · 11 comments
Closed

Check if CtPackage contains a package-info.java #2955

msteinbeck opened this issue May 3, 2019 · 11 comments

Comments

@msteinbeck
Copy link
Contributor

Is there a method in CtPackage which allows me to check whether a package contains a package-info.java file?

@nharrand
Copy link
Collaborator

nharrand commented May 3, 2019

Even if it is not super clean, you can do the following:

//Assuming that p is a CtPackage
((CtElementImpl) p).getPosition().toString().contains("package-info.java");

When there is no package-info.java file, (CtElementImpl) p).getPosition() returns Unknown file, when there is one, it returns a position in package-info.java.

@surli
Copy link
Collaborator

surli commented May 3, 2019

Actually you can check it from the compilation units:

if (cu.getUnitType() == CtCompilationUnit.UNIT_TYPE.PACKAGE_DECLARATION) {
  ... 
}

@nharrand
Copy link
Collaborator

nharrand commented May 3, 2019

I think cu is null when the package-info.java does not exists. (But I agree it is better.)

@nharrand
Copy link
Collaborator

nharrand commented May 3, 2019

May be we could add a something like boolean CtPackage#hasPackageInfo() to the API.

@msteinbeck
Copy link
Contributor Author

Similar to package-info.java, I need to know whether a CtPackage contains a module-info.java. How about adding an isEmpty method to CtPackage?

@nharrand
Copy link
Collaborator

nharrand commented May 3, 2019

Shouldn't module-info.java be associated with CtModule rather than CtPackage?
(Nitpicking) I don't really like the name isEmpty as I find it missleading, it could mean that the package does not contain any CtType. But regardless of the name, I agree that we should add the behavior.

@msteinbeck
Copy link
Contributor Author

msteinbeck commented May 3, 2019

I don't really like the name isEmpty as I find it missleading, it could mean that the package does not contain any CtType

That's exactly what I need. I want to know whether a package does not contain any CtType, package-info.java, and module-info.java (and other info files provided by future SDKs). I can't just check if the path of a package contains any *-info.java file because they might have been excluded from build, thus being not part of a model.

@msteinbeck
Copy link
Contributor Author

Due to being a CtNamedElement, checking for any CtModule (module-info.java) should be feasible with CtPackage#getElements(...).

@nharrand
Copy link
Collaborator

nharrand commented May 3, 2019

Ok I am sold on the isEmpty method then.
But, I am a bit fuzzy on modules. In my understanding, a module declares packages, but packages do not contain modules. It just happen that in most build system, package tree are reproduced in terms of directory tree. Am I missing something?

@nharrand
Copy link
Collaborator

nharrand commented May 3, 2019

I have implemented a PR ( #2957 ) containing CtPackage#hasPackageInfo() and CtPackage#isEmpty. But it does not solve your module-info problem. But I assume that something like ctPackage.getDeclaringModule().getPosition() could be done.

WDYT?

@nharrand
Copy link
Collaborator

nharrand commented Jun 5, 2019

I'll close this issue as I think #2957 solves this. Don't hesitate to reopen if I missed something.

@nharrand nharrand closed this as completed Jun 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants