本文整理汇总了Java中org.eclipse.jdt.internal.corext.util.SearchUtils.getEnclosingJavaElement方法的典型用法代码示例。如果您正苦于以下问题:Java SearchUtils.getEnclosingJavaElement方法的具体用法?Java SearchUtils.getEnclosingJavaElement怎么用?Java SearchUtils.getEnclosingJavaElement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.internal.corext.util.SearchUtils
的用法示例。
在下文中一共展示了SearchUtils.getEnclosingJavaElement方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addTypeImports
import org.eclipse.jdt.internal.corext.util.SearchUtils; //导入方法依赖的package包/类
/**
* Add new imports to types in <code>typeReferences</code> with package <code>fPackage</code>.
*
* @param typeReferences type references
* @throws CoreException should not happen
*/
private void addTypeImports(SearchResultGroup typeReferences) throws CoreException {
SearchMatch[] searchResults = typeReferences.getSearchResults();
for (int i = 0; i < searchResults.length; i++) {
SearchMatch result = searchResults[i];
IJavaElement enclosingElement = SearchUtils.getEnclosingJavaElement(result);
if (!(enclosingElement instanceof IImportDeclaration)) {
String reference = getNormalizedTypeReference(result);
if (!reference.startsWith(fPackage.getElementName())) {
// is unqualified
reference = cutOffInnerTypes(reference);
ImportChange importChange =
fImportsManager.getImportChange(typeReferences.getCompilationUnit());
importChange.addImport(fPackage.getElementName() + '.' + reference);
}
}
}
}
示例2: updateTypeImports
import org.eclipse.jdt.internal.corext.util.SearchUtils; //导入方法依赖的package包/类
/**
* Add new imports to types in <code>typeReferences</code> with package <code>fNewElementName
* </code> and remove old import with <code>fPackage</code>.
*
* @param typeReferences type references
* @throws CoreException should not happen
*/
private void updateTypeImports(SearchResultGroup typeReferences) throws CoreException {
SearchMatch[] searchResults = typeReferences.getSearchResults();
for (int i = 0; i < searchResults.length; i++) {
SearchMatch result = searchResults[i];
IJavaElement enclosingElement = SearchUtils.getEnclosingJavaElement(result);
if (enclosingElement instanceof IImportDeclaration) {
IImportDeclaration importDeclaration = (IImportDeclaration) enclosingElement;
updateImport(
typeReferences.getCompilationUnit(),
importDeclaration,
getUpdatedImport(importDeclaration));
} else {
String reference = getNormalizedTypeReference(result);
if (!reference.startsWith(fPackage.getElementName())) {
reference = cutOffInnerTypes(reference);
ImportChange importChange =
fImportsManager.getImportChange(typeReferences.getCompilationUnit());
importChange.removeImport(fPackage.getElementName() + '.' + reference);
importChange.addImport(getNewPackageName() + '.' + reference);
} // else: already found & updated with package reference search
}
}
}
示例3: getNamesakePackages
import org.eclipse.jdt.internal.corext.util.SearchUtils; //导入方法依赖的package包/类
/**
* @param scope search scope
* @param pm mrogress monitor
* @return all package fragments in <code>scope</code> with same name as <code>fPackage</code>, excluding fPackage
* @throws CoreException if search failed
*/
private IPackageFragment[] getNamesakePackages(IJavaSearchScope scope, IProgressMonitor pm) throws CoreException {
SearchPattern pattern= SearchPattern.createPattern(fPackage.getElementName(), IJavaSearchConstants.PACKAGE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
final HashSet<IPackageFragment> packageFragments= new HashSet<IPackageFragment>();
SearchRequestor requestor= new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
IJavaElement enclosingElement= SearchUtils.getEnclosingJavaElement(match);
if (enclosingElement instanceof IPackageFragment) {
IPackageFragment pack= (IPackageFragment) enclosingElement;
if (! fPackage.equals(pack))
packageFragments.add(pack);
}
}
};
new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
return packageFragments.toArray(new IPackageFragment[packageFragments.size()]);
}
示例4: addTypeImports
import org.eclipse.jdt.internal.corext.util.SearchUtils; //导入方法依赖的package包/类
/**
* Add new imports to types in <code>typeReferences</code> with package <code>fPackage</code>.
* @param typeReferences type references
* @throws CoreException should not happen
*/
private void addTypeImports(SearchResultGroup typeReferences) throws CoreException {
SearchMatch[] searchResults= typeReferences.getSearchResults();
for (int i= 0; i < searchResults.length; i++) {
SearchMatch result= searchResults[i];
IJavaElement enclosingElement= SearchUtils.getEnclosingJavaElement(result);
if (! (enclosingElement instanceof IImportDeclaration)) {
String reference= getNormalizedTypeReference(result);
if (! reference.startsWith(fPackage.getElementName())) {
// is unqualified
reference= cutOffInnerTypes(reference);
ImportChange importChange= fImportsManager.getImportChange(typeReferences.getCompilationUnit());
importChange.addImport(fPackage.getElementName() + '.' + reference);
}
}
}
}
示例5: updateTypeImports
import org.eclipse.jdt.internal.corext.util.SearchUtils; //导入方法依赖的package包/类
/**
* Add new imports to types in <code>typeReferences</code> with package <code>fNewElementName</code>
* and remove old import with <code>fPackage</code>.
* @param typeReferences type references
* @throws CoreException should not happen
*/
private void updateTypeImports(SearchResultGroup typeReferences) throws CoreException {
SearchMatch[] searchResults= typeReferences.getSearchResults();
for (int i= 0; i < searchResults.length; i++) {
SearchMatch result= searchResults[i];
IJavaElement enclosingElement= SearchUtils.getEnclosingJavaElement(result);
if (enclosingElement instanceof IImportDeclaration) {
IImportDeclaration importDeclaration= (IImportDeclaration) enclosingElement;
updateImport(typeReferences.getCompilationUnit(), importDeclaration, getUpdatedImport(importDeclaration));
} else {
String reference= getNormalizedTypeReference(result);
if (! reference.startsWith(fPackage.getElementName())) {
reference= cutOffInnerTypes(reference);
ImportChange importChange= fImportsManager.getImportChange(typeReferences.getCompilationUnit());
importChange.removeImport(fPackage.getElementName() + '.' + reference);
importChange.addImport(getNewPackageName() + '.' + reference);
} // else: already found & updated with package reference search
}
}
}
示例6: extractElements
import org.eclipse.jdt.internal.corext.util.SearchUtils; //导入方法依赖的package包/类
private static Set<IJavaElement> extractElements(SearchMatch[] searchResults, int elementType) {
Set<IJavaElement> elements = new HashSet<IJavaElement>();
for (int i = 0; i < searchResults.length; i++) {
IJavaElement el = SearchUtils.getEnclosingJavaElement(searchResults[i]);
if (el.exists() && el.getElementType() == elementType) elements.add(el);
}
return elements;
}
示例7: getNamesakePackages
import org.eclipse.jdt.internal.corext.util.SearchUtils; //导入方法依赖的package包/类
/**
* @param scope search scope
* @param pm mrogress monitor
* @return all package fragments in <code>scope</code> with same name as <code>fPackage</code>,
* excluding fPackage
* @throws CoreException if search failed
*/
private IPackageFragment[] getNamesakePackages(IJavaSearchScope scope, IProgressMonitor pm)
throws CoreException {
SearchPattern pattern =
SearchPattern.createPattern(
fPackage.getElementName(),
IJavaSearchConstants.PACKAGE,
IJavaSearchConstants.DECLARATIONS,
SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
final HashSet<IPackageFragment> packageFragments = new HashSet<IPackageFragment>();
SearchRequestor requestor =
new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
IJavaElement enclosingElement = SearchUtils.getEnclosingJavaElement(match);
if (enclosingElement instanceof IPackageFragment) {
IPackageFragment pack = (IPackageFragment) enclosingElement;
if (!fPackage.equals(pack)) packageFragments.add(pack);
}
}
};
new SearchEngine()
.search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
return packageFragments.toArray(new IPackageFragment[packageFragments.size()]);
}
示例8: extractElements
import org.eclipse.jdt.internal.corext.util.SearchUtils; //导入方法依赖的package包/类
private static Set<IJavaElement> extractElements(SearchMatch[] searchResults, int elementType) {
Set<IJavaElement> elements= new HashSet<IJavaElement>();
for (int i= 0; i < searchResults.length; i++) {
IJavaElement el= SearchUtils.getEnclosingJavaElement(searchResults[i]);
if (el.exists() && el.getElementType() == elementType)
elements.add(el);
}
return elements;
}
示例9: addReferenceUpdates
import org.eclipse.jdt.internal.corext.util.SearchUtils; //导入方法依赖的package包/类
private void addReferenceUpdates(TextChangeManager changeManager, ICompilationUnit movedUnit, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException, CoreException {
List<ICompilationUnit> cuList= Arrays.asList(fCus);
SearchResultGroup[] references= getReferences(movedUnit, pm, status);
for (int i= 0; i < references.length; i++) {
SearchResultGroup searchResultGroup= references[i];
ICompilationUnit referencingCu= searchResultGroup.getCompilationUnit();
if (referencingCu == null)
continue;
boolean simpleReferencesNeedNewImport= simpleReferencesNeedNewImport(movedUnit, referencingCu, cuList);
SearchMatch[] results= searchResultGroup.getSearchResults();
for (int j= 0; j < results.length; j++) {
// TODO: should update type references with results from addImport
TypeReference reference= (TypeReference) results[j];
if (reference.isImportDeclaration()) {
ImportRewrite rewrite= getImportRewrite(referencingCu);
IImportDeclaration importDecl= (IImportDeclaration) SearchUtils.getEnclosingJavaElement(results[j]);
if (Flags.isStatic(importDecl.getFlags())) {
rewrite.removeStaticImport(importDecl.getElementName());
addStaticImport(movedUnit, importDecl, rewrite);
} else {
rewrite.removeImport(importDecl.getElementName());
rewrite.addImport(createStringForNewImport(movedUnit, importDecl));
}
} else if (reference.isQualified()) {
TextChange textChange= changeManager.get(referencingCu);
String changeName= RefactoringCoreMessages.MoveCuUpdateCreator_update_references;
TextEdit replaceEdit= new ReplaceEdit(reference.getOffset(), reference.getSimpleNameStart() - reference.getOffset(), fNewPackage);
TextChangeCompatibility.addTextEdit(textChange, changeName, replaceEdit);
} else if (simpleReferencesNeedNewImport) {
ImportRewrite importEdit= getImportRewrite(referencingCu);
String typeName= reference.getSimpleName();
importEdit.addImport(getQualifiedType(fDestination.getElementName(), typeName));
}
}
}
}
示例10: acceptSearchMatch
import org.eclipse.jdt.internal.corext.util.SearchUtils; //导入方法依赖的package包/类
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
if (filterMatch(match))
return;
/*
* Processing is done in collector to reuse the buffer which was
* already required by the search engine to locate the matches.
*/
// [start, end[ include qualification.
IJavaElement element= SearchUtils.getEnclosingJavaElement(match);
int accuracy= match.getAccuracy();
int start= match.getOffset();
int length= match.getLength();
boolean insideDocComment= match.isInsideDocComment();
IResource res= match.getResource();
if (element.getAncestor(IJavaElement.IMPORT_DECLARATION) != null) {
collectMatch(TypeReference.createImportReference(element, accuracy, start, length, insideDocComment, res));
} else {
ICompilationUnit unit= (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
if (unit != null) {
IBuffer buffer= unit.getBuffer();
String matchText= buffer.getText(start, length);
if (fSource.isDefaultPackage()) {
collectMatch(TypeReference.createSimpleReference(element, accuracy, start, length, insideDocComment, res, matchText));
} else {
// assert: matchText doesn't start nor end with comment
int simpleNameStart= getLastSimpleNameStart(matchText);
if (simpleNameStart != 0) {
collectMatch(TypeReference.createQualifiedReference(element, accuracy, start, length, insideDocComment, res, start + simpleNameStart));
} else {
collectMatch(TypeReference.createSimpleReference(element, accuracy, start, length, insideDocComment, res, matchText));
}
}
}
}
}
示例11: addReferenceUpdates
import org.eclipse.jdt.internal.corext.util.SearchUtils; //导入方法依赖的package包/类
private void addReferenceUpdates(IProgressMonitor pm) throws CoreException {
pm.beginTask(
"",
fOccurrences.length
+ fReferencesToTypesInPackage.size()
+ fReferencesToTypesInNamesakes.size()); // $NON-NLS-1$
for (int i = 0; i < fOccurrences.length; i++) {
ICompilationUnit cu = fOccurrences[i].getCompilationUnit();
if (cu == null) continue;
SearchMatch[] results = fOccurrences[i].getSearchResults();
for (int j = 0; j < results.length; j++) {
SearchMatch result = results[j];
IJavaElement enclosingElement = SearchUtils.getEnclosingJavaElement(result);
if (enclosingElement instanceof IImportDeclaration) {
IImportDeclaration importDeclaration = (IImportDeclaration) enclosingElement;
String updatedImport = getUpdatedImport(importDeclaration);
updateImport(cu, importDeclaration, updatedImport);
} else { // is reference
TextChangeCompatibility.addTextEdit(
fTextChangeManager.get(cu),
RefactoringCoreMessages.RenamePackageRefactoring_update_reference,
createTextChange(result));
}
}
if (fReferencesToTypesInNamesakes.size() != 0) {
SearchResultGroup typeRefsRequiringOldNameImport =
extractGroupFor(cu, fReferencesToTypesInNamesakes);
if (typeRefsRequiringOldNameImport != null)
addTypeImports(typeRefsRequiringOldNameImport);
}
if (fReferencesToTypesInPackage.size() != 0) {
SearchResultGroup typeRefsRequiringNewNameImport =
extractGroupFor(cu, fReferencesToTypesInPackage);
if (typeRefsRequiringNewNameImport != null)
updateTypeImports(typeRefsRequiringNewNameImport);
}
pm.worked(1);
}
if (fReferencesToTypesInNamesakes.size() != 0) {
for (Iterator<SearchResultGroup> iter = fReferencesToTypesInNamesakes.iterator();
iter.hasNext(); ) {
SearchResultGroup referencesToTypesInNamesakes = iter.next();
addTypeImports(referencesToTypesInNamesakes);
pm.worked(1);
}
}
if (fReferencesToTypesInPackage.size() != 0) {
for (Iterator<SearchResultGroup> iter = fReferencesToTypesInPackage.iterator();
iter.hasNext(); ) {
SearchResultGroup namesakeReferencesToPackage = iter.next();
updateTypeImports(namesakeReferencesToPackage);
pm.worked(1);
}
}
pm.done();
}
示例12: addReferenceUpdates
import org.eclipse.jdt.internal.corext.util.SearchUtils; //导入方法依赖的package包/类
private void addReferenceUpdates(
TextChangeManager changeManager,
ICompilationUnit movedUnit,
IProgressMonitor pm,
RefactoringStatus status)
throws JavaModelException, CoreException {
List<ICompilationUnit> cuList = Arrays.asList(fCus);
SearchResultGroup[] references = getReferences(movedUnit, pm, status);
for (int i = 0; i < references.length; i++) {
SearchResultGroup searchResultGroup = references[i];
ICompilationUnit referencingCu = searchResultGroup.getCompilationUnit();
if (referencingCu == null) continue;
boolean simpleReferencesNeedNewImport =
simpleReferencesNeedNewImport(movedUnit, referencingCu, cuList);
SearchMatch[] results = searchResultGroup.getSearchResults();
for (int j = 0; j < results.length; j++) {
// TODO: should update type references with results from addImport
TypeReference reference = (TypeReference) results[j];
if (reference.isImportDeclaration()) {
ImportRewrite rewrite = getImportRewrite(referencingCu);
IImportDeclaration importDecl =
(IImportDeclaration) SearchUtils.getEnclosingJavaElement(results[j]);
if (Flags.isStatic(importDecl.getFlags())) {
rewrite.removeStaticImport(importDecl.getElementName());
addStaticImport(movedUnit, importDecl, rewrite);
} else {
rewrite.removeImport(importDecl.getElementName());
rewrite.addImport(createStringForNewImport(movedUnit, importDecl));
}
} else if (reference.isQualified()) {
TextChange textChange = changeManager.get(referencingCu);
String changeName = RefactoringCoreMessages.MoveCuUpdateCreator_update_references;
TextEdit replaceEdit =
new ReplaceEdit(
reference.getOffset(),
reference.getSimpleNameStart() - reference.getOffset(),
fNewPackage);
TextChangeCompatibility.addTextEdit(textChange, changeName, replaceEdit);
} else if (simpleReferencesNeedNewImport) {
ImportRewrite importEdit = getImportRewrite(referencingCu);
String typeName = reference.getSimpleName();
importEdit.addImport(getQualifiedType(fDestination.getElementName(), typeName));
}
}
}
}
示例13: acceptSearchMatch
import org.eclipse.jdt.internal.corext.util.SearchUtils; //导入方法依赖的package包/类
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
if (filterMatch(match)) return;
/*
* Processing is done in collector to reuse the buffer which was
* already required by the search engine to locate the matches.
*/
// [start, end[ include qualification.
IJavaElement element = SearchUtils.getEnclosingJavaElement(match);
int accuracy = match.getAccuracy();
int start = match.getOffset();
int length = match.getLength();
boolean insideDocComment = match.isInsideDocComment();
IResource res = match.getResource();
if (element.getAncestor(IJavaElement.IMPORT_DECLARATION) != null) {
collectMatch(
TypeReference.createImportReference(
element, accuracy, start, length, insideDocComment, res));
} else {
ICompilationUnit unit =
(ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
if (unit != null) {
IBuffer buffer = unit.getBuffer();
String matchText = buffer.getText(start, length);
if (fSource.isDefaultPackage()) {
collectMatch(
TypeReference.createSimpleReference(
element, accuracy, start, length, insideDocComment, res, matchText));
} else {
// assert: matchText doesn't start nor end with comment
int simpleNameStart = getLastSimpleNameStart(matchText);
if (simpleNameStart != 0) {
collectMatch(
TypeReference.createQualifiedReference(
element,
accuracy,
start,
length,
insideDocComment,
res,
start + simpleNameStart));
} else {
collectMatch(
TypeReference.createSimpleReference(
element, accuracy, start, length, insideDocComment, res, matchText));
}
}
}
}
}
示例14: addReferenceUpdates
import org.eclipse.jdt.internal.corext.util.SearchUtils; //导入方法依赖的package包/类
private void addReferenceUpdates(IProgressMonitor pm) throws CoreException {
pm.beginTask("", fOccurrences.length + fReferencesToTypesInPackage.size() + fReferencesToTypesInNamesakes.size()); //$NON-NLS-1$
for (int i= 0; i < fOccurrences.length; i++){
ICompilationUnit cu= fOccurrences[i].getCompilationUnit();
if (cu == null)
continue;
SearchMatch[] results= fOccurrences[i].getSearchResults();
for (int j= 0; j < results.length; j++){
SearchMatch result= results[j];
IJavaElement enclosingElement= SearchUtils.getEnclosingJavaElement(result);
if (enclosingElement instanceof IImportDeclaration) {
IImportDeclaration importDeclaration= (IImportDeclaration) enclosingElement;
String updatedImport= getUpdatedImport(importDeclaration);
updateImport(cu, importDeclaration, updatedImport);
} else { // is reference
TextChangeCompatibility.addTextEdit(fTextChangeManager.get(cu), RefactoringCoreMessages.RenamePackageRefactoring_update_reference, createTextChange(result));
}
}
if (fReferencesToTypesInNamesakes.size() != 0) {
SearchResultGroup typeRefsRequiringOldNameImport= extractGroupFor(cu, fReferencesToTypesInNamesakes);
if (typeRefsRequiringOldNameImport != null)
addTypeImports(typeRefsRequiringOldNameImport);
}
if (fReferencesToTypesInPackage.size() != 0) {
SearchResultGroup typeRefsRequiringNewNameImport= extractGroupFor(cu, fReferencesToTypesInPackage);
if (typeRefsRequiringNewNameImport != null)
updateTypeImports(typeRefsRequiringNewNameImport);
}
pm.worked(1);
}
if (fReferencesToTypesInNamesakes.size() != 0) {
for (Iterator<SearchResultGroup> iter= fReferencesToTypesInNamesakes.iterator(); iter.hasNext();) {
SearchResultGroup referencesToTypesInNamesakes= iter.next();
addTypeImports(referencesToTypesInNamesakes);
pm.worked(1);
}
}
if (fReferencesToTypesInPackage.size() != 0) {
for (Iterator<SearchResultGroup> iter= fReferencesToTypesInPackage.iterator(); iter.hasNext();) {
SearchResultGroup namesakeReferencesToPackage= iter.next();
updateTypeImports(namesakeReferencesToPackage);
pm.worked(1);
}
}
pm.done();
}