java.lang.reflect.Field.getType()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(170)

本文整理了Java中java.lang.reflect.Field.getType()方法的一些代码示例,展示了Field.getType()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Field.getType()方法的具体详情如下:
包路径:java.lang.reflect.Field
类名称:Field
方法名:getType

Field.getType介绍

[英]Return the Class associated with the type of this field.
[中]返回与此字段类型关联的类。

代码示例

代码示例来源:origin: spring-projects/spring-framework

public FieldPropertyHandler(Field field) {
  super(field.getType(), true, true);
  this.field = field;
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Class<?> getObjectType() {
  return (this.fieldObject != null ? this.fieldObject.getType() : null);
}

代码示例来源:origin: libgdx/libgdx

/** Returns a Class object that identifies the declared type for the field. */
public Class getType () {
  return field.getType();
}

代码示例来源:origin: libgdx/libgdx

/** Returns a Class object that identifies the declared type for the field. */
public Class getType () {
  return field.getType();
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Class<?> getPropertyType() {
  if (this.member instanceof Method) {
    return ((Method) this.member).getReturnType();
  }
  else {
    return ((Field) this.member).getType();
  }
}

代码示例来源:origin: spring-projects/spring-framework

protected final Class<?> getResourceType() {
  if (this.isField) {
    return ((Field) this.member).getType();
  }
  else if (this.pd != null) {
    return this.pd.getPropertyType();
  }
  else {
    return ((Method) this.member).getParameterTypes()[0];
  }
}

代码示例来源:origin: spring-projects/spring-framework

private static void ensureSpringRulesAreNotPresent(Class<?> testClass) {
  for (Field field : testClass.getFields()) {
    Assert.state(!SpringClassRule.class.isAssignableFrom(field.getType()), () -> String.format(
        "Detected SpringClassRule field in test class [%s], " +
        "but SpringClassRule cannot be used with the SpringJUnit4ClassRunner.", testClass.getName()));
    Assert.state(!SpringMethodRule.class.isAssignableFrom(field.getType()), () -> String.format(
        "Detected SpringMethodRule field in test class [%s], " +
        "but SpringMethodRule cannot be used with the SpringJUnit4ClassRunner.", testClass.getName()));
  }
}

代码示例来源:origin: skylot/jadx

private void addDefaultValue(JadxCLIArgs args, Field f, StringBuilder opt) {
  Class<?> fieldType = f.getType();
  if (fieldType == int.class) {
    try {
      int val = f.getInt(args);
      opt.append(" (default: ").append(val).append(")");
    } catch (Exception e) {
      // ignore
    }
  }
}

代码示例来源:origin: skylot/jadx

private void readAndroidRStyleClass() {
  try {
    Class<?> rStyleCls = Class.forName(ANDROID_R_STYLE_CLS);
    for (Field f : rStyleCls.getFields()) {
      styleMap.put(f.getInt(f.getType()), f.getName());
    }
  } catch (Exception th) {
    LOG.error("Android R class loading failed", th);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Return the type declared by the underlying field or method/constructor parameter,
 * indicating the injection type.
 */
public Class<?> getDeclaredType() {
  return (this.field != null ? this.field.getType() : obtainMethodParameter().getParameterType());
}

代码示例来源:origin: apache/incubator-dubbo

@Override
protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {
  Class<?> referenceClass = field.getType();
  referenceBean = buildReferenceBean(reference, referenceClass);
  ReflectionUtils.makeAccessible(field);
  field.set(bean, referenceBean.getObject());
}

代码示例来源:origin: apache/incubator-dubbo

@Override
protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {
  Class<?> referenceClass = field.getType();
  referenceBean = buildReferenceBean(reference, referenceClass);
  ReflectionUtils.makeAccessible(field);
  field.set(bean, referenceBean.getObject());
}

代码示例来源:origin: apache/incubator-dubbo

static void logDeserializeError(Field field, Object obj, Object value,
  Throwable e)
  throws IOException {
  String fieldName = (field.getDeclaringClass().getName()
    + "." + field.getName());
  if (e instanceof HessianFieldException)
    throw (HessianFieldException) e;
  else if (e instanceof IOException)
    throw new HessianFieldException(fieldName + ": " + e.getMessage(), e);
  if (value != null)
    throw new HessianFieldException(fieldName + ": " + value.getClass().getName() + " (" + value + ")"
      + " cannot be assigned to '" + field.getType().getName() + "'", e);
  else
    throw new HessianFieldException(fieldName + ": " + field.getType().getName() + " cannot be assigned from null", e);
}

代码示例来源:origin: apache/incubator-dubbo

@Override
  void deserialize(AbstractHessianInput in, Object obj)
    throws IOException {
    Object value = null;
    try {
      value = in.readObject(_field.getType());
      _field.set(obj, value);
    } catch (Exception e) {
      logDeserializeError(_field, obj, value, e);
    }
  }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
  void deserialize(AbstractHessianInput in, Object obj)
    throws IOException {
    Object value = null;
    try {
      Type[] types = ((ParameterizedType)_field.getGenericType()).getActualTypeArguments();
      value = in.readObject(_field.getType(),
        isPrimitive(types[0]) ? (Class<?>)types[0] : null
      );
      _field.set(obj, value);
    } catch (Exception e) {
      logDeserializeError(_field, obj, value, e);
    }
  }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
  void deserialize(AbstractHessianInput in, Object obj)
    throws IOException {
    Object value = null;
    try {
      Type[] types = ((ParameterizedType)_field.getGenericType()).getActualTypeArguments();
      value = in.readObject(_field.getType(),
          isPrimitive(types[0]) ? (Class<?>)types[0] : null,
          isPrimitive(types[1]) ? (Class<?>)types[1] : null
        );
      _field.set(obj, value);
    } catch (Exception e) {
      logDeserializeError(_field, obj, value, e);
    }
  }
}

代码示例来源:origin: org.mockito/mockito-core

public static MockitoException fieldInitialisationThrewException(Field field, Throwable details) {
  return new InjectMocksException(join(
      "Cannot instantiate @InjectMocks field named '" + field.getName() + "' of type '" + field.getType() + "'.",
      "You haven't provided the instance at field declaration so I tried to construct the instance.",
      "However the constructor or the initialization block threw an exception : " + details.getMessage(),
      ""), details);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Create a new type descriptor from a {@link Field}.
 * <p>Use this constructor when a source or target conversion point is a field.
 * @param field the field
 */
public TypeDescriptor(Field field) {
  this.resolvableType = ResolvableType.forField(field);
  this.type = this.resolvableType.resolve(field.getType());
  this.annotatedElement = new AnnotatedElementAdapter(field.getAnnotations());
}

代码示例来源:origin: google/guava

@Override
 public boolean apply(Field input) {
  int modifiers = input.getModifiers();
  return isPublic(modifiers)
    && isStatic(modifiers)
    && isFinal(modifiers)
    && MediaType.class.equals(input.getType());
 }
});

代码示例来源:origin: google/guava

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();
}

相关文章

微信公众号

最新文章

更多