dependencies {
compile 'com.android.support:support-annotations:22.2.0'
}
public static final int COLOR_RED = 0;
public static final int COLOR_GREEN = 1;
public static final int COLOR_YELLOW = 2;
public void setColor(int color) {
//some code here
}
//调用
setColor(COLOR_RED)
// ColorEnum.java
public enum ColorEmun {
RED,
GREEN,
YELLOW
}
public void setColorEnum(ColorEmun colorEnum) {
//some code here
}
setColorEnum(ColorEmun.GREEN);
public class Colors {
@IntDef({RED, GREEN, YELLOW})
@Retention(RetentionPolicy.SOURCE)
public @interface LightColors{}
public static final int RED = 0;
public static final int GREEN = 1;
public static final int YELLOW = 2;
}
@Nullable
private String obtainReferrerFromIntent(@NonNull Intent intent) {
return intent.getStringExtra("apps_referrer");
}
setReferrer(null);//提示警告
//不提示警告
String referrer = getIntent().getStringExtra("apps_referrer");
setReferrer(referrer);
//提示警告
String referrer = getIntent().getStringExtra("apps_referrer");
if (referrer == null) {
setReferrer(referrer);
}
private void setReferrer(@NonNull String referrer) {
//some code here
}
float currentProgress;
public void setCurrentProgress(@FloatRange(from=0.0f, to=1.0f) float progress) {
currentProgress = progress;
}
setCurrentProgress(11);
Value must be >=0.0 and <= 1.0(was 11)
private void setKey(@Size(6) String key) {
}
private void setData(@Size(max = 1) String[] data) {
}
setData(new String[]{"b", "a"});//error occurs
private void setItemData(@Size(multiple = 3) String[] data) {
}
@RequiresPermission(Manifest.permission.SET_WALLPAPER)
public void changeWallpaper(Bitmap bitmap) throws IOException {
}
public String getStringById(int stringResId) {
return getResources().getString(stringResId);
}
getStringById(R.mipmap.ic_launcher)
public String getStringById(@StringRes int stringResId) {
return getResources().getString(stringResId);
}
public void setTextColor(int color) {
mTextColor = ColorStateList.valueOf(color);
updateTextColors();
}
myTextView.setTextColor(R.color.colorAccent);
public void setTextColor(@ColorInt int color) {
mTextColor = ColorStateList.valueOf(color);
updateTextColors();
}
@CheckResult
public String trim(String s) {
return s.trim();
}
new AsyncTask<Void, Void, Void>() {
//doInBackground is already annotated with @WorkerThread
@Override
protected Void doInBackground(Void... params) {
return null;
updateViews();//error
}
};
@UiThread
public void updateViews() {
Log.i(LOGTAG, "updateViews ThreadInfo=" + Thread.currentThread());
}
new Thread(){
@Override
public void run() {
super.run();
updateViews();
}
}.start();
@Keep
public static int getBitmapWidth(Bitmap bitmap) {
return bitmap.getWidth();
}
class ExampleActivity extends Activity {
@BindView(R.id.user) EditText username;
@BindView(R.id.pass) EditText password;
@BindString(R.string.login_error) String loginErrorMessage;
@OnClick(R.id.submit) void submit() {
// TODO call server...
}
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
// TODO Use fields...
}
}
public class MainActivity extends AppCompatActivity {
@BindView(R.id.myTextView)
TextView myTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
}
public static Unbinder bind(@NonNull Activity target) {
return getViewBinder(target).bind(Finder.ACTIVITY, target, target);
}
@NonNull @CheckResult @UiThread
static ViewBinder<Object> getViewBinder(@NonNull Object target) {
Class<?> targetClass = target.getClass();
if (debug) Log.d(TAG, "Looking up view binder for " + targetClass.getName());
return findViewBinderForClass(targetClass);
}
@NonNull @CheckResult @UiThread
private static ViewBinder<Object> findViewBinderForClass(Class<?> cls) {
//如果内存集合BINDERS中包含,则不再查找
ViewBinder<Object> viewBinder = BINDERS.get(cls);
if (viewBinder != null) {
if (debug) Log.d(TAG, "HIT: Cached in view binder map.");
return viewBinder;
}
String clsName = cls.getName();
if (clsName.startsWith("android.") || clsName.startsWith("java.")) {
if (debug) Log.d(TAG, "MISS: Reached framework class. Abandoning search.");
return NOP_VIEW_BINDER;
}
//noinspection TryWithIdenticalCatches Resolves to API 19+ only type.
try {
//使用反射创建实例
Class<?> viewBindingClass = Class.forName(clsName + "_ViewBinder");
//noinspection unchecked
viewBinder = (ViewBinder<Object>) viewBindingClass.newInstance();
if (debug) Log.d(TAG, "HIT: Loaded view binder class.");
} catch (ClassNotFoundException e) {
//如果没有找到,对父类进行查找
if (debug) Log.d(TAG, "Not found. Trying superclass " + cls.getSuperclass().getName());
viewBinder = findViewBinderForClass(cls.getSuperclass());
} catch (InstantiationException e) {
throw new RuntimeException("Unable to create view binder for " + clsName, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Unable to create view binder for " + clsName, e);
}
//加入内存集合,便于后续的查找
BINDERS.put(cls, viewBinder);
return viewBinder;
}
➜ androidannotationsample javap -c MainActivity_ViewBinder
Warning: Binary file MainActivity_ViewBinder contains com.example.admin.androidannotationsample.MainActivity_ViewBinder
Compiled from "MainActivity_ViewBinder.java"
public final class com.example.admin.androidannotationsample.MainActivity_ViewBinder implements butterknife.internal.ViewBinder<com.example.admin.androidannotationsample.MainActivity> {
public com.example.admin.androidannotationsample.MainActivity_ViewBinder();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public butterknife.Unbinder bind(butterknife.internal.Finder, com.example.admin.androidannotationsample.MainActivity, java.lang.Object);
Code:
0: new #2 // class com/example/admin/androidannotationsample/MainActivity_ViewBinding
3: dup
4: aload_2
5: aload_1
6: aload_3 // 创建ViewBinding实例
7: invokespecial #3 // Method com/example/admin/androidannotationsample/MainActivity_ViewBinding."<init>":(Lcom/example/admin/androidannotationsample/MainActivity;Lbutterknife/internal/Finder;Ljava/lang/Object;)V
10: areturn
public butterknife.Unbinder bind(butterknife.internal.Finder, java.lang.Object, java.lang.Object);
Code:
0: aload_0
1: aload_1
2: aload_2
3: checkcast #4 // class com/example/admin/androidannotationsample/MainActivity
6: aload_3 //调用上面的重载方法
7: invokevirtual #5 // Method bind:(Lbutterknife/internal/Finder;Lcom/example/admin/androidannotationsample/MainActivity;Ljava/lang/Object;)Lbutterknife/Unbinder;
10: areturn
}
➜ androidannotationsample javap -c MainActivity_ViewBinding
Warning: Binary file MainActivity_ViewBinding contains com.example.admin.androidannotationsample.MainActivity_ViewBinding
Compiled from "MainActivity_ViewBinding.java"
public class com.example.admin.androidannotationsample.MainActivity_ViewBinding<T extends com.example.admin.androidannotationsample.MainActivity> implements butterknife.Unbinder {
protected T target;
public com.example.admin.androidannotationsample.MainActivity_ViewBinding(T, butterknife.internal.Finder, java.lang.Object);
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: aload_1
6: putfield #2 // Field target:Lcom/example/admin/androidannotationsample/MainActivity;
9: aload_1
10: aload_2
11: aload_3 //调用Finder.findRequireViewAsType找到View,并进行类型转换,并复制给MainActivity中对一个的变量
12: ldc #4 // int 2131427412
14: ldc #5 // String field 'myTextView'
16: ldc #6 // class android/widget/TextView
// 内部实际调用了findViewById
18: invokevirtual #7 // Method butterknife/internal/Finder.findRequiredViewAsType:(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/Class;)Ljava/lang/Object;
21: checkcast #6 // class android/widget/TextView
24: putfield #8 // Field com/example/admin/androidannotationsample/MainActivity.myTextView:Landroid/widget/TextView;
27: return
public void unbind();
Code:
0: aload_0
1: getfield #2 // Field target:Lcom/example/admin/androidannotationsample/MainActivity;
4: astore_1
5: aload_1
6: ifnonnull 19
9: new #9 // class java/lang/IllegalStateException
12: dup
13: ldc #10 // String Bindings already cleared.
15: invokespecial #11 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
18: athrow
19: aload_1
20: aconst_null // 解除绑定,设置对应的变量为null
21: putfield #8 // Field com/example/admin/androidannotationsample/MainActivity.myTextView:Landroid/widget/TextView;
24: aload_0
25: aconst_null
26: putfield #2 // Field target:Lcom/example/admin/androidannotationsample/MainActivity;
29: return
}
package butterknife.internal;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.IdRes;
import android.view.View;
@SuppressWarnings("UnusedDeclaration") // Used by generated code.
public enum Finder {
VIEW {
@Override public View findOptionalView(Object source, @IdRes int id) {
return ((View) source).findViewById(id);
}
@Override public Context getContext(Object source) {
return ((View) source).getContext();
}
@Override protected String getResourceEntryName(Object source, @IdRes int id) {
final View view = (View) source;
// In edit mode, getResourceEntryName() is unsupported due to use of BridgeResources
if (view.isInEditMode()) {
return "<unavailable while editing>";
}
return super.getResourceEntryName(source, id);
}
},
ACTIVITY {
@Override public View findOptionalView(Object source, @IdRes int id) {
return ((Activity) source).findViewById(id);
}
@Override public Context getContext(Object source) {
return (Activity) source;
}
},
DIALOG {
@Override public View findOptionalView(Object source, @IdRes int id) {
return ((Dialog) source).findViewById(id);
}
@Override public Context getContext(Object source) {
return ((Dialog) source).getContext();
}
};
//查找对应的Finder,如上面的ACTIVITY, DIALOG, VIEW
public abstract View findOptionalView(Object source, @IdRes int id);
public final <T> T findOptionalViewAsType(Object source, @IdRes int id, String who,
Class<T> cls) {
View view = findOptionalView(source, id);
return castView(view, id, who, cls);
}
public final View findRequiredView(Object source, @IdRes int id, String who) {
View view = findOptionalView(source, id);
if (view != null) {
return view;
}
String name = getResourceEntryName(source, id);
throw new IllegalStateException("Required view '"
+ name
+ "' with ID "
+ id
+ " for "
+ who
+ " was not found. If this view is optional add '@Nullable' (fields) or '@Optional'"
+ " (methods) annotation.");
}
//来自ViewBinding的调用
public final <T> T findRequiredViewAsType(Object source, @IdRes int id, String who,
Class<T> cls) {
View view = findRequiredView(source, id, who);
return castView(view, id, who, cls);
}
public final <T> T castView(View view, @IdRes int id, String who, Class<T> cls) {
try {
return cls.cast(view);
} catch (ClassCastException e) {
String name = getResourceEntryName(view, id);
throw new IllegalStateException("View '"
+ name
+ "' with ID "
+ id
+ " for "
+ who
+ " was of the wrong type. See cause for more info.", e);
}
}
@SuppressWarnings("unchecked") // That's the point.
public final <T> T castParam(Object value, String from, int fromPos, String to, int toPos) {
try {
return (T) value;
} catch (ClassCastException e) {
throw new IllegalStateException("Parameter #"
+ (fromPos + 1)
+ " of method '"
+ from
+ "' was of the wrong type for parameter #"
+ (toPos + 1)
+ " of method '"
+ to
+ "'. See cause for more info.", e);
}
}
protected String getResourceEntryName(Object source, @IdRes int id) {
return getContext(source).getResources().getResourceEntryName(id);
}
public abstract Context getContext(Object source);
}
public class EventBusTest {
private static final String LOGTAG = "EventBusTest";
Bus mBus = new Bus();
public void test() {
mBus.register(this);
}
class NetworkChangedEvent {
}
@Produce
public NetworkChangedEvent sendNetworkChangedEvent() {
return new NetworkChangedEvent();
}
@Subscribe
public void onNetworkChanged(NetworkChangedEvent event) {
Log.i(LOGTAG, "onNetworkChanged event=" + event);
}
}
public void register(Object object) {
if (object == null) {
throw new NullPointerException("Object to register must not be null.");
}
enforcer.enforce(this);
//查找object中的Subscriber
Map<Class<?>, Set<EventHandler>> foundHandlersMap = handlerFinder.findAllSubscribers(object);
for (Class<?> type : foundHandlersMap.keySet()) {
Set<EventHandler> handlers = handlersByType.get(type);
if (handlers == null) {
//concurrent put if absent
Set<EventHandler> handlersCreation = new CopyOnWriteArraySet<EventHandler>();
handlers = handlersByType.putIfAbsent(type, handlersCreation);
if (handlers == null) {
handlers = handlersCreation;
}
}
final Set<EventHandler> foundHandlers = foundHandlersMap.get(type);
if (!handlers.addAll(foundHandlers)) {
throw new IllegalArgumentException("Object already registered.");
}
}
for (Map.Entry<Class<?>, Set<EventHandler>> entry : foundHandlersMap.entrySet()) {
Class<?> type = entry.getKey();
EventProducer producer = producersByType.get(type);
if (producer != null && producer.isValid()) {
Set<EventHandler> foundHandlers = entry.getValue();
for (EventHandler foundHandler : foundHandlers) {
if (!producer.isValid()) {
break;
}
if (foundHandler.isValid()) {
dispatchProducerResultToHandler(foundHandler, producer);
}
}
}
}
}
interface HandlerFinder {
Map<Class<?>, EventProducer> findAllProducers(Object listener);
Map<Class<?>, Set<EventHandler>> findAllSubscribers(Object listener);
//Otto注解查找器
HandlerFinder ANNOTATED = new HandlerFinder() {
@Override
public Map<Class<?>, EventProducer> findAllProducers(Object listener) {
return AnnotatedHandlerFinder.findAllProducers(listener);
}
@Override
public Map<Class<?>, Set<EventHandler>> findAllSubscribers(Object listener) {
return AnnotatedHandlerFinder.findAllSubscribers(listener);
}
};
/** This implementation finds all methods marked with a {@link Subscribe} annotation. */
static Map<Class<?>, Set<EventHandler>> findAllSubscribers(Object listener) {
Class<?> listenerClass = listener.getClass();
Map<Class<?>, Set<EventHandler>> handlersInMethod = new HashMap<Class<?>, Set<EventHandler>>();
Map<Class<?>, Set<Method>> methods = SUBSCRIBERS_CACHE.get(listenerClass);
if (null == methods) {
methods = new HashMap<Class<?>, Set<Method>>();
loadAnnotatedSubscriberMethods(listenerClass, methods);
}
if (!methods.isEmpty()) {
for (Map.Entry<Class<?>, Set<Method>> e : methods.entrySet()) {
Set<EventHandler> handlers = new HashSet<EventHandler>();
for (Method m : e.getValue()) {
handlers.add(new EventHandler(listener, m));
}
handlersInMethod.put(e.getKey(), handlers);
}
}
return handlersInMethod;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有