本文整理汇总了Java中com.nick.scalpel.core.utils.ReflectionUtils类的典型用法代码示例。如果您正苦于以下问题:Java ReflectionUtils类的具体用法?Java ReflectionUtils怎么用?Java ReflectionUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ReflectionUtils类属于com.nick.scalpel.core.utils包,在下文中一共展示了ReflectionUtils类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: wire
import com.nick.scalpel.core.utils.ReflectionUtils; //导入依赖的package包/类
@Override
public void wire(Object o) {
RequireRoot autoRequireRoot = o.getClass().getAnnotation(RequireRoot.class);
RequireRoot.Mode mode = autoRequireRoot.mode();
switch (mode) {
case Sync:
logV("Requesting root sync.");
mRequester.requestRoot();
break;
case Async:
RequireRoot.Callback callback;
String callbackStr = autoRequireRoot.callback();
Preconditions.checkState(!TextUtils.isEmpty(callbackStr), "Should specify a callback when using Async mode.");
switch (callbackStr) {
case "this":
Preconditions.checkState(o instanceof RequireRoot.Callback, "Invalid callback reference.");
callback = (RequireRoot.Callback) o;
break;
default:
Field field = ReflectionUtils.findField(o, callbackStr);
Preconditions.checkNotNull(field, "No field named:" + field);
Object fieldObject = ReflectionUtils.getField(field, o);
Preconditions.checkNotNull(fieldObject, "Null field:" + field);
Preconditions.checkState(fieldObject instanceof RequireRoot.Callback, "Invalid callback reference.");
callback = (RequireRoot.Callback) fieldObject;
break;
}
logV("Requesting root async, callback:" + callback);
SharedExecutor.get().execute(new AsyncRootRequester(callback));
}
}
示例2: getCallback
import com.nick.scalpel.core.utils.ReflectionUtils; //导入依赖的package包/类
@Nullable
@Override
protected Handler.Callback getCallback(Object object, Field field) {
MainThreadHandler handler = field.getAnnotation(MainThreadHandler.class);
String callbackStr = handler.callback();
if (TextUtils.isEmpty(callbackStr)) return null;
Object fieldObj;
switch (callbackStr) {
case ThisThatNull.THIS:
fieldObj = object;
break;
default:
Field callbackField = ReflectionUtils.findField(object, callbackStr);
Preconditions.checkNotNull(callbackField);
ReflectionUtils.makeAccessible(callbackField);
fieldObj = ReflectionUtils.getField(callbackField, object);
Preconditions.checkNotNull(fieldObj);
break;
}
boolean isCallback = fieldObj instanceof Handler.Callback;
Preconditions.checkState(isCallback);
return (Handler.Callback) fieldObj;
}
示例3: getCallback
import com.nick.scalpel.core.utils.ReflectionUtils; //导入依赖的package包/类
@Nullable
@Override
protected Handler.Callback getCallback(Object object, Field field) {
WorkerThreadHandler handler = field.getAnnotation(WorkerThreadHandler.class);
String callbackStr = handler.callback();
if (TextUtils.isEmpty(callbackStr)) return null;
Object fieldObj;
switch (callbackStr) {
case ThisThatNull.THIS:
fieldObj = object;
break;
default:
Field callbackField = ReflectionUtils.findField(object, callbackStr);
Preconditions.checkNotNull(callbackField);
ReflectionUtils.makeAccessible(callbackField);
fieldObj = ReflectionUtils.getField(callbackField, object);
Preconditions.checkNotNull(fieldObj);
break;
}
boolean isCallback = fieldObj instanceof Handler.Callback;
Preconditions.checkState(isCallback);
return (Handler.Callback) fieldObj;
}
示例4: wire
import com.nick.scalpel.core.utils.ReflectionUtils; //导入依赖的package包/类
@Override
public void wire(Activity activity, Field field) {
ReflectionUtils.makeAccessible(field);
Object fieldObject = ReflectionUtils.getField(field, activity);
if (fieldObject == null) {
mAutoFinder.wire(activity, field);
}
autoWire(activity, field);
}
示例5: wire
import com.nick.scalpel.core.utils.ReflectionUtils; //导入依赖的package包/类
@Override
public final void wire(Context context, Object object, Field field) {
makeAccessible(field);
Object fieldObject = getField(field, object);
if (fieldObject != null && !ReflectionUtils.isBaseDataType(field.getType())) return;
int id = getIdRes(field);
if (id <= 0) invalidArg();
wireFromId(id, context, object, field);
}
示例6: wire
import com.nick.scalpel.core.utils.ReflectionUtils; //导入依赖的package包/类
@Override
public void wire(final Activity activity, final Field field) {
ReflectionUtils.makeAccessible(field);
final String fieldName = field.getName();
boolean registered = mLifeCycleManager.registerActivityLifecycleCallbacks(new LifeCycleCallbackAdapter() {
@Override
public void onActivityDestroyed(Activity destroyed) {
super.onActivityDestroyed(destroyed);
if (destroyed == activity) {
logV("Recycling field: " + fieldName);
Object fieldObj = ReflectionUtils.getField(field, activity);
if (fieldObj == null) return;
try {
if (fieldObj instanceof AutoRecycle.Recyclable) {
((AutoRecycle.Recyclable) fieldObj).recycle();
}
} catch (Exception ignored) {
} finally {
ReflectionUtils.setField(field, activity, null);
mLifeCycleManager.unRegisterActivityLifecycleCallbacks(this);
}
}
}
});
if (!registered) {
logE("Failed to register life cycle callback!");
}
}
示例7: wire
import com.nick.scalpel.core.utils.ReflectionUtils; //导入依赖的package包/类
@Override
public void wire(Context context, Object object, Field field) {
ReflectionUtils.makeAccessible(field);
Object fieldObject = ReflectionUtils.getField(field, object);
if (fieldObject != null) return;
ReflectionUtils.setField(field, object, this.mApp);
}
示例8: autoWire
import com.nick.scalpel.core.utils.ReflectionUtils; //导入依赖的package包/类
private void autoWire(final Object o, Field field) {
logD("Auto wiring: " + field.getName());
Object fieldObjectWired = ReflectionUtils.getField(field, o);
if (fieldObjectWired == null) return;
boolean isView = fieldObjectWired instanceof View;
if (!isView)
throw new IllegalArgumentException("Object " + fieldObjectWired + " is not instance of View.");
View view = (View) fieldObjectWired;
OnTouch onClick = field.getAnnotation(OnTouch.class);
String listener = onClick.listener();
String action = onClick.action();
if (!TextUtils.isEmpty(listener)) {
Field onTouchListenerField = ReflectionUtils.findField(o, listener);
if (onTouchListenerField == null)
throw new NullPointerException("No such listener:" + listener);
ReflectionUtils.makeAccessible(onTouchListenerField);
Object onTouchListenerObj = ReflectionUtils.getField(onTouchListenerField, o);
if (onTouchListenerObj == null)
throw new NullPointerException("Null listener:" + listener);
boolean isListener = onTouchListenerObj instanceof View.OnTouchListener;
if (!isListener)
throw new IllegalArgumentException("Object " + onTouchListenerObj + " is not instance of OnTouchListener.");
View.OnTouchListener onTouchListener = (View.OnTouchListener) onTouchListenerObj;
view.setOnTouchListener(onTouchListener);
logD("OnTouchWirer listener, Auto wired: " + field.getName());
} else if (!TextUtils.isEmpty(action)) {
final String[] args = onClick.args();
Class[] argClz = new Class[args.length];
for (int i = 0; i < args.length; i++) {
argClz[i] = String.class;
}
Method actionMethod = ReflectionUtils.findMethod(o.getClass(), action, argClz);
if (actionMethod == null)
throw new NullPointerException("No such method:" + action + " with args:" + Arrays.toString(argClz));
ReflectionUtils.makeAccessible(actionMethod);
final Method finalActionMethod = actionMethod;
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Object result = ReflectionUtils.invokeMethod(finalActionMethod, o, args);
logD("OnTouchWirer onTouch invoked: " + result);
return true;
}
});
logD("OnTouchWirer actions, Auto wired: " + field.getName());
}
}
示例9: autoWire
import com.nick.scalpel.core.utils.ReflectionUtils; //导入依赖的package包/类
private void autoWire(final Object o, Field field) {
logD("Auto wiring: " + field.getName());
Object fieldObjectWired = ReflectionUtils.getField(field, o);
if (fieldObjectWired == null) return;
boolean isView = fieldObjectWired instanceof View;
if (!isView)
throw new IllegalArgumentException("Object " + fieldObjectWired + " is not instance of View.");
View view = (View) fieldObjectWired;
OnClick onClick = field.getAnnotation(OnClick.class);
String listener = onClick.listener();
String action = onClick.action();
if (!TextUtils.isEmpty(listener)) {
Field onClickListenerField = ReflectionUtils.findField(o, listener);
if (onClickListenerField == null)
throw new NullPointerException("No such listener:" + listener);
ReflectionUtils.makeAccessible(onClickListenerField);
Object onClickListenerObj = ReflectionUtils.getField(onClickListenerField, o);
if (onClickListenerObj == null)
throw new NullPointerException("Null listener:" + listener);
boolean isListener = onClickListenerObj instanceof View.OnClickListener;
if (!isListener)
throw new IllegalArgumentException("Object " + onClickListenerObj + " is not instance of OnClickListener.");
View.OnClickListener onClickListener = (View.OnClickListener) onClickListenerObj;
view.setOnClickListener(onClickListener);
logD("OnClickWirer listener, Auto wired: " + field.getName());
} else if (!TextUtils.isEmpty(action)) {
final String[] args = onClick.args();
Class[] argClz = new Class[args.length];
for (int i = 0; i < args.length; i++) {
argClz[i] = String.class;
}
Method actionMethod = ReflectionUtils.findMethod(o.getClass(), action, argClz);
if (actionMethod == null)
throw new NullPointerException("No such method:" + action + " with args:" + Arrays.toString(argClz));
ReflectionUtils.makeAccessible(actionMethod);
final Method finalActionMethod = actionMethod;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ReflectionUtils.invokeMethod(finalActionMethod, o, args);
}
});
logD("OnClickWirer actions, Auto wired: " + field.getName());
}
}
示例10: wire
import com.nick.scalpel.core.utils.ReflectionUtils; //导入依赖的package包/类
@Override
public void wire(final Context context, final Object object, Field field) {
ReflectionUtils.makeAccessible(field);
Object fieldObject = ReflectionUtils.getField(field, object);
Preconditions.checkNotNull(fieldObject, "Null field:" + field);
boolean isReceive = fieldObject instanceof BroadcastReceiver;
Preconditions.checkState(isReceive, "Not a receiver:" + field);
RegisterReceiver registerReceiver = field.getAnnotation(RegisterReceiver.class);
String[] actions = registerReceiver.actions();
boolean autoUnRegister = registerReceiver.autoUnRegister();
boolean isActivity = object instanceof Activity;
Preconditions.checkState(!autoUnRegister || isActivity, "Auto unregister only work for activities.");
boolean hasAction = actions.length > 0 && !TextUtils.isEmpty(actions[0]);
Preconditions.checkState(hasAction, "Invalid actions:" + Arrays.toString(actions));
final BroadcastReceiver receiver = (BroadcastReceiver) fieldObject;
IntentFilter filter = new IntentFilter();
for (String action : actions) {
filter.addAction(action);
}
context.registerReceiver(receiver, filter);
if (autoUnRegister) {
final String fieldName = field.getName();
boolean registered = mLifeCycleManager.registerActivityLifecycleCallbacks(new LifeCycleCallbackAdapter() {
@Override
public void onActivityDestroyed(Activity activity) {
super.onActivityDestroyed(activity);
if (activity == object) {
logV("UnRegister receiver for: " + fieldName);
context.unregisterReceiver(receiver);
mLifeCycleManager.unRegisterActivityLifecycleCallbacks(this);
}
}
});
if (!registered) {
logE("Failed to register life cycle callback!");
}
}
}
示例11: doExtendedHelp
import com.nick.scalpel.core.utils.ReflectionUtils; //导入依赖的package包/类
@Override
public void doExtendedHelp(final ListView view, Field field, Object targetObj) {
DataProvider provider = field.getAnnotation(DataProvider.class);
if (provider == null) return;
String providerName = provider.name();
Preconditions.checkState(!TextUtils.isEmpty(providerName));
Object listViewDataProviderObj;
switch (providerName) {
case ThisThatNull.THIS:
listViewDataProviderObj = targetObj;
break;
default:
Field providerField = ReflectionUtils.findField(targetObj, providerName);
Preconditions.checkNotNull(providerField);
ReflectionUtils.makeAccessible(providerField);
listViewDataProviderObj = ReflectionUtils.getField(providerField, targetObj);
Preconditions.checkNotNull(listViewDataProviderObj);
}
Preconditions.checkState(listViewDataProviderObj instanceof ListViewDataProvider);
final ListViewDataProvider listViewDataProvider = (ListViewDataProvider) listViewDataProviderObj;
ListViewViewProvider listViewViewProvider = null;
ViewProvider viewProvider = field.getAnnotation(ViewProvider.class);
if (viewProvider != null) {
final int id = viewProvider.id();
listViewViewProvider = new ListViewViewProvider() {
@Override
public int getItemViewId() {
return id;
}
};
}
final ListViewViewProvider finalListViewViewProvider = listViewViewProvider;
SharedExecutor.get().execute(new Runnable() {
@Override
public void run() {
listViewDataProvider.loadInBackground();
mHandler.post(new Runnable() {
@Override
public void run() {
QuickAdapter quickAdapter = new QuickAdapter(listViewDataProvider,
finalListViewViewProvider, view.getContext());// Using view.context to keep the activity theme.
view.setAdapter(quickAdapter);
}
});
}
});
}
示例12: wire
import com.nick.scalpel.core.utils.ReflectionUtils; //导入依赖的package包/类
@Override
public void wire(final Object o) {
Class clz = o.getClass();
int[] events = null;
if (clz.isAnnotationPresent(Events.class)) {
Events annotation = (Events) clz.getAnnotation(Events.class);
events = annotation.value();
}
Method[] methods = clz.getDeclaredMethods();
for (final Method m : methods) {
ReflectionUtils.makeAccessible(m);
int modifier = m.getModifiers();
boolean isPublic = Modifier.isPublic(modifier);
if (!isPublic) continue;
String methodName = m.getName();
boolean isHandle = methodName.startsWith("handle")
|| m.isAnnotationPresent(ReceiverMethod.class);
if (!isHandle) continue;
boolean noParam;
Class[] params = m.getParameterTypes();
noParam = params.length == 0;
final boolean eventParam = params.length == 1 && params[0] == Event.class;
if (!noParam && !eventParam) continue;
int[] usingEvents = events;
if (m.isAnnotationPresent(Events.class)) {
Events methodAnno = m.getAnnotation(Events.class);
usingEvents = methodAnno.value();
logD("Using method annotation events for:" + methodName);
}
if (usingEvents == null) continue;
final boolean callInMain = m.isAnnotationPresent(CallInMainThread.class);
TaggedEventsReceiver receiver = new TaggedEventsReceiver(usingEvents, o, methodName) {
@Override
public void onReceive(@NonNull Event event) {
logV("Invoking for event:" + event);
if (eventParam)
ReflectionUtils.invokeMethod(m, o, event);
else ReflectionUtils.invokeMethod(m, o);
}
@Override
public boolean callInMainThread() {
return callInMain;
}
};
logV("Creating receiver:" + receiver);
mSubscriber.subscribe(receiver);
saveReceiver(receiver);
}
}