/**
   * Returns the reason for why the Javadoc of the Java element could not be retrieved.
   *
   * @param element whose Javadoc could not be retrieved
   * @param root the root of the Java element
   * @return the String message for why the Javadoc could not be retrieved for the Java element or
   *     <code>null</code> if the Java element is from a source container
   * @since 3.9
   */
  public static String getExplanationForMissingJavadoc(
      IJavaElement element, IPackageFragmentRoot root) {
    String message = null;
    try {
      boolean isBinary = (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY);
      if (isBinary) {
        boolean hasAttachedJavadoc = JavaDocLocations.getJavadocBaseLocation(element) != null;
        boolean hasAttachedSource = root.getSourceAttachmentPath() != null;
        IOpenable openable = element.getOpenable();
        boolean hasSource = openable.getBuffer() != null;

        // Provide hint why there's no Java doc
        if (!hasAttachedSource && !hasAttachedJavadoc)
          message = CorextMessages.JavaDocLocations_noAttachments;
        else if (!hasAttachedJavadoc && !hasSource)
          message = CorextMessages.JavaDocLocations_noAttachedJavadoc;
        else if (!hasAttachedSource) message = CorextMessages.JavaDocLocations_noAttachedSource;
        else if (!hasSource) message = CorextMessages.JavaDocLocations_noInformation;
      }
    } catch (JavaModelException e) {
      message = CorextMessages.JavaDocLocations_error_gettingJavadoc;
      JavaPlugin.log(e);
    }
    return message;
  }
  /*
   * (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;
  }
 protected IPackageFragmentRoot getFragmentRoot(IJavaElement elem) {
   IPackageFragmentRoot initRoot = null;
   if (elem != null) {
     initRoot = (IPackageFragmentRoot) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
     try {
       if (initRoot == null || initRoot.getKind() != IPackageFragmentRoot.K_SOURCE) {
         IJavaProject jproject = elem.getJavaProject();
         if (jproject != null) {
           initRoot = null;
           if (jproject.exists()) {
             IPackageFragmentRoot[] roots = jproject.getPackageFragmentRoots();
             for (int i = 0; i < roots.length; i++) {
               if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                 initRoot = roots[i];
                 break;
               }
             }
           }
           if (initRoot == null) {
             initRoot = jproject.getPackageFragmentRoot(jproject.getResource());
           }
         }
       }
     } catch (JavaModelException e) {
       // TODO
       e.printStackTrace();
     }
   }
   return initRoot;
 }
  public static URL getJavadocBaseLocation(IJavaElement element) throws JavaModelException {
    if (element.getElementType() == IJavaElement.JAVA_PROJECT) {
      return getProjectJavadocLocation((IJavaProject) element);
    }

    IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(element);
    if (root == null) {
      return null;
    }

    if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
      IClasspathEntry entry = root.getResolvedClasspathEntry();
      URL javadocLocation = getLibraryJavadocLocation(entry);
      if (javadocLocation != null) {
        return getLibraryJavadocLocation(entry);
      }
      entry = root.getRawClasspathEntry();
      switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
        case IClasspathEntry.CPE_VARIABLE:
          return getLibraryJavadocLocation(entry);
        default:
          return null;
      }
    } else {
      return getProjectJavadocLocation(root.getJavaProject());
    }
  }
 /**
  * Initializes the source folder field with a valid package fragment root. The package fragment
  * root is computed from the given Java element.
  *
  * @param elem the Java element used to compute the initial package fragment root used as the
  *     source folder
  */
 protected void initContainerPage(IJavaElement elem) {
   IPackageFragmentRoot initRoot = null;
   if (elem != null) {
     initRoot = JavaModelUtil.getPackageFragmentRoot(elem);
     try {
       if (initRoot == null || initRoot.getKind() != IPackageFragmentRoot.K_SOURCE) {
         IJavaProject jproject = elem.getJavaProject();
         if (jproject != null) {
           initRoot = null;
           if (jproject.exists()) {
             IPackageFragmentRoot[] roots = jproject.getPackageFragmentRoots();
             for (int i = 0; i < roots.length; i++) {
               if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                 initRoot = roots[i];
                 break;
               }
             }
           }
           if (initRoot == null) {
             initRoot = jproject.getPackageFragmentRoot(jproject.getResource());
           }
         }
       }
     } catch (JavaModelException e) {
       JavaPlugin.log(e);
     }
   }
   setPackageFragmentRoot(initRoot, true);
 }
  protected List<String> getSourceFolders(IProject project) {
    List<String> result = new ArrayList<String>();
    IJavaProject javaProject = JavaCore.create(project);
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    try {
      for (IPackageFragmentRoot packageFragmentRoot : javaProject.getPackageFragmentRoots()) {
        if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
          IPath path = packageFragmentRoot.getPath();
          IFolder folder = root.getFolder(path);
          String location = folder.getLocation().toString();

          if (!location.contains("src-gen")) {
            result.add(location);
          }
        }
      }

      for (IProject referencedProject : javaProject.getProject().getReferencedProjects()) {
        if (referencedProject.isAccessible() && referencedProject.hasNature(JavaCore.NATURE_ID)) {
          result.addAll(getSourceFolders(referencedProject));
        }
      }

    } catch (JavaModelException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();

    } catch (CoreException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return result;
  }
Esempio n. 7
0
 private ISourceFileLocator createSourceFileLocator(IPackageFragmentRoot root)
     throws JavaModelException {
   if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
     return new SourceFolderSourceFileLocator(root);
   } else {
     return new LibrarySourceFileLocator(root);
   }
 }
 private void collectCompilationUnits(IPackageFragmentRoot root, Collection result)
     throws JavaModelException {
   if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
     IJavaElement[] children = root.getChildren();
     for (int i = 0; i < children.length; i++) {
       collectCompilationUnits((IPackageFragment) children[i], result);
     }
   }
 }
Esempio n. 9
0
  /**
   * This method will return the first source folder of the Java project
   *
   * @param javaProject - the java project where the source folder needs to be indentified
   * @return
   * @throws JavaModelException
   */
  public static IPackageFragmentRoot getSourceFolder(IJavaProject javaProject)
      throws JavaModelException {
    for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
      if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
        return root;
      }
    }

    return null;
  }
 public static ISourceLocation findLocation(IPackageFragmentRoot root) throws JavaModelException {
   if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
     IPath path = EclEmmaCorePlugin.getAbsolutePath(root.getPath());
     return new SourceLocation(path, new Path(IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH));
   } else {
     IPath path = root.getSourceAttachmentPath();
     if (path != null) {
       path = EclEmmaCorePlugin.getAbsolutePath(path);
       return new SourceLocation(path, root.getSourceAttachmentRootPath());
     } else {
       return null;
     }
   }
 }
 /**
  * Get the source folder for the project
  *
  * @param project
  */
 private IPackageFragmentRoot[] getPackageFragmentRoots(IProject project) {
   IJavaProject p = JavaCore.create(project);
   List<IPackageFragmentRoot> result = new ArrayList<IPackageFragmentRoot>();
   try {
     IPackageFragmentRoot[] candidates = p.getAllPackageFragmentRoots();
     for (IPackageFragmentRoot candidate : candidates) {
       if (candidate.getKind() == IPackageFragmentRoot.K_SOURCE) {
         result.add(candidate);
       }
     }
   } catch (JavaModelException e) {
   }
   return result.toArray(new IPackageFragmentRoot[] {});
 }
Esempio n. 12
0
 private IStatus validateClassFile() {
   IPackageFragmentRoot root = getPackageFragmentRoot();
   try {
     if (root.getKind() != IPackageFragmentRoot.K_BINARY)
       return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, root);
   } catch (JavaModelException e) {
     return e.getJavaModelStatus();
   }
   IJavaProject project = getJavaProject();
   return JavaConventions.validateClassFileName(
       getElementName(),
       project.getOption(JavaCore.COMPILER_SOURCE, true),
       project.getOption(JavaCore.COMPILER_COMPLIANCE, true));
 }
Esempio n. 13
0
  public static List<ICompilationUnit> collectCompilationUnits(IJavaProject project)
      throws JavaModelException {

    List<ICompilationUnit> result = new ArrayList<ICompilationUnit>();

    IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
    for (int i = 0; i < roots.length; ++i) {
      IPackageFragmentRoot root = roots[i];
      if (IPackageFragmentRoot.K_SOURCE == root.getKind()) {
        collectCompilationUnits(result, root);
      }
    }

    return result;
  }
 /*
  * @param projects a collection of IJavaProject
  * @return Array of IPackageFragmentRoot, one element for each packageFragmentRoot which lies within a project in <code> projects </code> .
  */
 private static IPackageFragmentRoot[] getAllScopeElements(
     IJavaProject project, boolean onlySourceRoots) throws JavaModelException {
   Collection<IJavaProject> referencingProjects = getReferencingProjects(project);
   List<IPackageFragmentRoot> result = new ArrayList<IPackageFragmentRoot>();
   for (Iterator<IJavaProject> it = referencingProjects.iterator(); it.hasNext(); ) {
     IJavaProject javaProject = it.next();
     IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
     // Add all package fragment roots except archives
     for (int i = 0; i < roots.length; i++) {
       IPackageFragmentRoot root = roots[i];
       if (!onlySourceRoots || root.getKind() == IPackageFragmentRoot.K_SOURCE) result.add(root);
     }
   }
   return result.toArray(new IPackageFragmentRoot[result.size()]);
 }
 /** Attempts to set the defaults values from the given {@link UriPathTemplateCategory} */
 private void setDefaultValues(final UriPathTemplateCategory category) {
   final IJavaProject javaProject = category.getJavaProject();
   try {
     final IPackageFragmentRoot[] packageFragmentRoots = javaProject.getAllPackageFragmentRoots();
     for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
       if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
         setPackageFragmentRoot(packageFragmentRoot, true);
         break;
       }
     }
   } catch (JavaModelException e) {
     Logger.error(
         "Failed to set the default values from project '" + javaProject.getElementName() + "'",
         e);
   }
 }
Esempio n. 16
0
 private boolean isEnabled(IStructuredSelection selection) {
   Object[] selected = selection.toArray();
   for (int i = 0; i < selected.length; i++) {
     try {
       if (selected[i] instanceof IJavaElement) {
         IJavaElement elem = (IJavaElement) selected[i];
         if (elem.exists()) {
           switch (elem.getElementType()) {
             case IJavaElement.TYPE:
               return elem.getParent().getElementType()
                   == IJavaElement.COMPILATION_UNIT; // for browsing perspective
             case IJavaElement.COMPILATION_UNIT:
               return true;
             case IJavaElement.IMPORT_CONTAINER:
               return true;
             case IJavaElement.PACKAGE_FRAGMENT:
             case IJavaElement.PACKAGE_FRAGMENT_ROOT:
               IPackageFragmentRoot root =
                   (IPackageFragmentRoot) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
               return (root.getKind() == IPackageFragmentRoot.K_SOURCE);
             case IJavaElement.JAVA_PROJECT:
               // https://bugs.eclipse.org/bugs/show_bug.cgi?id=65638
               return true;
           }
         }
       } else if (selected[i] instanceof LogicalPackage) {
         return true;
       } else if (selected[i] instanceof IWorkingSet) {
         IWorkingSet workingSet = (IWorkingSet) selected[i];
         return IWorkingSetIDs.JAVA.equals(workingSet.getId());
       }
     } catch (JavaModelException e) {
       if (!e.isDoesNotExist()) {
         JavaPlugin.log(e);
       }
     }
   }
   return false;
 }
Esempio n. 17
0
 private static URL findResourceURL(
     IJavaProject javaProject, Set<IJavaProject> visited, boolean isFirstProject, String name) {
   if (visited.contains(javaProject)) return null;
   visited.add(javaProject);
   try {
     IPath outPath =
         javaProject
             .getProject()
             .getLocation()
             .removeLastSegments(1)
             .append(javaProject.getOutputLocation());
     outPath = outPath.addTrailingSeparator();
     {
       URL url = toURL(outPath.append(name));
       if (url != null) {
         return url;
       }
     }
     for (IPackageFragmentRoot fragment : javaProject.getPackageFragmentRoots()) {
       if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
         URL url = toURL(fragment.getResource().getLocation().append(name));
         if (url != null) {
           return url;
         }
       }
     }
     // urls.add(out);
     IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
     for (IClasspathEntry entry : entries) {
       switch (entry.getEntryKind()) {
         case IClasspathEntry.CPE_LIBRARY:
           {
             // TODO
             IClasspathEntry resolveEntry = JavaCore.getResolvedClasspathEntry(entry);
             File file = resolveEntry.getPath().toFile();
             IPath path = resolveEntry.getPath();
             if (!file.exists()) {
               String projectName = path.segment(0);
               IProject project =
                   javaProject.getProject().getWorkspace().getRoot().getProject(projectName);
               path = project.getLocation().append(path.removeFirstSegments(1));
             }
             String spec = "jar:file:" + path.toString() + "!/" + name;
             try {
               URL url2 = new URL(spec);
               url2.getContent();
               return url2;
             } catch (Exception e) {
             }
           }
           break;
         case IClasspathEntry.CPE_CONTAINER:
           break;
         case IClasspathEntry.CPE_VARIABLE:
           {
             {
               // TODO
               URL url = toURL(outPath.append(name));
               if (url != null) {
                 return url;
               }
             }
           }
           break;
         case IClasspathEntry.CPE_PROJECT:
           {
             if (isFirstProject || entry.isExported()) {
               URL url = findResourceURL(getJavaProject(entry), visited, false, name);
               if (url != null) {
                 return url;
               }
             }
             break;
           }
       }
     }
   } catch (JavaModelException e) {
     e.printStackTrace();
   }
   return null;
 }
  /**
   * This method is a hook which gets called after the source folder's text input field has changed.
   * This default implementation updates the model and returns an error status. The underlying model
   * is only valid if the returned status is OK.
   *
   * @return the model's error status
   */
  protected IStatus containerChanged() {
    StatusInfo status = new StatusInfo();

    fCurrRoot = null;
    String str = getPackageFragmentRootText();
    if (str.length() == 0) {
      status.setError(NewWizardMessages.NewContainerWizardPage_error_EnterContainerName);
      return status;
    }
    IPath path = new Path(str);
    IResource res = fWorkspaceRoot.findMember(path);
    if (res != null) {
      int resType = res.getType();
      if (resType == IResource.PROJECT || resType == IResource.FOLDER) {
        IProject proj = res.getProject();
        if (!proj.isOpen()) {
          status.setError(
              Messages.format(
                  NewWizardMessages.NewContainerWizardPage_error_ProjectClosed,
                  BasicElementLabels.getPathLabel(proj.getFullPath(), false)));
          return status;
        }
        IJavaProject jproject = JavaCore.create(proj);
        fCurrRoot = jproject.getPackageFragmentRoot(res);
        if (res.exists()) {
          if (res.isVirtual()) {
            status.setError(NewWizardMessages.NewContainerWizardPage_error_FolderIsVirtual);
            return status;
          }
          try {
            if (!proj.hasNature(JavaCore.NATURE_ID)) {
              if (resType == IResource.PROJECT) {
                status.setError(NewWizardMessages.NewContainerWizardPage_warning_NotAJavaProject);
              } else {
                status.setWarning(
                    NewWizardMessages.NewContainerWizardPage_warning_NotInAJavaProject);
              }
              return status;
            }
            if (fCurrRoot.isArchive()) {
              status.setError(
                  Messages.format(
                      NewWizardMessages.NewContainerWizardPage_error_ContainerIsBinary,
                      BasicElementLabels.getPathLabel(path, false)));
              return status;
            }
            if (fCurrRoot.getKind() == IPackageFragmentRoot.K_BINARY) {
              status.setWarning(
                  Messages.format(
                      NewWizardMessages.NewContainerWizardPage_warning_inside_classfolder,
                      BasicElementLabels.getPathLabel(path, false)));
            } else if (!jproject.isOnClasspath(fCurrRoot)) {
              status.setWarning(
                  Messages.format(
                      NewWizardMessages.NewContainerWizardPage_warning_NotOnClassPath,
                      BasicElementLabels.getPathLabel(path, false)));
            }
          } catch (JavaModelException e) {
            status.setWarning(NewWizardMessages.NewContainerWizardPage_warning_NotOnClassPath);
          } catch (CoreException e) {
            status.setWarning(NewWizardMessages.NewContainerWizardPage_warning_NotAJavaProject);
          }
        }
        return status;
      } else {
        status.setError(
            Messages.format(
                NewWizardMessages.NewContainerWizardPage_error_NotAFolder,
                BasicElementLabels.getPathLabel(path, false)));
        return status;
      }
    } else {
      status.setError(
          Messages.format(
              NewWizardMessages.NewContainerWizardPage_error_ContainerDoesNotExist,
              BasicElementLabels.getPathLabel(path, false)));
      return status;
    }
  }
  public JavadocHelpContext(IContext context, Object[] elements) throws JavaModelException {
    Assert.isNotNull(elements);
    if (context instanceof IContext2) fTitle = ((IContext2) context).getTitle();

    List helpResources = new ArrayList();

    String javadocSummary = null;
    for (int i = 0; i < elements.length; i++) {
      if (elements[i] instanceof IJavaElement) {
        IJavaElement element = (IJavaElement) elements[i];
        // if element isn't on the build path skip it
        if (!ActionUtil.isOnBuildPath(element)) continue;

        // Create Javadoc summary
        if (BUG_85721_FIXED) {
          if (javadocSummary == null) {
            javadocSummary = retrieveText(element);
            if (javadocSummary != null) {
              String elementLabel =
                  JavaElementLabels.getTextLabel(element, JavaElementLabels.ALL_DEFAULT);

              // FIXME: needs to be NLSed once the code becomes active
              javadocSummary =
                  "<b>Javadoc for "
                      + elementLabel
                      + ":</b><br>"
                      + javadocSummary; //$NON-NLS-1$//$NON-NLS-2$
            }
          } else {
            javadocSummary = ""; // no Javadoc summary for multiple selection //$NON-NLS-1$
          }
        }

        URL url = JavaUI.getJavadocLocation(element, true);
        if (url == null || doesNotExist(url)) {
          IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(element);
          if (root != null) {
            url = JavaUI.getJavadocBaseLocation(element);
            if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
              element = element.getJavaProject();
            } else {
              element = root;
            }
            url = JavaUI.getJavadocLocation(element, false);
          }
        }
        if (url != null) {
          IHelpResource javaResource = new JavaUIHelpResource(element, getURLString(url));
          helpResources.add(javaResource);
        }
      }
    }

    // Add static help topics
    if (context != null) {
      IHelpResource[] resources = context.getRelatedTopics();
      if (resources != null) {
        for (int j = 0; j < resources.length; j++) {
          helpResources.add(resources[j]);
        }
      }
    }

    fHelpResources =
        (IHelpResource[]) helpResources.toArray(new IHelpResource[helpResources.size()]);

    if (context != null) fText = context.getText();

    if (BUG_85721_FIXED) {
      if (javadocSummary != null && javadocSummary.length() > 0) {
        if (fText != null) fText = context.getText() + "<br><br>" + javadocSummary; // $NON-NLS-1$
        else fText = javadocSummary;
      }
    }

    if (fText == null) fText = ""; // $NON-NLS-1$
  }