Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Finding secondary types
Finding secondary types [message #1537067] Tue, 30 December 2014 16:10 Go to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi,

is there a way to find secondary types on an IJavaProject without
trapping into the loop in JavaModelManager.secondaryTypes(IJavaProject,
boolean, IProgressMonitor) at
// Wait for the end of indexing or a cancel
while (this.indexManager.awaitingJobsCount() > 0) { .. } ?

In Xtext we had a bug that secondary types are not visible and I changed
the lookup of ITypes to IJavaProject.findType(pack, type, monitor) to
include secondary types in the result. This caused a significant
performance degration in our unit tests (build jobs timed out). So I
hacked my way around and did smth like

JavaProject casted = (JavaProject) javaProject;
NameLookup nameLookup =
casted.newNameLookup(DefaultWorkingCopyOwner.PRIMARY);
NameLookup.Answer answer = nameLookup.findType(
typeName,
packageName,
false,
NameLookup.ACCEPT_ALL,
false);
type = answer == null ? null : answer.type;

But (no surprise here) the secondary types are not yet visible without
the up-to-date-check. I tried to figure out the code path that is used
by the Java compiler / binding resolution to find secondary types, but
that doesn't seem to be public API.

Is there any way to improve the lookup time for this "esotheric" case,
make it correct and still don't let the common case suffer from
significant overhead?

Best,
Sebastian
Re: Finding secondary types [message #1537399 is a reply to message #1537067] Tue, 30 December 2014 20:32 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Hi Sebastian,

I'm not firm in all details of JavaModel and indexing, but since you already tried using internal classes: would directly invoking org.eclipse.jdt.internal.core.search.BasicSearchEngine.searchAllSecondaryTypeNames(IPackageFragmentRoot[], IRestrictedAccessTypeRequestor, boolean, IProgressMonitor) work as a fallback if no primary type was found?

If this doesn't help I guess you need an answer from Jay...
Stephan
Re: Finding secondary types [message #1541869 is a reply to message #1537399] Fri, 02 January 2015 09:24 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Thanks for the support, Stephan. For the record: eventually I did
something along these lines. Maybe you spot something stupid that I
shouldn't use:

private IType findSecondaryType(String packageName, final String
typeName) throws JavaModelException {
IPackageFragmentRoot[] sourceFolders = getSourceFolders();
IndexManager indexManager = JavaModelManager.getIndexManager();
if (indexManager.awaitingJobsCount() > 0) { // still indexing - don't
enter a busy wait loop but ask the source folders directly
for(IPackageFragmentRoot sourceFolder: sourceFolders) {
IPackageFragment packageFragment =
sourceFolder.getPackageFragment(packageName);
if (packageFragment.exists()) {
ICompilationUnit[] units = packageFragment.getCompilationUnits();
for(ICompilationUnit unit: units) {
IType type = unit.getType(typeName);
if (type.exists()) {
return type;
}
}
}
}
return null;
}

// code below is adapted from BasicSearchEnginge.searchAllSecondaryTypes

// index is ready, query it for a secondary type
final TypeDeclarationPattern pattern = new TypeDeclarationPattern(
packageName == null ? CharOperation.NO_CHAR : packageName.toCharArray(),
CharOperation.NO_CHAR_CHAR, // top level type - no enclosing type names
typeName.toCharArray(),
IIndexConstants.SECONDARY_SUFFIX,
SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);

// Get working copy path(s). Store in a single string in case of only
one to optimize comparison in requestor
final HashSet<String> workingCopyPaths = new HashSet<String>();
String workingCopyPath = null;
ICompilationUnit[] copies =
JavaModelManager.getJavaModelManager().getWorkingCopies(DefaultWorkingCopyOwner.PRIMARY,
false/*don't add primary WCs a second time*/);
final int copiesLength = copies == null ? 0 : copies.length;
if (copies != null) {
if (copiesLength == 1) {
ICompilationUnit singleWC = copies[0];
if (singleWC.getPackageDeclaration(packageName).exists()) {
IType result = singleWC.getType(typeName);
if (result.exists()) {
return result;
}
}
workingCopyPath = copies[0].getPath().toString();
} else {
for (int i = 0; i < copiesLength; i++) {
ICompilationUnit workingCopy = copies[i];
if (workingCopy.getPackageDeclaration(packageName).exists()) {
IType result = workingCopy.getType(typeName);
if (result.exists()) {
return result;
}
}
workingCopyPaths.add(workingCopy.getPath().toString());
}
}
}
final String singleWkcpPath = workingCopyPath;
final Wrapper<IType> result = Wrapper.forType(IType.class);

IndexQueryRequestor searchRequestor = new IndexQueryRequestor(){
@Override
public boolean acceptIndexMatch(String documentPath, SearchPattern
indexRecord, SearchParticipant participant, AccessRuleSet access) {
// Filter unexpected types
switch (copiesLength) {
case 0:
break;
case 1:
if (singleWkcpPath == null) {
throw new IllegalStateException();
}
if (singleWkcpPath.equals(documentPath)) {
return true; // filter out *the* working copy
}
break;
default:
if (workingCopyPaths.contains(documentPath)) {
return true; // filter out working copies
}
break;
}
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new
Path(documentPath));
ICompilationUnit unit = JavaCore.createCompilationUnitFrom(file);
IType type = unit.getType(typeName);
result.set(type);
return false;
}
};

try {
indexManager.performConcurrentJob(
new PatternSearchJob(
pattern,
BasicSearchEngine.getDefaultSearchParticipant(), // Java search only
BasicSearchEngine.createJavaSearchScope(sourceFolders),
searchRequestor),
IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
null);
} catch (OperationCanceledException oce) {
// do nothing
}
return result.get();
}

Best,
Sebastian
Re: Finding secondary types [message #1549511 is a reply to message #1541869] Tue, 06 January 2015 15:24 Go to previous message
abdo koupp is currently offline abdo kouppFriend
Messages: 1
Registered: January 2015
Junior Member
Thanks for the support,
Previous Topic:virtual java eclipse
Next Topic:Need Guidance for IDE : Java Programmer learning Ruby
Goto Forum:
  


Current Time: Thu Apr 25 10:38:37 GMT 2024

Powered by FUDForum. Page generated in 0.03768 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top