Reduces result for a single analyzer

@param analyzerJob
@param slaveResults
@param resultMap
@param reductionErrors
424751
@SuppressWarnings("unchecked")
    private void reduce(AnalyzerJob analyzerJob, Collection<AnalyzerResult> slaveResults,
            Map<ComponentJob, AnalyzerResult> resultMap, List<AnalysisResultReductionException> reductionErrors) {

        if (slaveResults.size() == 1) {
            // special case where these was only 1 slave job
            final AnalyzerResult firstResult = slaveResults.iterator().next();
            resultMap.put(analyzerJob, firstResult);
            _analysisListener.analyzerSuccess(_masterJob, analyzerJob, firstResult);
            return;
        }

        final Class<? extends AnalyzerResultReducer<?>> reducerClass = analyzerJob.getDescriptor()
                .getResultReducerClass();

        final ComponentDescriptor<? extends AnalyzerResultReducer<?>> reducerDescriptor = Descriptors
                .ofComponent(reducerClass);

        AnalyzerResultReducer<AnalyzerResult> reducer = null;
        boolean success = false;
        try {
            reducer = (AnalyzerResultReducer<AnalyzerResult>) reducerDescriptor.newInstance();

            _lifeCycleHelper.assignProvidedProperties(reducerDescriptor, reducer);
            _lifeCycleHelper.initialize(reducerDescriptor, reducer);

            final AnalyzerResult reducedResult = reducer.reduce(slaveResults);
            resultMap.put(analyzerJob, reducedResult);
            
            success = true;
            _analysisListener.analyzerSuccess(_masterJob, analyzerJob, reducedResult);

        } catch (Exception e) {
            AnalysisResultReductionException reductionError = new AnalysisResultReductionException(analyzerJob,
                    slaveResults, e);
            reductionErrors.add(reductionError);

            _analysisListener.errorInComponent(_masterJob, analyzerJob, null, e);
        } finally {
            if (reducer != null) {
                _lifeCycleHelper.close(reducerDescriptor, reducer, success);
            }
        }
    }
231528
public void analyze() {
        final Resources resources = new ProjectAnalyzer(analysis.classPaths)
                .analyze(analysis.projectClassPaths, analysis.projectSourcePaths, analysis.ignoredResources);

        if (resources.isEmpty()) {
            LogProvider.info("Empty JAX-RS analysis result, omitting output");
            return;
        }

        final Project project = new Project(analysis.projectName, analysis.projectVersion, resources);
        final byte[] output = analysis.backend.render(project);

        if (analysis.outputLocation != null) {
            outputToFile(output, analysis.outputLocation);
        } else {
            outputToConsole(output);
        }
    }
1172003
void analyze(final MethodResult methodResult) {
        lock.lock();
        try {
            buildPackagePrefix(methodResult.getParentResource().getOriginalClass());

            determineReturnTypes(methodResult).stream()
                    // FEATURE handle several sub-resource impl's
                    .reduce((l, r) -> JavaUtils.determineMostSpecificType(l, r))
                    .ifPresent(t -> registerSubResourceJob(t, methodResult.getSubResource()));
        } finally {
            lock.unlock();
        }
    }
874180
@Override
    public ValueNumberDataflow analyze(IAnalysisCache analysisCache, MethodDescriptor descriptor) throws CheckedAnalysisException {
        MethodGen methodGen = getMethodGen(analysisCache, descriptor);
        if (methodGen == null) {
            throw new MethodUnprofitableException(descriptor);
        }

        DepthFirstSearch dfs = getDepthFirstSearch(analysisCache, descriptor);
        LoadedFieldSet loadedFieldSet = getLoadedFieldSet(analysisCache, descriptor);
        ValueNumberAnalysis analysis = new ValueNumberAnalysis(methodGen, dfs, loadedFieldSet, AnalysisContext
                .currentAnalysisContext().getLookupFailureCallback());
        analysis.setMergeTree(new MergeTree(analysis.getFactory()));
        CFG cfg = getCFG(analysisCache, descriptor);

        ValueNumberDataflow vnaDataflow = new ValueNumberDataflow(cfg, analysis);
        vnaDataflow.execute();
        if (ClassContext.DUMP_DATAFLOW_ANALYSIS) {
            TreeSet<Location> tree = new TreeSet<>();
            for (Iterator<Location> locs = cfg.locationIterator(); locs.hasNext();) {
                Location loc = locs.next();
                tree.add(loc);
            }
            System.out.println("\n\nValue number analysis for " + descriptor.getName() + descriptor.getSignature() + " {");
            for (Location loc : tree) {
                System.out.println("\nBefore: " + vnaDataflow.getFactAtLocation(loc));
                System.out.println("Location: " + loc);
                System.out.println("After: " + vnaDataflow.getFactAfterLocation(loc));
            }
            System.out.println("}\n");
        }
        return vnaDataflow;
    }
229425
private static void reduceItems(FileOutputCollector collector, Reducer reducable, ArrayList<String> items)
            throws IOException {
        if(items != null) {
            // call reducer
            reducable.reduce(items, collector);
        }
    }
1110350
public void attach(final AnalyzerResult explorationResult) {
        final ResultProducer resultProducer;
        if (explorationResult == null) {
            resultProducer = null;
        } else {
            resultProducer = new DefaultResultProducer(explorationResult);
        }
        attach(resultProducer);
    }
439090
public AnalysisResultFuture run() {
        try {
            // the injection manager is job scoped
            final InjectionManager injectionManager = _configuration.getEnvironment().getInjectionManagerFactory()
                    .getInjectionManager(_configuration, _job);

            final LifeCycleHelper rowProcessingLifeCycleHelper =
                    new LifeCycleHelper(injectionManager, _includeNonDistributedTasks);

            final RowProcessingPublishers publishers =
                    new RowProcessingPublishers(_job, _analysisListener, _errorAware, _taskRunner,
                            rowProcessingLifeCycleHelper);

            final AnalysisJobMetrics analysisJobMetrics = publishers.getAnalysisJobMetrics();

            // A task listener that will register either succesfull executions
            // or unexpected errors (which will be delegated to the
            // errorListener)
            final JobCompletionTaskListener jobCompletionTaskListener =
                    new JobCompletionTaskListener(analysisJobMetrics, _analysisListener, 1);

            _analysisListener.jobBegin(_job, analysisJobMetrics);

            validateSingleTableInput(_job);

            // at this point we are done validating the job, it will run.
            scheduleRowProcessing(publishers, rowProcessingLifeCycleHelper, jobCompletionTaskListener,
                    analysisJobMetrics);

            return new AnalysisResultFutureImpl(_resultQueue, jobCompletionTaskListener, _errorAware);
        } catch (final RuntimeException e) {
            _analysisListener.errorUnknown(_job, e);
            throw e;
        }

    }
971222
public boolean cancelJob(String slaveJobId) {
        final AnalysisResultFuture resultFuture = _runningJobs.remove(slaveJobId);
        if (resultFuture != null) {
            resultFuture.cancel();
            return true;
        }
        return false;
    }
456252
@Override
    public MethodGen analyze(IAnalysisCache analysisCache, MethodDescriptor descriptor) throws CheckedAnalysisException {
        Method method = getMethod(analysisCache, descriptor);

        if (method.getCode() == null) {
            return null;
        }
        XMethod xmethod =  XFactory.createXMethod(descriptor);
        if (xmethod.usesInvokeDynamic() && false) {
            AnalysisContext.currentAnalysisContext().analysisSkippedDueToInvokeDynamic(xmethod);
            return null;
        }

        try {
            AnalysisContext analysisContext = AnalysisContext.currentAnalysisContext();
            JavaClass jclass = getJavaClass(analysisCache, descriptor.getClassDescriptor());
            ConstantPoolGen cpg = getConstantPoolGen(analysisCache, descriptor.getClassDescriptor());

            String methodName = method.getName();
            int codeLength = method.getCode().getCode().length;
            String superclassName = jclass.getSuperclassName();
            if (codeLength > 6000 && Const.STATIC_INITIALIZER_NAME.equals(methodName) && "java.lang.Enum".equals(superclassName)) {
                analysisContext.getLookupFailureCallback().reportSkippedAnalysis(
                        new JavaClassAndMethod(jclass, method).toMethodDescriptor());
                return null;
            }
            if (analysisContext.getBoolProperty(AnalysisFeatures.SKIP_HUGE_METHODS)) {
                if (codeLength > 6000 || (Const.STATIC_INITIALIZER_NAME.equals(methodName) || "getContents".equals(methodName)) && codeLength > 2000) {
                    analysisContext.getLookupFailureCallback().reportSkippedAnalysis(
                            new JavaClassAndMethod(jclass, method).toMethodDescriptor());
                    return null;
                }
            }

            return new MethodGen(method, jclass.getClassName(), cpg);
        } catch (Exception e) {
            AnalysisContext.logError("Error constructing methodGen", e);
            return null;
        }
    }
432934
@Override
    public void handleResult(RHI result) {
        if (wrappedResultHandler != null) {
            wrappedResultHandler.handleResult(resultTransformer.transform(result));
        }
    }
913640
private void listErrors(final Result result) {
        for (JSError warning : result.warnings) {
            getLog().warn(warning.toString());
        }

        for (JSError error : result.errors) {
            getLog().error(error.toString());
        }
    }
444330
@Override
    public BackwardTypeQualifierDataflowFactory analyze(IAnalysisCache analysisCache, MethodDescriptor descriptor)
            throws CheckedAnalysisException {
        return new BackwardTypeQualifierDataflowFactory(descriptor);
    }
26459
private void slowerButAccurateCountTasks(JobInfo info, JobInProgress job) {
    // Count maps
    int totalMaps = job.numMapTasks;
    int finishedMaps = 0;
    int runningMaps = 0;
    int runningMapTips = 0;
    for (TaskInProgress tip :
      job.getTasks(org.apache.hadoop.mapreduce.TaskType.MAP)) {
      if (tip.isComplete()) {
        finishedMaps += 1;
      } else if (tip.isRunning()) {
        runningMaps += tip.getActiveTasks().size();
        runningMapTips += 1;
      }
    }
    info.totalInitedTasks = job.numMapTasks + job.numReduceTasks;
    info.runningMaps = runningMaps;
    infosummary.totalRunningMaps += runningMaps;
    poolMgr.incRunningTasks(info.poolName, TaskType.MAP, runningMaps);
    info.neededSpeculativeMaps =  taskSelector.neededSpeculativeMaps(job);
    info.neededMaps = (totalMaps - runningMapTips - finishedMaps
        + info.neededSpeculativeMaps);
    // Count reduces
    int totalReduces = job.numReduceTasks;
    int finishedReduces = 0;
    int runningReduces = 0;
    int runningReduceTips = 0;
    for (TaskInProgress tip :
      job.getTasks(org.apache.hadoop.mapreduce.TaskType.REDUCE)) {
      if (tip.isComplete()) {
        finishedReduces += 1;
      } else if (tip.isRunning()) {
        runningReduces += tip.getActiveTasks().size();
        runningReduceTips += 1;
      }
    }
    info.runningReduces = runningReduces;
    infosummary.totalRunningReduces += runningReduces;
    poolMgr.incRunningTasks(info.poolName, TaskType.REDUCE, runningReduces);
    if (job.scheduleReduces()) {
      info.neededSpeculativeReduces =
          taskSelector.neededSpeculativeReduces(job);
      info.neededReduces = (totalReduces - runningReduceTips - finishedReduces
          + info.neededSpeculativeReduces);
    } else {
      info.neededReduces = 0;
    }
  }
4833
@Override
    public void analyzeDependency(Dependency dependency, Engine engine) throws AnalysisException {
        try {
            final byte[] fileContent = IOUtils.toByteArray(new FileInputStream(dependency.getActualFile()));
            final ScannerFacade scanner = new ScannerFacade(jsRepository);
            final List<JsLibraryResult> results = scanner.scanScript(dependency.getActualFile().getAbsolutePath(), fileContent, 0);

            if (results.size() > 0) {
                for (JsLibraryResult libraryResult : results) {

                    final JsLibrary lib = libraryResult.getLibrary();
                    dependency.setName(lib.getName());
                    dependency.setVersion(libraryResult.getDetectedVersion());
                    try {
                        final PackageURL purl = PackageURLBuilder.aPackageURL().withType("javascript")
                                .withName(lib.getName()).withVersion(libraryResult.getDetectedVersion()).build();
                        dependency.addSoftwareIdentifier(new PurlIdentifier(purl, Confidence.HIGHEST));
                    } catch (MalformedPackageURLException ex) {
                        LOGGER.debug("Unable to build package url for retireJS", ex);
                        final GenericIdentifier id = new GenericIdentifier("javascript:" + lib.getName() + "@"
                                + libraryResult.getDetectedVersion(), Confidence.HIGHEST);
                        dependency.addSoftwareIdentifier(id);
                    }

                    dependency.addEvidence(EvidenceType.VERSION, "file", "version", libraryResult.getDetectedVersion(), Confidence.HIGH);
                    dependency.addEvidence(EvidenceType.PRODUCT, "file", "name", libraryResult.getLibrary().getName(), Confidence.HIGH);

                    final List<Vulnerability> vulns = new ArrayList<>();
                    final JsVulnerability jsVuln = libraryResult.getVuln();

                    if (jsVuln.getIdentifiers().containsKey("CVE") || jsVuln.getIdentifiers().containsKey("osvdb")) {
                        /* CVEs and OSVDB are an array of Strings - each one a unique vulnerability.
                         * So the JsVulnerability we are operating on may actually be representing
                         * multiple vulnerabilities. */

                        //TODO - can we refactor this to avoid russian doll syndrome (i.e. nesting)?
                        //CSOFF: NestedForDepth
                        for (Map.Entry<String, List<String>> entry : jsVuln.getIdentifiers().entrySet()) {
                            final String key = entry.getKey();
                            final List<String> value = entry.getValue();
                            if ("CVE".equals(key)) {
                                for (String cve : value) {
                                    Vulnerability vuln = engine.getDatabase().getVulnerability(StringUtils.trim(cve));
                                    if (vuln == null) {
                                        /* The CVE does not exist in the database and is likely in a
                                         * reserved state. Create a new one without adding it to the
                                         * database and populate it as best as possible. */
                                        vuln = new Vulnerability();
                                        vuln.setName(cve);
                                        vuln.setUnscoredSeverity(jsVuln.getSeverity());
                                        vuln.setSource(Vulnerability.Source.RETIREJS);
                                    }
                                    for (String info : jsVuln.getInfo()) {
                                        vuln.addReference("info", "info", info);
                                    }
                                    vulns.add(vuln);
                                }
                            } else if ("osvdb".equals(key)) {
                                //todo - convert to map/collect
                                value.stream().forEach((osvdb) -> {
                                    final Vulnerability vuln = new Vulnerability();
                                    vuln.setName(osvdb);
                                    vuln.setSource(Vulnerability.Source.RETIREJS);
                                    vuln.setUnscoredSeverity(jsVuln.getSeverity());
                                    jsVuln.getInfo().forEach((info) -> {
                                        vuln.addReference("info", "info", info);
                                    });
                                    vulns.add(vuln);
                                });
                            }
                            dependency.addVulnerabilities(vulns);
                        }
                        //CSON: NestedForDepth
                    } else {
                        final Vulnerability individualVuln = new Vulnerability();
                        /* ISSUE, BUG, etc are all individual vulnerabilities. The result of this
                         * iteration will be one vulnerability. */
                        for (Map.Entry<String, List<String>> entry : jsVuln.getIdentifiers().entrySet()) {
                            final String key = entry.getKey();
                            final List<String> value = entry.getValue();
                            // CSOFF: NeedBraces
                            if (null != key) {
                                switch (key) {
                                    case "issue":
                                        individualVuln.setName(libraryResult.getLibrary().getName() + " issue: " + value.get(0));
                                        individualVuln.addReference(key, key, value.get(0));
                                        break;
                                    case "bug":
                                        individualVuln.setName(libraryResult.getLibrary().getName() + " bug: " + value.get(0));
                                        individualVuln.addReference(key, key, value.get(0));
                                        break;
                                    case "summary":
                                        if (null == individualVuln.getName()) {
                                            individualVuln.setName(value.get(0));
                                        }
                                        individualVuln.setDescription(value.get(0));
                                        break;
                                    case "release":
                                        individualVuln.addReference(key, key, value.get(0));
                                        break;
                                    default:
                                        break;
                                }
                            }
                            // CSON: NeedBraces
                            individualVuln.setSource(Vulnerability.Source.RETIREJS);
                            individualVuln.setUnscoredSeverity(jsVuln.getSeverity());
                            for (String info : jsVuln.getInfo()) {
                                individualVuln.addReference("info", "info", info);
                            }
                        }
                        dependency.addVulnerability(individualVuln);
                    }
                }
            } else if (getSettings().getBoolean(Settings.KEYS.ANALYZER_RETIREJS_FILTER_NON_VULNERABLE, false)) {
                engine.removeDependency(dependency);
            }
        } catch (IOException | DatabaseException e) {
            throw new AnalysisException(e);
        }
    }
1194369
@Override
    public StoreDataflow analyze(IAnalysisCache analysisCache, MethodDescriptor descriptor) throws CheckedAnalysisException {
        MethodGen methodGen = getMethodGen(analysisCache, descriptor);
        if (methodGen == null) {
            return null;
        }
        StoreAnalysis analysis = new StoreAnalysis(getDepthFirstSearch(analysisCache, descriptor), getConstantPoolGen(
                analysisCache, descriptor.getClassDescriptor()));
        StoreDataflow dataflow = new StoreDataflow(getCFG(analysisCache, descriptor), analysis);
        dataflow.execute();
        return dataflow;
    }
1230148
@NotNull
    public static <Key extends Serializable, ValueIn, MapOutput extends Serializable, ReducerResult extends Serializable> ReducerResult compute(@NotNull AnalyticsContext analyticsContext,
                                                                                                                                                @NotNull MapReduceTask<Key, ValueIn, MapOutput, ReducerResult> mapReduceTask) {

        Timer timer = MetricsFactory.getMetrics().getTimer("analytics.compute");

        ClusterGroup clusterGroup = analyticsContext.getIgnite()
                                                    .cluster()
                                                    .forServers();


        ReducerResult execute = analyticsContext.getIgnite()
                                                .compute(clusterGroup)
                                                .execute(mapReduceTask, null);

        timer.stop();

        LOGGER.debug("Complete compute operation in {}", timer);

        return execute;
    }
336105
def finish_job(client, cleanup_job, job_completed_normally, client_outputs, pulsar_outputs):
    
    collection_failure_exceptions = []
    if job_completed_normally:
        output_collector = ClientOutputCollector(client)
        action_mapper = FileActionMapper(client)
        results_stager = ResultsCollector(output_collector, action_mapper, client_outputs, pulsar_outputs)
        collection_failure_exceptions = results_stager.collect()
    _clean(collection_failure_exceptions, cleanup_job, client)
    return collection_failure_exceptions