Search in sources :

Example 81 with JavaModelException

use of org.eclipse.jdt.core.JavaModelException in project xtext-xtend by eclipse.

the class XtendUIValidator method getExpectedPackageName.

protected String getExpectedPackageName(XtendFile xtendFile) {
    URI fileURI = xtendFile.eResource().getURI();
    for (Pair<IStorage, IProject> storage : storage2UriMapper.getStorages(fileURI)) {
        if (storage.getFirst() instanceof IFile) {
            IPath fileWorkspacePath = storage.getFirst().getFullPath();
            IJavaProject javaProject = JavaCore.create(storage.getSecond());
            if (javaProject != null && javaProject.exists() && javaProject.isOpen()) {
                try {
                    for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                        if (!root.isArchive() && !root.isExternal()) {
                            IResource resource = root.getResource();
                            if (resource != null) {
                                IPath sourceFolderPath = resource.getFullPath();
                                if (sourceFolderPath.isPrefixOf(fileWorkspacePath)) {
                                    IPath claspathRelativePath = fileWorkspacePath.makeRelativeTo(sourceFolderPath);
                                    return claspathRelativePath.removeLastSegments(1).toString().replace("/", ".");
                                }
                            }
                        }
                    }
                } catch (JavaModelException e) {
                    LOG.error("Error resolving expected path for XtendFile", e);
                }
            }
        }
    }
    return null;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) IStorage(org.eclipse.core.resources.IStorage) URI(org.eclipse.emf.common.util.URI) IProject(org.eclipse.core.resources.IProject) IResource(org.eclipse.core.resources.IResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 82 with JavaModelException

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

the class JavaSearchScopeTestCaseLister method getTestCases.

@Override
public String[] getTestCases(boolean includeNonSource, ITestCaseFilter filter) throws TestCaseListException {
    final List<IJavaElement> testCaseList = new LinkedList<IJavaElement>();
    //$NON-NLS-1$ //$NON-NLS-2$
    search(Arrays.asList("junit.framework.TestCase", "junit.framework.TestSuite"), testCaseList);
    // Remove non-source and excludes
    Set<String> testCaseNames = new LinkedHashSet<String>();
    for (Iterator<IJavaElement> iter = testCaseList.iterator(); iter.hasNext(); ) {
        boolean omit = false;
        IJavaElement element = iter.next();
        try {
            IType type = (IType) element.getAncestor(IJavaElement.TYPE);
            if (Flags.isAbstract(type.getFlags())) {
                omit = true;
            }
            if (!includeNonSource) {
                IPackageFragment pkgFragment = (IPackageFragment) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                if (pkgFragment.getCompilationUnits().length == 0) {
                    omit = true;
                }
            }
        } catch (JavaModelException e) {
            throw new TestCaseListException(e);
        }
        String className = getClassName(element);
        if (filter != null && !filter.select(className)) {
            omit = true;
        }
        if (!omit) {
            testCaseNames.add(className);
        }
    }
    return testCaseNames.toArray(new String[0]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) LinkedList(java.util.LinkedList) IType(org.eclipse.jdt.core.IType)

Example 83 with JavaModelException

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

the class PlainJavaInitHandler method initializeClasspath.

@Override
protected void initializeClasspath(IJavaProject javaProject) throws ServerException {
    IClasspathEntry[] projectClasspath;
    try {
        projectClasspath = javaProject.getRawClasspath();
    } catch (JavaModelException e) {
        LOG.warn("Can't get classpath for: " + javaProject.getProject().getFullPath().toOSString(), e);
        throw new ServerException(e);
    }
    //default classpath
    IClasspathEntry[] defaultClasspath = new IClasspathEntry[] { JavaCore.newSourceEntry(javaProject.getPath()) };
    if (!Arrays.equals(defaultClasspath, projectClasspath)) {
        //classpath is already initialized
        return;
    }
    RegisteredProject project = projectRegistryProvider.get().getProject(javaProject.getPath().toOSString());
    List<String> sourceFolders = project.getAttributes().get(Constants.SOURCE_FOLDER);
    List<String> library = project.getAttributes().get(LIBRARY_FOLDER);
    classpathBuilder.generateClasspath(javaProject, sourceFolders, library);
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) ServerException(org.eclipse.che.api.core.ServerException) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) RegisteredProject(org.eclipse.che.api.project.server.RegisteredProject)

Example 84 with JavaModelException

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

the class CompilationUnitChangeNode method createChildNodes.

@Override
protected ChildNode[] createChildNodes() {
    TextEditBasedChange change = getTextEditBasedChange();
    if (change instanceof MultiStateTextFileChange) {
        // no edit preview & edit disabling possible in the MultiStateTextFileChange (edits must be applied
        return new ChildNode[0];
    // in sequence)
    }
    ICompilationUnit cunit = (ICompilationUnit) change.getAdapter(ICompilationUnit.class);
    if (cunit != null) {
        List<ChildNode> children = new ArrayList<ChildNode>(5);
        Map<IJavaElement, JavaLanguageNode> map = new HashMap<IJavaElement, JavaLanguageNode>(20);
        TextEditBasedChangeGroup[] changes = getSortedChangeGroups(change);
        for (int i = 0; i < changes.length; i++) {
            TextEditBasedChangeGroup tec = changes[i];
            try {
                IJavaElement element = getModifiedJavaElement(tec, cunit);
                if (element.equals(cunit)) {
                    children.add(createTextEditGroupNode(this, tec));
                } else {
                    JavaLanguageNode pjce = getChangeElement(map, element, children, this);
                    pjce.addChild(createTextEditGroupNode(pjce, tec));
                }
            } catch (JavaModelException e) {
                children.add(createTextEditGroupNode(this, tec));
            }
        }
        return children.toArray(new ChildNode[children.size()]);
    } else {
        return EMPTY_CHILDREN;
    }
}
Also used : MultiStateTextFileChange(org.eclipse.ltk.core.refactoring.MultiStateTextFileChange) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TextEditBasedChangeGroup(org.eclipse.ltk.core.refactoring.TextEditBasedChangeGroup) TextEditBasedChange(org.eclipse.ltk.core.refactoring.TextEditBasedChange)

Example 85 with JavaModelException

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

the class CompilationUnitChangeNode method getModifiedJavaElement.

private IJavaElement getModifiedJavaElement(TextEditBasedChangeGroup edit, ICompilationUnit cunit) throws JavaModelException {
    IRegion range = edit.getRegion();
    if (range.getOffset() == 0 && range.getLength() == 0)
        return cunit;
    IJavaElement result = cunit.getElementAt(range.getOffset());
    if (result == null)
        return cunit;
    try {
        while (true) {
            ISourceReference ref = (ISourceReference) result;
            IRegion sRange = new Region(ref.getSourceRange().getOffset(), ref.getSourceRange().getLength());
            if (result.getElementType() == IJavaElement.COMPILATION_UNIT || result.getParent() == null || coveredBy(edit, sRange))
                break;
            result = result.getParent();
        }
    } catch (JavaModelException e) {
    // Do nothing, use old value.
    } catch (ClassCastException e) {
    // Do nothing, use old value.
    }
    return result;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) ISourceReference(org.eclipse.jdt.core.ISourceReference) IRegion(org.eclipse.jface.text.IRegion)

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