Search in sources :

Example 6 with JavaModelException

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

the class ContextSensitiveImportRewriteContext method findInContext.

@Override
public int findInContext(String qualifier, String name, int kind) {
    IBinding[] declarationsInScope = getDeclarationsInScope();
    for (int i = 0; i < declarationsInScope.length; i++) {
        if (declarationsInScope[i] instanceof ITypeBinding) {
            ITypeBinding typeBinding = (ITypeBinding) declarationsInScope[i];
            if (isSameType(typeBinding, qualifier, name)) {
                return RES_NAME_FOUND;
            } else if (isConflicting(typeBinding, name)) {
                return RES_NAME_CONFLICT;
            }
        } else if (declarationsInScope[i] != null) {
            if (isConflicting(declarationsInScope[i], name)) {
                return RES_NAME_CONFLICT;
            }
        }
    }
    Name[] names = getImportedNames();
    for (int i = 0; i < names.length; i++) {
        IBinding binding = names[i].resolveBinding();
        if (binding instanceof ITypeBinding && !binding.isRecovered()) {
            ITypeBinding typeBinding = (ITypeBinding) binding;
            if (isConflictingType(typeBinding, qualifier, name)) {
                return RES_NAME_CONFLICT;
            }
        }
    }
    List<AbstractTypeDeclaration> list = fCompilationUnit.types();
    for (Iterator<AbstractTypeDeclaration> iter = list.iterator(); iter.hasNext(); ) {
        AbstractTypeDeclaration type = iter.next();
        ITypeBinding binding = type.resolveBinding();
        if (binding != null) {
            if (isSameType(binding, qualifier, name)) {
                return RES_NAME_FOUND;
            } else {
                ITypeBinding decl = containingDeclaration(binding, qualifier, name);
                while (decl != null && !decl.equals(binding)) {
                    int modifiers = decl.getModifiers();
                    if (Modifier.isPrivate(modifiers))
                        return RES_NAME_CONFLICT;
                    decl = decl.getDeclaringClass();
                }
            }
        }
    }
    String[] addedImports = fImportRewrite.getAddedImports();
    String qualifiedName = JavaModelUtil.concatenateName(qualifier, name);
    for (int i = 0; i < addedImports.length; i++) {
        String addedImport = addedImports[i];
        if (qualifiedName.equals(addedImport)) {
            return RES_NAME_FOUND;
        } else {
            if (isConflicting(name, addedImport))
                return RES_NAME_CONFLICT;
        }
    }
    if (qualifier.equals("java.lang")) {
        //$NON-NLS-1$
        //No explicit import statement required
        ITypeRoot typeRoot = fCompilationUnit.getTypeRoot();
        if (typeRoot != null) {
            IPackageFragment packageFragment = (IPackageFragment) typeRoot.getParent();
            try {
                ICompilationUnit[] compilationUnits = packageFragment.getCompilationUnits();
                for (int i = 0; i < compilationUnits.length; i++) {
                    ICompilationUnit cu = compilationUnits[i];
                    IType[] allTypes = cu.getAllTypes();
                    for (int j = 0; j < allTypes.length; j++) {
                        IType type = allTypes[j];
                        String packageTypeName = type.getFullyQualifiedName();
                        if (isConflicting(name, packageTypeName))
                            return RES_NAME_CONFLICT;
                    }
                }
            } catch (JavaModelException e) {
            }
        }
    }
    return fImportRewrite.getDefaultImportRewriteContext().findInContext(qualifier, name, kind);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) IType(org.eclipse.jdt.core.IType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 7 with JavaModelException

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

the class HandleFactory method getJarPkgFragmentRoot.

private PackageFragmentRoot getJarPkgFragmentRoot(IPath jarPath, Object target, IJavaProject[] projects) {
    for (int i = 0, projectCount = projects.length; i < projectCount; i++) {
        try {
            JavaProject javaProject = (JavaProject) projects[i];
            IClasspathEntry classpathEnty = javaProject.getClasspathEntryFor(jarPath);
            if (classpathEnty != null) {
                if (target instanceof IFile) {
                    // internal jar
                    return (PackageFragmentRoot) javaProject.getPackageFragmentRoot((IFile) target);
                } else {
                    // external jar
                    return (PackageFragmentRoot) javaProject.getPackageFragmentRoot0(jarPath);
                }
            }
        } catch (JavaModelException e) {
        // JavaModelException from getResolvedClasspath - a problem occurred while accessing project: nothing we can do, ignore
        }
    }
    return null;
}
Also used : JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Example 8 with JavaModelException

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

the class ASTNodes method getNodeSource.

/**
     * Returns the source of the given node from the location where it was parsed.
     * @param node the node to get the source from
     * @param extendedRange if set, the extended ranges of the nodes should ne used
     * @param removeIndent if set, the indentation is removed.
     * @return return the source for the given node or null if accessing the source failed.
     */
public static String getNodeSource(ASTNode node, boolean extendedRange, boolean removeIndent) {
    ASTNode root = node.getRoot();
    if (root instanceof CompilationUnit) {
        CompilationUnit astRoot = (CompilationUnit) root;
        ITypeRoot typeRoot = astRoot.getTypeRoot();
        try {
            if (typeRoot != null && typeRoot.getBuffer() != null) {
                IBuffer buffer = typeRoot.getBuffer();
                int offset = extendedRange ? astRoot.getExtendedStartPosition(node) : node.getStartPosition();
                int length = extendedRange ? astRoot.getExtendedLength(node) : node.getLength();
                String str = buffer.getText(offset, length);
                if (removeIndent) {
                    IJavaProject project = typeRoot.getJavaProject();
                    int indent = StubUtility.getIndentUsed(buffer, node.getStartPosition(), project);
                    str = Strings.changeIndent(str, indent, project, new String(), typeRoot.findRecommendedLineSeparator());
                }
                return str;
            }
        } catch (JavaModelException e) {
        // ignore
        }
    }
    return null;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) IBuffer(org.eclipse.jdt.core.IBuffer)

Example 9 with JavaModelException

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

the class RenameMethodProcessor method createChange.

@Override
public Change createChange(IProgressMonitor monitor) throws CoreException {
    try {
        final TextChange[] changes = fChangeManager.getAllChanges();
        final List<TextChange> list = new ArrayList<TextChange>(changes.length);
        list.addAll(Arrays.asList(changes));
        String project = null;
        IJavaProject javaProject = fMethod.getJavaProject();
        if (javaProject != null)
            project = javaProject.getElementName();
        int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE;
        try {
            if (!Flags.isPrivate(fMethod.getFlags()))
                flags |= RefactoringDescriptor.MULTI_CHANGE;
        } catch (JavaModelException exception) {
            JavaPlugin.log(exception);
        }
        final IType declaring = fMethod.getDeclaringType();
        try {
            if (declaring.isAnonymous() || declaring.isLocal())
                flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
        } catch (JavaModelException exception) {
            JavaPlugin.log(exception);
        }
        final String description = Messages.format(RefactoringCoreMessages.RenameMethodProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fMethod.getElementName()));
        final String header = Messages.format(RefactoringCoreMessages.RenameMethodProcessor_descriptor_description, new String[] { JavaElementLabels.getTextLabel(fMethod, JavaElementLabels.ALL_FULLY_QUALIFIED), BasicElementLabels.getJavaElementName(getNewElementName()) });
        final String comment = new JDTRefactoringDescriptorComment(project, this, header).asString();
        final RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_METHOD);
        descriptor.setProject(project);
        descriptor.setDescription(description);
        descriptor.setComment(comment);
        descriptor.setFlags(flags);
        descriptor.setJavaElement(fMethod);
        descriptor.setNewName(getNewElementName());
        descriptor.setUpdateReferences(fUpdateReferences);
        descriptor.setKeepOriginal(fDelegateUpdating);
        descriptor.setDeprecateDelegate(fDelegateDeprecation);
        return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenameMethodProcessor_change_name, list.toArray(new Change[list.size()]));
    } finally {
        monitor.done();
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) ArrayList(java.util.ArrayList) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextChange(org.eclipse.ltk.core.refactoring.TextChange) Change(org.eclipse.ltk.core.refactoring.Change) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) IType(org.eclipse.jdt.core.IType) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) IJavaProject(org.eclipse.jdt.core.IJavaProject) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)

Example 10 with JavaModelException

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

the class RenameTypeProcessor method createChange.

@Override
public Change createChange(IProgressMonitor monitor) throws CoreException {
    try {
        monitor.beginTask(RefactoringCoreMessages.RenameTypeRefactoring_creating_change, 4);
        String project = null;
        IJavaProject javaProject = fType.getJavaProject();
        if (javaProject != null)
            project = javaProject.getElementName();
        int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE;
        try {
            if (!Flags.isPrivate(fType.getFlags()))
                flags |= RefactoringDescriptor.MULTI_CHANGE;
            if (fType.isAnonymous() || fType.isLocal())
                flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
        } catch (JavaModelException exception) {
            JavaPlugin.log(exception);
        }
        final String description = Messages.format(RefactoringCoreMessages.RenameTypeProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fType.getElementName()));
        final String header = Messages.format(RefactoringCoreMessages.RenameTypeProcessor_descriptor_description, new String[] { JavaElementLabels.getElementLabel(fType, JavaElementLabels.ALL_FULLY_QUALIFIED), getNewElementLabel() });
        final String comment = new JDTRefactoringDescriptorComment(project, this, header).asString();
        final RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_TYPE);
        descriptor.setProject(project);
        descriptor.setDescription(description);
        descriptor.setComment(comment);
        descriptor.setFlags(flags);
        descriptor.setJavaElement(fType);
        descriptor.setNewName(getNewElementName());
        descriptor.setUpdateQualifiedNames(fUpdateQualifiedNames);
        descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
        descriptor.setUpdateReferences(fUpdateReferences);
        if (//$NON-NLS-1$
        fUpdateQualifiedNames && fFilePatterns != null && !"".equals(fFilePatterns))
            descriptor.setFileNamePatterns(fFilePatterns);
        descriptor.setUpdateSimilarDeclarations(fUpdateSimilarElements);
        descriptor.setMatchStrategy(fRenamingStrategy);
        final DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenameTypeProcessor_change_name);
        if (fChangeManager.containsChangesIn(fType.getCompilationUnit())) {
            TextChange textChange = fChangeManager.get(fType.getCompilationUnit());
            if (textChange instanceof TextFileChange) {
                ((TextFileChange) textChange).setSaveMode(TextFileChange.FORCE_SAVE);
            }
        }
        result.addAll(fChangeManager.getAllChanges());
        if (willRenameCU()) {
            IResource resource = fType.getCompilationUnit().getResource();
            if (resource != null && resource.isLinked()) {
                String ext = resource.getFileExtension();
                String renamedResourceName;
                if (ext == null)
                    renamedResourceName = getNewElementName();
                else
                    renamedResourceName = getNewElementName() + '.' + ext;
                result.add(new RenameResourceChange(fType.getCompilationUnit().getPath(), renamedResourceName));
            } else {
                String renamedCUName = JavaModelUtil.getRenamedCUName(fType.getCompilationUnit(), getNewElementName());
                result.add(new RenameCompilationUnitChange(fType.getCompilationUnit(), renamedCUName));
            }
        }
        monitor.worked(1);
        return result;
    } finally {
        fChangeManager = null;
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) RenameResourceChange(org.eclipse.ltk.core.refactoring.resource.RenameResourceChange) TextChange(org.eclipse.ltk.core.refactoring.TextChange) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) IResource(org.eclipse.core.resources.IResource) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) RenameCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange)

Aggregations

JavaModelException (org.eclipse.jdt.core.JavaModelException)162 IJavaProject (org.eclipse.jdt.core.IJavaProject)41 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)40 IType (org.eclipse.jdt.core.IType)39 IJavaElement (org.eclipse.jdt.core.IJavaElement)34 CoreException (org.eclipse.core.runtime.CoreException)28 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)23 ArrayList (java.util.ArrayList)21 IPath (org.eclipse.core.runtime.IPath)20 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)15 HashMap (java.util.HashMap)14 IResource (org.eclipse.core.resources.IResource)13 IMethod (org.eclipse.jdt.core.IMethod)12 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)12 IFile (org.eclipse.core.resources.IFile)11 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)11 SimpleName (org.eclipse.jdt.core.dom.SimpleName)9 Iterator (java.util.Iterator)8 ISourceRange (org.eclipse.jdt.core.ISourceRange)8