/*
* Copyright 2016 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 com.google.android.libraries.remixer.annotation;
import com.google.android.libraries.remixer.Remixer;
import java.util.Locale;
/**
* Used to bind activities to their implicitly generated variables.
*/
public class RemixerBinder {
/**
* Binds an activity's remixer to its generated variables.
*
* @throws RemixerBindingException When there is an issue instantiating the Binder class
*/
@SuppressWarnings("ClassNewInstance")
public static <T> void bind(T activity) {
if (Remixer.getRegisteredDataTypes().isEmpty()) {
throw new IllegalStateException(
"There are no registered data types for remixer. This indicates that you have not "
+ " initialized Remixer at all and it will fail in runtime. Please run "
+ "RemixerInitialization.initRemixer in your application class. See the Remixer README "
+ "for detailed instructions. ");
}
try {
Class<?> bindingClass =
Class.forName(activity.getClass().getCanonicalName() + "_RemixerBinder");
@SuppressWarnings("unchecked")
Binder<T> binder = (Binder<T>) bindingClass.newInstance();
binder.bindInstance(activity);
} catch (ClassNotFoundException ex) {
throw new RemixerBindingException(
"Remixer binder class can not be found or initialized for "
+ activity.getClass().toString(),
ex);
} catch (InstantiationException ex) {
throw new RemixerBindingException(
"Remixer binder class can not instantiated for "
+ activity.getClass().toString(),
ex);
} catch (IllegalAccessException ex) {
throw new RemixerBindingException(
"Remixer binder class can not be instantiated for "
+ activity.getClass().toString(),
ex);
}
}
/**
* Interface that autogenerated binders implement.
*/
public interface Binder<T> {
/**
* Bind an activity's remixer instance to its generated variables.
*/
void bindInstance(T activity);
}
}