/*
   * (non-Javadoc)
   *
   * @see org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator#
   * isSelectedValid(java.lang.Object)
   */
  @Override
  public boolean isSelectedValid(Object element) {
    boolean isValid = false;
    try {
      if (element instanceof IJavaProject) {
        IJavaProject jproject = (IJavaProject) element;
        IPath path = jproject.getProject().getFullPath();
        isValid = (jproject.findPackageFragmentRoot(path) != null);
      } else if (element instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) element;

        boolean isSrc = (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE);
        boolean isGen =
            packageFragmentRoot.getElementName().equals(IAndroidConstants.GEN_SRC_FOLDER)
                && (packageFragmentRoot.getParent() instanceof IJavaProject);

        isValid = isSrc && !isGen;
      } else {
        isValid = true;
      }
    } catch (JavaModelException e) {
      AndmoreLogger.error(ElementTreeValidator.class, e.getLocalizedMessage(), e);
    }
    return isValid;
  }
 /**
  * Tests that making Javadoc changes to the source file TestClass2 cause the workspace baseline to
  * be updated.
  *
  * <p>This test adds a @noinstantiate tag to the source file TestClass2
  */
 public void testWPUpdateSourceTypeChanged() throws Exception {
   IJavaProject project = getTestingProject();
   assertNotNull("The testing project must exist", project);
   IPackageFragmentRoot root =
       project.findPackageFragmentRoot(
           new Path(project.getElementName()).append(ProjectUtils.SRC_FOLDER).makeAbsolute());
   assertNotNull("the 'src' package fragment root must exist", root);
   NullProgressMonitor monitor = new NullProgressMonitor();
   IPackageFragment fragment = root.getPackageFragment("a.b.c");
   FileUtils.importFileFromDirectory(
       SRC_LOC.append("TestClass2.java").toFile(), fragment.getPath(), monitor);
   ICompilationUnit element =
       (ICompilationUnit) project.findElement(new Path("a/b/c/TestClass2.java"));
   assertNotNull("TestClass2 must exist in the test project", element);
   updateTagInSource(element, "TestClass2", null, "@noinstantiate", false);
   IApiDescription desc = getTestProjectApiDescription();
   assertNotNull("the testing project api description must exist", desc);
   IApiAnnotations annot = desc.resolveAnnotations(Factory.typeDescriptor("a.b.c.TestClass2"));
   assertNotNull("the annotations for a.b.c.TestClass2 cannot be null", annot);
   assertTrue(
       "there must be a noinstantiate setting for TestClass2",
       (annot.getRestrictions() & RestrictionModifiers.NO_INSTANTIATE) != 0);
   assertTrue(
       "there must be a noextend setting for TestClass2",
       (annot.getRestrictions() & RestrictionModifiers.NO_EXTEND) != 0);
 }
 /**
  * Adds the package with the given name to the given package fragment root
  *
  * @param the project to add the package to
  * @param srcroot the absolute path to the package fragment root to add the new package to
  * @param packagename the name of the new package
  * @return the new {@link IPackageFragment} or <code>null</code>
  */
 public IPackageFragment assertTestPackage(IJavaProject project, IPath srcroot, String packagename)
     throws JavaModelException {
   IPackageFragment fragment = null;
   IPackageFragmentRoot root = project.findPackageFragmentRoot(srcroot);
   assertNotNull("the 'src' package fragment root must exist", root);
   fragment = root.createPackageFragment(packagename, true, new NullProgressMonitor());
   assertNotNull("the new package '" + packagename + "' should have been created", fragment);
   return fragment;
 }
 /**
  * Tests that adding a source file to an API aware project causes the workspace description to be
  * updated This test adds <code>a.b.c.TestClass1</code> to the plug-in project
  */
 public void testWPUpdateSourceAdded() throws Exception {
   IJavaProject project = getTestingProject();
   assertNotNull("The testing project must exist", project);
   IPackageFragmentRoot root =
       project.findPackageFragmentRoot(
           new Path(project.getElementName())
               .append(ProjectUtils.SRC_FOLDER)
               .makeAbsolute()
               .makeAbsolute());
   assertNotNull("the 'src' package fragment root must exist", root);
   assertTestSource(root, TESTING_PACKAGE, "TestClass1");
   assertSourceResctriction(TESTING_PACKAGE, "TestClass1", RestrictionModifiers.NO_INSTANTIATE);
 }
 /**
  * Tests that removing a source file from an API aware project causes the workspace description to
  * be updated
  */
 public void testWPUpdateSourceRemoved() throws Exception {
   IJavaProject project = getTestingProject();
   assertNotNull("The testing project must exist", project);
   IPackageFragmentRoot root =
       project.findPackageFragmentRoot(
           new Path(project.getElementName()).append(ProjectUtils.SRC_FOLDER).makeAbsolute());
   assertNotNull("the 'src' package fragment root must exist", root);
   assertTestSource(root, TESTING_PACKAGE, "TestClass1");
   IJavaElement element = project.findElement(new Path("a/b/c/TestClass1.java"));
   assertNotNull("the class a.b.c.TestClass1 must exist in the project", element);
   element.getResource().delete(true, new NullProgressMonitor());
   IApiDescription desc = getTestProjectApiDescription();
   assertNotNull("the testing project api description must exist", desc);
   IApiAnnotations annot = desc.resolveAnnotations(Factory.typeDescriptor("a.b.c.TestClass1"));
   assertNull("the annotations for a.b.c.TestClass1 should no longer be present", annot);
 }
 private static Map<IPackageFragmentRoot, IProject> getPackageFragmentRootsOnModelPath() {
   Map<IPackageFragmentRoot, IProject> packageFragmentRoots = new HashMap<>();
   for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
     IJavaProject javaProject = JavaCore.create(project);
     try {
       for (IClasspathEntry entry : getModelPathEntries(javaProject)) {
         Optional.ofNullable(javaProject.findPackageFragmentRoot(entry.getPath()))
             .ifPresent(
                 packageFragmentRoot -> packageFragmentRoots.put(packageFragmentRoot, project));
       }
     } catch (JavaModelException e) {
       continue;
     }
   }
   return packageFragmentRoots;
 }
Exemple #7
0
 private static IPackageFragmentRoot findPackageFragmentRoot(
     IJavaProject javaProject, IPath filePath) throws JavaModelException {
   if (filePath.isEmpty() || filePath.isRoot()) {
     return null;
   } else {
     IResource possibleFragmentResource =
         ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(filePath);
     if (possibleFragmentResource != null) {
       filePath = possibleFragmentResource.getFullPath();
     }
     IPackageFragmentRoot fragment = javaProject.findPackageFragmentRoot(filePath);
     if (fragment != null) {
       return fragment;
     } else {
       return findPackageFragmentRoot(javaProject, filePath.removeLastSegments(1));
     }
   }
 }
  //	@Override
  public IPackageFragmentRoot getPackageFragmentRoot() {

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

      try {

        IJavaProject project = JavaCore.create(Util.getProject(projectText));
        IPath moduleXmlPath = new Path(moduleText);
        root =
            project.findPackageFragmentRoot(
                new Path("/").append(projectText).append(moduleXmlPath.segment(0))); // $NON-NLS-1$

      } catch (JavaModelException e) {
        Activator.logException(e);
      }
    }
    return root;
  }
 /**
  * Tests that removing a tag from a field updates the workspace baseline
  *
  * <p>This test adds a @noextend tag to the field 'field' in TestField9
  */
 public void testWPUpdateSourceFieldRemoveTag() throws Exception {
   IJavaProject project = getTestingProject();
   assertNotNull("The testing project must exist", project);
   IPackageFragmentRoot root =
       project.findPackageFragmentRoot(
           new Path(project.getElementName()).append(ProjectUtils.SRC_FOLDER).makeAbsolute());
   assertNotNull("the 'src' package fragment root must exist", root);
   assertTestSource(root, TESTING_PACKAGE, "TestField9");
   ICompilationUnit element =
       (ICompilationUnit) project.findElement(new Path("a/b/c/TestField9.java"));
   assertNotNull("TestField9 must exist in the test project", element);
   updateTagInSource(element, "field1", null, "@noreference", true);
   IApiDescription desc = getTestProjectApiDescription();
   assertNotNull("the testing project api description must exist", desc);
   IApiAnnotations annot =
       desc.resolveAnnotations(Factory.fieldDescriptor("a.b.c.TestField9", "field"));
   assertNotNull("the annotations for 'field' cannot be null", annot);
   assertTrue("there must be a no restrictions for 'field'", annot.getRestrictions() == 0);
 }
 /**
  * Tests that removing a tag from a type updates the workspace baseline
  *
  * <p>This test removes a @noinstantiate tag to an inner class in TestClass3
  */
 public void testWPUpdateSourceTypeRemoveTag() throws Exception {
   IJavaProject project = getTestingProject();
   assertNotNull("The testing project must exist", project);
   IPackageFragmentRoot root =
       project.findPackageFragmentRoot(
           new Path(project.getElementName()).append(ProjectUtils.SRC_FOLDER).makeAbsolute());
   assertNotNull("the 'src' package fragment root must exist", root);
   assertTestSource(root, TESTING_PACKAGE, "TestClass3");
   ICompilationUnit element =
       (ICompilationUnit) project.findElement(new Path("a/b/c/TestClass3.java"));
   assertNotNull("TestClass3 must exist in the test project", element);
   updateTagInSource(element, "InnerTestClass3", null, "@noextend", true);
   IApiDescription desc = getTestProjectApiDescription();
   assertNotNull("the testing project api description must exist", desc);
   IApiAnnotations annot =
       desc.resolveAnnotations(Factory.typeDescriptor("a.b.c.TestClass3$InnerTestClass3"));
   assertNotNull("the annotations for 'InnerTestClass3' cannot be null", annot);
   assertTrue(
       "there must be a no restrictions for 'InnerTestClass3'",
       (annot.getRestrictions() & RestrictionModifiers.NO_INSTANTIATE) == 0);
 }
 /**
  * Tests that changing the javadoc for a method updates the workspace baseline
  *
  * <p>This test adds a @noextend tag to the method foo() in TestClass1
  */
 public void testWPUpdateSourceMethodChanged() throws Exception {
   IJavaProject project = getTestingProject();
   assertNotNull("The testing project must exist", project);
   IPackageFragmentRoot root =
       project.findPackageFragmentRoot(
           new Path(project.getElementName()).append(ProjectUtils.SRC_FOLDER).makeAbsolute());
   assertNotNull("the 'src' package fragment root must exist", root);
   assertTestSource(root, TESTING_PACKAGE, "TestClass1");
   ICompilationUnit element =
       (ICompilationUnit) project.findElement(new Path("a/b/c/TestClass1.java"));
   assertNotNull("TestClass1 must exist in the test project", element);
   updateTagInSource(element, "foo", "()V", "@nooverride", false);
   IApiDescription desc = getTestProjectApiDescription();
   assertNotNull("the testing project api description must exist", desc);
   IApiAnnotations annot =
       desc.resolveAnnotations(Factory.methodDescriptor("a.b.c.TestClass1", "foo", "()V"));
   assertNotNull("the annotations for foo() cannot be null", annot);
   assertTrue(
       "there must be a nooverride setting for foo()",
       (annot.getRestrictions() & RestrictionModifiers.NO_OVERRIDE) != 0);
 }