use of org.eclipse.jdt.core.IClassFile in project che by eclipse.
the class JavaNavigation method findDeclaration.
public OpenDeclarationDescriptor findDeclaration(IJavaProject project, String fqn, int offset) throws JavaModelException {
IJavaElement originalElement = null;
IType type = project.findType(fqn);
ICodeAssist codeAssist;
if (type.isBinary()) {
codeAssist = type.getClassFile();
} else {
codeAssist = type.getCompilationUnit();
}
IJavaElement[] elements = null;
if (codeAssist != null) {
elements = codeAssist.codeSelect(offset, 0);
}
if (elements != null && elements.length > 0) {
originalElement = elements[0];
}
IJavaElement element = originalElement;
while (element != null) {
if (element instanceof ICompilationUnit) {
ICompilationUnit unit = ((ICompilationUnit) element).getPrimary();
return compilationUnitNavigation(unit, originalElement);
}
if (element instanceof IClassFile) {
return classFileNavigation((IClassFile) element, originalElement);
}
element = element.getParent();
}
return null;
}
use of org.eclipse.jdt.core.IClassFile in project che by eclipse.
the class JavaNavigation method getPackageContent.
/* (non-Javadoc)
* @see org.eclipse.jdt.ui.StandardJavaElementContentProvider#getPackageContent(org.eclipse.jdt.core.IPackageFragment)
*/
protected Object[] getPackageContent(IPackageFragment fragment) throws JavaModelException {
// hierarchical package mode
ArrayList<Object> result = new ArrayList<Object>();
getHierarchicalPackageChildren((IPackageFragmentRoot) fragment.getParent(), fragment, result);
IClassFile[] classFiles = fragment.getClassFiles();
List<IClassFile> filtered = new ArrayList<>();
//filter inner classes
for (IClassFile classFile : classFiles) {
if (!classFile.getElementName().contains("$")) {
filtered.add(classFile);
}
}
Object[] nonPackages = concatenate(filtered.toArray(), fragment.getNonJavaResources());
if (result.isEmpty())
return nonPackages;
Collections.addAll(result, nonPackages);
return result.toArray();
}
use of org.eclipse.jdt.core.IClassFile in project che by eclipse.
the class JavaDebuggerUtils method findFqnByPosition.
/**
* Return nested class fqn if line with number {@code lineNumber} contains such element, otherwise return outer class fqn.
*
* @param projectPath
* project path which contains class with {@code outerClassFqn}
* @param outerClassFqn
* fqn outer class
* @param lineNumber
* line position to search
* @throws DebuggerException
*/
public String findFqnByPosition(String projectPath, String outerClassFqn, int lineNumber) throws DebuggerException {
if (projectPath == null) {
return outerClassFqn;
}
IJavaProject project = MODEL.getJavaProject(projectPath);
IType outerClass;
IMember iMember;
try {
outerClass = project.findType(outerClassFqn);
if (outerClass == null) {
return outerClassFqn;
}
String source;
if (outerClass.isBinary()) {
IClassFile classFile = outerClass.getClassFile();
source = classFile.getSource();
} else {
ICompilationUnit unit = outerClass.getCompilationUnit();
source = unit.getSource();
}
Document document = new Document(source);
IRegion region = document.getLineInformation(lineNumber);
int start = region.getOffset();
int end = start + region.getLength();
iMember = binSearch(outerClass, start, end);
} catch (JavaModelException e) {
throw new DebuggerException(format("Unable to find source for class with fqn '%s' in the project '%s'", outerClassFqn, project), e);
} catch (BadLocationException e) {
throw new DebuggerException("Unable to calculate breakpoint location", e);
}
if (iMember instanceof IType) {
return ((IType) iMember).getFullyQualifiedName();
}
if (iMember != null) {
return iMember.getDeclaringType().getFullyQualifiedName();
}
return outerClassFqn;
}
use of org.eclipse.jdt.core.IClassFile in project che by eclipse.
the class InlineMethodRefactoring method resolveSourceProvider.
private static SourceProvider resolveSourceProvider(RefactoringStatus status, ITypeRoot typeRoot, ASTNode invocation) {
CompilationUnit root = (CompilationUnit) invocation.getRoot();
IMethodBinding methodBinding = Invocations.resolveBinding(invocation);
if (methodBinding == null) {
status.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
return null;
}
MethodDeclaration declaration = (MethodDeclaration) root.findDeclaringNode(methodBinding);
if (declaration != null) {
return new SourceProvider(typeRoot, declaration);
}
IMethod method = (IMethod) methodBinding.getJavaElement();
if (method != null) {
CompilationUnit methodDeclarationAstRoot;
ICompilationUnit methodCu = method.getCompilationUnit();
if (methodCu != null) {
methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(methodCu, true);
} else {
IClassFile classFile = method.getClassFile();
if (!JavaElementUtil.isSourceAvailable(classFile)) {
String methodLabel = JavaElementLabels.getTextLabel(method, JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.M_PARAMETER_TYPES);
status.addFatalError(Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_error_classFile, methodLabel));
return null;
}
methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(classFile, true);
}
ASTNode node = methodDeclarationAstRoot.findDeclaringNode(methodBinding.getMethodDeclaration().getKey());
if (node instanceof MethodDeclaration) {
return new SourceProvider(methodDeclarationAstRoot.getTypeRoot(), (MethodDeclaration) node);
}
}
status.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
return null;
}
use of org.eclipse.jdt.core.IClassFile in project tdi-studio-se by Talend.
the class OpenDeclarationAction method getMethodNameRange.
/**
* Gets the source range.
*
* @param editorInput The editor input
* @return The source range
* @throws JavaModelException
*/
private ISourceRange getMethodNameRange(IEditorInput editorInput) throws JavaModelException {
ITypeRoot typeRoot = JavaUI.getEditorInputTypeRoot(editorInput);
LinkedList<ISourceRange> sourceRanges = new LinkedList<ISourceRange>();
if (typeRoot instanceof IClassFile) {
// class file
IType type = ((IClassFile) typeRoot).getType();
getMethodNameRange(type.getChildren(), 0, sourceRanges);
} else if (typeRoot instanceof ICompilationUnit) {
// java file
ICompilationUnit unit = (ICompilationUnit) typeRoot;
IType[] allTypes = unit.getAllTypes();
for (IType type : allTypes) {
getMethodNameRange(type.getChildren(), 0, sourceRanges);
}
} else {
return null;
}
if (sourceRanges.isEmpty()) {
return null;
}
return sourceRanges.getFirst();
}
Aggregations