Search in sources :

Example 11 with ITypeRoot

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

the class JavaElementToDtoConverter method getTypes.

private List<Type> getTypes(Object parent) throws JavaModelException {
    List<Type> result = new ArrayList<>();
    Set<Object> objects = childrens.get(parent);
    if (objects == null) {
        return result;
    }
    for (Object object : objects) {
        if (object instanceof IType) {
            IType type = (IType) object;
            Type dtoType = DtoFactory.newDto(Type.class);
            dtoType.setElementName(type.getElementName());
            dtoType.setLabel(JavaElementLabels.getElementLabel(type, JavaElementLabels.ALL_DEFAULT));
            dtoType.setHandleIdentifier(type.getHandleIdentifier());
            dtoType.setFlags(type.getFlags());
            dtoType.setTypes(getTypes(object));
            dtoType.setFields(getFields(object));
            dtoType.setMethods(getMethods(object));
            dtoType.setInitializers(getInitializes(object));
            if (parent instanceof ITypeRoot) {
                IType primaryType = ((ITypeRoot) parent).findPrimaryType();
                dtoType.setPrimary(type.equals(primaryType));
            } else {
                dtoType.setPrimary(false);
            }
            result.add(dtoType);
        }
    }
    return result;
}
Also used : Type(org.eclipse.che.ide.ext.java.shared.dto.model.Type) IType(org.eclipse.jdt.core.IType) ArrayList(java.util.ArrayList) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) IType(org.eclipse.jdt.core.IType)

Example 12 with ITypeRoot

use of org.eclipse.jdt.core.ITypeRoot 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 13 with ITypeRoot

use of org.eclipse.jdt.core.ITypeRoot 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)

Example 14 with ITypeRoot

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

the class IndexSelector method getQualifiedNames.

private char[][][] getQualifiedNames(ObjectVector types) {
    final int size = types.size;
    char[][][] focusQualifiedNames = null;
    IJavaElement javaElement = this.pattern.focus;
    int index = 0;
    while (javaElement != null && !(javaElement instanceof ITypeRoot)) {
        javaElement = javaElement.getParent();
    }
    if (javaElement != null) {
        IType primaryType = ((ITypeRoot) javaElement).findPrimaryType();
        if (primaryType != null) {
            focusQualifiedNames = new char[size + 1][][];
            focusQualifiedNames[index++] = CharOperation.splitOn('.', primaryType.getFullyQualifiedName().toCharArray());
        }
    }
    if (focusQualifiedNames == null) {
        focusQualifiedNames = new char[size][][];
    }
    for (int i = 0; i < size; i++) {
        focusQualifiedNames[index++] = CharOperation.splitOn('.', ((IType) (types.elementAt(i))).getFullyQualifiedName().toCharArray());
    }
    return focusQualifiedNames.length == 0 ? null : ReferenceCollection.internQualifiedNames(focusQualifiedNames, true);
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) IType(org.eclipse.jdt.core.IType)

Example 15 with ITypeRoot

use of org.eclipse.jdt.core.ITypeRoot in project bndtools by bndtools.

the class NewTypeWizardPage method initTypePage.

/**
     * Initializes all fields provided by the page with a given selection.
     *
     * @param elem
     *            the selection used to initialize this page or <code>
     * null</code> if no selection was available
     */
protected void initTypePage(IJavaElement elem) {
    //$NON-NLS-1$
    String initSuperclass = "java.lang.Object";
    ArrayList<String> initSuperinterfaces = new ArrayList<String>(5);
    IJavaProject project = null;
    IPackageFragment pack = null;
    IType enclosingType = null;
    if (elem != null) {
        // evaluate the enclosing type
        project = elem.getJavaProject();
        pack = (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
        IType typeInCU = (IType) elem.getAncestor(IJavaElement.TYPE);
        if (typeInCU != null) {
            if (typeInCU.getCompilationUnit() != null) {
                enclosingType = typeInCU;
            }
        } else {
            ITypeRoot cu = (ITypeRoot) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
            if (cu != null) {
                enclosingType = cu.findPrimaryType();
            }
        }
        try {
            IType type = null;
            if (elem.getElementType() == IJavaElement.TYPE) {
                type = (IType) elem;
                if (type.exists()) {
                    String superName = SuperInterfaceSelectionDialog.getNameWithTypeParameters(type);
                    if (type.isInterface()) {
                        initSuperinterfaces.add(superName);
                    } else {
                        initSuperclass = superName;
                    }
                }
            }
        } catch (JavaModelException e) {
            JavaPlugin.log(e);
        // ignore this exception now
        }
    }
    //$NON-NLS-1$
    String typeName = "";
    ITextSelection selection = getCurrentTextSelection();
    if (selection != null) {
        String text = selection.getText();
        if (text != null && validateJavaTypeName(text, project).isOK()) {
            typeName = text;
        }
    }
    setPackageFragment(pack, true);
    setEnclosingType(enclosingType, true);
    setEnclosingTypeSelection(false, true);
    setTypeName(typeName, true);
    setSuperClass(initSuperclass, true);
    setSuperInterfaces(initSuperinterfaces, true);
    // from project or workspace
    setAddComments(StubUtility.doAddComments(project), true);
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) ArrayList(java.util.ArrayList) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) ITextSelection(org.eclipse.jface.text.ITextSelection) IType(org.eclipse.jdt.core.IType)

Aggregations

ITypeRoot (org.eclipse.jdt.core.ITypeRoot)15 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)7 IType (org.eclipse.jdt.core.IType)7 JavaModelException (org.eclipse.jdt.core.JavaModelException)6 IJavaProject (org.eclipse.jdt.core.IJavaProject)5 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 ArrayList (java.util.ArrayList)3 IClassFile (org.eclipse.jdt.core.IClassFile)3 IJavaElement (org.eclipse.jdt.core.IJavaElement)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 IBinding (org.eclipse.jdt.core.dom.IBinding)3 SimpleName (org.eclipse.jdt.core.dom.SimpleName)3 Type (org.eclipse.che.ide.ext.java.shared.dto.model.Type)2 IMethod (org.eclipse.jdt.core.IMethod)2 IOpenable (org.eclipse.jdt.core.IOpenable)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 Name (org.eclipse.jdt.core.dom.Name)2 LinkedList (java.util.LinkedList)1