Example usage for org.eclipse.jdt.core IJavaProject exists

List of usage examples for org.eclipse.jdt.core IJavaProject exists

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject exists.

Prototype

boolean exists();

Source Link

Document

Returns whether this Java element exists in the model.

Usage

From source file:de.devboost.eclipse.jdtutilities.JDTUtility.java

License:Open Source License

/**
 * Returns the Java type that has the given qualified name and is contained
 * in a project with the given name. If no type is found, <code>null</code> 
 * is returned.//from ww w. j a  va 2 s  .co m
 */
public IType getType(String projectName, String qualifiedName) {

    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    IProject project = root.getProject(projectName);
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null) {
        return null;
    }
    if (!javaProject.exists()) {
        return null;
    }
    try {
        IType type = javaProject.findType(qualifiedName);
        return type;
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

/**
 * @param javaElement//from  ww w. j  a va 2s.  c  o m
 * @return absolute path of generated bytecode package for given element
 * @throws JavaModelException
 */
private static String getPackageOutputPath(IJavaElement javaElement) throws JavaModelException {
    String dir = ""; //$NON-NLS-1$
    if (javaElement == null) {
        return dir;
    }

    IJavaProject project = javaElement.getJavaProject();

    if (project == null) {
        return dir;
    }
    // default bytecode location
    IPath path = project.getOutputLocation();

    IResource resource = javaElement.getUnderlyingResource();
    if (resource == null) {
        return dir;
    }
    // resolve multiple output locations here
    if (project.exists() && project.getProject().isOpen()) {
        IClasspathEntry entries[] = project.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry classpathEntry = entries[i];
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath outputPath = classpathEntry.getOutputLocation();
                if (outputPath != null && classpathEntry.getPath().isPrefixOf(resource.getFullPath())) {
                    path = outputPath;
                    break;
                }
            }
        }
    }

    if (path == null) {
        // check the default location if not already included
        IPath def = project.getOutputLocation();
        if (def != null && def.isPrefixOf(resource.getFullPath())) {
            path = def;
        }
    }

    if (path == null) {
        return dir;
    }

    IWorkspace workspace = ResourcesPlugin.getWorkspace();

    if (!project.getPath().equals(path)) {
        IFolder outputFolder = workspace.getRoot().getFolder(path);
        if (outputFolder != null) {
            // linked resources will be resolved here!
            IPath rawPath = outputFolder.getRawLocation();
            if (rawPath != null) {
                path = rawPath;
            }
        }
    } else {
        path = project.getProject().getLocation();
    }

    // here we should resolve path variables,
    // probably existing at first place of path
    IPathVariableManager pathManager = workspace.getPathVariableManager();
    path = pathManager.resolvePath(path);

    if (path == null) {
        return dir;
    }

    if (isPackageRoot(project, resource)) {
        dir = path.toOSString();
    } else {
        String packPath = EclipseUtils.getJavaPackageName(javaElement).replace(Signature.C_DOT,
                PACKAGE_SEPARATOR);
        dir = path.append(packPath).toOSString();
    }
    return dir;
}

From source file:de.loskutov.eclipse.jdepend.views.TreeObject.java

License:Open Source License

protected String getPackageOutputPath(IJavaElement jElement) throws JavaModelException {
    String dir = ""; //$NON-NLS-1$
    if (jElement == null) {
        return dir;
    }//from  w ww  .j a  v  a  2 s  .  c o m

    IJavaProject project = jElement.getJavaProject();

    if (project == null) {
        return dir;
    }
    IPath path = project.getOutputLocation();

    IResource resource = jElement.getUnderlyingResource();
    if (resource == null) {
        return dir;
    }
    // resolve multiple output locations here
    if (project.exists() && project.getProject().isOpen()) {
        IClasspathEntry entries[] = project.getRawClasspath();

        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry classpathEntry = entries[i];
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath outputPath = classpathEntry.getOutputLocation();
                if (outputPath != null) {
                    // if this source folder contains specified java resource
                    // then take bytecode location from himself
                    if (classpathEntry.getPath().isPrefixOf(resource.getFullPath())) {
                        path = outputPath;
                        break;
                    }
                }
            }
        }
    }

    if (path == null) {
        // check the default location if not already included
        IPath def = project.getOutputLocation();
        if (def != null && def.isPrefixOf(resource.getFullPath())) {
            path = def;
        }
    }

    if (path == null) {
        return dir;
    }

    IWorkspace workspace = ResourcesPlugin.getWorkspace();

    if (!project.getPath().equals(path)) {
        IFolder outputFolder = workspace.getRoot().getFolder(path);
        if (outputFolder != null) {
            // linked resources will be resolved here!
            IPath rawPath = outputFolder.getRawLocation();
            if (rawPath != null) {
                path = rawPath;
            }
        }
    } else {
        path = project.getProject().getLocation();
    }

    if (path == null) {
        return dir;
    }

    // here we should resolve path variables,
    // probably existing at first place of path
    IPathVariableManager pathManager = workspace.getPathVariableManager();
    path = pathManager.resolvePath(path);

    if (path == null) {
        return dir;
    }

    boolean packageRoot = false;
    try {
        packageRoot = isPackageRoot(project, resource);
    } catch (JavaModelException e) {
        // seems to be a bug in 3.3
    }
    if (packageRoot) {
        dir = path.toOSString();
    } else {
        String packPath = getPackageName().replace('.', '/');
        dir = path.append(packPath).toOSString();
    }
    return dir;
}

From source file:egovframework.hdev.imp.ide.common.DeviceAPIIdeUtils.java

License:Apache License

/**
 * ?  ?/*ww w. j a v  a2s  .  c  o  m*/
 * @param classpathEntry
 * @param append
 * @throws CoreException
 */
public static void assignClasspathEntryToJavaProject(IProject project, IClasspathEntry classpathEntry,
        boolean append) throws CoreException {
    IJavaProject javaProject = JavaCore.create(project);
    if ((javaProject == null) || (!javaProject.exists()))
        return;
    try {

        IClasspathEntry[] classpath;
        ArrayList<IClasspathEntry> entries;
        if (append) {
            classpath = javaProject.getRawClasspath();
            entries = new ArrayList<IClasspathEntry>(Arrays.asList(classpath));
        } else {
            entries = new ArrayList<IClasspathEntry>();
        }

        entries.add(classpathEntry);

        classpath = entries.toArray(new IClasspathEntry[entries.size()]);
        javaProject.setRawClasspath(classpath, null);
    } catch (JavaModelException e) {
        DeviceAPIIdeLog.logError(e);
    }
}

From source file:egovframework.hdev.imp.ide.common.DeviceAPIIdeUtils.java

License:Apache License

/**
 * ?  ?/*from w  w  w  .  j  a v  a 2  s .co m*/
 * @param project
 * @param collection
 * @param append
 * @throws CoreException
 */
public static void assignClasspathEntryToJavaProject(IProject project, Collection<IClasspathEntry> collection,
        boolean append) throws CoreException {
    IJavaProject javaProject = JavaCore.create(project);
    if ((javaProject == null) || (!javaProject.exists()))
        return;
    try {
        IClasspathEntry[] classpath;
        ArrayList<IClasspathEntry> entries;
        if (append) {
            classpath = javaProject.getRawClasspath();
            entries = new ArrayList<IClasspathEntry>(Arrays.asList(classpath));
        } else {
            entries = new ArrayList<IClasspathEntry>();
        }

        entries.addAll(collection);

        classpath = entries.toArray(new IClasspathEntry[entries.size()]);
        javaProject.setRawClasspath(classpath, null);
    } catch (JavaModelException e) {
        DeviceAPIIdeLog.logError(e);
    }
}

From source file:egovframework.hdev.imp.ide.common.DeviceAPIIdeUtils.java

License:Apache License

/**
 * ?  /*from w w  w. j  a  va  2s  . co  m*/
 * @param project
 * @throws CoreException
 */
@SuppressWarnings("unchecked")
public static void sortClasspathEntry(IProject project) throws CoreException {
    IJavaProject javaProject = JavaCore.create(project);

    if ((javaProject == null) || (!javaProject.exists()))
        return;
    try {
        IClasspathEntry[] classpath;
        ArrayList<IClasspathEntry> entries;

        classpath = javaProject.getRawClasspath();
        entries = new ArrayList<IClasspathEntry>(Arrays.asList(classpath));

        DeviceAPIIdeUtils utils = new DeviceAPIIdeUtils();
        ClasspathComparator classpathComparator = utils.new ClasspathComparator();
        Collections.sort(entries, classpathComparator);

        classpath = entries.toArray(new IClasspathEntry[entries.size()]);
        javaProject.setRawClasspath(classpath, null);
    } catch (JavaModelException e) {
        DeviceAPIIdeLog.logError(e);
    }
}

From source file:egovframework.hdev.imp.ide.common.DeviceAPIIdeUtils.java

License:Apache License

/**
 * ?  /*w ww .  ja v  a2s. co m*/
 * @param project
 * @throws CoreException
 */
public static void removeClasspathEntry(IProject project, IClasspathEntry classpathEntry) throws CoreException {
    IJavaProject javaProject = JavaCore.create(project);

    if ((javaProject == null) || (!javaProject.exists()))
        return;

    try {
        IClasspathEntry[] classpath;
        ArrayList<IClasspathEntry> entries;

        classpath = javaProject.getRawClasspath();
        entries = new ArrayList<IClasspathEntry>(Arrays.asList(classpath));

        for (int i = 0; i < entries.size(); i++) {

            if (entries.get(i).getPath().toString().compareTo(classpathEntry.getPath().toString()) == 0) {

                entries.remove(i);
            }
        }

        classpath = entries.toArray(new IClasspathEntry[entries.size()]);
        javaProject.setRawClasspath(classpath, null);
    } catch (JavaModelException e) {
        DeviceAPIIdeLog.logError(e);
    }
}

From source file:es.bsc.servicess.ide.wizards.ServiceSsCommonWizardPage.java

License:Apache License

private static IStatus validateJavaTypeName(String text, IJavaProject project) {

    if (project == null || !project.exists()) {
        return JavaConventions.validateJavaTypeName(text, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3);
    }//from w  ww . j  a  v a  2  s  .  co  m
    return JavaConventionsUtil.validateJavaTypeName(text, project);
}

From source file:es.bsc.servicess.ide.wizards.ServiceSsCommonWizardPage.java

License:Apache License

private static IStatus validatePackageName(String text, IJavaProject project) {
    if (project == null || !project.exists()) {
        return JavaConventions.validatePackageName(text, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3);
    }/*from ww w .  j ava  2 s  .c  o m*/
    return JavaConventionsUtil.validatePackageName(text, project);
}

From source file:es.bsc.servicess.ide.wizards.ServiceSsCommonWizardPage.java

License:Apache License

/**
 * A hook method that gets called when the package field has changed. The
 * method validates the package name and returns the status of the
 * validation. The validation also updates the package fragment model.
 * <p>/* w  w w. j  av  a 2  s.  c om*/
 * Subclasses may extend this method to perform their own validation.
 * </p>
 * 
 * @return the status of the validation
 */
protected IStatus packageChanged() {
    StatusInfo status = new StatusInfo();
    IPackageFragmentRoot root = getPackageFragmentRoot();
    fPackageDialogField.enableButton(root != null);

    IJavaProject project = root != null ? root.getJavaProject() : null;

    String packName = getPackageText();
    if (packName.length() > 0) {
        IStatus val = validatePackageName(packName, project);
        if (val.getSeverity() == IStatus.ERROR) {
            status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_InvalidPackageName,
                    val.getMessage()));
            return status;
        } else if (val.getSeverity() == IStatus.WARNING) {
            status.setWarning(Messages.format(
                    NewWizardMessages.NewTypeWizardPage_warning_DiscouragedPackageName, val.getMessage()));
            // continue
        }
    } else {
        status.setWarning(NewWizardMessages.NewTypeWizardPage_warning_DefaultPackageDiscouraged);
    }

    if (project != null) {
        if (project.exists() && packName.length() > 0) {
            try {
                IPath rootPath = root.getPath();
                IPath outputPath = project.getOutputLocation();
                if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
                    // if the bin folder is inside of our root, don't allow
                    // to name a package
                    // like the bin folder
                    IPath packagePath = rootPath.append(packName.replace('.', '/'));
                    if (outputPath.isPrefixOf(packagePath)) {
                        status.setError(NewWizardMessages.NewTypeWizardPage_error_ClashOutputLocation);
                        return status;
                    }
                }
            } catch (JavaModelException e) {
                JavaPlugin.log(e);
                // let pass
            }
        }

        fCurrPackage = root.getPackageFragment(packName);
    } else {
        status.setError(""); //$NON-NLS-1$
    }
    return status;
}