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

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

Introduction

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

Prototype

IPackageFragment findPackageFragment(IPath path) throws JavaModelException;

Source Link

Document

Returns the first existing package fragment on this project's classpath whose path matches the given (absolute) path, or null if none exist.

Usage

From source file:in.cypal.studio.gwt.ui.wizards.NewGwtRemoteServiceWizardPage.java

License:Apache License

public IPackageFragment getPackageFragment() {

    IPackageFragment packageFragment = null;
    if (!projectText.equals("")) {//$NON-NLS-1$

        try {/*from  ww  w .j  av  a  2s  .  c  o m*/
            IJavaProject javaProject = JavaCore.create(Util.getProject(projectText));
            IPath moduleXmlPath = new Path(moduleText);
            IPath packageFragmentPath = new Path("/").append(projectText) //$NON-NLS-1$
                    .append(moduleXmlPath.removeLastSegments(1));
            if (isImplCreation)
                packageFragmentPath = packageFragmentPath.append("server");//$NON-NLS-1$
            else
                packageFragmentPath = packageFragmentPath.append("client");//$NON-NLS-1$

            IFolder folder = javaProject.getProject().getFolder(packageFragmentPath.removeFirstSegments(1));
            if (!folder.exists())
                folder.create(true, true, null);
            packageFragment = javaProject.findPackageFragment(packageFragmentPath);
        } catch (Exception e) {
            Activator.logException(e);
        }
    }

    return packageFragment;
}

From source file:in.cypal.studio.gwt.ui.wizards.NewGwtRemoteServiceWizardPage.java

License:Apache License

public IPackageFragment getBasePackageFragment() {

    IPackageFragment packageFragment = null;
    if (!projectText.equals("")) {//$NON-NLS-1$

        try {/*from  w w  w.  j  a  v  a 2 s . c o m*/
            IJavaProject project = JavaCore.create(Util.getProject(projectText));
            IPath moduleXmlPath = new Path(moduleText);
            IPath packageFragmentPath = new Path("/").append(projectText) //$NON-NLS-1$
                    .append(moduleXmlPath.removeLastSegments(1));
            packageFragment = project.findPackageFragment(packageFragmentPath);
        } catch (JavaModelException e) {
            Activator.logException(e);
        }
    }

    return packageFragment;
}

From source file:org.bonitasoft.studio.common.repository.filestore.PackageFileStore.java

License:Open Source License

public IPackageFragment getPackageFragment() {
    IJavaProject project = RepositoryManager.getInstance().getCurrentRepository().getJavaProject();
    try {/*  ww  w .  j a  va2s  .  c  o m*/
        return project.findPackageFragment(
                getParentStore().getResource().getFullPath().append(packageName.replace(".", "/")));
    } catch (JavaModelException e) {
        BonitaStudioLog.error(e);
    }
    return null;
}

From source file:org.bonitasoft.studio.common.repository.filestore.PackageFileStore.java

License:Open Source License

private IPackageFragment retrieveParentPackageFragment(IJavaProject project, IPackageFragment packageFragment)
        throws JavaModelException {
    final IPath pathOfParentPackageFragment = packageFragment.getResource().getParent().getFullPath();
    IPackageFragment parent = project.findPackageFragment(pathOfParentPackageFragment);
    return parent;
}

From source file:org.bonitasoft.studio.common.repository.filestore.SourceFileStore.java

License:Open Source License

@Override
public void rename(String newQualifiedClassName) {
    IJavaProject project = RepositoryManager.getInstance().getCurrentRepository().getJavaProject();
    String packageName = "";
    String className = newQualifiedClassName;

    if (newQualifiedClassName.indexOf(".") != -1) {
        packageName = newQualifiedClassName.substring(0, newQualifiedClassName.lastIndexOf("."));
        className = newQualifiedClassName.substring(newQualifiedClassName.lastIndexOf(".") + 1,
                newQualifiedClassName.length());
    }// w  w  w.  jav  a2 s . c om

    try {
        final IRepositoryStore<?> store = getParentStore();
        IPackageFragmentRoot root = project.findPackageFragmentRoot(store.getResource().getFullPath());
        root.createPackageFragment(packageName, true, Repository.NULL_PROGRESS_MONITOR);
        IPackageFragment targetContainer = project
                .findPackageFragment(store.getResource().getFullPath().append(packageName.replace(".", "/")));
        IType type = project.findType(qualifiedClassName);
        if (type != null) {
            type.getCompilationUnit().move(targetContainer, null, className + ".java", true,
                    Repository.NULL_PROGRESS_MONITOR);
            qualifiedClassName = newQualifiedClassName;
        }
    } catch (Exception e) {
        BonitaStudioLog.error(e);
    }
}

From source file:org.bonitasoft.studio.common.repository.filestore.SourceFileStore.java

License:Open Source License

private void deleteRecursivelyEmptyPackages(IJavaProject project, IPackageFragment packageFragment)
        throws JavaModelException {
    if (packageFragment != null) {
        while (!packageFragment.hasChildren()) {
            //I don't find another way than passing through IResource, directly using IJavaElement seems not possible.
            final IPath pathOfParentPackageFragment = packageFragment.getResource().getParent().getFullPath();
            IPackageFragment parent = project.findPackageFragment(pathOfParentPackageFragment);
            packageFragment.delete(true, new NullProgressMonitor());
            if (parent instanceof IPackageFragment && !parent.isDefaultPackage()) {
                packageFragment = (IPackageFragment) parent;
            } else {
                return;
            }/*from w  w  w.j a v  a  2 s.c  o  m*/
        }
    }
}

From source file:org.bonitasoft.studio.common.repository.store.SourceRepositoryStore.java

License:Open Source License

@Override
public T getChild(String fileName) {
    if (fileName == null) {
        return null;
    }//from  w  w w .j  a  v a 2 s. c  om
    if (fileName.endsWith(".java") || fileName.endsWith(".groovy")) {
        T fileStore = super.getChild(fileName);
        if (fileStore != null) {
            return fileStore;
        }
    }
    try {
        IJavaProject javaProject = RepositoryManager.getInstance().getCurrentRepository().getJavaProject();
        IType javaType = javaProject.findType(fileName);
        if (javaType != null && javaType instanceof SourceType) {
            return (T) new SourceFileStore(fileName, this);
        } else if (javaType == null) { //package name
            IPackageFragment packageFragment = javaProject
                    .findPackageFragment(getResource().getFullPath().append(fileName.replace(".", "/")));
            if (packageFragment != null) {
                return (T) new PackageFileStore(fileName, this);
            }
        }
    } catch (Exception e) {
        BonitaStudioLog.error(e);
    }

    return null;
}

From source file:org.eclipse.ajdt.internal.ui.wizards.exports.AJJarFileExportOperation.java

License:Open Source License

/**
 * Exports the passed resource to the JAR file
 *
 * @param element the resource or JavaElement to export
 *//* w w  w. j a  v a  2s. com*/
protected void exportElement(Object element, IProgressMonitor progressMonitor) throws InterruptedException {
    // AspectJ Change Begin
    if (!AspectJPlugin.USING_CU_PROVIDER) {
        // Don't export AJCompilationUnits because they are duplicates of files that we also export.
        if (element instanceof AJCompilationUnit) {
            return;
        }
    }
    // AspectJ Change End
    int leadSegmentsToRemove = 1;
    IPackageFragmentRoot pkgRoot = null;
    boolean isInJavaProject = false;
    IResource resource = null;
    IJavaProject jProject = null;
    if (element instanceof IJavaElement) {
        isInJavaProject = true;
        IJavaElement je = (IJavaElement) element;
        int type = je.getElementType();
        if (type != IJavaElement.CLASS_FILE && type != IJavaElement.COMPILATION_UNIT) {
            exportJavaElement(progressMonitor, je);
            return;
        }
        try {
            resource = je.getUnderlyingResource();
        } catch (JavaModelException ex) {
            addWarning(Messages.format(JarPackagerMessages.JarFileExportOperation_resourceNotFound,
                    je.getElementName()), ex);
            return;
        }
        jProject = je.getJavaProject();
        pkgRoot = JavaModelUtil.getPackageFragmentRoot(je);
    } else
        resource = (IResource) element;

    if (!resource.isAccessible()) {
        addWarning(Messages.format(JarPackagerMessages.JarFileExportOperation_resourceNotFound,
                resource.getFullPath()), null);
        return;
    }

    if (resource.getType() == IResource.FILE) {
        if (!isInJavaProject) {
            // check if it's a Java resource
            try {
                isInJavaProject = resource.getProject().hasNature(JavaCore.NATURE_ID);
            } catch (CoreException ex) {
                addWarning(
                        Messages.format(JarPackagerMessages.JarFileExportOperation_projectNatureNotDeterminable,
                                resource.getFullPath()),
                        ex);
                return;
            }
            if (isInJavaProject) {
                jProject = JavaCore.create(resource.getProject());
                try {
                    IPackageFragment pkgFragment = jProject
                            .findPackageFragment(resource.getFullPath().removeLastSegments(1));
                    if (pkgFragment != null)
                        pkgRoot = JavaModelUtil.getPackageFragmentRoot(pkgFragment);
                    else
                        pkgRoot = findPackageFragmentRoot(jProject,
                                resource.getFullPath().removeLastSegments(1));
                } catch (JavaModelException ex) {
                    addWarning(Messages.format(
                            JarPackagerMessages.JarFileExportOperation_javaPackageNotDeterminable,
                            resource.getFullPath()), ex);
                    return;
                }
            }
        }

        if (pkgRoot != null && jProject != null) {
            leadSegmentsToRemove = pkgRoot.getPath().segmentCount();
            boolean isOnBuildPath;
            isOnBuildPath = jProject.isOnClasspath(resource);
            if (!isOnBuildPath || (mustUseSourceFolderHierarchy()
                    && !pkgRoot.getElementName().equals(IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH)))
                leadSegmentsToRemove--;
        }

        IPath destinationPath = resource.getFullPath().removeFirstSegments(leadSegmentsToRemove);

        boolean isInOutputFolder = false;
        if (isInJavaProject && jProject != null) {
            try {
                isInOutputFolder = jProject.getOutputLocation().isPrefixOf(resource.getFullPath());
            } catch (JavaModelException ex) {
                isInOutputFolder = false;
            }
        }

        exportClassFiles(progressMonitor, pkgRoot, resource, jProject, destinationPath);
        exportResource(progressMonitor, pkgRoot, isInJavaProject, resource, destinationPath, isInOutputFolder);

        progressMonitor.worked(1);
        ModalContext.checkCanceled(progressMonitor);

    } else
        exportContainer(progressMonitor, (IContainer) resource);
}

From source file:org.eclipse.che.jdt.refactoring.RefactoringManager.java

License:Open Source License

private Object getDestination(String projectPath, DestinationType type, String destination)
        throws RefactoringException, JavaModelException {
    IJavaProject javaProject = JavaModelManager.getJavaModelManager().getJavaModel()
            .getJavaProject(projectPath);
    if (javaProject == null) {
        throw new RefactoringException("Can't find project: " + projectPath);
    }/* w  ww .  ja  v a 2 s  .  co  m*/
    switch (type) {
    case PACKAGE:
        return javaProject.findPackageFragment(new Path(destination));

    case RESOURCE:
    case SOURCE_REFERENCE:
    default:
        throw new UnsupportedOperationException("Can't use destination for 'RESOURCE' or 'SOURCE_REFERENCE'.");

    }
}

From source file:org.eclipse.che.jdt.rest.RefactoringService.java

License:Open Source License

/**
 * Create move refactoring session.//from   ww w .j  a  va 2 s. c om
 *
 * @param cmr
 *         move settings, contains resource paths to move.
 * @return refactoring session id.
 * @throws JavaModelException
 *         when JavaModel has a failure
 * @throws RefactoringException
 *         when impossible to create move refactoring session
 */
@POST
@Path("move/create")
@Consumes("application/json")
@Produces("text/plain")
public String createMoveRefactoring(CreateMoveRefactoring cmr) throws JavaModelException, RefactoringException {
    IJavaProject javaProject = model.getJavaProject(cmr.getProjectPath());
    IJavaElement[] javaElements;
    try {
        Function<ElementToMove, IJavaElement> map = javaElement -> {
            try {
                if (javaElement.isPack()) {
                    return javaProject
                            .findPackageFragment(new org.eclipse.core.runtime.Path(javaElement.getPath()));
                } else {
                    return javaProject.findType(javaElement.getPath()).getCompilationUnit();

                }
            } catch (JavaModelException e) {
                throw new IllegalArgumentException(e);
            }

        };
        javaElements = cmr.getElements().stream().map(map).toArray(IJavaElement[]::new);

    } catch (IllegalArgumentException e) {
        if (e.getCause() instanceof JavaModelException) {
            throw (JavaModelException) e.getCause();
        } else {
            throw e;
        }
    }
    if (RefactoringAvailabilityTester.isMoveAvailable(new IResource[0], javaElements)) {
        return manager.createMoveRefactoringSession(javaElements);
    }

    throw new RefactoringException("Can't create move refactoring.");
}