Home | History | Annotate | Download | only in annotationprocessor
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.databinding.annotationprocessor;
     18 
     19 import android.databinding.BindingBuildInfo;
     20 import android.databinding.tool.processing.ScopedException;
     21 import android.databinding.tool.util.L;
     22 import android.databinding.tool.util.Preconditions;
     23 
     24 import java.lang.annotation.Annotation;
     25 
     26 import javax.annotation.processing.RoundEnvironment;
     27 import javax.lang.model.element.Element;
     28 
     29 public class BuildInfoUtil {
     30     private static BindingBuildInfo sCached;
     31     public static BindingBuildInfo load(RoundEnvironment roundEnvironment) {
     32         if (sCached == null) {
     33             sCached = extractNotNull(roundEnvironment, BindingBuildInfo.class);
     34             if (sCached != null) {
     35                 L.setDebugLog(sCached.enableDebugLogs());
     36                 ScopedException.encodeOutput(sCached.printEncodedError());
     37             }
     38         }
     39         return sCached;
     40     }
     41 
     42     private static <T extends Annotation> T extractNotNull(RoundEnvironment roundEnv,
     43             Class<T> annotationClass) {
     44         T result = null;
     45         for (Element element : roundEnv.getElementsAnnotatedWith(annotationClass)) {
     46             final T info = element.getAnnotation(annotationClass);
     47             if (info == null) {
     48                 continue; // It gets confused between BindingAppInfo and BinderBundle
     49             }
     50             Preconditions.check(result == null, "Should have only one %s",
     51                     annotationClass.getCanonicalName());
     52             result = info;
     53         }
     54         return result;
     55     }
     56 }
     57