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

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

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

Field.getName介绍

[英]Returns the name of this field.
[中]返回此字段的名称。

代码示例

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

/** Returns the name of the field. */
public String getName () {
  return field.getName();
}

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

@Override
 public int compare(Field left, Field right) {
  return left.getName().compareTo(right.getName());
 }
};

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

public FieldTypeProvider(Field field) {
  this.fieldName = field.getName();
  this.declaringClass = field.getDeclaringClass();
  this.field = field;
}

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

@Override
public String toString() {
  return (this.field != null ? "field '" + this.field.getName() + "'" : String.valueOf(this.methodParameter));
}

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

/**
 * Create a new descriptor for a field.
 * @param field the field to wrap
 * @param required whether the dependency is required
 * @param eager whether this dependency is 'eager' in the sense of
 * eagerly resolving potential target beans for type matching
 */
public DependencyDescriptor(Field field, boolean required, boolean eager) {
  super(field);
  this.declaringClass = field.getDeclaringClass();
  this.fieldName = field.getName();
  this.required = required;
  this.eager = eager;
}

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

/**
 * We need to detect this as "code-style" AspectJ aspects should not be
 * interpreted by Spring AOP.
 */
private boolean compiledByAjc(Class<?> clazz) {
  // The AJTypeSystem goes to great lengths to provide a uniform appearance between code-style and
  // annotation-style aspects. Therefore there is no 'clean' way to tell them apart. Here we rely on
  // an implementation detail of the AspectJ compiler.
  for (Field field : clazz.getDeclaredFields()) {
    if (field.getName().startsWith(AJC_MAGIC)) {
      return true;
    }
  }
  return false;
}

代码示例来源: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: apache/incubator-dubbo

public static Map<String, Field> getBeanPropertyFields(Class cl) {
  Map<String, Field> properties = new HashMap<String, Field>();
  for (; cl != null; cl = cl.getSuperclass()) {
    Field[] fields = cl.getDeclaredFields();
    for (Field field : fields) {
      if (Modifier.isTransient(field.getModifiers())
          || Modifier.isStatic(field.getModifiers())) {
        continue;
      }
      field.setAccessible(true);
      properties.put(field.getName(), field);
    }
  }
  return properties;
}

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

public static Map<String, Field> getBeanPropertyFields(Class cl) {
  Map<String, Field> properties = new HashMap<String, Field>();
  for (; cl != null; cl = cl.getSuperclass()) {
    Field[] fields = cl.getDeclaredFields();
    for (Field field : fields) {
      if (Modifier.isTransient(field.getModifiers())
          || Modifier.isStatic(field.getModifiers())) {
        continue;
      }
      field.setAccessible(true);
      properties.put(field.getName(), field);
    }
  }
  return properties;
}

代码示例来源:origin: alibaba/fastjson

private static FieldInfo getField(List<FieldInfo> fieldList, String propertyName) {
  for (FieldInfo item : fieldList) {
    if (item.name.equals(propertyName)) {
      return item;
    }
    Field field = item.field;
    if (field != null && item.getAnnotation() != null && field.getName().equals(propertyName)) {
      return item;
    }
  }
  return null;
}

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

private static String[] calculateMatches(final String name, Class<?> clazz, final int maxDistance) {
  final List<String> candidates = new ArrayList<>();
  ReflectionUtils.doWithFields(clazz, field -> {
    String possibleAlternative = field.getName();
    if (calculateStringDistance(name, possibleAlternative) <= maxDistance) {
      candidates.add(possibleAlternative);
    }
  });
  Collections.sort(candidates);
  return StringUtils.toStringArray(candidates);
}

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

/**
 * Determine the name of the wrapped parameter/field.
 * @return the declared name (never {@code null})
 */
@Nullable
public String getDependencyName() {
  return (this.field != null ? this.field.getName() : obtainMethodParameter().getParameterName());
}

代码示例来源: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: spring-projects/spring-framework

@Override
  public void setValue(@Nullable Object value) throws Exception {
    try {
      ReflectionUtils.makeAccessible(this.field);
      this.field.set(getWrappedInstance(), value);
    }
    catch (IllegalAccessException ex) {
      throw new InvalidPropertyException(getWrappedClass(), this.field.getName(),
          "Field is not accessible", ex);
    }
  }
}

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

@Override
@Nullable
public Object getValue() throws Exception {
  try {
    ReflectionUtils.makeAccessible(this.field);
    return this.field.get(getWrappedInstance());
  }
  catch (IllegalAccessException ex) {
    throw new InvalidPropertyException(getWrappedClass(),
        this.field.getName(), "Field is not accessible", ex);
  }
}

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

private void writeDefinition20(AbstractHessianOutput out)
    throws IOException {
  out.writeClassFieldLength(_fields.length);
  for (int i = 0; i < _fields.length; i++) {
    Field field = _fields[i];
    out.writeString(field.getName());
  }
}

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

private void writeObject10(Object obj, AbstractHessianOutput out)
    throws IOException {
  for (int i = 0; i < _fields.length; i++) {
    Field field = _fields[i];
    out.writeString(field.getName());
    _fieldSerializers[i].serialize(out, obj, field);
  }
  out.writeMapEnd();
}

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

@GwtIncompatible // reflection
public void testConstants_charset() throws Exception {
 for (Field field : getConstantFields()) {
  Optional<Charset> charset = ((MediaType) field.get(null)).charset();
  if (field.getName().endsWith("_UTF_8")) {
   assertThat(charset).hasValue(UTF_8);
  } else {
   assertThat(charset).isAbsent();
  }
 }
}

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

static void assertConstantNameMatchesString(
  Class<?> clazz,
  ImmutableBiMap<String, String> specialCases,
  ImmutableSet<String> uppercaseAcronyms)
  throws IllegalAccessException {
 for (Field field : relevantFields(clazz)) {
  assertEquals(
    upperToHttpHeaderName(field.getName(), specialCases, uppercaseAcronyms), field.get(null));
 }
}

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

@GwtIncompatible // reflection
public void testGetField() {
 Field foo = Enums.getField(AnEnum.FOO);
 assertEquals("FOO", foo.getName());
 assertTrue(foo.isAnnotationPresent(ExampleAnnotation.class));
 Field bar = Enums.getField(AnEnum.BAR);
 assertEquals("BAR", bar.getName());
 assertFalse(bar.isAnnotationPresent(ExampleAnnotation.class));
}

相关文章

微信公众号

最新文章

更多