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