Search in sources :

Example 66 with JavaModelException

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

the class DeltaProcessor method traverseDelta.

/*
     * Converts an <code>IResourceDelta</code> and its children into
     * the corresponding <code>IJavaElementDelta</code>s.
     */
private void traverseDelta(IResourceDelta delta, int elementType, RootInfo rootInfo, OutputsInfo outputsInfo) {
    IResource res = delta.getResource();
    // set stack of elements
    if (this.currentElement == null && rootInfo != null) {
        this.currentElement = rootInfo.project;
    }
    // process current delta
    boolean processChildren = true;
    if (res instanceof IProject) {
        // reset source element parser cache
        this.sourceElementParserCache = null;
        processChildren = updateCurrentDeltaAndIndex(delta, elementType == IJavaElement.PACKAGE_FRAGMENT_ROOT ? // case of prj=src
        IJavaElement.JAVA_PROJECT : elementType, rootInfo);
    } else if (rootInfo != null) {
        processChildren = updateCurrentDeltaAndIndex(delta, elementType, rootInfo);
    } else {
        // not yet inside a package fragment root
        processChildren = true;
    }
    // process children if needed
    if (processChildren) {
        IResourceDelta[] children = (IResourceDelta[]) delta.getAffectedChildren();
        boolean oneChildOnClasspath = false;
        int length = children.length;
        IResourceDelta[] orphanChildren = null;
        Openable parent = null;
        boolean isValidParent = true;
        for (int i = 0; i < length; i++) {
            IResourceDelta child = children[i];
            IResource childRes = child.getResource();
            // check source attachment change
            checkSourceAttachmentChange(child, childRes);
            // find out whether the child is a package fragment root of the current project
            IPath childPath = externalPath(childRes);
            int childKind = child.getKind();
            RootInfo childRootInfo = rootInfo(childPath, childKind);
            RootInfo originalChildRootInfo = childRootInfo;
            if (childRootInfo != null && !childRootInfo.isRootOfProject(childPath)) {
                // package fragment root of another project (dealt with later)
                childRootInfo = null;
            }
            // compute child type
            int childType = elementType(childRes, childKind, elementType, rootInfo == null ? childRootInfo : rootInfo);
            // is childRes in the output folder and is it filtered out ?
            boolean isResFilteredFromOutput = isResFilteredFromOutput(rootInfo, outputsInfo, childRes, childType);
            boolean isNestedRoot = rootInfo != null && childRootInfo != null;
            if (!isResFilteredFromOutput && !isNestedRoot) {
                // do not treat as non-java rsc if nested root
                traverseDelta(child, childType, rootInfo == null ? childRootInfo : rootInfo, // traverse delta for child in the same project
                outputsInfo);
                if (childType == NON_JAVA_RESOURCE) {
                    if (rootInfo != null) {
                        // if inside a package fragment root
                        if (!isValidParent)
                            continue;
                        if (parent == null) {
                            // find the parent of the non-java resource to attach to
                            if (this.currentElement == null || !rootInfo.project.equals(this.currentElement.getJavaProject())) {
                                // note if currentElement is the
                                // IJavaModel, getJavaProject() is null
                                // force the currentProject to be used
                                this.currentElement = rootInfo.project;
                            }
                            if (elementType == IJavaElement.JAVA_PROJECT || (elementType == IJavaElement.PACKAGE_FRAGMENT_ROOT && res instanceof IProject)) {
                                // NB: attach non-java resource to project (not to its package fragment root)
                                parent = rootInfo.project;
                            } else {
                                parent = createElement(res, elementType, rootInfo);
                            }
                            if (parent == null) {
                                isValidParent = false;
                                continue;
                            }
                        }
                        // add child as non java resource
                        try {
                            nonJavaResourcesChanged(parent, child);
                        } catch (JavaModelException e) {
                        // ignore
                        }
                    } else {
                        // the non-java resource (or its parent folder) will be attached to the java project
                        if (orphanChildren == null)
                            orphanChildren = new IResourceDelta[length];
                        orphanChildren[i] = child;
                    }
                } else {
                    if (rootInfo == null && childRootInfo == null) {
                        // the non-java resource (or its parent folder) will be attached to the java project
                        if (orphanChildren == null)
                            orphanChildren = new IResourceDelta[length];
                        orphanChildren[i] = child;
                    }
                }
            } else {
                // to avoid reporting child delta as non-java resource delta
                oneChildOnClasspath = true;
            }
            // but it is a package fragment root of another project, traverse delta too
            if (isNestedRoot || (childRootInfo == null && originalChildRootInfo != null)) {
                traverseDelta(child, IJavaElement.PACKAGE_FRAGMENT_ROOT, originalChildRootInfo, // binary output of childRootInfo.project cannot be this root
                null);
            }
            // if the child is a package fragment root of one or several other projects
            ArrayList rootList;
            if ((rootList = otherRootsInfo(childPath, childKind)) != null) {
                Iterator iterator = rootList.iterator();
                while (iterator.hasNext()) {
                    originalChildRootInfo = (RootInfo) iterator.next();
                    this.currentElement = // ensure that 2 roots refering to the same resource don't share the current element (see
                    null;
                    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=210746 )
                    traverseDelta(child, IJavaElement.PACKAGE_FRAGMENT_ROOT, originalChildRootInfo, // binary output of childRootInfo.project cannot be this root
                    null);
                }
            }
        }
        if (orphanChildren != null && (// orphan children are siblings of a package fragment root
        oneChildOnClasspath || res instanceof IProject)) {
        // non-java resource directly under a project
        //				// attach orphan children
        //				IProject rscProject = res.getProject();
        //				JavaProject adoptiveProject = (JavaProject)JavaCore.create(rscProject);
        //				if (adoptiveProject != null
        //						&& JavaProject.hasJavaNature(rscProject)) { // delta iff Java project (18698)
        //					for (int i = 0; i < length; i++) {
        //						if (orphanChildren[i] != null) {
        //							try {
        //								nonJavaResourcesChanged(adoptiveProject, orphanChildren[i]);
        //							} catch (JavaModelException e) {
        //								// ignore
        //							}
        //						}
        //					}
        //				}
        }
    // else resource delta will be added by parent
    }
// else resource delta will be added by parent
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject) IResourceDelta(org.eclipse.core.resources.IResourceDelta)

Example 67 with JavaModelException

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

the class IntroduceIndirectionRefactoring method setIntermediaryTypeName.

/**
	 * @param fullyQualifiedTypeName the fully qualified name of the intermediary method
	 * @return status for type name. Use {@link #setIntermediaryMethodName(String)} to check for overridden methods.
	 */
public RefactoringStatus setIntermediaryTypeName(String fullyQualifiedTypeName) {
    IType target = null;
    try {
        if (fullyQualifiedTypeName.length() == 0)
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_type_not_selected_error);
        // find type (now including secondaries)
        target = getProject().findType(fullyQualifiedTypeName, new NullProgressMonitor());
        if (target == null || !target.exists())
            return RefactoringStatus.createErrorStatus(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_type_does_not_exist_error, BasicElementLabels.getJavaElementName(fullyQualifiedTypeName)));
        if (target.isAnnotation())
            return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_in_annotation);
        if (target.isInterface() && !(JavaModelUtil.is18OrHigher(target.getJavaProject()) && JavaModelUtil.is18OrHigher(getProject())))
            return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_on_interface);
    } catch (JavaModelException e) {
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_unable_determine_declaring_type);
    }
    if (target.isReadOnly())
        return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_in_readonly);
    if (target.isBinary())
        return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_in_binary);
    fIntermediaryType = target;
    return new RefactoringStatus();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IType(org.eclipse.jdt.core.IType)

Example 68 with JavaModelException

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

the class IntroduceFactoryRefactoring method setFactoryClass.

/**
	 * Sets the class on which the generated factory method is to be placed.
	 * @param fullyQualifiedTypeName an <code>IType</code> referring to an existing class
	 * @return return the resulting status
	 */
public RefactoringStatus setFactoryClass(String fullyQualifiedTypeName) {
    IType factoryType;
    try {
        factoryType = findFactoryClass(fullyQualifiedTypeName);
        if (factoryType == null)
            return RefactoringStatus.createErrorStatus(Messages.format(RefactoringCoreMessages.IntroduceFactory_noSuchClass, BasicElementLabels.getJavaElementName(fullyQualifiedTypeName)));
        if (factoryType.isAnnotation())
            return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceFactory_cantPutFactoryMethodOnAnnotation);
        if (factoryType.isInterface())
            return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceFactory_cantPutFactoryMethodOnInterface);
    } catch (JavaModelException e) {
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_cantCheckForInterface);
    }
    ICompilationUnit factoryUnitHandle = factoryType.getCompilationUnit();
    if (factoryType.isBinary())
        return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceFactory_cantPutFactoryInBinaryClass);
    else {
        try {
            if (!fFactoryUnitHandle.equals(factoryUnitHandle)) {
                fFactoryCU = getASTFor(factoryUnitHandle);
                fFactoryUnitHandle = factoryUnitHandle;
            }
            fFactoryOwningClass = (AbstractTypeDeclaration) ASTNodes.getParent(NodeFinder.perform(fFactoryCU, factoryType.getNameRange()), AbstractTypeDeclaration.class);
            String factoryPkg = factoryType.getPackageFragment().getElementName();
            String ctorPkg = fCtorOwningClass.resolveBinding().getPackage().getName();
            if (!factoryPkg.equals(ctorPkg))
                fConstructorVisibility = Modifier.PUBLIC;
            else if (fFactoryOwningClass != fCtorOwningClass)
                // No such thing as Modifier.PACKAGE...
                fConstructorVisibility = 0;
            if (fFactoryOwningClass != fCtorOwningClass)
                // No such thing as Modifier.PACKAGE...
                fConstructorVisibility = 0;
        } catch (JavaModelException e) {
            return RefactoringStatus.createFatalErrorStatus(e.getMessage());
        }
        return new RefactoringStatus();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IType(org.eclipse.jdt.core.IType)

Example 69 with JavaModelException

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

the class IntroduceIndirectionRefactoring method createChange.

// ******************** CHANGE CREATION ***********************
@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    final Map<String, String> arguments = new HashMap<String, String>();
    String project = null;
    IJavaProject javaProject = fTargetMethod.getJavaProject();
    if (javaProject != null)
        project = javaProject.getElementName();
    int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE;
    final IType declaring = fTargetMethod.getDeclaringType();
    try {
        if (declaring.isLocal() || declaring.isAnonymous())
            flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
    } catch (JavaModelException exception) {
        JavaPlugin.log(exception);
    }
    final String description = Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fTargetMethod.getElementName()));
    final String header = Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_descriptor_description, new String[] { JavaElementLabels.getTextLabel(fTargetMethod, JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getTextLabel(declaring, JavaElementLabels.ALL_FULLY_QUALIFIED) });
    final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
    comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_original_pattern, JavaElementLabels.getTextLabel(fTargetMethod, JavaElementLabels.ALL_FULLY_QUALIFIED)));
    comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_method_pattern, BasicElementLabels.getJavaElementName(fIntermediaryMethodName)));
    comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_declaring_pattern, JavaElementLabels.getTextLabel(fIntermediaryType, JavaElementLabels.ALL_FULLY_QUALIFIED)));
    if (fUpdateReferences)
        comment.addSetting(RefactoringCoreMessages.JavaRefactoringDescriptor_update_references);
    final IntroduceIndirectionDescriptor descriptor = RefactoringSignatureDescriptorFactory.createIntroduceIndirectionDescriptor(project, description, comment.asString(), arguments, flags);
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fTargetMethod));
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fIntermediaryMethodName);
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + 1, JavaRefactoringDescriptorUtil.elementToHandle(project, fIntermediaryType));
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_REFERENCES, Boolean.valueOf(fUpdateReferences).toString());
    return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.IntroduceIndirectionRefactoring_introduce_indirection, fTextChangeManager.getAllChanges());
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IntroduceIndirectionDescriptor(org.eclipse.jdt.core.refactoring.descriptors.IntroduceIndirectionDescriptor) IJavaProject(org.eclipse.jdt.core.IJavaProject) HashMap(java.util.HashMap) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) IType(org.eclipse.jdt.core.IType) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment)

Example 70 with JavaModelException

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

the class ExtractTempRefactoring method getExcludedVariableNames.

private String[] getExcludedVariableNames() {
    if (fExcludedVariableNames == null) {
        try {
            IBinding[] bindings = new ScopeAnalyzer(fCompilationUnitNode).getDeclarationsInScope(getSelectedExpression().getStartPosition(), ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY);
            fExcludedVariableNames = new String[bindings.length];
            for (int i = 0; i < bindings.length; i++) {
                fExcludedVariableNames[i] = bindings[i].getName();
            }
        } catch (JavaModelException e) {
            fExcludedVariableNames = new String[0];
        }
    }
    return fExcludedVariableNames;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IBinding(org.eclipse.jdt.core.dom.IBinding) ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer)

Aggregations

JavaModelException (org.eclipse.jdt.core.JavaModelException)172 IJavaProject (org.eclipse.jdt.core.IJavaProject)44 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)41 IType (org.eclipse.jdt.core.IType)40 IJavaElement (org.eclipse.jdt.core.IJavaElement)35 CoreException (org.eclipse.core.runtime.CoreException)30 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)25 IPath (org.eclipse.core.runtime.IPath)24 ArrayList (java.util.ArrayList)21 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)17 IFile (org.eclipse.core.resources.IFile)15 IResource (org.eclipse.core.resources.IResource)15 HashMap (java.util.HashMap)14 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)13 IMethod (org.eclipse.jdt.core.IMethod)12 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)12 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