Search in sources :

Example 1 with IClassFile

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;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ICodeAssist(org.eclipse.jdt.core.ICodeAssist) IClassFile(org.eclipse.jdt.core.IClassFile) IType(org.eclipse.jdt.core.IType)

Example 2 with IClassFile

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();
}
Also used : IClassFile(org.eclipse.jdt.core.IClassFile) ArrayList(java.util.ArrayList)

Example 3 with IClassFile

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClassFile(org.eclipse.jdt.core.IClassFile) DebuggerException(org.eclipse.che.api.debugger.server.exceptions.DebuggerException) Document(org.eclipse.jface.text.Document) IMember(org.eclipse.jdt.core.IMember) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) IType(org.eclipse.jdt.core.IType)

Example 4 with IClassFile

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) IClassFile(org.eclipse.jdt.core.IClassFile) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMethod(org.eclipse.jdt.core.IMethod)

Example 5 with IClassFile

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();
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IClassFile(org.eclipse.jdt.core.IClassFile) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) LinkedList(java.util.LinkedList) ISourceRange(org.eclipse.jdt.core.ISourceRange) IType(org.eclipse.jdt.core.IType)

Aggregations

IClassFile (org.eclipse.jdt.core.IClassFile)34 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)20 IJavaElement (org.eclipse.jdt.core.IJavaElement)13 IType (org.eclipse.jdt.core.IType)9 JavaModelException (org.eclipse.jdt.core.JavaModelException)8 CoreException (org.eclipse.core.runtime.CoreException)7 ArrayList (java.util.ArrayList)6 ISourceRange (org.eclipse.jdt.core.ISourceRange)5 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 ITypeRoot (org.eclipse.jdt.core.ITypeRoot)4 BadLocationException (org.eclipse.jface.text.BadLocationException)4 IResource (org.eclipse.core.resources.IResource)3 IStorage (org.eclipse.core.resources.IStorage)3 IMethod (org.eclipse.jdt.core.IMethod)3 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)3 ISourceReference (org.eclipse.jdt.core.ISourceReference)3 Document (org.eclipse.jface.text.Document)3 IOException (java.io.IOException)2 DebuggerException (org.eclipse.che.api.debugger.server.exceptions.DebuggerException)2 JarEntry (org.eclipse.che.ide.ext.java.shared.JarEntry)2