List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot createPackageFragment
IPackageFragment createPackageFragment(String name, boolean force, IProgressMonitor monitor) throws JavaModelException;
From source file:org.jboss.tools.common.java.generation.JavaBeanGenerator.java
License:Open Source License
private void doGenerateJava(IJavaProject javaproject, String filepath, Properties p) throws CoreException { IPackageFragmentRoot root = getJavaProjectSrcRoot(javaproject); String pkgname = p.getProperty(PARAM_PACKAGENAME); IPackageFragment pack = root.getPackageFragment(pkgname); if (!pack.exists()) { pack = root.createPackageFragment(pkgname, true, null); }//w w w . j a v a 2 s. c o m String shortname = p.getProperty(PARAM_SHORTNAME); String lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ ICompilationUnit parentCU = pack.createCompilationUnit(shortname + ".java", "", false, null); //$NON-NLS-1$ //$NON-NLS-2$ ICompilationUnit createdWorkingCopy = (ICompilationUnit) parentCU.getWorkingCopy(null); /// imports= new ImportsStructure(createdWorkingCopy, prefOrder, threshold, false); /// imports.addImport(pack.getElementName(), getTypeName()); String typeContent = constructTypeStub(p, lineDelimiter); String cuContent = buildClassContent(parentCU, shortname, typeContent, lineDelimiter); createdWorkingCopy.getBuffer().setContents(cuContent); IType createdType = createdWorkingCopy.getType(shortname); /// imports.create(false, new SubProgressMonitor(monitor, 1)); ICompilationUnit cu = createdType.getCompilationUnit(); synchronized (cu) { cu.reconcile(ICompilationUnit.NO_AST, true, null, null); } /// imports.create(false, new SubProgressMonitor(monitor, 1)); /// synchronized(cu) { /// cu.reconcile(); /// } ISourceRange range = createdType.getSourceRange(); IBuffer buf = cu.getBuffer(); String originalContent = buf.getText(range.getOffset(), range.getLength()); String formattedContent = codeFormat2(CodeFormatter.K_CLASS_BODY_DECLARATIONS, originalContent, 0, lineDelimiter, cu.getJavaProject()); buf.replace(range.getOffset(), range.getLength(), formattedContent); cu.commitWorkingCopy(false, null); }
From source file:org.jboss.tools.fuse.transformation.editor.internal.wizards.TransformTestWizardPage.java
License:Open Source License
private ICompilationUnit createJavaClass(String packageName, String className) { try {/* w w w . j a v a 2 s .com*/ boolean isSpring = CamelFileTypeHelper.isSpringFile(project, camelFilePath); boolean isBlueprint = CamelFileTypeHelper.isBlueprintFile(project, camelFilePath); if (!isSpring && !isBlueprint) { // obviously we're not dealing with a camel file here return null; } List<Dependency> dependencies = isBlueprint ? getRequiredBlueprintTestDependencies() : getRequiredSpringTestDependencies(); Display.getDefault().asyncExec(() -> updateProjectDeps(dependencies)); // refresh the project in case we added dependencies project.refreshLocal(IProject.DEPTH_INFINITE, null); // Ensure build of Java classes has completed try { Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null); } catch (final InterruptedException ignored) { } IPath srcPath; if (getPackageFragmentRoot() != null) { srcPath = getPackageFragmentRoot().getPath().makeAbsolute(); srcPath = srcPath.removeFirstSegments(1); IFolder folder = javaProject.getProject().getFolder(srcPath); if (!JavaUtil.findFolderOnProjectClasspath(javaProject, folder)) { JavaUtil.addFolderToProjectClasspath(javaProject, folder); } if (!folder.exists()) { try { folder.refreshLocal(IResource.DEPTH_INFINITE, null); } catch (CoreException e) { e.printStackTrace(); } } } else { IFolder folder = javaProject.getProject().getFolder("src/test/java"); //$NON-NLS-1$ if (!folder.exists()) { IFolder srcFolder = javaProject.getProject().getFolder("src"); //$NON-NLS-1$ if (!srcFolder.exists()) { srcFolder.create(true, true, null); } IFolder testFolder = srcFolder.getFolder("test"); //$NON-NLS-1$ if (!testFolder.exists()) { testFolder.create(true, true, null); } IFolder javaFolder = testFolder.getFolder("java"); //$NON-NLS-1$ if (!javaFolder.exists()) { javaFolder.create(true, true, null); } } if (!JavaUtil.findFolderOnProjectClasspath(javaProject, folder)) { JavaUtil.addFolderToProjectClasspath(javaProject, folder); } srcPath = folder.getProjectRelativePath(); } IFolder srcFolder = project.getFolder(srcPath); if (srcFolder == null || !srcFolder.exists()) { srcPath = javaProject.getPath().append(srcPath.makeRelativeTo(project.getLocation())); srcFolder = project.getFolder(srcPath); } IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(srcFolder); if (packageName == null) { packageName = ""; //$NON-NLS-1$ } if (root != null) { IPackageFragment pkg = root.createPackageFragment(packageName, false, null); StringBuilder clsContent = new StringBuilder(); String filePath = getCamelFilePath(); IResource res = project.findMember(filePath); IPath respath = JavaUtil.getJavaPathForResource(res); filePath = respath.makeRelative().toString(); if (isSpring || isBlueprint) { String codeTemplate = TestGenerator.createTransformTestText(transformID, packageName, className, filePath, isSpring); if (codeTemplate != null) { clsContent.append(codeTemplate); } ICompilationUnit wrapperCls = pkg.createCompilationUnit(className + ".java", //$NON-NLS-1$ clsContent.toString(), true, null); wrapperCls.save(null, true); return wrapperCls; } return null; } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.jboss.tools.ws.creation.core.commands.ClientSampleCreationCommand.java
License:Open Source License
/** * create a java class/*from w w w . ja v a 2 s . c om*/ * * @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.ImplementationClassCreationCommand.java
License:Open Source License
private IPackageFragment createImplPackage(String implPackage) throws JavaModelException { IPackageFragmentRoot root = JBossWSCreationUtils.getPackageFragmentRoot(project, model.getJavaSourceFolder()); return root.createPackageFragment(implPackage, false, 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) {/*from w w w. j av a2s .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("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) {/* ww w . ja v a 2 s . 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; } }
From source file:org.jboss.tools.ws.creation.core.commands.ServiceCreationCommand.java
License:Open Source License
private ICompilationUnit createJavaClass(String packageName, String className, IJavaProject project) { try {/*from ww w .ja v a2s . c o m*/ 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("@WebService()").append(LINE_SEPARATOR); //$NON-NLS-1$ 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.jws.WebMethod", null, null); //$NON-NLS-1$ wrapperCls.createImport("javax.jws.WebService", null, null); //$NON-NLS-1$ IType serviceClsType = wrapperCls.findPrimaryType(); clsContent = new StringBuffer(); clsContent.append("@WebMethod()"); //$NON-NLS-1$ clsContent.append(LINE_SEPARATOR); clsContent.append("public String sayHello(String name) {"); //$NON-NLS-1$ clsContent.append(LINE_SEPARATOR); clsContent.append(" System.out.println(\"Hello: \" + name);"); //$NON-NLS-1$ clsContent.append(LINE_SEPARATOR); clsContent.append(" return \"Hello \" + name + \"!\";"); //$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; } }
From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java
License:Open Source License
/** * @return/*from w w w. j a v a 2 s . c o m*/ * @throws JavaModelException */ public static IPackageFragment createPackage(IJavaProject javaProject, String pkgName) throws JavaModelException { IFolder folder = javaProject.getProject().getFolder("src/main/java"); IPackageFragmentRoot packageFragmentRoot = javaProject.getPackageFragmentRoot(folder); IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(pkgName); if (!packageFragment.exists()) { packageFragment = packageFragmentRoot.createPackageFragment("org.jboss.tools.ws.jaxrs.sample", false, new NullProgressMonitor()); } Assert.assertTrue("Target package does not exist", packageFragment.exists()); return packageFragment; }
From source file:org.jboss.tools.ws.jaxws.ui.commands.ClientSampleCreationCommand.java
License:Open Source License
/** * create a java class/*www .j a v a2s. c o m*/ * * @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) { JBossJAXWSUIPlugin.getDefault().logError(e); return null; } }
From source file:org.jboss.tools.ws.jaxws.ui.commands.ServiceCreationCommand.java
License:Open Source License
private ICompilationUnit createJavaClass(String packageName, String className, IJavaProject project) { try {/*from w ww.ja v a 2s. c om*/ 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("@WebService()").append(LINE_SEPARATOR); //$NON-NLS-1$ 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.jws.WebMethod", null, null); //$NON-NLS-1$ wrapperCls.createImport("javax.jws.WebService", null, null); //$NON-NLS-1$ IType serviceClsType = wrapperCls.findPrimaryType(); clsContent = new StringBuffer(); clsContent.append("@WebMethod()"); //$NON-NLS-1$ clsContent.append(LINE_SEPARATOR); clsContent.append("public String sayHello(String name) {"); //$NON-NLS-1$ clsContent.append(LINE_SEPARATOR); clsContent.append(" System.out.println(\"Hello: \" + name);"); //$NON-NLS-1$ clsContent.append(LINE_SEPARATOR); clsContent.append(" return \"Hello \" + name + \"!\";"); //$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) { JBossJAXWSUIPlugin.getDefault().logError(e); return null; } }