Search in sources :

Example 26 with JavaModelException

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

the class SourceMapper method mapSource.

/**
     * Maps the given source code to the given binary type and its children.
     * If a non-null java element is passed, finds the name range for the
     * given java element without storing it.
     */
public synchronized ISourceRange mapSource(IType type, char[] contents, IBinaryType info, IJavaElement elementToFind) {
    this.binaryType = (BinaryType) type;
    // check whether it is already mapped
    if (this.sourceRanges.get(type) != null)
        return (elementToFind != null) ? getNameRange(elementToFind) : null;
    this.importsTable.remove(this.binaryType);
    this.importsCounterTable.remove(this.binaryType);
    this.searchedElement = elementToFind;
    this.types = new IType[1];
    this.typeDeclarationStarts = new int[1];
    this.typeNameRanges = new SourceRange[1];
    this.typeModifiers = new int[1];
    this.typeDepth = -1;
    this.memberDeclarationStart = new int[1];
    this.memberName = new String[1];
    this.memberNameRange = new SourceRange[1];
    this.methodParameterTypes = new char[1][][];
    this.methodParameterNames = new char[1][][];
    this.anonymousCounter = 0;
    HashMap oldSourceRanges = null;
    if (elementToFind != null) {
        oldSourceRanges = (HashMap) this.sourceRanges.clone();
    }
    try {
        IProblemFactory factory = new DefaultProblemFactory();
        SourceElementParser parser = null;
        this.anonymousClassName = 0;
        if (info == null) {
            try {
                info = (IBinaryType) this.binaryType.getElementInfo();
            } catch (JavaModelException e) {
                return null;
            }
        }
        boolean isAnonymousClass = info.isAnonymous();
        char[] fullName = info.getName();
        if (isAnonymousClass) {
            String eltName = this.binaryType.getParent().getElementName();
            eltName = eltName.substring(eltName.lastIndexOf('$') + 1, eltName.length());
            try {
                this.anonymousClassName = Integer.parseInt(eltName);
            } catch (NumberFormatException e) {
            // ignore
            }
        }
        boolean doFullParse = hasToRetrieveSourceRangesForLocalClass(fullName);
        parser = new SourceElementParser(this, factory, new CompilerOptions(this.options), doFullParse, true);
        // disable javadoc parsing
        parser.javadocParser.checkDocComment = false;
        IJavaElement javaElement = this.binaryType.getCompilationUnit();
        if (javaElement == null)
            javaElement = this.binaryType.getParent();
        parser.parseCompilationUnit(new BasicCompilationUnit(contents, null, this.binaryType.sourceFileName(info), javaElement), doFullParse, null);
        if (elementToFind != null) {
            ISourceRange range = getNameRange(elementToFind);
            return range;
        } else {
            return null;
        }
    } finally {
        if (elementToFind != null) {
            this.sourceRanges = oldSourceRanges;
        }
        this.binaryType = null;
        this.searchedElement = null;
        this.types = null;
        this.typeDeclarationStarts = null;
        this.typeNameRanges = null;
        this.typeDepth = -1;
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) HashMap(java.util.HashMap) BasicCompilationUnit(org.eclipse.jdt.internal.core.BasicCompilationUnit) SourceElementParser(org.eclipse.jdt.internal.compiler.SourceElementParser) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) DefaultProblemFactory(org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory) IProblemFactory(org.eclipse.jdt.internal.compiler.IProblemFactory) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 27 with JavaModelException

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

the class DeltaProcessor method processResourceDelta.

/*
     * Converts a <code>IResourceDelta</code> rooted in a <code>Workspace</code> into
     * the corresponding set of <code>IJavaElementDelta</code>, rooted in the
     * relevant <code>JavaModel</code>s.
     */
private IJavaElementDelta processResourceDelta(IResourceDelta changes) {
    try {
        IJavaModel model = this.manager.getJavaModel();
        if (!model.isOpen()) {
            // force opening of java model so that java element delta are reported
            try {
                model.open(null);
            } catch (JavaModelException e) {
                if (VERBOSE) {
                    e.printStackTrace();
                }
                return null;
            }
        }
        this.state.initializeRoots(false);
        this.currentElement = null;
        // get the workspace delta, and start processing there.
        //			IResourceDelta[] deltas = (IResourceDelta[])changes.getAffectedChildren(IResourceDelta.ADDED | IResourceDelta.REMOVED |
        // IResourceDelta.CHANGED, IContainer.INCLUDE_HIDDEN);
        //			for (int i = 0; i < deltas.length; i++) {
        //				IResourceDelta delta = deltas[i];
        //				File res = delta.getFile();
        //
        //				// find out the element type
        //				RootInfo rootInfo = null;
        //				int elementType;
        //				IProject proj = (IProject)res;
        //				boolean wasJavaProject = this.state.findJavaProject(proj.getName()) != null;
        //				boolean isJavaProject = JavaProject.hasJavaNature(proj);
        //				if (!wasJavaProject && !isJavaProject) {
        //					elementType = NON_JAVA_RESOURCE;
        //				} else {
        //					IPath rootPath = externalPath(res);
        //					rootInfo = enclosingRootInfo(rootPath, delta.getKind());
        //					if (rootInfo != null && rootInfo.isRootOfProject(rootPath)) {
        //						elementType = IJavaElement.PACKAGE_FRAGMENT_ROOT;
        //					} else {
        //						elementType = IJavaElement.JAVA_PROJECT;
        //					}
        //				}
        //
        //				// traverse delta
        //				traverseDelta(changes, IJavaElement.JAVA_PROJECT, null, null);
        updateCurrentDeltaAndIndex(changes, IJavaElement.COMPILATION_UNIT, null);
        //
        //				if (elementType == NON_JAVA_RESOURCE
        //						|| (wasJavaProject != isJavaProject && (delta.getKind()) == IResourceDelta.CHANGED)) { // project has changed
        // nature (description or open/closed)
        //					try {
        //						// add child as non java resource
        //						nonJavaResourcesChanged((JavaModel)model, delta);
        //					} catch (JavaModelException e) {
        //						// java model could not be opened
        //					}
        //				}
        //
        //			}
        resetProjectCaches();
        return this.currentDelta;
    } finally {
        this.currentDelta = null;
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaModel(org.eclipse.jdt.core.IJavaModel)

Example 28 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 29 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 30 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)

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