<commit_before>/*******************************************************************************
 * Copyright (c) 2004 - 2005 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/
/*
 * Created on Jan 26, 2005
  */
package org.eclipse.mylar.java.search;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.ui.search.JavaSearchQuery;
import org.eclipse.jdt.internal.ui.search.JavaSearchResult;
import org.eclipse.jdt.ui.search.ElementQuerySpecification;
import org.eclipse.jdt.ui.search.QuerySpecification;
import org.eclipse.mylar.core.IMylarStructureBridge;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.core.model.ITaskscapeNode;
import org.eclipse.mylar.core.search.IActiveSearchListener;
import org.eclipse.mylar.core.search.IMylarSearchOperation;
import org.eclipse.mylar.core.search.RelationshipProvider;
import org.eclipse.mylar.java.JavaStructureBridge;
import org.eclipse.search.ui.ISearchResult;



/**
 * @author <NAME>
 */
public abstract class AbstractJavaRelationshipProvider extends RelationshipProvider {
    
    protected AbstractJavaRelationshipProvider(String structureKind, String id) {
        super(structureKind, id);
    }
    
    @Override
    protected void findRelated(final ITaskscapeNode node, int degreeOfSeparation) {
        if (node == null) return;
        if (!node.getStructureKind().equals(JavaStructureBridge.EXTENSION)) return;
        IJavaElement javaElement = JavaCore.create(node.getElementHandle());
        if (!acceptElement(javaElement)) {
            return; 
        }
        
        IJavaSearchScope scope = createJavaSearchScope(javaElement, degreeOfSeparation);
        if (scope != null) runJob(node,  degreeOfSeparation, getId());
    }

    private IJavaSearchScope createJavaSearchScope(IJavaElement element, int degreeOfSeparation) {
        List<ITaskscapeNode> landmarks = MylarPlugin.getTaskscapeManager().getActiveTaskscape().getLandmarks();
        List<ITaskscapeNode> interestingElements = MylarPlugin.getTaskscapeManager().getActiveTaskscape().getInteresting();
        Set<IJavaElement> searchElements = new HashSet<IJavaElement>();
        int includeMask = IJavaSearchScope.SOURCES;
        if (degreeOfSeparation == 1) {
            for (ITaskscapeNode landmark : landmarks) {
            	IMylarStructureBridge sbridge = MylarPlugin.getDefault().getStructureBridge(landmark.getStructureKind());
            	Object o = sbridge.getObjectForHandle(landmark.getElementHandle());
            	if(o instanceof IJavaElement){
	                IJavaElement landmarkElement = (IJavaElement)o;
	                if (landmarkElement instanceof IMember && !landmark.getDegreeOfInterest().isPredicted()) {
	                    searchElements.add(((IMember)landmarkElement).getCompilationUnit());
	                } else if (landmarkElement instanceof ICompilationUnit) {
	                    searchElements.add(landmarkElement);
	                }
            	}
            } 
        } else if (degreeOfSeparation == 2) {
            for (ITaskscapeNode interesting : interestingElements) {
            	IMylarStructureBridge sbridge = MylarPlugin.getDefault().getStructureBridge(interesting.getStructureKind());
            	Object o = sbridge.getObjectForHandle(interesting.getElementHandle());
            	if(o instanceof IJavaElement){
	                IJavaElement interestingElement = (IJavaElement)o;
	                if (interestingElement instanceof IMember && !interesting.getDegreeOfInterest().isPredicted()) {
	                    searchElements.add(((IMember)interestingElement).getCompilationUnit());
	                } else if (interestingElement instanceof ICompilationUnit) {
	                    searchElements.add(interestingElement);
	                }
            	}
            }  
        } else if (degreeOfSeparation == 3 || degreeOfSeparation == 4) {
            for (ITaskscapeNode interesting : interestingElements) {
            	IMylarStructureBridge sbridge = MylarPlugin.getDefault().getStructureBridge(interesting.getStructureKind());
            	Object o = sbridge.getObjectForHandle(interesting.getElementHandle());
            	IProject project = sbridge.getProjectForObject(o);// TODO what to do when the element is not a java element, how determine if a javaProject?
            	
            	if(project != null && JavaProject.hasJavaNature(project)){
            		IJavaProject javaProject = JavaCore.create(project);//((IJavaElement)o).getJavaProject();
            		if (javaProject != null) searchElements.add(javaProject);
            	}
            }   
            if (degreeOfSeparation == 4) {
                includeMask = IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SYSTEM_LIBRARIES;
            }
        } else if (degreeOfSeparation == 5) {
            return SearchEngine.createWorkspaceScope();
        } 
     
        if (searchElements.size() == 0) {
            return null;
        } else {    
            IJavaElement[] elements = new IJavaElement[searchElements.size()];
            int j = 0;
            for (IJavaElement searchElement : searchElements) {
                elements[j] = searchElement;
                j++;
            } 
            return SearchEngine.createJavaSearchScope(elements, includeMask);
        }
    }
    
    protected boolean acceptResultElement(IJavaElement element) {
        return !(element instanceof IImportDeclaration);
    }
    
    protected boolean acceptElement(IJavaElement javaElement) {
        return javaElement != null 
            && (javaElement instanceof IMember || javaElement instanceof IType);
    }

    private void runJob(
            final ITaskscapeNode node, 
            final int degreeOfSeparation, 
            final String kind) {
        
        int limitTo = 0;
        if (kind.equals(JavaReferencesProvider.ID)) {
            limitTo = IJavaSearchConstants.REFERENCES;
        } else if (kind.equals(JavaImplementorsProvider.ID)) {
            limitTo = IJavaSearchConstants.IMPLEMENTORS;
        } else if (kind.equals(JUnitReferencesProvider.ID)) {
            limitTo = IJavaSearchConstants.REFERENCES; 
        } else if (kind.equals(JavaReadAccessProvider.ID)) {
            limitTo = IJavaSearchConstants.REFERENCES; 
        } else if (kind.equals(JavaWriteAccessProvider.ID)) {
            limitTo = IJavaSearchConstants.REFERENCES; 
        }
        
        final JavaSearchOperation query = (JavaSearchOperation)getSearchOperation(node, limitTo, degreeOfSeparation);
        if(query == null) return;
        
        JavaSearchJob job = new JavaSearchJob(query.getLabel(), query);
        query.addListener(new IActiveSearchListener() {
        
        	private boolean gathered = false;
        
        	public boolean resultsGathered() {
    			return gathered;
    		} 
        	
           public void searchCompleted(List l) {
               if (l == null) return;
                List<IJavaElement> relatedHandles = new ArrayList<IJavaElement>();
                Object[] elements = l.toArray();
                for (int i = 0; i < elements.length; i++) {
                    if (elements[i] instanceof IJavaElement) relatedHandles.add((IJavaElement)elements[i]);
                } 
                for(IJavaElement element : relatedHandles) {
                    if (!acceptResultElement(element)) continue;

                        incrementInterest(degreeOfSeparation, JavaStructureBridge.EXTENSION, element.getHandleIdentifier());
                } 
                gathered = true;
            }

		
        });
        job.schedule();
    }

    @Override
    public IMylarSearchOperation getSearchOperation(ITaskscapeNode node, int limitTo, int degreeOfSeparation){
    	IJavaElement javaElement = JavaCore.create(node.getElementHandle());
        IJavaSearchScope scope = createJavaSearchScope(javaElement, degreeOfSeparation);

        if(scope == null) return null;
        
        QuerySpecification specs = new ElementQuerySpecification(
                javaElement, 
                limitTo,  
                scope, 
                "Mylar degree of separation: " + degreeOfSeparation);
      <commit_msg>added a null check for the element to search for to try to get rid of the npe<commit_after>/*******************************************************************************
 * Copyright (c) 2004 - 2005 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/
/*
 * Created on Jan 26, 2005
  */
package org.eclipse.mylar.java.search;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.ui.search.JavaSearchQuery;
import org.eclipse.jdt.internal.ui.search.JavaSearchResult;
import org.eclipse.jdt.ui.search.ElementQuerySpecification;
import org.eclipse.jdt.ui.search.QuerySpecification;
import org.eclipse.mylar.core.IMylarStructureBridge;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.core.model.ITaskscapeNode;
import org.eclipse.mylar.core.search.IActiveSearchListener;
import org.eclipse.mylar.core.search.IMylarSearchOperation;
import org.eclipse.mylar.core.search.RelationshipProvider;
import org.eclipse.mylar.java.JavaStructureBridge;
import org.eclipse.search.ui.ISearchResult;



/**
 * @author <NAME>
 */
public abstract class AbstractJavaRelationshipProvider extends RelationshipProvider {
    
    protected AbstractJavaRelationshipProvider(String structureKind, String id) {
        super(structureKind, id);
    }
    
    @Override
    protected void findRelated(final ITaskscapeNode node, int degreeOfSeparation) {
        if (node == null) return;
        if (!node.getStructureKind().equals(JavaStructureBridge.EXTENSION)) return;
        IJavaElement javaElement = JavaCore.create(node.getElementHandle());
        if (!acceptElement(javaElement)) {
            return; 
        }
        
        IJavaSearchScope scope = createJavaSearchScope(javaElement, degreeOfSeparation);
        if (scope != null) runJob(node,  degreeOfSeparation, getId());
    }

    private IJavaSearchScope createJavaSearchScope(IJavaElement element, int degreeOfSeparation) {
        List<ITaskscapeNode> landmarks = MylarPlugin.getTaskscapeManager().getActiveTaskscape().getLandmarks();
        List<ITaskscapeNode> interestingElements = MylarPlugin.getTaskscapeManager().getActiveTaskscape().getInteresting();
        Set<IJavaElement> searchElements = new HashSet<IJavaElement>();
        int includeMask = IJavaSearchScope.SOURCES;
        if (degreeOfSeparation == 1) {
            for (ITaskscapeNode landmark : landmarks) {
            	IMylarStructureBridge sbridge = MylarPlugin.getDefault().getStructureBridge(landmark.getStructureKind());
            	Object o = sbridge.getObjectForHandle(landmark.getElementHandle());
            	if(o instanceof IJavaElement){
	                IJavaElement landmarkElement = (IJavaElement)o;
	                if (landmarkElement instanceof IMember && !landmark.getDegreeOfInterest().isPredicted()) {
	                    searchElements.add(((IMember)landmarkElement).getCompilationUnit());
	                } else if (landmarkElement instanceof ICompilationUnit) {
	                    searchElements.add(landmarkElement);
	                }
            	}
            } 
        } else if (degreeOfSeparation == 2) {
            for (ITaskscapeNode interesting : interestingElements) {
            	IMylarStructureBridge sbridge = MylarPlugin.getDefault().getStructureBridge(interesting.getStructureKind());
            	Object o = sbridge.getObjectForHandle(interesting.getElementHandle());
            	if(o instanceof IJavaElement){
	                IJavaElement interestingElement = (IJavaElement)o;
	                if (interestingElement instanceof IMember && !interesting.getDegreeOfInterest().isPredicted()) {
	                    searchElements.add(((IMember)interestingElement).getCompilationUnit());
	                } else if (interestingElement instanceof ICompilationUnit) {
	                    searchElements.add(interestingElement);
	                }
            	}
            }  
        } else if (degreeOfSeparation == 3 || degreeOfSeparation == 4) {
            for (ITaskscapeNode interesting : interestingElements) {
            	IMylarStructureBridge sbridge = MylarPlugin.getDefault().getStructureBridge(interesting.getStructureKind());
            	Object o = sbridge.getObjectForHandle(interesting.getElementHandle());
            	IProject project = sbridge.getProjectForObject(o);// TODO what to do when the element is not a java element, how determine if a javaProject?
            	
            	if(project != null && JavaProject.hasJavaNature(project)){
            		IJavaProject javaProject = JavaCore.create(project);//((IJavaElement)o).getJavaProject();
            		if (javaProject != null) searchElements.add(javaProject);
            	}
            }   
            if (degreeOfSeparation == 4) {
                includeMask = IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SYSTEM_LIBRARIES;
            }
        } else if (degreeOfSeparation == 5) {
            return SearchEngine.createWorkspaceScope();
        } 
     
        if (searchElements.size() == 0) {
            return null;
        } else {    
            IJavaElement[] elements = new IJavaElement[searchElements.size()];
            int j = 0;
            for (IJavaElement searchElement : searchElements) {
                elements[j] = searchElement;
                j++;
            } 
            return SearchEngine.createJavaSearchScope(elements, includeMask);
        }
    }
    
    protected boolean acceptResultElement(IJavaElement element) {
        return !(element instanceof IImportDeclaration);
    }
    
    protected boolean acceptElement(IJavaElement javaElement) {
        return javaElement != null 
            && (javaElement instanceof IMember || javaElement instanceof IType);
    }

    private void runJob(
            final ITaskscapeNode node, 
            final int degreeOfSeparation, 
            final String kind) {
        
        int limitTo = 0;
        if (kind.equals(JavaReferencesProvider.ID)) {
            limitTo = IJavaSearchConstants.REFERENCES;
        } else if (kind.equals(JavaImplementorsProvider.ID)) {
            limitTo = IJavaSearchConstants.IMPLEMENTORS;
        } else if (kind.equals(JUnitReferencesProvider.ID)) {
            limitTo = IJavaSearchConstants.REFERENCES; 
        } else if (kind.equals(JavaReadAccessProvider.ID)) {
            limitTo = IJavaSearchConstants.REFERENCES; 
        } else if (kind.equals(JavaWriteAccessProvider.ID)) {
            limitTo = IJavaSearchConstants.REFERENCES; 
        }
        
        final JavaSearchOperation query = (JavaSearchOperation)getSearchOperation(node, limitTo, degreeOfSeparation);
        if(query == null) return;
        
        JavaSearchJob job = new JavaSearchJob(query.getLabel(), query);
        query.addListener(new IActiveSearchListener() {
        
        	private boolean gathered = false;
        
        	public boolean resultsGathered() {
    			return gathered;
    		} 
        	
           public void searchCompleted(List l) {
               if (l == null) return;
                List<IJavaElement> relatedHandles = new ArrayList<IJavaElement>();
                Object[] elements = l.toArray();
                for (int i = 0; i < elements.length; i++) {
                    if (elements[i] instanceof IJavaElement) relatedHandles.add((IJavaElement)elements[i]);
                } 
                for(IJavaElement element : relatedHandles) {
                    if (!acceptResultElement(element)) continue;

                        incrementInterest(degreeOfSeparation, JavaStructureBridge.EXTENSION, element.getHandleIdentifier());
                } 
                gathered = true;
            }

		
        });
        job.schedule();
    }

    @Override
    public IMylarSearchOperation getSearchOperation(ITaskscapeNode node, int limitTo, int degreeOfSeparation){
    	IJavaElement javaElement = JavaCore.create(node.getElementHandle());
    	if(javaElement == null)
    		return null;
    	
        IJavaSearchScope scope = createJavaSearchScope(javaElement, degreeOfSeparation);

        if(scope == null) return null;
        
        QuerySpecification specs = new ElementQuerySpecification(
                javaElement, 
                limitTo,  
                scope, 
                "Mylar degree of separation: " + degreeOfSeparation);
      
{'fraction_non_alphanumeric': 0.1048267849598648, 'fraction_numerical': 0.002957329953527672, 'mean_word_length': 2.763314785373609, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2410323', 'n_tokens_mistral': 5053, 'n_tokens_neox': 4832, 'n_words': 995}