下面列出了org.eclipse.jdt.core.dom.IBinding#getJavaElement ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private IMethod getMethod(ASTNode node) throws JavaModelException {
IBinding binding;
if (node instanceof MethodInvocation) {
binding = ((MethodInvocation) node).resolveMethodBinding();
} else if (node instanceof MethodRef) {
binding = ((MethodRef) node).resolveBinding();
} else if (node instanceof ClassInstanceCreation) {
binding = ((ClassInstanceCreation) node).resolveConstructorBinding();
} else {
binding = null;
}
if (binding != null) {
IJavaElement javaElement = binding.getJavaElement();
if (javaElement instanceof IMethod) {
IMethod method = (IMethod) javaElement;
return method;
}
}
return null;
}
private static boolean canModifyAccessRules(IBinding binding) {
IJavaElement element= binding.getJavaElement();
if (element == null)
return false;
IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element);
if (root == null)
return false;
try {
IClasspathEntry classpathEntry= root.getRawClasspathEntry();
if (classpathEntry == null)
return false;
if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
return true;
if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
ClasspathContainerInitializer classpathContainerInitializer= JavaCore.getClasspathContainerInitializer(classpathEntry.getPath().segment(0));
IStatus status= classpathContainerInitializer.getAccessRulesStatus(classpathEntry.getPath(), root.getJavaProject());
return status.isOK();
}
} catch (JavaModelException e) {
return false;
}
return false;
}
private boolean isBinaryOrPackage(ASTNode node) {
if (node instanceof Name) {
IBinding resolvedBinding = ((Name) node).resolveBinding();
IJavaElement element = resolvedBinding != null ? resolvedBinding.getJavaElement() : null;
try {
if (element == null || element.getElementType() == IJavaElement.PACKAGE_FRAGMENT || !RefactoringAvailabilityTester.isRenameElementAvailable(element)) {
return true;
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
return true;
}
}
return false;
}
private static boolean getRenameRefactoringProposal(IInvocationContext context, ASTNode node, IProblemLocation[] locations, Collection<ICommandAccess> resultingCollections)
throws CoreException {
if (!(context instanceof AssistContext)) {
return false;
}
IEditorPart editor= ((AssistContext)context).getEditor();
if (!(editor instanceof JavaEditor))
return false;
if (!(node instanceof SimpleName)) {
return false;
}
SimpleName name= (SimpleName) node;
IBinding binding= name.resolveBinding();
if (binding == null) {
return false;
}
IJavaElement javaElement= binding.getJavaElement();
if (javaElement == null || !RefactoringAvailabilityTester.isRenameElementAvailable(javaElement)) {
return false;
}
if (resultingCollections == null) {
return true;
}
RenameRefactoringProposal proposal= new RenameRefactoringProposal((JavaEditor) editor);
if (locations.length != 0) {
proposal.setRelevance(IProposalRelevance.RENAME_REFACTORING_ERROR);
} else if (containsQuickFixableRenameLocal(locations)) {
proposal.setRelevance(IProposalRelevance.RENAME_REFACTORING_QUICK_FIX);
}
resultingCollections.add(proposal);
return true;
}
private Object getSelectedElement(JavaEditor editor) {
ISourceViewer viewer= editor.getViewer();
if (viewer == null)
return null;
Point selectedRange= viewer.getSelectedRange();
int length= selectedRange.y;
int offset= selectedRange.x;
ITypeRoot element= JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
if (element == null)
return null;
CompilationUnit ast= SharedASTProvider.getAST(element, SharedASTProvider.WAIT_YES, null);
if (ast == null)
return null;
NodeFinder finder= new NodeFinder(ast, offset, length);
ASTNode node= finder.getCoveringNode();
IBinding binding= null;
if (node instanceof Name) {
binding= getConstructorBindingIfAvailable((Name)node);
if (binding != null)
return binding;
binding= ((Name)node).resolveBinding();
} else if (node instanceof MethodInvocation) {
binding= ((MethodInvocation)node).resolveMethodBinding();
} else if (node instanceof MethodDeclaration) {
binding= ((MethodDeclaration)node).resolveBinding();
} else if (node instanceof Type) {
binding= ((Type)node).resolveBinding();
} else if (node instanceof AnonymousClassDeclaration) {
binding= ((AnonymousClassDeclaration)node).resolveBinding();
} else if (node instanceof TypeDeclaration) {
binding= ((TypeDeclaration)node).resolveBinding();
} else if (node instanceof CompilationUnit) {
return ((CompilationUnit)node).getJavaElement();
} else if (node instanceof Expression) {
binding= ((Expression)node).resolveTypeBinding();
} else if (node instanceof ImportDeclaration) {
binding= ((ImportDeclaration)node).resolveBinding();
} else if (node instanceof MemberRef) {
binding= ((MemberRef)node).resolveBinding();
} else if (node instanceof MemberValuePair) {
binding= ((MemberValuePair)node).resolveMemberValuePairBinding();
} else if (node instanceof PackageDeclaration) {
binding= ((PackageDeclaration)node).resolveBinding();
} else if (node instanceof TypeParameter) {
binding= ((TypeParameter)node).resolveBinding();
} else if (node instanceof VariableDeclaration) {
binding= ((VariableDeclaration)node).resolveBinding();
}
if (binding != null)
return binding.getJavaElement();
return null;
}