/**
   * Gets the fully qualified class name for an active file. For example, its value is foo.bar.Baz.
   *
   * @param file Get fully qualified class file.
   * @return The fully qualified class name. For example,foo.bar.Baz.
   */
  private String getFullyQualifedClassName(IFile file) {
    String fullClassName = "";
    if (file.exists() && file.getName().endsWith(EclipseSensorConstants.JAVA_EXT)) {
      ICompilationUnit compilationUnit = (ICompilationUnit) JavaCore.create(file);
      String className = compilationUnit.getElementName();
      if (className.endsWith(EclipseSensorConstants.JAVA_EXT)) {
        className = className.substring(0, className.length() - 5);
      }

      try {
        IPackageDeclaration[] packageDeclarations = compilationUnit.getPackageDeclarations();
        // Should only have one package declaration
        if (packageDeclarations == null || packageDeclarations.length == 0) {
          fullClassName = className;
        } else {
          fullClassName = packageDeclarations[0].getElementName() + '.' + className;
        }
      } catch (JavaModelException e) {
        // This exception will be thrown if user is working on a Java but did not open
        // it with "Java Perspective". Thus, the Java Model does not exist to parse
        // Java files. So we only log out exception while Eclipse's Java Perspective
        // exits.
        if (!e.isDoesNotExist()) {
          EclipseSensorPlugin.getDefault().log(file.getName(), e);
        }
      }
    }

    return fullClassName;
  }
 /** @since 2.4 */
 @Override
 public Map<URI, IStorage> getAllEntries(IPackageFragmentRoot root) {
   try {
     IResource underlyingResource = root.getUnderlyingResource();
     if (underlyingResource instanceof IFolder) {
       return host.getAllEntries((IFolder) underlyingResource);
     }
   } catch (JavaModelException e) {
     if (!e.isDoesNotExist()) log.error(e.getMessage(), e);
     return emptyMap();
   }
   PackageFragmentRootData data = getData(root);
   return data.uri2Storage;
 }
 /** @since 2.4 */
 private void updateCache(IJavaProject project) {
   Set<PackageFragmentRootData> datas = newHashSet();
   try {
     if (project.exists() && project.getProject().isAccessible()) {
       for (IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
         boolean isCachable = root.isArchive() || root.isExternal();
         if (isCachable) datas.add(getCachedData(root));
       }
     }
   } catch (JavaModelException e) {
     if (!e.isDoesNotExist())
       log.error("Error getting package fragments roots of " + project.getElementName(), e);
   } finally {
     clearCache(project, datas);
   }
 }
Example #4
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;
 }
 /**
  * Construct a Classloader with the classpathentries from the provided and all upstream-projects,
  * except the output folders of the local project.
  */
 protected URLClassLoader createClassLoaderForJavaProject(final IJavaProject projectToUse) {
   final LinkedHashSet<URL> urls = CollectionLiterals.<URL>newLinkedHashSet();
   try {
     boolean _isOutputFolderIncluded = this.isOutputFolderIncluded();
     HashSet<IJavaProject> _newHashSet = CollectionLiterals.<IJavaProject>newHashSet();
     this.collectClasspathURLs(projectToUse, urls, _isOutputFolderIncluded, _newHashSet);
   } catch (final Throwable _t) {
     if (_t instanceof JavaModelException) {
       final JavaModelException e = (JavaModelException) _t;
       boolean _isDoesNotExist = e.isDoesNotExist();
       boolean _not = (!_isDoesNotExist);
       if (_not) {
         String _message = e.getMessage();
         JdtBasedProcessorProvider.LOG.error(_message, e);
       }
     } else {
       throw Exceptions.sneakyThrow(_t);
     }
   }
   ClassLoader _parentClassLoader = this.getParentClassLoader();
   return new URLClassLoader(
       ((URL[]) Conversions.unwrapArray(urls, URL.class)), _parentClassLoader);
 }