List of usage examples for org.eclipse.jdt.core IJavaProject findPackageFragmentRoot
IPackageFragmentRoot findPackageFragmentRoot(IPath path) throws JavaModelException;
null
if one does not exist. 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 w ww . ja v a 2 s . co 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
/** * Create a compilation unit from the given filename content, in the given package, with the given name * // w w w .j a v a 2 s.c o m * @param fileName * the filename containing the source code, in the /resources folder of the test bundle, or null if the * created compilation unit must remain empty after its creation * @param pkg * the target package * @param unitName * the target compilation unit name * @return the created compilation unit * @throws JavaModelException */ public static ICompilationUnit createCompilationUnit(IJavaProject javaProject, String fileName, String pkg, String unitName, Bundle bundle) throws JavaModelException { String contents = ""; if (fileName != null) { contents = getResourceContent(fileName, bundle); } IPackageFragmentRoot sourceFolder = javaProject .findPackageFragmentRoot(javaProject.getProject().getFullPath().append("src/main/java")); IPackageFragment packageFragment = sourceFolder.getPackageFragment(pkg); ICompilationUnit foocompilationUnit = packageFragment.createCompilationUnit(unitName, contents, true, new NullProgressMonitor()); saveAndClose(foocompilationUnit); return foocompilationUnit; }
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 w w . j av a 2 s . co 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) { JBossJAXWSUIPlugin.getDefault().logError(e); return null; } }
From source file:org.mybatis.generator.eclipse.core.callback.EclipseShellCallback.java
License:Apache License
private IPackageFragmentRoot getSpecificSourceFolder(IJavaProject javaProject, String targetProject) throws ShellException { try {/*w w w. ja v a 2 s.c o m*/ Path path = new Path("/" + targetProject); IPackageFragmentRoot pfr = javaProject.findPackageFragmentRoot(path); if (pfr == null) { StringBuffer sb = new StringBuffer(); sb.append("Cannot find source folder "); sb.append(targetProject); throw new ShellException(sb.toString()); } return pfr; } catch (CoreException e) { throw new ShellException(e.getStatus().getMessage(), e); } }
From source file:org.openlegacy.ide.eclipse.actions.AbstractGenerateCodeDialog.java
License:Open Source License
protected IPackageFragmentRoot chooseContainer() { IJavaElement initElement = getSourceFolder(); Class<?>[] acceptedClasses = new Class[] { IPackageFragmentRoot.class, IJavaProject.class }; TypedElementSelectionValidator validator = new TypedElementSelectionValidator(acceptedClasses, false) { @Override/*from ww w. j a va2 s. com*/ public boolean isSelectedValid(Object element) { try { if (element instanceof IJavaProject) { IJavaProject jproject = (IJavaProject) element; IPath path = jproject.getProject().getFullPath(); return (jproject.findPackageFragmentRoot(path) != null); } else if (element instanceof IPackageFragmentRoot) { return (((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE); } return true; } catch (JavaModelException e) { } return false; } }; acceptedClasses = new Class[] { IJavaModel.class, IPackageFragmentRoot.class, IJavaProject.class }; ViewerFilter filter = new TypedViewerFilter(acceptedClasses) { @Override public boolean select(Viewer viewer, Object parent, Object element) { if (element instanceof IPackageFragmentRoot) { try { return (((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE); } catch (JavaModelException e) { } } else if (element instanceof IJavaProject) { IJavaProject javaPr = (IJavaProject) element; return javaPr.isOpen(); } return super.select(viewer, parent, element); } }; StandardJavaElementContentProvider provider = new StandardJavaElementContentProvider(); ILabelProvider labelProvider = new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT); ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(getShell(), labelProvider, provider); dialog.setValidator(validator); dialog.setComparator(new JavaElementComparator()); dialog.setTitle(""); //$NON-NLS-1$ dialog.setMessage(""); //$NON-NLS-1$ dialog.addFilter(filter); IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); dialog.setInput(JavaCore.create(root)); dialog.setInitialSelection(initElement); dialog.setHelpAvailable(false); if (dialog.open() == Window.OK) { Object element = dialog.getFirstResult(); if (element instanceof IJavaProject) { IJavaProject jproject = (IJavaProject) element; return jproject.getPackageFragmentRoot(jproject.getProject()); } else if (element instanceof IPackageFragmentRoot) { return (IPackageFragmentRoot) element; } return null; } return null; }
From source file:org.seasar.s2junit4plugin.action.OpenDiconPairAction.java
License:Apache License
/** * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) *//* www . ja va 2 s. c om*/ public void run(IAction action) { IResource resource = ResouceUtil.getCurrentSelectedResouce(); if (resource instanceof IFile) { IFile file = (IFile) resource; try { ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom(file); IJavaProject javaProject = compilationUnit.getJavaProject(); String compiliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true); try { if (Float.parseFloat(compiliance) < 1.5) { Logger.debug("not S2Junit4 environemnt. copliance=" + compiliance); //$NON-NLS-1$ return; } } catch (NumberFormatException nfe) { Logger.warn("Illegal Compiler Level: " + compiliance, nfe); //$NON-NLS-1$ return; } IType type = getClassPairType(compilationUnit); if (type == null) { return; } IFile diconPair = getDiconPair(type); Logger.debug("diconPair=" + diconPair); //$NON-NLS-1$ IWorkbench workbench = Activator.getDefault().getWorkbench(); if (diconPair != null) { IDE.openEditor(workbench.getActiveWorkbenchWindow().getActivePage(), diconPair); } else { if (!MessageDialog.openQuestion(workbench.getActiveWorkbenchWindow().getShell(), action.getText(), Messages.getString("OpenDiconPairAction.creationConfirmMessage"))) { //$NON-NLS-1$ return; } String testResourcesPath = PreferenceStoreUtil.getPreferenceStoreOfWorkspace() .getString(Constants.PREF_TEST_RESOURCES_PATH); IFolder folder = javaProject.getProject().getFolder(testResourcesPath); IPackageFragmentRoot packageFragmentRoot = javaProject .findPackageFragmentRoot(folder.getFullPath()); NewDiconWizard wizard = new NewDiconWizard(type.getPackageFragment(), packageFragmentRoot, type.getTypeQualifiedName() + ".dicon"); //$NON-NLS-1$ wizard.init(workbench, new StructuredSelection()); new WizardDialog(workbench.getActiveWorkbenchWindow().getShell(), wizard).open(); } } catch (Exception e) { Logger.error(e, this); } } }
From source file:org.seasar.s2junit4plugin.wizard.NewS2JUnit4TestCaseWizardPageOne.java
License:Apache License
/** * Initialized the page with the current selection * @param selection The selection//from www.j a v a 2s. c o m */ public void init(IStructuredSelection selection) { IJavaElement element = getInitialJavaElement(selection); try { String mainTestPath = PreferenceStoreUtil.getPreferenceStoreOfWorkspace() .getString(Constants.PREF_TEST_JAVA_PATH); IJavaProject javaProject = element.getJavaProject(); IFolder folder = javaProject.getProject().getFolder(mainTestPath); if (folder.exists()) { IPackageFragmentRoot findPackageFragmentRoot = javaProject .findPackageFragmentRoot(folder.getFullPath()); setPackageFragmentRoot(findPackageFragmentRoot, true); } else { initContainerPage(element); } } catch (JavaModelException e) { Logger.error(e, this); } initTypePage(element); // put default class to test if (element != null) { IType classToTest = null; // evaluate the enclosing type IType typeInCompUnit = (IType) element.getAncestor(IJavaElement.TYPE); if (typeInCompUnit != null) { if (typeInCompUnit.getCompilationUnit() != null) { classToTest = typeInCompUnit; } } else { ICompilationUnit cu = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) classToTest = cu.findPrimaryType(); else { if (element instanceof IClassFile) { try { IClassFile cf = (IClassFile) element; if (cf.isStructureKnown()) classToTest = cf.getType(); } catch (JavaModelException e) { Logger.error(e, this); } } } } if (classToTest != null) { try { if (!TestSearchEngine.isTestImplementor(classToTest)) { setClassUnderTest(classToTest.getFullyQualifiedName('.')); } } catch (JavaModelException e) { Logger.error(e, this); } } } restoreWidgetValues(); boolean isJunit4 = false; // if (element != null && element.getElementType() != IJavaElement.JAVA_MODEL) { // IJavaProject project= element.getJavaProject(); // try { // isJunit4= project.findType(JUnitPlugin.JUNIT4_ANNOTATION_NAME) != null; // } catch (JavaModelException e) { // // ignore // } // } if (store.getString(Constants.PREF_TEST_GENERATION_TYPE) .equals(org.seasar.s2junit4plugin.Messages.getString("Preference.JUnit4"))) { isJunit4 = true; } setJUnit4(isJunit4, true); boolean isS2Junit4 = false; // if (element != null && element.getElementType() != IJavaElement.JAVA_MODEL) { // IJavaProject project= element.getJavaProject(); // try { // isS2Junit4= project.findType("org.seasar.framework.unit.Seasar2") != null; // } catch (JavaModelException e) { // // ignore // } // } if (store.getString(Constants.PREF_TEST_GENERATION_TYPE) .equals(org.seasar.s2junit4plugin.Messages.getString("Preference.S2JUnit4"))) { isS2Junit4 = true; } setS2JUnit4(isS2Junit4, true); updateStatus(getStatusList()); }
From source file:org.springframework.ide.eclipse.beans.ui.properties.NonJavaResourceContentProvider.java
License:Open Source License
private Object[] getResources(IContainer container) { try {// w ww .j a va 2 s.c om IResource[] members = container.members(); IJavaProject javaProject = JavaCore.create(container.getProject()); if (javaProject == null || !javaProject.exists()) { return members; } boolean isFolderOnClasspath = javaProject.isOnClasspath(container); List<Object> nonJavaResources = new ArrayList<Object>(); // Can be on classpath but as a member of non-java resource folder for (IResource member : members) { // A resource can also be a java element in the case of // exclusion and inclusion filters. We therefore exclude Java // elements from the list of non-Java resources. if (isFolderOnClasspath) { if (javaProject.findPackageFragmentRoot(member.getFullPath()) == null) { nonJavaResources.add(member); } } else if (!javaProject.isOnClasspath(member)) { nonJavaResources.add(member); } } return nonJavaResources.toArray(); } catch (CoreException e) { return NO_CHILDREN; } }
From source file:org.switchyard.tools.ui.JavaUtil.java
License:Open Source License
/** * Returns the first source folder in the project. If the project is a maven * project, the first source folder configured will be used. * /*from ww w . j a va 2 s. com*/ * @param project the Java project * * @return the source root; may be null. */ public static IPackageFragmentRoot getFirstJavaSourceRoot(IJavaProject project) { if (project == null) { return null; } try { IPackageFragmentRoot sourceRoot = null; IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getProject(project.getProject()); if (facade == null) { for (IPackageFragmentRoot frag : project.getPackageFragmentRoots()) { if (frag.getKind() == IPackageFragmentRoot.K_SOURCE) { sourceRoot = frag; break; } } } else { IPath projectPath = project.getPath(); for (IPath sourcePath : facade.getCompileSourceLocations()) { IPackageFragmentRoot frag = project.findPackageFragmentRoot(projectPath.append(sourcePath)); if (frag != null) { sourceRoot = frag; break; } } } return sourceRoot; } catch (JavaModelException e) { return null; } }
From source file:x10dt.search.core.pdb.X10FactGenerator.java
License:Open Source License
private void processEntries(final CompilerOptionsBuilder cmpOptBuilder, final IWorkspaceRoot wsRoot, final IClasspathEntry[] entries, final IJavaProject javaProject, final IResource contextResource, final boolean isInRuntime) throws JavaModelException, AnalysisException { for (final IClasspathEntry pathEntry : entries) { switch (pathEntry.getEntryKind()) { case IClasspathEntry.CPE_SOURCE: if (pathEntry.getPath().isRoot()) { cmpOptBuilder.addToSourcePath(pathEntry.getPath().toFile()); } else { cmpOptBuilder.addToSourcePath(wsRoot.getLocation().append(pathEntry.getPath()).toFile()); }//from w ww .jav a2 s . co m if (pathEntry.getPath().segmentCount() > 1) { processSourceFolder(cmpOptBuilder, wsRoot.getFolder(pathEntry.getPath()), contextResource); } break; case IClasspathEntry.CPE_LIBRARY: try { final IPackageFragmentRoot pkgRoot = javaProject.findPackageFragmentRoot(pathEntry.getPath()); if ((pkgRoot != null) && pkgRoot.exists()) { final File localFile; if (pkgRoot.isExternal()) { localFile = pathEntry.getPath().toFile(); } else { localFile = pkgRoot.getResource().getLocation().toFile(); } cmpOptBuilder.addToClassPath(localFile.getAbsolutePath()); if (isInRuntime) { cmpOptBuilder.addToSourcePath(localFile); } final ZipFile zipFile; if (JAR_EXT.equals(pathEntry.getPath().getFileExtension())) { zipFile = new JarFile(localFile); } else { zipFile = new ZipFile(localFile); } processLibrary(cmpOptBuilder, zipFile, localFile, contextResource, isInRuntime); } } catch (IOException except) { throw new AnalysisException(NLS.bind(Messages.XFG_JarReadingError, pathEntry.getPath()), except); } break; case IClasspathEntry.CPE_CONTAINER: final IClasspathContainer cpContainer = JavaCore.getClasspathContainer(pathEntry.getPath(), javaProject); processEntries(cmpOptBuilder, wsRoot, cpContainer.getClasspathEntries(), javaProject, contextResource, true); break; case IClasspathEntry.CPE_PROJECT: final IResource projectResource = ResourcesPlugin.getWorkspace().getRoot() .findMember(pathEntry.getPath()); if ((projectResource != null) && projectResource.isAccessible()) { final IJavaProject newJavaProject = JavaCore.create((IProject) projectResource); processEntries(cmpOptBuilder, wsRoot, newJavaProject.getRawClasspath(), newJavaProject, contextResource, false); } break; case IClasspathEntry.CPE_VARIABLE: processEntries(cmpOptBuilder, wsRoot, new IClasspathEntry[] { JavaCore.getResolvedClasspathEntry(pathEntry) }, javaProject, contextResource, false); break; } } }