"Is a class with this name visible in this context?"


Looking to implement an inspection (already present in Eclipse!) for whether a type parameter name hides a visible class name. Obviously IDEA does searches for non-qualified class names already, but I don't seem to be able to find any easy way to do it through the Open API. Does such a thing exist, or should I file a JIRA item?

--Dave Griffith

6 comments
Comment actions Permalink

There can't be any inspections in Eclipse which IDEA doesn't have too!

final PsiManager manager = ...
final PsiShortNamesCache = manager.getShortNamesCache();
final PsiClass[] classes =
shortNameCache.getClassesByName("non-qualified-name",
GlobalSearchScope.allScope(clazz.getProject()));

Bas

Dave Griffith wrote:

Looking to implement an inspection (already present in Eclipse!) for
whether a type parameter name hides a visible class name. Obviously
IDEA does searches for non-qualified class names already, but I don't
seem to be able to find any easy way to do it through the Open API.
Does such a thing exist, or should I file a JIRA item?

--Dave Griffith

0
Comment actions Permalink

By the way, how about an inspection for whether a class name hides
another class name in the project (in another package naturally) or its
libraries? And does Eclipse have anymore interesting inspections?

Bas

Dave Griffith wrote:

Looking to implement an inspection (already present in Eclipse!) for
whether a type parameter name hides a visible class name. Obviously
IDEA does searches for non-qualified class names already, but I don't
seem to be able to find any easy way to do it through the Open API.
Does such a thing exist, or should I file a JIRA item?

--Dave Griffith

0
Comment actions Permalink

That tells you if another class visible anywhere with that name. My question was for another class visible with that short name that would be usable at the current location without changing the file's imports. In short, I need a "currently imported" scope for the current file. That said, I'm not sure that your semantics wouldn't be better, though, or perhaps there should be a toggle. Let me think about it.

--Dave Griffith

0
Comment actions Permalink

+ By the way, how about an inspection for whether a class name hides
another class name in the project (in another package naturally) or its
libraries?+

I've thought along these lines, but haven't yet pulled the trigger. Fear of false positives is one problem, but the second is that such an inspection would only solve a small part of the problem it points to. I've thought of (and even started) a plugin which would scan through all of a project's classes and resources, including those in libraries, and produce a report of all possible conflicts of (fully-qualified) class names and resource paths. The goal would be to identify any possible deployment issues in advance, letting you know that the three different XML libraries you have could hose you if your deployment classpath were to get rearranged. Can't be done in an inspection under the current API, since inspections can only report on project classes, not libraries or resources.

And does Eclipse have anymore interesting inspections?

Eclipse per se doesn't have many inspections or intentions, although they added a few new ones this last milestone. I've added "Missing @Override annotation" and "Missing @Deprecated annotation", as well as a wicked-cool intention for filling in the branches of an enum switch statement. This type parameter visibility one is the only exciting one outstanding. For your benighted Eclipse-using colleagues who want IDEAIG level of code inspections, their best bet is EclipsePro Audit, from Instantiations. It basically covers all of the same code inspection ground as IDEAIGMetricsReloadedspell checking, including interactive error-checking and quickfixes, and runs a mere $499 a seat. Otherwise, Eclipse uses mostly muddle along with FindBugs and PMD plugins, but with only batch reporting and no quickfixes that does away with about %85 of the value of a static analysis product.

--Dave Griffith

0
Comment actions Permalink

If I understand you correctly now, this would suit your purpose:

final PsiElement element = ....
final String unqualifiedClassName = ....

final PsiManager manager = element.getManager();
final PsiFile containingFile = element.getContainingFile();
final PsiResolveHelper resolveHelper = manager.getResolveHelper();
final PsiClass clazz =
resolveHelper.resolveReferencedClass(unqualifiedClassName, containingFile);
if (clazz != null) {
System.out.println("class found");
}

Hope this helps,
Bas

Dave Griffith wrote:

That tells you if another class visible anywhere with that name. My
question was for another class visible with that short name that
would be usable at the current location +without changing the
file's imports+. In short, I need a "currently imported" scope
for the current file. That said, I'm not sure that your semantics
wouldn't be better, though, or perhaps there should be a toggle. Let
me think about it.

--Dave Griffith

0
Comment actions Permalink

Cool. Worked like a charm. The inspection will be in the next EAP.

--Dave Griffith

0

Please sign in to leave a comment.