本文整理匯總了Java中java.lang.reflect.Field.getType方法的典型用法代碼示例。如果您正苦於以下問題:Java Field.getType方法的具體用法?Java Field.getType怎麽用?Java Field.getType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.reflect.Field
的用法示例。
在下文中一共展示了Field.getType方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: inject
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* Injects all fields that are marked with the {@link InjectView} annotation.
* <p>
* For each field marked with the InjectView annotation, a call to
* {@link Activity#findViewById(int)} will be made, passing in the resource id stored in the
* value() method of the InjectView annotation as the int parameter, and the result of this call
* will be assigned to the field.
*
* @throws IllegalStateException if injection fails, common causes being that you have used an
* invalid id value, or you haven't called setContentView() on your Activity.
*/
public void inject() {
for (Field field : mActivity.getClass().getDeclaredFields()) {
for (Annotation annotation : field.getAnnotations()) {
if (annotation.annotationType().equals(InjectView.class)) {
try {
Class<?> fieldType = field.getType();
int idValue = InjectView.class.cast(annotation).value();
field.setAccessible(true);
Object injectedValue = fieldType.cast(mActivity.findViewById(idValue));
if (injectedValue == null) {
throw new IllegalStateException("findViewById(" + idValue
+ ") gave null for " +
field + ", can't inject");
}
field.set(mActivity, injectedValue);
field.setAccessible(false);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
}
}
}
示例2: setPrimitive
import java.lang.reflect.Field; //導入方法依賴的package包/類
private void setPrimitive(Object o, Field f, String value) throws IllegalArgumentException, IllegalAccessException {
if (f.getType() == boolean.class) {
f.set(o, Boolean.valueOf(value));
}
else if (f.getType() == byte.class) {
f.set(o, Byte.valueOf(value));
}
else if (f.getType() == char.class) {
f.set(o, value.charAt(0));
}
else if (f.getType() == short.class) {
f.set(o, Short.valueOf(value));
}
else if (f.getType() == int.class) {
f.set(o, Integer.valueOf(value));
}
else if (f.getType() == long.class) {
f.set(o, Long.valueOf(value));
}
else if (f.getType() == float.class) {
f.set(o, Float.valueOf(value));
}
else if (f.getType() == double.class) {
f.set(o, Double.valueOf(value));
}
}
示例3: getFieldRealClass
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* 獲取正確的變量類型。
*
* @return
*/
Class getFieldRealClass(Field field)
{
Class<?> ftype = field.getType();
if (field.getGenericType() == null || superGenericClasses == null)
{
return ftype;
}
for (int i = 0; i < superGenericClasses.length; i++)
{
if (WPTool.isAssignable(superGenericClasses[i], ftype))
{
return superGenericClasses[i];
}
}
return ftype;
}
示例4: AsMetaField
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* Instantiates a new as meta field.
*
* @param pOwner the owner
* @param pField the field
*/
public AsMetaField(String pOwner, Field pField) {
owner = pOwner;
name = pField.getName();
isArray = pField.getType().getComponentType() != null;
Class<?> tTypeClass = isArray ? pField.getType().getComponentType() : pField.getType();
type = As.getTypeName(tTypeClass);
Class<?> tConstantType = As.getMetaDataHandler().getConstantType(pField);
if (tConstantType != null) {
constant = As.getTypeName(tConstantType);
}
else if (Enum.class.isAssignableFrom(tTypeClass)) {
constant = type;
}
Class<?> tReferencedType = As.getMetaDataHandler().getReferencedType(pField);
referencedType = tReferencedType != null ? As.getTypeName(tReferencedType) : null;
mBusinessType = As.getMetaDataHandler().getBusinessType(pField);
if (mBusinessType != null) {
businessType = mBusinessType.name();
}
isObject = As.getType(type) != null;
}
示例5: injectContext
import java.lang.reflect.Field; //導入方法依賴的package包/類
public static void injectContext(Object instance, RouteDefinition definition, RoutingContext routeContext) throws ContextException {
if (instance == null) {
return;
}
if (hasContext(instance.getClass())) {
Field[] fields = instance.getClass().getDeclaredFields();
for (Field field : fields) {
Annotation found = field.getAnnotation(Context.class);
if (found != null) {
Object context = provideContext(definition, field.getType(), null, routeContext);
try {
field.setAccessible(true);
field.set(instance, context);
}
catch (IllegalAccessException e) {
throw new ContextException("Can't provide @Context for: " + field.getType() + " - " + e.getMessage());
}
}
}
}
}
示例6: getField
import java.lang.reflect.Field; //導入方法依賴的package包/類
public static Field getField(Class p_getField_0_, Class p_getField_1_)
{
try
{
Field[] afield = p_getField_0_.getDeclaredFields();
for (int i = 0; i < afield.length; ++i)
{
Field field = afield[i];
if (field.getType() == p_getField_1_)
{
field.setAccessible(true);
return field;
}
}
return null;
}
catch (Exception var5)
{
return null;
}
}
示例7: MemberName
import java.lang.reflect.Field; //導入方法依賴的package包/類
@SuppressWarnings("LeakingThisInConstructor")
public MemberName(Field fld, boolean makeSetter) {
Objects.requireNonNull(fld);
// fill in vmtarget, vmindex while we have fld in hand:
MethodHandleNatives.init(this, fld);
assert(isResolved() && this.clazz != null);
this.name = fld.getName();
this.type = fld.getType();
assert((REF_putStatic - REF_getStatic) == (REF_putField - REF_getField));
byte refKind = this.getReferenceKind();
assert(refKind == (isStatic() ? REF_getStatic : REF_getField));
if (makeSetter) {
changeReferenceKind((byte)(refKind + (REF_putStatic - REF_getStatic)), refKind);
}
}
示例8: initializeControllerViews
import java.lang.reflect.Field; //導入方法依賴的package包/類
private void initializeControllerViews(Controller controller)
throws InstantiationException, IllegalAccessException {
Class<? extends Controller> controllerClass = controller.getClass();
for (Field field : controllerClass.getDeclaredFields()) {
Class<?> fieldType = field.getType();
if (View.class.isAssignableFrom(fieldType)) {
View view = (View) fieldType.newInstance();
field.setAccessible(true);
field.set(controller, view);
field.setAccessible(false);
// The next line is handled by View#setController()
// controller.addView(view);
AllowController annotation = fieldType.getAnnotation(AllowController.class);
if (annotation != null) {
boolean valid = false;
for (Class<? extends Controller> c : annotation.value()) {
if (c.isInstance(controller)) {
view.setController(controller);
view.initializeView();
valid = true;
}
}
if (!valid) {
throw new RuntimeException(
fieldType.getSimpleName() + " is not allowed in " + controllerClass.getSimpleName());
}
}
}
}
}
示例9: mockMember
import java.lang.reflect.Field; //導入方法依賴的package包/類
private static void mockMember(Object member, Object mock, Field f)
throws IllegalAccessException {
Class<?> t = f.getType();
if (t.isInstance(mock)) {
f.setAccessible(true);
f.set(member, mock);
}
}
示例10: relevantFields
import java.lang.reflect.Field; //導入方法依賴的package包/類
static ImmutableSet<Field> relevantFields(Class<?> cls) {
ImmutableSet.Builder<Field> builder = ImmutableSet.builder();
for (Field field : cls.getDeclaredFields()) {
/*
* Coverage mode generates synthetic fields. If we ever add private
* fields, they will cause similar problems, and we may want to switch
* this check to isAccessible().
*/
if (!field.isSynthetic() && field.getType() == String.class) {
builder.add(field);
}
}
return builder.build();
}
示例11: toOpcodeString
import java.lang.reflect.Field; //導入方法依賴的package包/類
public static String toOpcodeString(int opcode) {
Class c = AwkTuples.class;
Field[] fields = c.getDeclaredFields();
try {
for (Field field : fields) {
if ((field.getModifiers() & Modifier.STATIC) > 0 && field.getType() == Integer.TYPE && field.getInt(null) == opcode) {
return field.getName();
}
}
} catch (IllegalAccessException iac) {
LOG.error("Failed to create OP-Code string", iac);
return "[" + opcode + ": " + iac + "]";
}
return "{" + opcode + "}";
}
示例12: setPathValue
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* Sets given value specified by path of provided object.
* Example:
* Given path: "address.street"
* means that on the given object there is a field named "address"
* that, once referenced, contains field named "street"
*
* @param instance - object which contains the field.
* @param path - path to be used
* @param value - value to be set on the object.
* @throws NoSuchMethodException - exception
* @throws IllegalAccessException - exception
* @throws InvocationTargetException - exception
* @throws InstantiationException - exception
*/
public static void setPathValue(Object instance, String path, Object value) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
checkNotNull(path);
Object ref = instance;
Field refField = null;
String[] split = path.split("\\.");
for (int i = 0; i < split.length; i++) {
String property = split[i];
refField = getDeclaredField(ref.getClass(), property);
if (!makeFieldAccessible(refField)) {
throw new RandomitoException("Path cannot be accessed: " + path);
}
if (i < split.length - 1) {
ref = refField.get(ref);
if (ref == null) {
throw new RandomitoException("Property is null: '"
+ join(subarray(split, 0, i + 1), ".")
+ "'. Maybe increase 'depth' parameter?");
}
}
}
if (value == null) {
refField.set(instance, null);
return;
}
Class<?> type = refField.getType();
if (type.isPrimitive()) {
type = ClassUtils.primitiveToWrapper(type);
}
if (Number.class.isAssignableFrom(type)) {
refField.set(ref, stringToNumber(value, type));
} else {
refField.set(ref, value);
}
}
示例13: HtmlAdapter
import java.lang.reflect.Field; //導入方法依賴的package包/類
HtmlAdapter(Jspoon jspoon, Class<T> clazz) {
this.jspoon = jspoon;
this.clazz = clazz;
htmlFieldCache = new LinkedHashMap<>();
Field[] declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
Class<?> fieldClass = field.getType();
// Annotated field
Selector selector = field.getAnnotation(Selector.class);
// Not annotated field of annotated class
if (selector == null) {
selector = fieldClass.getAnnotation(Selector.class);
}
// Not annotated field - List of annotated type
if (selector == null && List.class.isAssignableFrom(fieldClass)) {
selector = getSelectorFromListType(field);
}
if (selector != null) {
addCachedHtmlField(field, selector, fieldClass);
}
}
if (htmlFieldCache.isEmpty()) {
throw new EmptySelectorException(clazz);
}
}
示例14: getFields
import java.lang.reflect.Field; //導入方法依賴的package包/類
public static Field[] getFields(Object p_getFields_0_, Field[] p_getFields_1_, Class p_getFields_2_, Object p_getFields_3_)
{
try
{
List<Field> list = new ArrayList();
for (int i = 0; i < p_getFields_1_.length; ++i)
{
Field field = p_getFields_1_[i];
if (field.getType() == p_getFields_2_)
{
boolean flag = Modifier.isStatic(field.getModifiers());
if ((p_getFields_0_ != null || flag) && (p_getFields_0_ == null || !flag))
{
field.setAccessible(true);
Object object = field.get(p_getFields_0_);
if (object == p_getFields_3_)
{
list.add(field);
}
else if (object != null && p_getFields_3_ != null && object.equals(p_getFields_3_))
{
list.add(field);
}
}
}
}
Field[] afield = (Field[])((Field[])list.toArray(new Field[list.size()]));
return afield;
}
catch (Exception var9)
{
return null;
}
}
示例15: ObjectStreamField
import java.lang.reflect.Field; //導入方法依賴的package包/類
ObjectStreamField(Field field) {
this(field.getName(), field.getType());
this.field = field;
}