Example usage for org.eclipse.jdt.core IPackageFragmentRoot getPackageFragment

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getPackageFragment

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot getPackageFragment.

Prototype

IPackageFragment getPackageFragment(String packageName);

Source Link

Document

Returns the package fragment with the given package name.

Usage

From source file:es.bsc.servicess.ide.PackagingUtils.java

License:Apache License

private static void processOrchestrationClassForExternalPackage(String orchClass, List<String> methods,
        IPackageFragmentRoot sourceDir, IFolder outFolder, IFolder classes, String runtimePath,
        List<Dependency> dependencies, IProgressMonitor myProgressMonitor) throws CoreException {
    IPackageFragment pf = sourceDir.getPackageFragment(Signature.getQualifier(orchClass));
    copyInterface(Signature.getSimpleName(orchClass), pf, sourceDir.getPath(), outFolder, classes,
            myProgressMonitor);//from  w  w w  .  ja  v  a2  s.c  o m
    preInstrumentOrchestration(runtimePath, orchClass, methods, classes, dependencies, myProgressMonitor);
    instrumentOrchestrations(runtimePath, new String[] { orchClass }, classes, dependencies, myProgressMonitor);
}

From source file:es.bsc.servicess.ide.ProjectMetadata.java

License:Apache License

/**Get the main Java package of the Service source folder
 * @param project Java project where the service is implemented
 * @return/*from   ww w.  j  a v a2 s  . c  om*/
 * @throws JavaModelException
 */
public IPackageFragment getMainPackageFragment(IJavaProject project) throws JavaModelException {
    IPackageFragmentRoot root = getPackageFragmentRoot(project);
    if (root != null) {
        return root.getPackageFragment(getMainPackageName());
    } else
        return null;
}

From source file:es.bsc.servicess.ide.wizards.coretypes.BinaryMethodSpecificTreatment.java

License:Apache License

/** Create the method core element declaring class
 * @param m Progress monitor/* w  ww.j  av a 2 s  .co  m*/
 * @return  Eclipse JDT IType object representing the core element declaring class
 * @throws JavaModelException
 */
private IType createDeclaringClassType(IProgressMonitor m) throws JavaModelException {
    IPackageFragmentRoot pfr = secondPage.getPackageFragmentRoot();
    String lineDelimiter = StubUtility.getLineDelimiterUsed(secondPage.getJavaProject());
    IPackageFragment pf;
    String st = Signature.getQualifier(declaringClass);
    if (st != null && st.trim().length() > 0) {
        pf = pfr.getPackageFragment(st);
        if (pf != null) {
            if (!pf.exists()) {
                pf = pfr.createPackageFragment(st, true, m);
            }
        } else {
            pf = pfr.createPackageFragment(st, true, m);
        }
    } else {
        IFile metadataFile = secondPage.getJavaProject().getProject().getFolder(ProjectMetadata.METADATA_FOLDER)
                .getFile(ProjectMetadata.METADATA_FILENAME);
        if (metadataFile != null) {
            try {
                ProjectMetadata pr_meta = new ProjectMetadata(
                        new File(metadataFile.getRawLocation().toOSString()));
                pfr = secondPage.getJavaProject().findPackageFragmentRoot(
                        secondPage.getJavaProject().getPath().append(pr_meta.getSourceDir()));
                if (pfr != null) {
                    pf = pfr.getPackageFragment(pr_meta.getMainPackageName() + ".coreelements");
                    if (pf.exists()) {
                        st = pf.getElementName();
                    } else
                        throw new JavaModelException(new Exception("Unable to find core elemet package"),
                                JavaModelStatus.ERROR);
                } else {
                    throw new JavaModelException(new Exception("Unable to find core elemet package root"),
                            JavaModelStatus.ERROR);
                }
            } catch (Exception e) {
                throw new JavaModelException(e, JavaModelStatus.ERROR);
            }
        } else
            throw new JavaModelException(new Exception("Unable to find project metadata file"),
                    JavaModelStatus.ERROR);
    }
    String name = Signature.getSimpleName(declaringClass);
    log.debug("Package: " + st + " Class name: " + name);
    ICompilationUnit cu = pf.getCompilationUnit(name + ".java");
    if (cu == null || !cu.exists()) {
        cu = pf.createCompilationUnit(name + ".java", "", false, m);
        cu.createPackageDeclaration(st, m);
        cu.createType(constructCEClassStub(name, lineDelimiter), null, false, m);
    }
    return cu.getType(name);
}

From source file:es.bsc.servicess.ide.wizards.coretypes.SpecificNewMethodComposite.java

License:Apache License

private IType createDeclaringClassType(IProgressMonitor m) throws JavaModelException {
    IPackageFragmentRoot pfr = thirdPage.getPackageFragmentRoot();
    String lineDelimiter = StubUtility.getLineDelimiterUsed(thirdPage.getJavaProject());
    IPackageFragment pf;//from  w  ww  .  ja  v  a  2  s .  c o m
    String st = Signature.getQualifier(declaringClass);
    if (st != null && st.trim().length() > 0) {
        pf = pfr.getPackageFragment(st);
        if (pf != null) {
            if (!pf.exists()) {
                pf = pfr.createPackageFragment(st, true, m);
            }
        } else {
            pf = pfr.createPackageFragment(st, true, m);
        }
    } else {
        IFile metadataFile = thirdPage.getJavaProject().getProject().getFolder(ProjectMetadata.METADATA_FOLDER)
                .getFile(ProjectMetadata.METADATA_FILENAME);
        if (metadataFile != null) {
            try {
                ProjectMetadata pr_meta = new ProjectMetadata(
                        new File(metadataFile.getRawLocation().toOSString()));
                pfr = thirdPage.getJavaProject().findPackageFragmentRoot(
                        thirdPage.getJavaProject().getPath().append(pr_meta.getSourceDir()));
                if (pfr != null) {
                    pf = pfr.getPackageFragment(pr_meta.getMainPackageName() + ".coreelements");
                    if (pf.exists()) {
                        st = pf.getElementName();
                    } else
                        throw new JavaModelException(new Exception("Unable to find core elemet package"),
                                JavaModelStatus.ERROR);
                } else {
                    throw new JavaModelException(new Exception("Unable to find core elemet package root"),
                            JavaModelStatus.ERROR);
                }
            } catch (Exception e) {
                throw new JavaModelException(e, JavaModelStatus.ERROR);
            }
        } else
            throw new JavaModelException(new Exception("Unable to find project metadata file"),
                    JavaModelStatus.ERROR);
    }
    String name = Signature.getSimpleName(declaringClass);
    System.out.println("Package: " + st + " Class name: " + name);
    ICompilationUnit cu = pf.getCompilationUnit(name + ".java");
    if (cu == null || !cu.exists()) {
        cu = pf.createCompilationUnit(name + ".java", "", false, m);
        cu.createPackageDeclaration(st, m);
        cu.createType(constructCEClassStub(name, lineDelimiter), null, false, m);
    }
    return cu.getType(name);
}

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>//from   w w w.  j  av a 2 s .  c  o  m
 * 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;
}

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

License:Apache License

@Override
protected void finishPage(IProgressMonitor arg0) throws InterruptedException, CoreException {

    String lineDelimiter = StubUtility.getLineDelimiterUsed(project);

    IFile file = project.getProject().getFolder(ProjectMetadata.METADATA_FOLDER)
            .getFile(ProjectMetadata.METADATA_FILENAME);
    try {/*from   ww  w .  j  a v  a 2s  .co m*/
        ProjectMetadata pr_meta = new ProjectMetadata(file.getRawLocation().toFile());
        IPackageFragmentRoot root = pr_meta.getPackageFragmentRoot(project);
        if (root == null || !root.exists())
            throw (new Exception("Main package fragment root not found"));
        IPackageFragment frag = root.getPackageFragment(fPage.getTypePackageName());
        if (frag == null || !frag.exists())
            root.createPackageFragment(fPage.getTypePackageName(), true, arg0);
        String className = fPage.getType().getElementName().replaceAll(" ", "") + "Itf";
        interfaceCU = frag.getCompilationUnit(className + ".java");
        if (interfaceCU == null || !interfaceCU.exists()) {
            interfaceCU = frag.createCompilationUnit(className + ".java", "", false,
                    new SubProgressMonitor(arg0, 2));
            interfaceCU.createPackageDeclaration(frag.getElementName(), arg0);
            String typeStub = constructInterfaceStub(className, lineDelimiter);
            interfaceCU.createType(typeStub, null, false, arg0);
        }
        pr_meta.addExternalOrchestrationClass(fPage.getTypeName(), fPage.getExternalLocation(),
                fPage.getIntraPackageLocation());
        pr_meta.toFile(file.getRawLocation().toFile());
        project.getProject().refreshLocal(Resource.DEPTH_INFINITE, arg0);
    } catch (Exception e) {
        throw new JavaModelException(e, JavaModelStatus.ERROR);
    }

}

From source file:es.bsc.servicess.ide.wizards.ServiceSsNewServiceClassPage.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>/*from  w ww.  j  a v  a 2  s.  com*/
 * 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;
}

From source file:fede.workspace.dependencies.eclipse.java.util.TypeUtil.java

License:Apache License

/**
 * Find type.//w  ww.j a  v  a2 s . c  om
 * 
 * @param project
 *            the project
 * @param qualifiedPackage
 *            the qualified package
 * @param typeDef
 *            the type def
 * 
 * @return the i type
 */
public static IType findType(IProject project, String qualifiedPackage, String typeDef) {
    IJavaProject jp = JavaCore.create(project);
    if (!jp.exists())
        return null;

    try {
        IPackageFragmentRoot[] packagesRoot = jp.getPackageFragmentRoots();
        for (IPackageFragmentRoot packageFragmentRoot : packagesRoot) {
            if (packageFragmentRoot.isExternal())
                continue;
            IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(qualifiedPackage);
            if (packageFragment.exists()) {
                IType ret = TypeUtil.findType(packageFragment, typeDef);
                if (ret != null)
                    return ret;
            }
        }
    } catch (JavaModelException e) {
        WSPlugin.logException(e);
    }

    return null;
}

From source file:fede.workspace.eclipse.java.fields.StringToPackageValueController.java

License:Apache License

/**
 * Abstract to visual value./*from  ww  w .j  ava  2 s .co m*/
 * 
 * @param value
 *            the value
 * 
 * @return the object
 */
public Object abstractToVisualValue(Object value) {
    List<String> packagesString = (List<String>) value;

    Item theCurrentItem = getItem();
    IJavaProject jp;
    try {
        jp = JavaProjectManager.getJavaProject(theCurrentItem);
    } catch (CoreException e) {
        IStatus status = new Status(IStatus.ERROR, WSJavaPlugin.PLUGIN_ID, 0,
                MessageFormat.format("Cannot find the java projet from the item {0}", theCurrentItem.getName()),
                null);
        WSJavaPlugin.getDefault().log(status);
        return new ArrayList<IPackageFragment>();
    }
    if (jp == null) {
        return new ArrayList<IPackageFragment>();
    }
    IPackageFragmentRoot[] rootPackages;
    try {
        rootPackages = jp.getPackageFragmentRoots();
    } catch (JavaModelException e) {
        WSJavaPlugin.getDefault().log(new Status(IStatus.ERROR, WSPlugin.PLUGIN_ID, 0, MessageFormat.format(
                "Cannot find the package fragment root from the item {0}", theCurrentItem.getName()), null));
        return new ArrayList<IPackageFragment>();
    }
    if (rootPackages == null || rootPackages.length == 0) {
        return new ArrayList<IPackageFragment>();
    }
    if (packagesString == null) {
        return new ArrayList<IPackageFragment>();
    }

    IPackageFragmentRoot packageSrc = null;
    for (IPackageFragmentRoot fr : rootPackages) {
        try {
            if (fr.getKind() == IPackageFragmentRoot.K_SOURCE) {
                packageSrc = fr;
                break;
            }
        } catch (JavaModelException e) {
            WSJavaPlugin.getDefault()
                    .log(new Status(IStatus.ERROR, WSPlugin.PLUGIN_ID, 0,
                            MessageFormat.format(
                                    "Cannot find the type of the package fragment root {1} from the item {0}",
                                    theCurrentItem.getId(), fr.getElementName()),
                            null));
        }
    }
    List<IPackageFragment> ret = new ArrayList<IPackageFragment>();
    for (String pString : packagesString) {
        IPackageFragment findPackage = null;
        for (IPackageFragmentRoot fr : rootPackages) {
            findPackage = fr.getPackageFragment(pString);
            if (findPackage.exists()) {
                break;
            }
        }
        if (packageSrc != null && (findPackage == null || !findPackage.exists())) {
            findPackage = packageSrc.getPackageFragment(pString);
        }
        if (findPackage != null) {
            ret.add(findPackage);
        }
    }
    return ret;
}

From source file:fede.workspace.eclipse.java.JavaProjectManager.java

License:Apache License

/**
 * Gets the package fragment corresponding to the spepcified package in the
 * source folder associated with the specified item.
 * //from   w w  w  . j av a2  s  .c  o m
 * @param item
 *            the item
 * @param packageName
 *            the package name
 * 
 * @return the package fragment
 * 
 * @throws CoreException
 *             the core exception
 */
public static IPackageFragment getPackageFragment(Item item, String packageName) throws CoreException {
    IPackageFragmentRoot source = getPackageFragmentRoot(item);

    if (source == null) {
        return null;
    }

    return source.getPackageFragment(packageName);
}