Search in sources :

Example 1 with IImportDeclaration

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

the class RenameTypeProcessor method checkTypesImportedInCu.

private RefactoringStatus checkTypesImportedInCu() throws CoreException {
    IImportDeclaration imp = getImportedType(fType.getCompilationUnit(), getNewElementName());
    if (imp == null)
        return null;
    String msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_imported, new Object[] { getNewElementLabel(), BasicElementLabels.getPathLabel(fType.getCompilationUnit().getResource().getFullPath(), false) });
    IJavaElement grandParent = imp.getParent().getParent();
    if (grandParent instanceof ICompilationUnit)
        return RefactoringStatus.createErrorStatus(msg, JavaStatusContext.create(imp));
    return null;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration)

Example 2 with IImportDeclaration

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

the class RenameTypeProcessor method checkImportedTypes.

private RefactoringStatus checkImportedTypes() throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    IImportDeclaration[] imports = fType.getCompilationUnit().getImports();
    for (int i = 0; i < imports.length; i++) analyzeImportDeclaration(imports[i], result);
    return result;
}
Also used : RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration)

Example 3 with IImportDeclaration

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

the class SourceTypeConverter method convert.

/*
	 * Convert a set of source element types into a parsed compilation unit declaration
	 * The argument types are then all grouped in the same unit. The argument types must
	 * at least contain one type.
	 */
private CompilationUnitDeclaration convert(ISourceType[] sourceTypes, CompilationResult compilationResult) throws JavaModelException {
    this.unit = new CompilationUnitDeclaration(this.problemReporter, compilationResult, 0);
    if (sourceTypes.length == 0)
        return this.unit;
    SourceTypeElementInfo topLevelTypeInfo = (SourceTypeElementInfo) sourceTypes[0];
    org.eclipse.jdt.core.ICompilationUnit cuHandle = topLevelTypeInfo.getHandle().getCompilationUnit();
    this.cu = (ICompilationUnit) cuHandle;
    final CompilationUnitElementInfo compilationUnitElementInfo = (CompilationUnitElementInfo) ((JavaElement) this.cu).getElementInfo();
    if (this.has1_5Compliance && (compilationUnitElementInfo.annotationNumber >= CompilationUnitElementInfo.ANNOTATION_THRESHOLD_FOR_DIET_PARSE || (compilationUnitElementInfo.hasFunctionalTypes && (this.flags & LOCAL_TYPE) != 0))) {
        // Also see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=405843
        if ((this.flags & LOCAL_TYPE) == 0) {
            return new Parser(this.problemReporter, true).dietParse(this.cu, compilationResult);
        } else {
            return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
        }
    }
    /* only positions available */
    int start = topLevelTypeInfo.getNameSourceStart();
    int end = topLevelTypeInfo.getNameSourceEnd();
    /* convert package and imports */
    String[] packageName = ((PackageFragment) cuHandle.getParent()).names;
    if (packageName.length > 0)
        // if its null then it is defined in the default package
        this.unit.currentPackage = createImportReference(packageName, start, end, false, ClassFileConstants.AccDefault);
    IImportDeclaration[] importDeclarations = topLevelTypeInfo.getHandle().getCompilationUnit().getImports();
    int importCount = importDeclarations.length;
    this.unit.imports = new ImportReference[importCount];
    for (int i = 0; i < importCount; i++) {
        ImportDeclaration importDeclaration = (ImportDeclaration) importDeclarations[i];
        ISourceImport sourceImport = (ISourceImport) importDeclaration.getElementInfo();
        String nameWithoutStar = importDeclaration.getNameWithoutStar();
        this.unit.imports[i] = createImportReference(Util.splitOn('.', nameWithoutStar, 0, nameWithoutStar.length()), sourceImport.getDeclarationSourceStart(), sourceImport.getDeclarationSourceEnd(), importDeclaration.isOnDemand(), sourceImport.getModifiers());
    }
    /* convert type(s) */
    try {
        int typeCount = sourceTypes.length;
        final TypeDeclaration[] types = new TypeDeclaration[typeCount];
        /*
			 * We used a temporary types collection to prevent this.unit.types from being null during a call to
			 * convert(...) when the source is syntactically incorrect and the parser is flushing the unit's types.
			 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=97466
			 */
        for (int i = 0; i < typeCount; i++) {
            SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) sourceTypes[i];
            types[i] = convert((SourceType) typeInfo.getHandle(), compilationResult);
        }
        this.unit.types = types;
        return this.unit;
    } catch (AnonymousMemberFound e) {
        return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
    }
}
Also used : PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) ISourceType(org.eclipse.jdt.internal.compiler.env.ISourceType) SourceType(org.eclipse.jdt.internal.core.SourceType) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration) Parser(org.eclipse.jdt.internal.compiler.parser.Parser) ISourceImport(org.eclipse.jdt.internal.compiler.env.ISourceImport) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) SourceTypeElementInfo(org.eclipse.jdt.internal.core.SourceTypeElementInfo) CompilationUnitElementInfo(org.eclipse.jdt.internal.core.CompilationUnitElementInfo) ImportDeclaration(org.eclipse.jdt.internal.core.ImportDeclaration) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 4 with IImportDeclaration

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

the class MoveCuUpdateCreator method addReferenceUpdates.

private void addReferenceUpdates(TextChangeManager changeManager, ICompilationUnit movedUnit, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException, CoreException {
    List<ICompilationUnit> cuList = Arrays.asList(fCus);
    SearchResultGroup[] references = getReferences(movedUnit, pm, status);
    for (int i = 0; i < references.length; i++) {
        SearchResultGroup searchResultGroup = references[i];
        ICompilationUnit referencingCu = searchResultGroup.getCompilationUnit();
        if (referencingCu == null)
            continue;
        boolean simpleReferencesNeedNewImport = simpleReferencesNeedNewImport(movedUnit, referencingCu, cuList);
        SearchMatch[] results = searchResultGroup.getSearchResults();
        for (int j = 0; j < results.length; j++) {
            // TODO: should update type references with results from addImport
            TypeReference reference = (TypeReference) results[j];
            if (reference.isImportDeclaration()) {
                ImportRewrite rewrite = getImportRewrite(referencingCu);
                IImportDeclaration importDecl = (IImportDeclaration) SearchUtils.getEnclosingJavaElement(results[j]);
                if (Flags.isStatic(importDecl.getFlags())) {
                    rewrite.removeStaticImport(importDecl.getElementName());
                    addStaticImport(movedUnit, importDecl, rewrite);
                } else {
                    rewrite.removeImport(importDecl.getElementName());
                    rewrite.addImport(createStringForNewImport(movedUnit, importDecl));
                }
            } else if (reference.isQualified()) {
                TextChange textChange = changeManager.get(referencingCu);
                String changeName = RefactoringCoreMessages.MoveCuUpdateCreator_update_references;
                TextEdit replaceEdit = new ReplaceEdit(reference.getOffset(), reference.getSimpleNameStart() - reference.getOffset(), fNewPackage);
                TextChangeCompatibility.addTextEdit(textChange, changeName, replaceEdit);
            } else if (simpleReferencesNeedNewImport) {
                ImportRewrite importEdit = getImportRewrite(referencingCu);
                String typeName = reference.getSimpleName();
                importEdit.addImport(getQualifiedType(fDestination.getElementName(), typeName));
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) TextChange(org.eclipse.ltk.core.refactoring.TextChange) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 5 with IImportDeclaration

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

the class JavaElementToDtoConverter method addImport.

private void addImport(List<ImportDeclaration> result, Set<Object> imports) throws JavaModelException {
    for (Object im : imports) {
        if (im instanceof IImportDeclaration) {
            IImportDeclaration dec = (IImportDeclaration) im;
            ImportDeclaration importDeclaration = DtoFactory.newDto(ImportDeclaration.class);
            importDeclaration.setFlags(dec.getFlags());
            importDeclaration.setHandleIdentifier(dec.getHandleIdentifier());
            importDeclaration.setElementName(dec.getElementName());
            result.add(importDeclaration);
        }
    }
}
Also used : ImportDeclaration(org.eclipse.che.ide.ext.java.shared.dto.model.ImportDeclaration) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration)

Aggregations

IImportDeclaration (org.eclipse.jdt.core.IImportDeclaration)9 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 IProject (org.eclipse.core.resources.IProject)2 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ImportDeclaration (org.eclipse.che.ide.ext.java.shared.dto.model.ImportDeclaration)1 IFile (org.eclipse.core.resources.IFile)1 IResource (org.eclipse.core.resources.IResource)1 CoreException (org.eclipse.core.runtime.CoreException)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)1 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)1 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)1