本文整理了Java中java.lang.Class.getDeclaredFields()
方法的一些代码示例,展示了Class.getDeclaredFields()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getDeclaredFields()
方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getDeclaredFields
[英]Returns an array containing Field objects for all fields declared in the class represented by this Class. If there are no fields or if this Class represents an array class, a primitive type or void then an empty array is returned.
[中]返回一个数组,该数组包含该类表示的类中声明的所有字段的字段对象。如果没有字段,或者该类表示数组类、基元类型或void,则返回空数组。
代码示例来源:origin: prestodb/presto
/**
* @since 2.7
*/
public static Field[] getDeclaredFields(Class<?> cls) {
return cls.getDeclaredFields();
}
代码示例来源:origin: redisson/redisson
/**
* @since 2.7
*/
public static Field[] getDeclaredFields(Class<?> cls) {
return cls.getDeclaredFields();
}
代码示例来源: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: google/guava
@Override
public sun.misc.Unsafe run() throws Exception {
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
for (java.lang.reflect.Field f : k.getDeclaredFields()) {
f.setAccessible(true);
Object x = f.get(null);
if (k.isInstance(x)) {
return k.cast(x);
}
}
throw new NoSuchFieldError("the Unsafe");
}
});
代码示例来源: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: stackoverflow.com
Field[] declaredFields = String.class.getDeclaredFields();
List<Field> staticFields = new ArrayList<Field>();
for (Field field : declaredFields) {
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
staticFields.add(field);
}
}
代码示例来源:origin: libgdx/libgdx
/** Returns an array of {@link Field} objects reflecting all the fields declared by the supplied class. */
static public Field[] getDeclaredFields (Class c) {
java.lang.reflect.Field[] fields = c.getDeclaredFields();
Field[] result = new Field[fields.length];
for (int i = 0, j = fields.length; i < j; i++) {
result[i] = new Field(fields[i]);
}
return result;
}
代码示例来源:origin: libgdx/libgdx
/** Returns an array of {@link Field} objects reflecting all the fields declared by the supplied class. */
static public Field[] getDeclaredFields (Class c) {
java.lang.reflect.Field[] fields = c.getDeclaredFields();
Field[] result = new Field[fields.length];
for (int i = 0, j = fields.length; i < j; i++) {
result[i] = new Field(fields[i]);
}
return result;
}
代码示例来源:origin: apache/incubator-dubbo
/**
* Creates a map of the classes fields.
*/
protected HashMap getFieldMap(Class cl) {
HashMap fieldMap = new HashMap();
for (; cl != null; cl = cl.getSuperclass()) {
Field[] fields = cl.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
if (Modifier.isTransient(field.getModifiers()) ||
Modifier.isStatic(field.getModifiers()))
continue;
// XXX: could parameterize the handler to only deal with public
field.setAccessible(true);
fieldMap.put(field.getName(), field);
}
}
return fieldMap;
}
}
代码示例来源:origin: redisson/redisson
public static FieldList<FieldDescription.InDefinedShape> getAllFields(Class<?> cls) {
List<Field> fields = new ArrayList<Field>();
for (Class<?> c = cls; c != null; c = c.getSuperclass()) {
Collections.addAll(fields, c.getDeclaredFields());
}
return new FieldList.ForLoadedFields(fields);
}
}
代码示例来源:origin: redisson/redisson
public static Field getDeclaredField(Class<?> clazz, String fieldName) throws NoSuchFieldException {
for (Class<?> c : getClassHierarchy(clazz)) {
for (Field field : c.getDeclaredFields()) {
if (field.getName().equals(fieldName)) {
return field;
}
}
}
throw new NoSuchFieldException("No such field: " + fieldName);
}
代码示例来源:origin: org.mockito/mockito-core
/**
* Instance fields declared in the class of the given instance.
*
* @param instance Instance from which declared fields will be retrieved.
* @return InstanceFields of this object instance.
*/
public static InstanceFields declaredFieldsOf(Object instance) {
List<InstanceField> instanceFields = new ArrayList<InstanceField>();
instanceFields.addAll(instanceFieldsIn(instance, instance.getClass().getDeclaredFields()));
return new InstanceFields(instance, instanceFields);
}
代码示例来源:origin: org.mockito/mockito-core
/**
* Instance fields declared in the class and superclasses of the given instance.
*
* @param instance Instance from which declared fields will be retrieved.
* @return InstanceFields of this object instance.
*/
public static InstanceFields allDeclaredFieldsOf(Object instance) {
List<InstanceField> instanceFields = new ArrayList<InstanceField>();
for (Class<?> clazz = instance.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
instanceFields.addAll(instanceFieldsIn(instance, clazz.getDeclaredFields()));
}
return new InstanceFields(instance, instanceFields);
}
代码示例来源:origin: org.mockito/mockito-core
private List<Field> orderedInstanceFieldsFrom(Class<?> awaitingInjectionClazz) {
List<Field> declaredFields = Arrays.asList(awaitingInjectionClazz.getDeclaredFields());
declaredFields = ListUtil.filter(declaredFields, notFinalOrStatic);
return sortSuperTypesLast(declaredFields);
}
}
代码示例来源: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();
}
代码示例来源:origin: google/guava
@GwtIncompatible // reflection
private static FluentIterable<Field> getConstantFields() {
return FluentIterable.from(asList(MediaType.class.getDeclaredFields()))
.filter(
new Predicate<Field>() {
@Override
public boolean apply(Field input) {
int modifiers = input.getModifiers();
return isPublic(modifiers)
&& isStatic(modifiers)
&& isFinal(modifiers)
&& MediaType.class.equals(input.getType());
}
});
}
代码示例来源:origin: apache/kafka
private void ensureEmptySelectorFields(Selector selector) throws Exception {
for (Field field : Selector.class.getDeclaredFields()) {
ensureEmptySelectorField(selector, field);
}
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
List<Field> fieldsErros = new ArrayList<>();
Arrays.asList(value.getClass().getDeclaredFields()).forEach(f -> {
f.setAccessible(true);
try {
if (!f.getName().equals(ID) && f.get(value) == null) {
fieldsErros.add(f);
context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate())
.addPropertyNode(f.getName())
.addConstraintViolation();
}
} catch (IllegalAccessException ex) {
throw new IllegalStateException(ex);
}
});
return fieldsErros.isEmpty();
}
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
List<Field> fieldsErros = new ArrayList<>();
Arrays.asList(value.getClass().getDeclaredFields()).forEach(f -> {
f.setAccessible(true);
try {
if (!f.getName().equals(ID) && f.get(value) == null) {
fieldsErros.add(f);
context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate())
.addPropertyNode(f.getName())
.addConstraintViolation();
}
} catch (IllegalAccessException ex) {
throw new IllegalStateException(ex);
}
});
return fieldsErros.isEmpty();
}
}
内容来源于网络,如有侵权,请联系作者删除!