Example usage for org.eclipse.jdt.core ICompilationUnit createPackageDeclaration

List of usage examples for org.eclipse.jdt.core ICompilationUnit createPackageDeclaration

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ICompilationUnit createPackageDeclaration.

Prototype

IPackageDeclaration createPackageDeclaration(String name, IProgressMonitor monitor) throws JavaModelException;

Source Link

Document

Creates and returns a package declaration in this compilation unit with the given package name.

Usage

From source file:com.gwtplatform.plugin.projectfile.ProjectClass.java

License:Apache License

private ICompilationUnit createCompilationUnitIfNeeded() throws JavaModelException {
    ICompilationUnit compilationUnit;
    if (type == null) {
        String cuName = elementName + ".java";
        IPackageFragment pack = root.createPackageFragment(packageName, false, new NullProgressMonitor());
        compilationUnit = pack.createCompilationUnit(cuName, "", false, new NullProgressMonitor());
        compilationUnit.createPackageDeclaration(packageName, new NullProgressMonitor());
    } else {// www  .ja  v a 2 s .com
        compilationUnit = type.getCompilationUnit();
    }
    return compilationUnit;
}

From source file:com.sun.codemodel.ClassGenerator.java

License:Apache License

private void modifyObjectFactory(ElementClass ec, IProgressMonitor pm) throws JavaModelException {
    JDefinedClass c = ec.getjDefClass();
    if (ec.getElementType() != null) {
        IPackageFragmentRoot pfr = project.getPackageFragmentRoot(generated);
        if (pfr != null && pfr.exists()) {
            IPackageFragment pf = pfr.getPackageFragment(c.getPackage().name());
            if (pf != null && pf.exists()) {
                ICompilationUnit of = pf.getCompilationUnit("ObjectFactory.java");
                if (of != null && !of.exists()) {
                    of = pf.createCompilationUnit("ObjectFactory.java", "", true, pm);
                    of.createPackageDeclaration(c.getPackage().name(), pm);
                    of.createImport("javax.xml.bind.annotation.XmlRegistry", null, Flags.AccDefault, pm);
                    String lineDelimiter = StubUtility.getLineDelimiterUsed(project);
                    IType t = of.createType(generateObjectFactory(lineDelimiter), null, false, pm);
                    t.createMethod("public ObjectFactory(){}" + lineDelimiter, null, false, pm);
                }//  w  w w .ja  v a  2 s  .c  o  m
                of = addClass(ec, of, pm);

            } else
                throw new JavaModelException(
                        new Exception("Error package " + c.getPackage().name() + " does not exists"),
                        JavaModelStatus.ERROR);

        } else
            throw new JavaModelException(
                    new Exception("Error package root " + generated.getName() + " does not exists"),
                    JavaModelStatus.ERROR);
    }
}

From source file:com.sun.codemodel.ClassGenerator.java

License:Apache License

public void generateDummy(ServiceCoreElement element, String orchClassName, IProgressMonitor pm)
        throws JavaModelException {
    // IPackageFragmentRoot root =
    // project.getPackageFragmentRoot(generated);
    String packName = getPackageName(element);
    IPackageFragment pack = pfr.getPackageFragment(packName);
    if (pack == null || !pack.exists()) {
        pack = pfr.createPackageFragment(packName, true, pm);
    }//from  w  ww . j  a  va  2s.  co  m
    String classname = getDummyClassName(element, orchClassName);
    ICompilationUnit cu = pack.getCompilationUnit(classname + ".java");
    if (cu == null || !cu.exists()) {
        cu = pack.createCompilationUnit(classname + ".java", "", true, pm);
    }
    cu.createPackageDeclaration(packName, pm);
    IType staticType = null;
    IType type = cu.getType(classname);
    if (type == null || !type.exists()) {
        type = cu.createType(generateDummyClassContent(classname), null, true, pm);
        staticType = type.createType(generateDummyStaticContent(), null, true, pm);
    } else {
        staticType = type.getType("Static");
        if (staticType == null || !staticType.exists()) {
            staticType = type.createType(generateDummyStaticContent(), null, true, pm);
        }
    }
    String methodContent = generateMethodContent(element);
    type.createMethod("public " + methodContent, null, true, pm);
    staticType.createMethod("public static " + methodContent, null, true, pm);

}

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//from  w  w  w  .jav 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;// w w w.  j a v  a 2s . 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.ServiceSsNewServiceClassWizard.java

License:Apache License

@Override
protected void finishPage(IProgressMonitor arg0) throws InterruptedException, CoreException {
    IPackageFragment frag = fPage.getPackageFragment();
    String lineDelimiter = StubUtility.getLineDelimiterUsed(frag.getJavaProject());
    cu_class = frag.createCompilationUnit(fPage.getTypeName() + ".java", "", false,
            new SubProgressMonitor(arg0, 2));
    cu_class.createPackageDeclaration(frag.getElementName(), arg0);
    cu_class.createImport("javax.jws.WebService", null, Flags.AccDefault, arg0);
    String classStub = constructClassStub(fPage.getTypeName(), lineDelimiter);

    IType type = cu_class.createType(classStub, null, false, arg0);
    ICompilationUnit cu = frag.createCompilationUnit(fPage.getTypeName().replaceAll(" ", "") + "Itf.java", "",
            false, new SubProgressMonitor(arg0, 2));
    cu.createPackageDeclaration(frag.getElementName(), arg0);
    String typeStub = constructInterfaceStub(fPage.getTypeName().replaceAll(" ", "") + "Itf", lineDelimiter);
    cu.createType(typeStub, null, false, arg0);
    IFile file = fPage.getJavaProject().getProject().getFolder(ProjectMetadata.METADATA_FOLDER)
            .getFile(ProjectMetadata.METADATA_FILENAME);
    try {//from  ww w  .  jav  a2s  .co  m
        ProjectMetadata pr_meta = new ProjectMetadata(file.getRawLocation().toFile());
        pr_meta.addOrchestrationClass(frag.getElementName() + "." + fPage.getTypeName(), fPage.getClassType());
        pr_meta.toFile(file.getRawLocation().toFile());
    } catch (Exception e) {
        throw new JavaModelException(e, JavaModelStatus.ERROR);
    }

}

From source file:org.hibernate.eclipse.console.test.project.SimpleTestProject.java

License:Open Source License

protected IType buildType(IPackageFragment pack, String cuName) throws JavaModelException {

    //create empty ICompilationUnit

    ICompilationUnit cu = pack.createCompilationUnit(cuName, "", false, null); //$NON-NLS-1$

    cu.createPackageDeclaration(pack.getElementName(), null);
    IType type = cu.createType("public class " + TYPE_NAME + " {}", null, false, null); //$NON-NLS-1$//$NON-NLS-2$
    type.createField("private String testField;", null, false, null); //$NON-NLS-1$
    type.createMethod("public String getTestField() {return this.testField;}", null, false, null); //$NON-NLS-1$
    type.createMethod("public void setTestField(String testField) {this.testField = testField;}", null, false, //$NON-NLS-1$
            null);//from ww w .  j  a  v a2 s.c om
    return type;
}

From source file:org.jboss.tools.ws.creation.core.commands.ClientSampleCreationCommand.java

License:Open Source License

/**
 * create a java class// ww  w  .  jav a  2  s .com
 * 
 * @param packageName
 * @param className
 * @param isInterface
 * @param interfaceName
 * @param javaProject
 * @return
 */
public ICompilationUnit createJavaClass(String packageName, String className, boolean isInterface,
        String interfaceName, IJavaProject javaProject) {
    try {
        IPackageFragmentRoot root = JBossWSCreationUtils.getPackageFragmentRoot(javaProject,
                model.getJavaSourceFolder());
        if (packageName == null) {
            packageName = ""; //$NON-NLS-1$
        }
        IPackageFragment pkg = root.createPackageFragment(packageName, false, null);
        ICompilationUnit wrapperCls = pkg.createCompilationUnit(className + ".java", "", true, null); //$NON-NLS-1$//$NON-NLS-2$
        if (!packageName.equals("")) { //$NON-NLS-1$
            wrapperCls.createPackageDeclaration(packageName, null);
        }

        String clsContent = ""; //$NON-NLS-1$
        if (isInterface) {
            clsContent = "public interface " + className + " {" //$NON-NLS-1$ //$NON-NLS-2$
                    + LINE_SEPARATOR;
            clsContent += "}" + LINE_SEPARATOR; //$NON-NLS-1$
        } else {
            clsContent = "public class " + className; //$NON-NLS-1$
            if (interfaceName != null) {
                clsContent += " implements " + interfaceName; //$NON-NLS-1$
            }
            clsContent += " {" + LINE_SEPARATOR; //$NON-NLS-1$
            clsContent += "}" + LINE_SEPARATOR; //$NON-NLS-1$
        }
        wrapperCls.createType(clsContent, null, true, null);

        wrapperCls.save(null, true);
        return wrapperCls;
    } catch (Exception e) {
        JBossWSCreationCorePlugin.getDefault().logError(e);
        return null;
    }
}

From source file:org.jboss.tools.ws.creation.core.commands.RSServiceCreationCommand.java

License:Open Source License

private ICompilationUnit createRESTApplicationClass(String packageName, String className,
        IJavaProject project) {//w w  w . j a  v  a2s.co m
    try {
        IPath srcPath = new Path(JBossWSCreationUtils.getJavaProjectSrcLocation(project.getProject()));
        srcPath = project.getPath().append(srcPath.makeRelativeTo(project.getProject().getLocation()));
        IPackageFragmentRoot root = project.findPackageFragmentRoot(srcPath);
        if (packageName == null) {
            packageName = ""; //$NON-NLS-1$
        }
        IPackageFragment pkg = root.createPackageFragment(packageName, false, null);
        ICompilationUnit wrapperCls = pkg.createCompilationUnit(className + ".java", "", true, null); //$NON-NLS-1$//$NON-NLS-2$
        if (!packageName.equals("")) { //$NON-NLS-1$
            wrapperCls.createPackageDeclaration(packageName, null);
        }

        StringBuffer clsContent = new StringBuffer();
        clsContent.append("public class ").append(className).append(" extends Application") //$NON-NLS-1$//$NON-NLS-2$
                .append(" {" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("}").append(LINE_SEPARATOR); //$NON-NLS-1$
        wrapperCls.createType(clsContent.toString(), null, true, null);

        wrapperCls.createImport("java.util.Set", null, null); //$NON-NLS-1$
        wrapperCls.createImport("java.util.HashSet", null, null); //$NON-NLS-1$
        wrapperCls.createImport("javax.ws.rs.core.Application", null, null); //$NON-NLS-1$

        IType serviceClsType = wrapperCls.findPrimaryType();
        clsContent = new StringBuffer();
        clsContent.append("private Set<Object> singletons = new HashSet<Object>();" + LINE_SEPARATOR); //$NON-NLS-1$
        serviceClsType.createField(clsContent.toString(), null, false, null);

        clsContent = new StringBuffer();
        clsContent.append("private Set<Class<?>> empty = new HashSet<Class<?>>();" + LINE_SEPARATOR); //$NON-NLS-1$
        serviceClsType.createField(clsContent.toString(), null, false, null);

        clsContent = new StringBuffer();
        clsContent.append(LINE_SEPARATOR);
        clsContent.append("public " + className + "(){" + LINE_SEPARATOR); //$NON-NLS-1$ //$NON-NLS-2$
        clsContent.append("     singletons.add(new " + JBossWSCreationUtils //$NON-NLS-1$
                .classNameFromQualifiedName(model.getServiceClasses().get(0)) + "());" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("}" + LINE_SEPARATOR); //$NON-NLS-1$
        serviceClsType.createMethod(clsContent.toString(), null, true, null);

        clsContent = new StringBuffer();
        clsContent.append(LINE_SEPARATOR);
        clsContent.append("@Override" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("public Set<Class<?>> getClasses() {" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("     return empty;" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("}" + LINE_SEPARATOR); //$NON-NLS-1$
        serviceClsType.createMethod(clsContent.toString(), null, true, null);

        clsContent = new StringBuffer();
        clsContent.append(LINE_SEPARATOR);
        clsContent.append("@Override" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("public Set<Object> getSingletons() {" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("     return singletons;" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("}" + LINE_SEPARATOR); //$NON-NLS-1$
        serviceClsType.createMethod(clsContent.toString(), null, true, null);

        wrapperCls.save(null, true);
        return wrapperCls;
    } catch (Exception e) {
        JBossWSCreationCorePlugin.getDefault().logError(e);
        return null;
    }
}

From source file:org.jboss.tools.ws.creation.core.commands.RSServiceCreationCommand.java

License:Open Source License

private ICompilationUnit createRESTAnnotatedJavaClass(String packageName, String className,
        IJavaProject project) {//  www . j  av a  2s. c  o m
    try {
        IPath srcPath = new Path(JBossWSCreationUtils.getJavaProjectSrcLocation(project.getProject()));
        srcPath = project.getPath().append(srcPath.makeRelativeTo(project.getProject().getLocation()));
        IPackageFragmentRoot root = project.findPackageFragmentRoot(srcPath);
        if (packageName == null) {
            packageName = ""; //$NON-NLS-1$
        }
        IPackageFragment pkg = root.createPackageFragment(packageName, false, null);
        ICompilationUnit wrapperCls = pkg.createCompilationUnit(className + ".java", "", true, null); //$NON-NLS-1$//$NON-NLS-2$
        if (!packageName.equals("")) { //$NON-NLS-1$
            wrapperCls.createPackageDeclaration(packageName, null);
        }

        StringBuffer clsContent = new StringBuffer();
        clsContent.append("@Path(\"/" + model.getServiceName() + "\")").append(LINE_SEPARATOR); //$NON-NLS-1$ //$NON-NLS-2$
        clsContent.append("public class ").append(className).append(" {" + LINE_SEPARATOR); //$NON-NLS-1$ //$NON-NLS-2$
        clsContent.append("}").append(LINE_SEPARATOR); //$NON-NLS-1$
        wrapperCls.createType(clsContent.toString(), null, true, null);

        wrapperCls.createImport("javax.ws.rs.Produces", null, null); //$NON-NLS-1$
        wrapperCls.createImport("javax.ws.rs.GET", null, null); //$NON-NLS-1$
        wrapperCls.createImport("javax.ws.rs.Path", null, null); //$NON-NLS-1$

        IType serviceClsType = wrapperCls.findPrimaryType();
        clsContent = new StringBuffer();
        clsContent.append("@GET()"); //$NON-NLS-1$
        clsContent.append(LINE_SEPARATOR);
        clsContent.append("@Produces(\"text/plain\")"); //$NON-NLS-1$
        clsContent.append(LINE_SEPARATOR);
        clsContent.append("public String sayHello() {"); //$NON-NLS-1$
        clsContent.append(LINE_SEPARATOR);
        clsContent.append("    return \"Hello World!\";"); //$NON-NLS-1$
        clsContent.append(LINE_SEPARATOR);
        clsContent.append("}"); //$NON-NLS-1$
        serviceClsType.createMethod(clsContent.toString(), null, true, null);
        wrapperCls.save(null, true);
        return wrapperCls;
    } catch (Exception e) {
        JBossWSCreationCorePlugin.getDefault().logError(e);
        return null;
    }
}