Search in sources :

Example 16 with IClassFile

use of org.eclipse.jdt.core.IClassFile in project che by eclipse.

the class SelectionConverter method resolveEnclosingElement.

//	public static IJavaElement getElementAtOffset(ITypeRoot input, ITextSelection selection) throws JavaModelException {
//		if (input instanceof ICompilationUnit) {
//			JavaModelUtil.reconcile((ICompilationUnit)input);
//		}
//		IJavaElement ref = input.getElementAt(selection.getOffset());
//		if (ref == null)
//			return input;
//		return ref;
//	}
//
//	public static IJavaElement resolveEnclosingElement(JavaEditor editor, ITextSelection selection) throws JavaModelException {
//		ITypeRoot input = getInput(editor);
//		if (input != null)
//			return resolveEnclosingElement(input, selection);
//		return null;
//	}
//
public static IJavaElement resolveEnclosingElement(IJavaElement input, ITextSelection selection) throws JavaModelException {
    IJavaElement atOffset = null;
    if (input instanceof ICompilationUnit) {
        ICompilationUnit cunit = (ICompilationUnit) input;
        JavaModelUtil.reconcile(cunit);
        atOffset = cunit.getElementAt(selection.getOffset());
    } else if (input instanceof IClassFile) {
        IClassFile cfile = (IClassFile) input;
        atOffset = cfile.getElementAt(selection.getOffset());
    } else {
        return null;
    }
    if (atOffset == null) {
        return input;
    } else {
        int selectionEnd = selection.getOffset() + selection.getLength();
        IJavaElement result = atOffset;
        if (atOffset instanceof ISourceReference) {
            ISourceRange range = ((ISourceReference) atOffset).getSourceRange();
            while (range.getOffset() + range.getLength() < selectionEnd) {
                result = result.getParent();
                if (!(result instanceof ISourceReference)) {
                    result = input;
                    break;
                }
                range = ((ISourceReference) result).getSourceRange();
            }
        }
        return result;
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IClassFile(org.eclipse.jdt.core.IClassFile) ISourceReference(org.eclipse.jdt.core.ISourceReference) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 17 with IClassFile

use of org.eclipse.jdt.core.IClassFile in project che by eclipse.

the class JavaElementToDtoConverter method addCompilationUnitsAndClassFiles.

private void addCompilationUnitsAndClassFiles(Object parent, List<CompilationUnit> compilationUnits, List<ClassFile> classFiles) throws JavaModelException {
    Set<Object> childrens = this.childrens.get(parent);
    for (Object children : childrens) {
        if (children instanceof ICompilationUnit) {
            ICompilationUnit unit = (ICompilationUnit) children;
            CompilationUnit compilationUnit = DtoFactory.newDto(CompilationUnit.class);
            compilationUnit.setElementName(unit.getElementName());
            compilationUnit.setProjectPath(unit.getJavaProject().getPath().toOSString());
            compilationUnit.setPath(unit.getResource().getFullPath().toOSString());
            compilationUnit.setHandleIdentifier(unit.getHandleIdentifier());
            compilationUnit.setLabel(JavaElementLabels.getElementLabel(unit, JavaElementLabels.ALL_DEFAULT));
            compilationUnit.setImports(getImports(children));
            compilationUnit.setTypes(getTypes(children));
            compilationUnits.add(compilationUnit);
        } else if (children instanceof IClassFile) {
            ClassFile classFile = DtoFactory.newDto(ClassFile.class);
            IClassFile clazz = (IClassFile) children;
            classFile.setHandleIdentifier(clazz.getHandleIdentifier());
            classFile.setElementName(clazz.getElementName());
            classFile.setPath(clazz.getType().getFullyQualifiedName());
            classFile.setLabel(JavaElementLabels.getElementLabel(clazz, JavaElementLabels.ALL_DEFAULT));
            classFile.setProjectPath(clazz.getJavaProject().getPath().toOSString());
            classFile.setType(getTypes(children).get(0));
            classFiles.add(classFile);
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.che.ide.ext.java.shared.dto.model.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IClassFile(org.eclipse.jdt.core.IClassFile) IClassFile(org.eclipse.jdt.core.IClassFile) ClassFile(org.eclipse.che.ide.ext.java.shared.dto.model.ClassFile)

Example 18 with IClassFile

use of org.eclipse.jdt.core.IClassFile in project che by eclipse.

the class JavaNavigation method getCompilationUnitByPath.

/**
     * Get the compilation unit representation of the java file.
     *
     * @param javaProject
     *         path to the project which is contained class file
     * @param fqn
     *         fully qualified name of the class file
     * @param isShowingInheritedMembers
     *         <code>true</code> iff inherited members are shown
     * @return instance of {@link CompilationUnit}
     * @throws JavaModelException
     *         when JavaModel has a failure
     */
public CompilationUnit getCompilationUnitByPath(IJavaProject javaProject, String fqn, boolean isShowingInheritedMembers) throws JavaModelException {
    IType type = javaProject.findType(fqn);
    CompilationUnit compilationUnit = DtoFactory.newDto(CompilationUnit.class);
    ITypeRoot unit;
    if (type.isBinary()) {
        unit = type.getClassFile();
        compilationUnit.setPath(((IClassFile) unit).getType().getFullyQualifiedName());
    } else {
        unit = type.getCompilationUnit();
        compilationUnit.setProjectPath(unit.getJavaProject().getPath().toOSString());
        compilationUnit.setPath(unit.getResource().getFullPath().toOSString());
    }
    compilationUnit.setElementName(unit.getElementName());
    compilationUnit.setHandleIdentifier(unit.getHandleIdentifier());
    compilationUnit.setLabel(org.eclipse.jdt.ui.JavaElementLabels.getElementLabel(unit, org.eclipse.jdt.ui.JavaElementLabels.ALL_DEFAULT));
    List<Type> types = new ArrayList<>(1);
    Type dtoType = convertToDTOType(type);
    dtoType.setPrimary(true);
    types.add(dtoType);
    compilationUnit.setTypes(types);
    if (isShowingInheritedMembers) {
        compilationUnit.setSuperTypes(calculateSuperTypes(type));
    }
    return compilationUnit;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.che.ide.ext.java.shared.dto.model.CompilationUnit) JarEntryType(org.eclipse.che.ide.ext.java.shared.JarEntry.JarEntryType) IType(org.eclipse.jdt.core.IType) Type(org.eclipse.che.ide.ext.java.shared.dto.model.Type) IClassFile(org.eclipse.jdt.core.IClassFile) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) ArrayList(java.util.ArrayList) IType(org.eclipse.jdt.core.IType)

Example 19 with IClassFile

use of org.eclipse.jdt.core.IClassFile in project che by eclipse.

the class JavaNavigation method getEntry.

public JarEntry getEntry(IJavaProject project, int rootId, String path) throws CoreException {
    IPackageFragmentRoot root = getPackageFragmentRoot(project, rootId);
    if (root == null) {
        return null;
    }
    if (path.startsWith("/")) {
        JarPackageFragmentRoot jarPackageFragmentRoot = (JarPackageFragmentRoot) root;
        ZipFile jar = null;
        try {
            jar = jarPackageFragmentRoot.getJar();
            ZipEntry entry = jar.getEntry(path.substring(1));
            if (entry != null) {
                JarEntry result = DtoFactory.getInstance().createDto(JarEntry.class);
                result.setType(JarEntryType.FILE);
                result.setPath(path);
                result.setName(entry.getName().substring(entry.getName().lastIndexOf("/") + 1));
                return result;
            }
        } finally {
            if (jar != null) {
                JavaModelManager.getJavaModelManager().closeZipFile(jar);
            }
        }
        Object[] resources = root.getNonJavaResources();
        for (Object resource : resources) {
            if (resource instanceof JarEntryFile) {
                JarEntryFile file = (JarEntryFile) resource;
                if (file.getFullPath().toOSString().equals(path)) {
                    return getJarEntryResource(file);
                }
            }
            if (resource instanceof JarEntryDirectory) {
                JarEntryDirectory directory = (JarEntryDirectory) resource;
                JarEntryFile file = findJarFile(directory, path);
                if (file != null) {
                    return getJarEntryResource(file);
                }
            }
        }
    } else {
        //java class or file
        IType type = project.findType(path);
        if (type != null && type.isBinary()) {
            IClassFile classFile = type.getClassFile();
            return getJarClass(classFile);
        }
    }
    return null;
}
Also used : JarEntryDirectory(org.eclipse.jdt.internal.core.JarEntryDirectory) IClassFile(org.eclipse.jdt.core.IClassFile) ZipFile(java.util.zip.ZipFile) JarPackageFragmentRoot(org.eclipse.jdt.internal.core.JarPackageFragmentRoot) ZipEntry(java.util.zip.ZipEntry) JarEntry(org.eclipse.che.ide.ext.java.shared.JarEntry) JarEntryFile(org.eclipse.jdt.internal.core.JarEntryFile) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IType(org.eclipse.jdt.core.IType)

Example 20 with IClassFile

use of org.eclipse.jdt.core.IClassFile in project che by eclipse.

the class ReplaceInvocationsRefactoring method resolveSourceProvider.

private SourceProvider resolveSourceProvider(IMethodBinding methodBinding, RefactoringStatus status) throws JavaModelException {
    final IMethod method = (IMethod) methodBinding.getJavaElement();
    ITypeRoot typeRoot;
    IDocument source;
    CompilationUnit methodDeclarationAstRoot;
    ICompilationUnit methodCu = (method).getCompilationUnit();
    if (methodCu != null) {
        typeRoot = methodCu;
        ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
        parser.setSource(methodCu);
        parser.setFocalPosition(method.getNameRange().getOffset());
        CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
        MethodDeclaration methodDecl = (MethodDeclaration) NodeFinder.perform(compilationUnit, method.getNameRange()).getParent();
        AST ast = compilationUnit.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        Block newBody = ast.newBlock();
        newBody.statements().add(rewrite.createStringPlaceholder(fBody, ASTNode.EMPTY_STATEMENT));
        rewrite.replace(methodDecl.getBody(), newBody, null);
        List<SingleVariableDeclaration> parameters = methodDecl.parameters();
        for (int i = 0; i < parameters.size(); i++) {
            SingleVariableDeclaration parameter = parameters.get(i);
            rewrite.set(parameter.getName(), SimpleName.IDENTIFIER_PROPERTY, fParameterNames[i], null);
        }
        TextEdit textEdit = rewrite.rewriteAST();
        Document document = new Document(methodCu.getBuffer().getContents());
        try {
            textEdit.apply(document);
        } catch (MalformedTreeException e) {
            JavaPlugin.log(e);
        } catch (BadLocationException e) {
            JavaPlugin.log(e);
        }
        source = document;
        methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(source.get(), methodCu, true, true, null);
    } else {
        IClassFile classFile = method.getClassFile();
        //TODO: use source if available?
        StubCreator stubCreator = new StubCreator(true) {

            @Override
            protected void appendMethodBody(IMethod currentMethod) throws JavaModelException {
                if (currentMethod.equals(method)) {
                    fBuffer.append(fBody);
                } else {
                    super.appendMethodBody(currentMethod);
                }
            }

            /*
				 * @see org.eclipse.jdt.internal.corext.refactoring.binary.StubCreator#appendMethodParameterName(org.eclipse.jdt.core.IMethod, int)
				 */
            @Override
            protected void appendMethodParameterName(IMethod currentMethod, int index) {
                if (currentMethod.equals(method)) {
                    fBuffer.append(fParameterNames[index]);
                } else {
                    super.appendMethodParameterName(currentMethod, index);
                }
            }
        };
        String stub = stubCreator.createStub(classFile.getType(), null);
        source = new Document(stub);
        methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(stub, classFile, true, true, null);
        typeRoot = classFile;
    }
    ASTNode node = methodDeclarationAstRoot.findDeclaringNode(methodBinding.getKey());
    if (node instanceof MethodDeclaration) {
        return new SourceProvider(typeRoot, source, (MethodDeclaration) node);
    } else {
        status.addFatalError(RefactoringCoreMessages.ReplaceInvocationsRefactoring_cannot_find_method_declaration);
        return null;
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) IClassFile(org.eclipse.jdt.core.IClassFile) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) StubCreator(org.eclipse.jdt.internal.corext.refactoring.binary.StubCreator) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) IMethod(org.eclipse.jdt.core.IMethod) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

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