/* * Copyright (C) 2015 The Android Open Source Project * * 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 android.databinding.tool.reflection; import android.databinding.tool.reflection.annotation.AnnotationAnalyzer; import android.databinding.tool.util.L; import android.databinding.tool.util.Preconditions; import java.util.Map; import javax.annotation.processing.ProcessingEnvironment; /** * This is the base class for several implementations of something that * acts like a ClassLoader. Different implementations work with the Annotation * Processor, ClassLoader, and an Android Studio plugin. */ public abstract class ModelAnalyzer { public static final String[] LIST_CLASS_NAMES = { "java.util.List", "android.util.SparseArray", "android.util.SparseBooleanArray", "android.util.SparseIntArray", "android.util.SparseLongArray", "android.util.LongSparseArray", "android.support.v4.util.LongSparseArray", }; public static final String MAP_CLASS_NAME = "java.util.Map"; public static final String STRING_CLASS_NAME = "java.lang.String"; public static final String OBJECT_CLASS_NAME = "java.lang.Object"; public static final String OBSERVABLE_CLASS_NAME = "android.databinding.Observable"; public static final String OBSERVABLE_LIST_CLASS_NAME = "android.databinding.ObservableList"; public static final String OBSERVABLE_MAP_CLASS_NAME = "android.databinding.ObservableMap"; public static final String[] OBSERVABLE_FIELDS = { "android.databinding.ObservableBoolean", "android.databinding.ObservableByte", "android.databinding.ObservableChar", "android.databinding.ObservableShort", "android.databinding.ObservableInt", "android.databinding.ObservableLong", "android.databinding.ObservableFloat", "android.databinding.ObservableDouble", "android.databinding.ObservableField", "android.databinding.ObservableParcelable", }; public static final String VIEW_DATA_BINDING = "android.databinding.ViewDataBinding"; public static final String VIEW_STUB_CLASS_NAME = "android.view.ViewStub"; private ModelClass[] mListTypes; private ModelClass mMapType; private ModelClass mStringType; private ModelClass mObjectType; private ModelClass mObservableType; private ModelClass mObservableListType; private ModelClass mObservableMapType; private ModelClass[] mObservableFieldTypes; private ModelClass mViewBindingType; private ModelClass mViewStubType; private static ModelAnalyzer sAnalyzer; protected void setInstance(ModelAnalyzer analyzer) { sAnalyzer = analyzer; } public ModelClass findCommonParentOf(ModelClass modelClass1, ModelClass modelClass2) { ModelClass curr = modelClass1; while (curr != null && !curr.isAssignableFrom(modelClass2)) { curr = curr.getSuperclass(); } if (curr == null) { if (modelClass1.isObject() && modelClass2.isInterface()) { return modelClass1; } else if (modelClass2.isObject() && modelClass1.isInterface()) { return modelClass2; } ModelClass primitive1 = modelClass1.unbox(); ModelClass primitive2 = modelClass2.unbox(); if (!modelClass1.equals(primitive1) || !modelClass2.equals(primitive2)) { return findCommonParentOf(primitive1, primitive2); } } Preconditions.checkNotNull(curr, "must be able to find a common parent for " + modelClass1 + " and " + modelClass2); return curr; } public abstract ModelClass loadPrimitive(String className); public static ModelAnalyzer getInstance() { return sAnalyzer; } public static void setProcessingEnvironment(ProcessingEnvironment processingEnvironment) { if (sAnalyzer != null) { throw new IllegalStateException("processing env is already created, you cannot " + "change class loader after that"); } L.d("setting processing env to %s", processingEnvironment); AnnotationAnalyzer annotationAnalyzer = new AnnotationAnalyzer(processingEnvironment); sAnalyzer = annotationAnalyzer; } /** * Takes a raw className (potentially w/ generics and arrays) and expands definitions using * the import statements. *
* For instance, this allows user to define variables
*