private long getContainerTimestamp(TypeNameMatch match) {
   try {
     IType type = match.getType();
     IResource resource = type.getResource();
     if (resource != null) {
       URI location = resource.getLocationURI();
       if (location != null) {
         IFileInfo info = EFS.getStore(location).fetchInfo();
         if (info.exists()) {
           // The element could be removed from the build path. So check
           // if the Java element still exists.
           IJavaElement element = JavaCore.create(resource);
           if (element != null && element.exists()) return info.getLastModified();
         }
       }
     } else { // external JAR
       IPackageFragmentRoot root = match.getPackageFragmentRoot();
       if (root.exists()) {
         IFileInfo info = EFS.getLocalFileSystem().getStore(root.getPath()).fetchInfo();
         if (info.exists()) {
           return info.getLastModified();
         }
       }
     }
   } catch (CoreException e) {
     // Fall through
   }
   return IResource.NULL_STAMP;
 }
 private boolean isVisible(TypeNameMatch curr, ICompilationUnit cu) {
   int flags = curr.getModifiers();
   if (Flags.isPrivate(flags)) {
     return false;
   }
   if (Flags.isPublic(flags) || Flags.isProtected(flags)) {
     return true;
   }
   return curr.getPackageName().equals(cu.getParent().getElementName());
 }
 public synchronized TypeNameMatch[] getFilteredTypeInfos(TypeInfoFilter filter) {
   Collection values = getValues();
   List result = new ArrayList();
   for (Iterator iter = values.iterator(); iter.hasNext(); ) {
     TypeNameMatch type = (TypeNameMatch) iter.next();
     if ((filter == null || filter.matchesHistoryElement(type))
         && !TypeFilter.isFiltered(type.getFullyQualifiedName())) result.add(type);
   }
   Collections.reverse(result);
   return (TypeNameMatch[]) result.toArray(new TypeNameMatch[result.size()]);
 }
 protected void setAttributes(Object object, Element typeElement) {
   TypeNameMatch type = (TypeNameMatch) object;
   String handleId = type.getType().getHandleIdentifier();
   typeElement.setAttribute(NODE_HANDLE, handleId);
   typeElement.setAttribute(NODE_MODIFIERS, Integer.toString(type.getModifiers()));
   Long timestamp = (Long) fTimestampMapping.get(type);
   if (timestamp == null) {
     typeElement.setAttribute(NODE_TIMESTAMP, Long.toString(IResource.NULL_STAMP));
   } else {
     typeElement.setAttribute(NODE_TIMESTAMP, timestamp.toString());
   }
 }
 private boolean isOfKind(TypeNameMatch curr, int typeKinds, boolean is50OrHigher) {
   int flags = curr.getModifiers();
   if (Flags.isAnnotation(flags)) {
     return is50OrHigher && ((typeKinds & SimilarElementsRequestor.ANNOTATIONS) != 0);
   }
   if (Flags.isEnum(flags)) {
     return is50OrHigher && ((typeKinds & SimilarElementsRequestor.ENUMS) != 0);
   }
   if (Flags.isInterface(flags)) {
     return (typeKinds & SimilarElementsRequestor.INTERFACES) != 0;
   }
   return (typeKinds & SimilarElementsRequestor.CLASSES) != 0;
 }
  /*
   * Finds a type by the simple name. From AddImportsOperation
   */
  private TypeNameMatch[] findAllTypes(
      String simpleTypeName,
      IJavaSearchScope searchScope,
      SimpleName nameNode,
      IProgressMonitor monitor,
      ICompilationUnit cu)
      throws JavaModelException {
    boolean is50OrHigher = JavaModelUtil.is50OrHigher(cu.getJavaProject());

    int typeKinds = SimilarElementsRequestor.ALL_TYPES;
    if (nameNode != null) {
      typeKinds = ASTResolving.getPossibleTypeKinds(nameNode, is50OrHigher);
    }

    ArrayList<TypeNameMatch> typeInfos = new ArrayList<TypeNameMatch>();
    TypeNameMatchCollector requestor = new TypeNameMatchCollector(typeInfos);
    new SearchEngine()
        .searchAllTypeNames(
            null,
            0,
            simpleTypeName.toCharArray(),
            SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE,
            getSearchForConstant(typeKinds),
            searchScope,
            requestor,
            IJavaSearchConstants.FORCE_IMMEDIATE_SEARCH,
            monitor);

    ArrayList<TypeNameMatch> typeRefsFound = new ArrayList<TypeNameMatch>(typeInfos.size());
    for (int i = 0, len = typeInfos.size(); i < len; i++) {
      TypeNameMatch curr = typeInfos.get(i);
      if (curr.getPackageName().length() > 0) { // do not suggest imports from the default package
        if (isOfKind(curr, typeKinds, is50OrHigher) && isVisible(curr, cu)) {
          typeRefsFound.add(curr);
        }
      }
    }
    return typeRefsFound.toArray(new TypeNameMatch[typeRefsFound.size()]);
  }
 private synchronized void internalCheckConsistency(IProgressMonitor monitor)
     throws OperationCanceledException {
   // Setting fNeedsConsistencyCheck is necessary here since
   // markAsInconsistent isn't synchronized.
   fNeedsConsistencyCheck = true;
   List typesToCheck = new ArrayList(getKeys());
   monitor.beginTask(CorextMessages.TypeInfoHistory_consistency_check, typesToCheck.size());
   monitor.setTaskName(CorextMessages.TypeInfoHistory_consistency_check);
   for (Iterator iter = typesToCheck.iterator(); iter.hasNext(); ) {
     TypeNameMatch type = (TypeNameMatch) iter.next();
     long currentTimestamp = getContainerTimestamp(type);
     Long lastTested = (Long) fTimestampMapping.get(type);
     if (lastTested != null
         && currentTimestamp != IResource.NULL_STAMP
         && currentTimestamp == lastTested.longValue()
         && !isContainerDirty(type)) continue;
     try {
       IType jType = type.getType();
       if (jType == null || !jType.exists()) {
         remove(type);
       } else {
         // copy over the modifiers since they may have changed
         int modifiers = jType.getFlags();
         if (modifiers != type.getModifiers()) {
           replace(type, SearchEngine.createTypeNameMatch(jType, modifiers));
         } else {
           fTimestampMapping.put(type, new Long(currentTimestamp));
         }
       }
     } catch (JavaModelException e) {
       remove(type);
     }
     if (monitor.isCanceled()) throw new OperationCanceledException();
     monitor.worked(1);
   }
   monitor.done();
   fNeedsConsistencyCheck = false;
 }
 public boolean isContainerDirty(TypeNameMatch match) {
   ICompilationUnit cu = match.getType().getCompilationUnit();
   if (cu == null) {
     return false;
   }
   IResource resource = cu.getResource();
   ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
   ITextFileBuffer textFileBuffer =
       manager.getTextFileBuffer(resource.getFullPath(), LocationKind.IFILE);
   if (textFileBuffer != null) {
     return textFileBuffer.isDirty();
   }
   return false;
 }
 @Override
 protected String extract(TypeNameMatch match) {
   return match.getPackageName();
 }
  private void collectProposals(
      IJavaProject project, String name, Collection<DefaultClasspathFixProposal> proposals)
      throws CoreException {
    int idx = name.lastIndexOf('.');
    char[] packageName =
        idx != -1 ? name.substring(0, idx).toCharArray() : null; // no package provided
    char[] typeName = name.substring(idx + 1).toCharArray();

    if (typeName.length == 1 && typeName[0] == '*') {
      typeName = null;
    }

    IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
    ArrayList<TypeNameMatch> res = new ArrayList<TypeNameMatch>();
    TypeNameMatchCollector requestor = new TypeNameMatchCollector(res);
    int matchMode = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
    new SearchEngine()
        .searchAllTypeNames(
            packageName,
            matchMode,
            typeName,
            matchMode,
            IJavaSearchConstants.TYPE,
            scope,
            requestor,
            IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
            null);

    if (res.isEmpty()) {
      return;
    }
    HashSet<Object> addedClaspaths = new HashSet<Object>();
    for (int i = 0; i < res.size(); i++) {
      TypeNameMatch curr = res.get(i);
      IType type = curr.getType();
      if (type != null) {
        IPackageFragmentRoot root =
            (IPackageFragmentRoot) type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        try {
          IClasspathEntry entry = root.getRawClasspathEntry();
          if (entry == null) {
            continue;
          }
          IJavaProject other = root.getJavaProject();
          int entryKind = entry.getEntryKind();
          if ((entry.isExported() || entryKind == IClasspathEntry.CPE_SOURCE)
              && addedClaspaths.add(other)) {
            IClasspathEntry newEntry = JavaCore.newProjectEntry(other.getPath());
            Change change = ClasspathFixProposal.newAddClasspathChange(project, newEntry);
            if (change != null) {
              String[] args = {
                BasicElementLabels.getResourceName(other.getElementName()),
                BasicElementLabels.getResourceName(project.getElementName())
              };
              String label =
                  Messages.format(
                      CorrectionMessages.ReorgCorrectionsSubProcessor_addcp_project_description,
                      args);
              String desc = label;
              DefaultClasspathFixProposal proposal =
                  new DefaultClasspathFixProposal(
                      label, change, desc, IProposalRelevance.ADD_PROJECT_TO_BUILDPATH);
              proposals.add(proposal);
            }
          }
          if (entryKind == IClasspathEntry.CPE_CONTAINER) {
            IPath entryPath = entry.getPath();
            if (isNonProjectSpecificContainer(entryPath)) {
              addLibraryProposal(project, root, entry, addedClaspaths, proposals);
            } else {
              try {
                IClasspathContainer classpathContainer =
                    JavaCore.getClasspathContainer(entryPath, root.getJavaProject());
                if (classpathContainer != null) {
                  IClasspathEntry entryInContainer =
                      JavaModelUtil.findEntryInContainer(classpathContainer, root.getPath());
                  if (entryInContainer != null) {
                    addLibraryProposal(project, root, entryInContainer, addedClaspaths, proposals);
                  }
                }
              } catch (CoreException e) {
                // ignore
              }
            }
          } else if ((entryKind == IClasspathEntry.CPE_LIBRARY
              || entryKind == IClasspathEntry.CPE_VARIABLE)) {
            addLibraryProposal(project, root, entry, addedClaspaths, proposals);
          }
        } catch (JavaModelException e) {
          // ignore
        }
      }
    }
  }