Verifier.java

  1. /*
  2.  * Copyright © 2017, Salesforce.com, Inc
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  *     * Redistributions of source code must retain the above copyright
  8.  *       notice, this list of conditions and the following disclaimer.
  9.  *     * Redistributions in binary form must reproduce the above copyright
  10.  *       notice, this list of conditions and the following disclaimer in the
  11.  *       documentation and/or other materials provided with the distribution.
  12.  *     * Neither the name of the <organization> nor the
  13.  *       names of its contributors may be used to endorse or promote products
  14.  *       derived from this software without specific prior written permission.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19.  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  20.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26.  */
  27. package com.salesforce.apt.graph.processing;

  28. import java.util.Collection;
  29. import java.util.Queue;
  30. import java.util.Set;
  31. import java.util.concurrent.ConcurrentLinkedQueue;
  32. import java.util.function.Consumer;

  33. import com.salesforce.apt.graph.model.DefinitionModel;
  34. import com.salesforce.apt.graph.model.errors.ErrorModel;
  35. import com.salesforce.apt.graph.model.storage.DefinitionModelStore;
  36. import com.salesforce.apt.graph.types.AssignabilityUtils;

  37. public class Verifier {
  38.  
  39.   public static class ErrorListener implements Consumer<ErrorModel> {
  40.    
  41.     private ConcurrentLinkedQueue<ErrorModel> errors = new ConcurrentLinkedQueue<>();
  42.    
  43.     private volatile boolean locked = false;
  44.    
  45.     @Override
  46.     public void accept(ErrorModel t) {
  47.       if (locked) {
  48.         throw new IllegalStateException("This error listener's contents have already been fetched, may not add new errors");
  49.       }
  50.       errors.add(t);
  51.     }

  52.     public boolean hasError() {
  53.       return !errors.isEmpty();
  54.     }
  55.    
  56.     public Queue<ErrorModel> getErrors() {
  57.       locked = true;
  58.       return errors;
  59.     }
  60.    
  61.   }
  62.  
  63.   /**
  64.    * Verified the definition model.
  65.    *
  66.    * @param definitions found definitions in this processing
  67.    * @param assignabilityUtils used to determine if the types are assignable (used to verify injection points).
  68.    * @param store already processed definitions stored as files.
  69.    * @return any errors found during the verification.  Note may not include all possible errors, as short
  70.    *     circuit logic is necessary in some places.
  71.    */
  72.   public Queue<ErrorModel> verifyDefinitions(
  73.       Collection<DefinitionModel> definitions,
  74.       AssignabilityUtils assignabilityUtils,
  75.       DefinitionModelStore store) {
  76.     ErrorListener el = new ErrorListener();
  77.     verifyDefinitions(definitions, store, el, assignabilityUtils);
  78.     return el.getErrors();
  79.   }
  80.  
  81.   /**
  82.    * Verified the definition model.  Registers all errors with the passed in {@link ErrorListener}.
  83.    *
  84.    * @param definitions found definitions in this processing
  85.    * @param supplier already processed definitions stored as files.
  86.    * @return any errors found during the verification.  Note may not include all possible errors, as short
  87.    *     circuit logic is necessary in some places.
  88.    */
  89.   private void verifyDefinitions(
  90.       Collection<DefinitionModel> definitions,
  91.       DefinitionModelStore store,
  92.       ErrorListener el,
  93.       AssignabilityUtils assignabilityUtils) {

  94.     /*
  95.      * Takes disjoint definitions that have recently been computed from scanning files and compares them against
  96.      * links them to each other in to a directed graph of definitions to dependencies.   The supplier will load
  97.      * pre-computed definitions from the file system.  No further processing is needed on the pre-computed and loaded
  98.      * DefinitionModels.
  99.      *
  100.      * This is stage 2 of the DefinitionModel's life cycle
  101.      */
  102.     new DefinitionJoiner().joinDefinitions(definitions, store, el);

  103.     //short circuit.
  104.     if (el.hasError()) {
  105.       return;
  106.     }
  107.    
  108.    
  109.    
  110.     /*
  111.      * Verifies that the directed graph is actually a directed acyclic graph.
  112.      *
  113.      * Note that definition heads may not include the full graph of Definitions, pre-computed DefinitionModels will
  114.      * be loaded from files, and will truncate the graph at many locations.
  115.      *
  116.      * This method doesn't mutate state in any way, aside from accessing data in the definition graph that will cause
  117.      * the will cause the lockedDefintionsMerged flag to be set to true in all definitions passed in, and the
  118.      * potential generation of errors
  119.      */
  120.     Set<DefinitionModel> definitionHeads = new DefinitionGraphInpector().inspectDefinitionGraph(definitions, el);

  121.     //short circuit.
  122.     if (el.hasError()) {
  123.       return;
  124.     }
  125.  
  126.     /*
  127.      * Check the expected entities are listed and types are correct for supplied object by usage.
  128.      */
  129.     new DefinitionContentInspector().inspectDefinitionGraph(definitionHeads, el, assignabilityUtils, store);

  130.   }

  131. }