Search in sources :

Example 61 with JavaModelException

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

the class DeltaProcessor method createExternalArchiveDelta.

/*
     * Check if external archives have changed for the given elements and create the corresponding deltas.
     * Returns whether at least one delta was created.
     */
private boolean createExternalArchiveDelta(HashSet refreshedElements, IProgressMonitor monitor) {
    HashMap externalArchivesStatus = new HashMap();
    boolean hasDelta = false;
    // find JARs to refresh
    HashSet archivePathsToRefresh = new HashSet();
    Iterator iterator = refreshedElements.iterator();
    while (iterator.hasNext()) {
        IJavaElement element = (IJavaElement) iterator.next();
        switch(element.getElementType()) {
            case IJavaElement.PACKAGE_FRAGMENT_ROOT:
                archivePathsToRefresh.add(element.getPath());
                break;
            case IJavaElement.JAVA_PROJECT:
                JavaProject javaProject = (JavaProject) element;
                if (!JavaProject.hasJavaNature(javaProject.getProject())) {
                    // project is not accessible or has lost its Java nature
                    break;
                }
                IClasspathEntry[] classpath;
                try {
                    classpath = javaProject.getResolvedClasspath();
                    for (int j = 0, cpLength = classpath.length; j < cpLength; j++) {
                        if (classpath[j].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                            archivePathsToRefresh.add(classpath[j].getPath());
                        }
                    }
                } catch (JavaModelException e) {
                // project doesn't exist -> ignore
                }
                break;
            case IJavaElement.JAVA_MODEL:
                //					}
                throw new UnsupportedOperationException();
        }
    }
    //		}
    return hasDelta;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaModelException(org.eclipse.jdt.core.JavaModelException) HashMap(java.util.HashMap) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Example 62 with JavaModelException

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

the class JavaProject method writeFileEntries.

/**
     * Writes the classpath in a sharable format (VCM-wise) only when necessary, that is, if  it is semantically different
     * from the existing one in file. Will never write an identical one.
     *
     * @param newClasspath IClasspathEntry[]
     * @param newOutputLocation IPath
     * @return boolean Return whether the .classpath file was modified.
     * @throws JavaModelException
     */
public boolean writeFileEntries(IClasspathEntry[] newClasspath, IClasspathEntry[] referencedEntries, IPath newOutputLocation) throws JavaModelException {
    if (!this.project.isAccessible())
        return false;
    Map unknownElements = new HashMap();
    IClasspathEntry[][] fileEntries = readFileEntries(unknownElements);
    if (fileEntries[0] != JavaProject.INVALID_CLASSPATH && areClasspathsEqual(newClasspath, newOutputLocation, fileEntries[0]) && (referencedEntries == null || areClasspathsEqual(referencedEntries, fileEntries[1]))) {
        // no need to save it, it is the same
        return false;
    }
    // actual file saving
    try {
        setSharedProperty(JavaProject.CLASSPATH_FILENAME, encodeClasspath(newClasspath, referencedEntries, newOutputLocation, true, unknownElements));
        return true;
    } catch (CoreException e) {
        throw new JavaModelException(e);
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) CoreException(org.eclipse.core.runtime.CoreException) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 63 with JavaModelException

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

the class JavaProject method setOptions.

@Override
public void setOptions(Map newOptions) {
    IEclipsePreferences projectPreferences = getEclipsePreferences();
    if (projectPreferences == null)
        return;
    try {
        if (newOptions == null) {
            projectPreferences.clear();
        } else {
            Iterator entries = newOptions.entrySet().iterator();
            JavaModelManager javaModelManager = getJavaModelManager();
            while (entries.hasNext()) {
                Map.Entry entry = (Map.Entry) entries.next();
                String key = (String) entry.getKey();
                String value = (String) entry.getValue();
                javaModelManager.storePreference(key, value, projectPreferences, newOptions);
            }
            // reset to default all options not in new map
            // @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=26255
            // @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=49691
            String[] pNames = projectPreferences.keys();
            int ln = pNames.length;
            for (int i = 0; i < ln; i++) {
                String key = pNames[i];
                if (!newOptions.containsKey(key)) {
                    // old preferences => remove from preferences table
                    projectPreferences.remove(key);
                }
            }
        }
        // persist options
        projectPreferences.flush();
        // flush cache immediately
        try {
            getPerProjectInfo().options = null;
        } catch (JavaModelException e) {
        // do nothing
        }
    } catch (BackingStoreException e) {
    // problem with pref store - quietly ignore
    }
}
Also used : JavaModelManager.getJavaModelManager(org.eclipse.jdt.internal.core.JavaModelManager.getJavaModelManager) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) JavaModelException(org.eclipse.jdt.core.JavaModelException) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) Iterator(java.util.Iterator) BackingStoreException(org.osgi.service.prefs.BackingStoreException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 64 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 65 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)

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