Example usage for org.eclipse.jdt.core JavaModelException getJavaModelStatus

List of usage examples for org.eclipse.jdt.core JavaModelException getJavaModelStatus

Introduction

In this page you can find the example usage for org.eclipse.jdt.core JavaModelException getJavaModelStatus.

Prototype

public IJavaModelStatus getJavaModelStatus() 

Source Link

Document

Returns the Java model status object for this exception.

Usage

From source file:com.android.ide.eclipse.adt.internal.actions.AddCompatibilityJarAction.java

License:Open Source License

/**
 * Install the compatibility jar into the given project.
 *
 * @param project The Android project to install the compatibility jar into
 * @param waitForFinish If true, block until the task has finished
 * @return true if the installation was successful (or if <code>waitForFinish</code>
 *    is false, if the installation is likely to be successful - e.g. the user has
 *    at least agreed to all installation prompts.)
 *//*from www .  ja  va 2s  .  com*/
public static boolean install(final IProject project, boolean waitForFinish) {

    final IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null) {
        // Should not happen.
        AdtPlugin.log(IStatus.ERROR, "JavaProject is null for %1$s", project); //$NON-NLS-1$
    }

    final Sdk sdk = Sdk.getCurrent();
    if (sdk == null) {
        AdtPlugin.printErrorToConsole(AddCompatibilityJarAction.class.getSimpleName(), // tag
                "Error: Android SDK is not loaded yet."); //$NON-NLS-1$
        return false;
    }

    // TODO: For the generic action, check the library isn't in the project already.

    // First call the package manager to make sure the package is installed
    // and get the installation path of the library.

    AdtUpdateDialog window = new AdtUpdateDialog(AdtPlugin.getDisplay().getActiveShell(),
            new AdtConsoleSdkLog(), sdk.getSdkLocation());

    Pair<Boolean, File> result = window.installExtraPackage("android", "compatibility"); //$NON-NLS-1$ //$NON-NLS-2$

    if (!result.getFirst().booleanValue()) {
        AdtPlugin.printErrorToConsole("Failed to install Android Compatibility library");
        return false;
    }

    // TODO these "v4" values needs to be dynamic, e.g. we could try to match
    // vN/android-support-vN.jar. Eventually we'll want to rely on info from the
    // package manifest anyway so this is irrelevant.

    File path = new File(result.getSecond(), "v4"); //$NON-NLS-1$
    final File jarPath = new File(path, "android-support-v4.jar"); //$NON-NLS-1$

    if (!jarPath.isFile()) {
        AdtPlugin.printErrorToConsole("Android Compatibility JAR not found:", jarPath.getAbsolutePath());
        return false;
    }

    // Then run an Eclipse asynchronous job to update the project

    Job job = new Job("Add Compatibility Library to Project") {
        @Override
        protected IStatus run(IProgressMonitor monitor) {
            try {
                monitor.beginTask("Add library to project build path", 3);

                IResource jarRes = copyJarIntoProject(project, jarPath, monitor);

                monitor.worked(1);

                IClasspathEntry libEntry = JavaCore.newLibraryEntry(jarRes.getFullPath(),
                        null /*sourceAttachmentPath*/, null /*sourceAttachmentRootPath*/ );

                if (!ProjectHelper.isEntryInClasspath(javaProject, libEntry)) {
                    ProjectHelper.addEntryToClasspath(javaProject, libEntry);
                }

                monitor.worked(1);

                return Status.OK_STATUS;
            } catch (JavaModelException e) {
                return e.getJavaModelStatus();
            } catch (CoreException e) {
                return e.getStatus();
            } catch (Exception e) {
                return new Status(Status.ERROR, AdtPlugin.PLUGIN_ID, Status.ERROR, "Failed", e); //$NON-NLS-1$
            } finally {
                if (monitor != null) {
                    monitor.done();
                }
            }
        }
    };
    job.schedule();

    if (waitForFinish) {
        try {
            job.join();
            return job.getState() == IStatus.OK;
        } catch (InterruptedException e) {
            AdtPlugin.log(e, null);
        }
    }

    return true;
}

From source file:com.android.ide.eclipse.adt.internal.actions.ConvertToAndroidAction.java

License:Open Source License

/**
 * Toggles sample nature on a project/*ww w.  j  a  va2s  .  c o m*/
 *
 * @param project to have sample nature added or removed
 */
private void convertProject(final IProject project) {
    new Job("Convert Project") {
        @Override
        protected IStatus run(IProgressMonitor monitor) {
            try {
                if (monitor != null) {
                    monitor.beginTask(String.format("Convert %1$s to Android", project.getName()), 5);
                }

                IProjectDescription description = project.getDescription();
                String[] natures = description.getNatureIds();

                // check if the project already has the android nature.
                for (int i = 0; i < natures.length; ++i) {
                    if (AndroidConstants.NATURE_DEFAULT.equals(natures[i])) {
                        // we shouldn't be here as the visibility of the item
                        // is dependent on the project.
                        return new Status(Status.WARNING, AdtPlugin.PLUGIN_ID,
                                "Project is already an Android project");
                    }
                }

                if (monitor != null) {
                    monitor.worked(1);
                }

                String[] newNatures = new String[natures.length + 1];
                System.arraycopy(natures, 0, newNatures, 1, natures.length);
                newNatures[0] = AndroidConstants.NATURE_DEFAULT;

                // set the new nature list in the project
                description.setNatureIds(newNatures);
                project.setDescription(description, null);
                if (monitor != null) {
                    monitor.worked(1);
                }

                // Fix the classpath entries.
                // get a java project
                IJavaProject javaProject = JavaCore.create(project);
                ProjectHelper.fixProjectClasspathEntries(javaProject);
                if (monitor != null) {
                    monitor.worked(1);
                }

                return Status.OK_STATUS;
            } catch (JavaModelException e) {
                return e.getJavaModelStatus();
            } catch (CoreException e) {
                return e.getStatus();
            } finally {
                if (monitor != null) {
                    monitor.done();
                }
            }
        }
    }.schedule();
}

From source file:com.android.ide.eclipse.adt.internal.actions.FixProjectAction.java

License:Open Source License

/**
 * Creates a job to fix the project//from   w  w  w.  j av  a2  s .  c om
 *
 * @param project the project to fix
 * @return a job to perform the fix (not yet scheduled)
 */
@NonNull
public static Job createFixProjectJob(@NonNull final IProject project) {
    return new Job("Fix Project Properties") {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            try {
                if (monitor != null) {
                    monitor.beginTask("Fix Project Properties", 6);
                }

                ProjectHelper.fixProject(project);
                if (monitor != null) {
                    monitor.worked(1);
                }

                // fix the nature order to have the proper project icon
                ProjectHelper.fixProjectNatureOrder(project);
                if (monitor != null) {
                    monitor.worked(1);
                }

                // now we fix the builders
                AndroidNature.configureResourceManagerBuilder(project);
                if (monitor != null) {
                    monitor.worked(1);
                }

                AndroidNature.configurePreBuilder(project);
                if (monitor != null) {
                    monitor.worked(1);
                }

                AndroidNature.configureApkBuilder(project);
                if (monitor != null) {
                    monitor.worked(1);
                }

                return Status.OK_STATUS;
            } catch (JavaModelException e) {
                return e.getJavaModelStatus();
            } catch (CoreException e) {
                return e.getStatus();
            } finally {
                if (monitor != null) {
                    monitor.done();
                }
            }
        }
    };
}

From source file:com.android.ide.eclipse.adt.project.ConvertToAndroidAction.java

License:Open Source License

/**
 * Toggles sample nature on a project/*from  w  w  w.j a va 2  s. com*/
 * 
 * @param project to have sample nature added or removed
 */
private void convertProject(final IProject project) {
    new Job("Convert Project") {
        @Override
        protected IStatus run(IProgressMonitor monitor) {
            try {
                if (monitor != null) {
                    monitor.beginTask(String.format("Convert %1$s to Android", project.getName()), 5);
                }

                IProjectDescription description = project.getDescription();
                String[] natures = description.getNatureIds();

                // check if the project already has the android nature.
                for (int i = 0; i < natures.length; ++i) {
                    if (AndroidConstants.NATURE.equals(natures[i])) {
                        // we shouldn't be here as the visibility of the item
                        // is dependent on the project.
                        return new Status(Status.WARNING, AdtPlugin.PLUGIN_ID,
                                "Project is already an Android project");
                    }
                }

                if (monitor != null) {
                    monitor.worked(1);
                }

                String[] newNatures = new String[natures.length + 1];
                System.arraycopy(natures, 0, newNatures, 1, natures.length);
                newNatures[0] = AndroidConstants.NATURE;

                // set the new nature list in the project
                description.setNatureIds(newNatures);
                project.setDescription(description, null);
                if (monitor != null) {
                    monitor.worked(1);
                }

                // Fix the classpath entries.
                // get a java project
                IJavaProject javaProject = JavaCore.create(project);
                ProjectHelper.fixProjectClasspathEntries(javaProject);
                if (monitor != null) {
                    monitor.worked(1);
                }

                return Status.OK_STATUS;
            } catch (JavaModelException e) {
                return e.getJavaModelStatus();
            } catch (CoreException e) {
                return e.getStatus();
            } finally {
                if (monitor != null) {
                    monitor.done();
                }
            }
        }
    }.schedule();
}

From source file:com.android.ide.eclipse.adt.project.FixProjectAction.java

License:Open Source License

private void fixProject(final IProject project) {
    new Job("Fix Project Properties") {

        @Override/*from www . j  a  v a2 s  .co  m*/
        protected IStatus run(IProgressMonitor monitor) {
            try {
                if (monitor != null) {
                    monitor.beginTask("Fix Project Properties", 6);
                }

                ProjectHelper.fixProject(project);
                if (monitor != null) {
                    monitor.worked(1);
                }

                // fix the nature order to have the proper project icon
                ProjectHelper.fixProjectNatureOrder(project);
                if (monitor != null) {
                    monitor.worked(1);
                }

                // now we fix the builders
                AndroidNature.configureResourceManagerBuilder(project);
                if (monitor != null) {
                    monitor.worked(1);
                }

                AndroidNature.configurePreBuilder(project);
                if (monitor != null) {
                    monitor.worked(1);
                }

                AndroidNature.configureApkBuilder(project);
                if (monitor != null) {
                    monitor.worked(1);
                }

                return Status.OK_STATUS;
            } catch (JavaModelException e) {
                return e.getJavaModelStatus();
            } catch (CoreException e) {
                return e.getStatus();
            } finally {
                if (monitor != null) {
                    monitor.done();
                }
            }
        }
    }.schedule();
}

From source file:com.codenvy.ide.ext.java.server.internal.core.ClassFile.java

License:Open Source License

private IStatus validateClassFile() {
    IPackageFragmentRoot root = getPackageFragmentRoot();
    try {/*from  w w w. j av  a  2 s.  c o m*/
        if (root.getKind() != IPackageFragmentRoot.K_BINARY)
            return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, root);
    } catch (JavaModelException e) {
        return e.getJavaModelStatus();
    }
    IJavaProject project = getJavaProject();
    if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(getElementName())) {
        return Status.OK_STATUS;
    }
    return Status.CANCEL_STATUS;
    //   return JavaConventions.validateClassFileName(getElementName(), project.getOption(JavaCore.COMPILER_SOURCE, true),
    //                                     project.getOption(JavaCore.COMPILER_COMPLIANCE, true));
}

From source file:com.codenvy.ide.ext.java.server.internal.core.CompilationUnit.java

License:Open Source License

/**
 * @see org.eclipse.jdt.internal.compiler.env.ICompilationUnit#getContents()
 *///  www  .  j  av  a  2  s  . c o  m
public char[] getContents() {
    IBuffer buffer = getBufferManager().getBuffer(this);
    if (buffer == null) {
        // no need to force opening of CU to get the content
        // also this cannot be a working copy, as its buffer is never closed while the working copy is alive
        File file = resource();
        // Get encoding from file
        String encoding;
        encoding = "UTF-8"; //file.getCharset();
        try {
            return Util.getResourceContentsAsCharArray(file, encoding);
        } catch (JavaModelException e) {
            if (manager.abortOnMissingSource.get() == Boolean.TRUE) {
                IOException ioException = e.getJavaModelStatus()
                        .getCode() == IJavaModelStatusConstants.IO_EXCEPTION ? (IOException) e.getException()
                                : new IOException(e.getMessage());
                throw new AbortCompilationUnit(null, ioException, encoding);
            } else {
                Util.log(e, Messages.bind(Messages.file_notFound, file.getAbsolutePath()));
            }
            return CharOperation.NO_CHAR;
        }
    }
    char[] contents = buffer.getCharacters();
    if (contents == null) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=129814
        if (manager.abortOnMissingSource.get() == Boolean.TRUE) {
            IOException ioException = new IOException(Messages.buffer_closed);
            IFile file = (IFile) getResource();
            // Get encoding from file
            String encoding;
            try {
                encoding = file.getCharset();
            } catch (CoreException ce) {
                // do not use any encoding
                encoding = null;
            }
            throw new AbortCompilationUnit(null, ioException, encoding);
        }
        return CharOperation.NO_CHAR;
    }
    return contents;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.CompilationUnit.java

License:Open Source License

protected IStatus validateCompilationUnit(File resource) {
    IPackageFragmentRoot root = getPackageFragmentRoot();
    // root never null as validation is not done for working copies
    try {/*w w w.  j a  v  a  2  s  . c  om*/
        if (root.getKind() != IPackageFragmentRoot.K_SOURCE)
            return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, root);
    } catch (JavaModelException e) {
        return e.getJavaModelStatus();
    }
    if (resource != null) {
        char[][] inclusionPatterns = ((PackageFragmentRoot) root).fullInclusionPatternChars();
        char[][] exclusionPatterns = ((PackageFragmentRoot) root).fullExclusionPatternChars();
        if (Util.isExcluded(new Path(resource.getPath()), inclusionPatterns, exclusionPatterns, false))
            return new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_NOT_ON_CLASSPATH, this);
        if (!resource.exists())
            return new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, this);
    }
    IJavaProject project = getJavaProject();
    return JavaConventions.validateCompilationUnitName(getElementName(),
            project.getOption(JavaCore.COMPILER_SOURCE, true),
            project.getOption(JavaCore.COMPILER_COMPLIANCE, true));
}

From source file:io.sarl.eclipse.wizards.elements.AbstractNewSarlElementWizardPage.java

License:Apache License

@Override
protected IStatus superClassChanged() {
    IStatus status = super.superClassChanged();
    assert status != null;
    if (status.isOK() && isSuperTypeActivated()) {
        final String className = getSuperClass();
        try {/*from  ww  w . j a v a  2s  . co  m*/
            if (!isValidExtendedType(className)) {
                status = SARLEclipsePlugin.getDefault().createStatus(IStatus.ERROR,
                        MessageFormat.format(getInvalidSubtypeErrorMessage(), className));
            }
        } catch (JavaModelException ex) {
            status = ex.getJavaModelStatus();
        }
    }
    return status;
}

From source file:net.sf.j2s.core.builder.AbstractImageBuilder.java

License:Open Source License

protected void createProblemFor(IResource resource, IMember javaElement, String message,
        String problemSeverity) {
    try {/*from www .  ja v a 2  s.c o  m*/
        IMarker marker = resource.createMarker(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);
        int severity = problemSeverity.equals(JavaCore.WARNING) ? IMarker.SEVERITY_WARNING
                : IMarker.SEVERITY_ERROR;

        ISourceRange range = null;
        if (javaElement != null) {
            try {
                range = javaElement.getNameRange();
            } catch (JavaModelException e) {
                if (e.getJavaModelStatus().getCode() != IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST) {
                    throw e;
                }
                if (!CharOperation.equals(javaElement.getElementName().toCharArray(),
                        TypeConstants.PACKAGE_INFO_NAME)) {
                    throw e;
                }
                // else silently swallow the exception as the synthetic interface type package-info has no
                // source range really. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=258145
            }
        }
        int start = range == null ? 0 : range.getOffset();
        int end = range == null ? 1 : start + range.getLength();
        marker.setAttributes(
                new String[] { IMarker.MESSAGE, IMarker.SEVERITY, IMarker.CHAR_START, IMarker.CHAR_END,
                        IMarker.SOURCE_ID },
                new Object[] { message, new Integer(severity), new Integer(start), new Integer(end),
                        JavaBuilder.SOURCE_ID });
    } catch (CoreException e) {
        throw internalException(e);
    }
}