/*
 * Copyright (C) 2014 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dagger.internal.codegen;

import dagger.MapKey;
import java.util.Set;
import javax.annotation.processing.Messager;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

/**
 * The annotation processor responsible for validating the mapKey annotation and auto-generate
 * implementation of annotations marked with &#064MapKey where necessary.
 *
 * @author Chenying Hou
 * @since 2.0
 */
public class MapKeyProcessingStep implements ProcessingStep {
  private final Messager messager;
  private final MapKeyValidator mapKeyValidator;
  private final MapKeyGenerator mapKeyGenerator;

  MapKeyProcessingStep(Messager messager, MapKeyValidator mapKeyValidator, MapKeyGenerator mapKeyGenerator) {
    this.messager = messager;
    this.mapKeyValidator = mapKeyValidator;
    this.mapKeyGenerator = mapKeyGenerator;
  }

  @Override
  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (!roundEnv.getElementsAnnotatedWith(MapKey.class).isEmpty()) {
      Set<? extends Element> mapKeyAnnotateds = roundEnv.getElementsAnnotatedWith(MapKey.class);
      // for each element annotated with @mapKey, validate it and auto generate key creator file for
      // any unwrapped key
      for (Element element : mapKeyAnnotateds) {
        ValidationReport<Element> mapKeyReport = mapKeyValidator.validate(element);
        mapKeyReport.printMessagesTo(messager);

        if (mapKeyReport.isClean()) {
          MapKey mapkey = element.getAnnotation(MapKey.class);
          if (!mapkey.unwrapValue()) {
            try {
              mapKeyGenerator.generate(element);
            } catch (SourceFileGenerationException e) {
              e.printMessageTo(messager);
            }
          }
        }
      }
    }
    return false;
  }
}

@@ -623,16 +623,69 @@
 degen;%0A%0A
+import com.google.auto.common.SuperficialValidation;%0A
 import d
@@ -1732,105 +1732,8 @@
 ) %7B%0A
-      Set%3C? extends Element%3E mapKeyAnnotateds = roundEnv.getElementsAnnotatedWith(MapKey.class);%0A
     
@@ -1889,28 +1889,123 @@
 t : 
-mapKeyAnnotateds) %7B%0A
+roundEnv.getElementsAnnotatedWith(MapKey.class)) %7B%0A        if (SuperficialValidation.validateElement(element)) %7B%0A  
     
@@ -2088,24 +2088,26 @@
 t);%0A        
+  
 mapKeyReport
@@ -2139,24 +2139,26 @@
 );%0A%0A        
+ 
+ 
 if (mapKeyRe
@@ -2171,24 +2171,26 @@
 sClean()) %7B%0A
+  
           Ma
@@ -2246,24 +2246,26 @@
 ;%0A          
+ 
+ 
 if (!mapkey.
@@ -2293,16 +2293,18 @@
         
+  
 try %7B%0A  
@@ -2311,24 +2311,26 @@
             
+ 
+ 
 mapKeyGenera
@@ -2344,32 +2344,34 @@
 erate(element);%0A
+  
             %7D ca
@@ -2424,16 +2424,18 @@
         
+  
 e.printM
@@ -2454,16 +2454,32 @@
 sager);%0A
+              %7D%0A
         

Apply superficial element validation to the MapKey processor's targets. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=75167440