本文整理匯總了Java中org.apache.commons.lang3.reflect.FieldUtils.getAllFields方法的典型用法代碼示例。如果您正苦於以下問題:Java FieldUtils.getAllFields方法的具體用法?Java FieldUtils.getAllFields怎麽用?Java FieldUtils.getAllFields使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.lang3.reflect.FieldUtils
的用法示例。
在下文中一共展示了FieldUtils.getAllFields方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: writeAllFieldsOfType
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
public static <T> void writeAllFieldsOfType(Object o, T t, Class<T> c) throws IllegalAccessException
{
boolean wroteSomething = false;
for (Field f : FieldUtils.getAllFields(o.getClass()))
{
if (!Modifier.isStatic(f.getModifiers()) && f.getType().equals(c))
{
FieldUtils.writeField(f, o, t, true);
wroteSomething = true;
}
}
if (!wroteSomething)
throw new IllegalAccessException();
}
示例2: validate
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
private void validate(Object object, Class<?> clazz) throws Exception {
for (Field field : FieldUtils.getAllFields(clazz)) {
int modifiers = field.getModifiers();
// Ignore static fields
if (Modifier.isStatic(modifiers)) {
continue;
}
assertTrue("Field is private", Modifier.isPrivate(modifiers));
assertTrue("Field is final", Modifier.isFinal(modifiers));
Method getter = clazz.getMethod(getterName(field.getName()));
assertNotNull("Getter exists", getter);
assertTrue("Getter is public", Modifier.isPublic(getter.getModifiers()));
}
// Check that hashCode, toString and equals are defined
assertNotNull(clazz.getDeclaredMethod("hashCode").invoke(object));
assertNotNull(clazz.getDeclaredMethod("toString").invoke(object));
assertTrue((Boolean) clazz.getDeclaredMethod("equals", Object.class).invoke(object, object));
}
示例3: writeAllStaticFieldsOfType
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
@SuppressWarnings({ "rawtypes" })
public static <T> void writeAllStaticFieldsOfType(Class d, T t, Class c) throws IllegalAccessException
{
boolean wroteSomething = false;
for (Field f : FieldUtils.getAllFields(d))
{
if (Modifier.isStatic(f.getModifiers()) && f.getType().equals(c))
{
FieldUtils.writeStaticField(f, t, true);
wroteSomething = true;
}
}
if (!wroteSomething)
throw new IllegalAccessException();
}
示例4: buildFieldMap
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
private void buildFieldMap(Map<String, MappingBean> fieldMap, Set<String> fields, Class<?> sourceClass, Class<?> destinationClass){
for(Field field: FieldUtils.getAllFields(sourceClass)){
fields.add(field.getName());
Optional<FieldMapping> optional = this.getFieldMapping(field, this.destinationClass);
if(optional.isPresent()){
FieldMapping fieldMapping = optional.get();
MappingBean bean = new MappingBean();
bean.setSourceField(field.getName());
bean.setMappedField(Strings.isNullOrEmpty(fieldMapping.value())?field.getName():fieldMapping.value());
bean.setMappedIn(fieldMapping.mappedIn());
bean.setMappedOut(fieldMapping.mappedOut());
bean.setIgnore(fieldMapping.ignore());
fieldMap.put(field.getName(),bean);
}
}
}
示例5: prepareFields
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
private void prepareFields() {
viewsForInjection = new ArrayList<>();
for (Field field : FieldUtils.getAllFields(this.getClass())) {
if (field.isAnnotationPresent(InjectView.class)) {
viewsForInjection.add(new ViewMembersInjector(field, field.getAnnotation(InjectView.class)));
}
}
}
示例6: getBXMLFieldValues
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
public static Map<String, Object> getBXMLFieldValues(Bindable obj) throws IllegalAccessException {
Map<String, Object> result = new HashMap<>();
Class<?> type = obj.getClass();
Field[] allFields = FieldUtils.getAllFields(type);
if (ArrayUtils.isNotEmpty(allFields)) {
for (Field field : allFields) {
BXML bxmlAnnotation = field.getAnnotation(BXML.class);
if (bxmlAnnotation != null) {
String id = bxmlAnnotation.id();
Object fieldValue = FieldUtils.readField(field, obj, true);
if (StringUtils.isNotBlank(id)) {
result.put(id, fieldValue);
} else {
result.put(field.getName(), fieldValue);
}
}
}
}
return result;
}
示例7: readFieldOfType
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public static <T> T readFieldOfType(Object o, Class<T> t) throws IllegalAccessException
{
for (Field f : FieldUtils.getAllFields(o.getClass()))
{
if (!Modifier.isStatic(f.getModifiers()) && f.getType().equals(t))
{
return (T) FieldUtils.readField(f, o, true);
}
}
throw new IllegalAccessException();
}
示例8: readParameterizedFieldOfType
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public static <C,T> C readParameterizedFieldOfType(Object o, Class<C> collection, Class<T> genertic) throws IllegalAccessException
{
for (Field f : FieldUtils.getAllFields(o.getClass()))
{
if (!Modifier.isStatic(f.getModifiers()) && f.getType().equals(collection) && f.getGenericType().equals(genertic))
{
return (C) FieldUtils.readField(f, o, true);
}
}
throw new IllegalAccessException();
}
示例9: readStaticFieldOfType
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T readStaticFieldOfType(Class c, Class<T> t) throws IllegalAccessException
{
for (Field f : FieldUtils.getAllFields(c))
{
if (Modifier.isStatic(f.getModifiers()) && f.getType().equals(t))
{
return (T) FieldUtils.readStaticField(f, true);
}
}
throw new IllegalAccessException();
}
示例10: writeFieldOfType
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
@SuppressWarnings({ })
public static <T> void writeFieldOfType(Object o, T t, Class<T> c) throws IllegalAccessException
{
for (Field f : FieldUtils.getAllFields(o.getClass()))
{
if (!Modifier.isStatic(f.getModifiers()) && f.getType().equals(c))
{
FieldUtils.writeField(f, o, t, true);
return;
}
}
throw new IllegalAccessException();
}
示例11: writeStaticFieldOfType
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
@SuppressWarnings({ "rawtypes" })
public static <T> void writeStaticFieldOfType(Class d, T t, Class<T> c) throws IllegalAccessException
{
for (Field f : FieldUtils.getAllFields(d))
{
if (Modifier.isStatic(f.getModifiers()) && f.getType().equals(c))
{
FieldUtils.writeStaticField(f, t, true);
return;
}
}
throw new IllegalAccessException();
}
示例12: copyAllFieldsFrom
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
public static <T> void copyAllFieldsFrom(T dest, T origin, Class<T> c)
{
for (Field f : FieldUtils.getAllFields(c))
{
if (!Modifier.isStatic(f.getModifiers()) && !Modifier.isFinal(f.getModifiers()))
{
copyField(f, dest, origin);
}
}
}
示例13: copyAllPublicFieldsFrom
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
public static <T> void copyAllPublicFieldsFrom(T dest, T origin, Class<T> c)
{
for (Field f : FieldUtils.getAllFields(c))
{
int modifiers = f.getModifiers();
if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers) && !Modifier.isFinal(modifiers))
{
copyField(f, dest, origin);
}
}
}
示例14: copyAllFieldsFromEx
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
public static <T> void copyAllFieldsFromEx(T dest, T origin, Class<T> c)
{
for (Field f : FieldUtils.getAllFields(c))
{
if (!Modifier.isStatic(f.getModifiers()))
{
copyField(f, dest, origin);
}
}
}
示例15: determineRootAutoLinkingPaths
import org.apache.commons.lang3.reflect.FieldUtils; //導入方法依賴的package包/類
/**
* Determines the root property paths relative to the given root object type against which to perform automatic
* linking.
*
* <p>This will be determined based on the presence of {@link Link} annotations on the given root object type.
* This method is invoked recursively as it walks the class structure looking for Link annotations. It uses the
* path
* and scanned arguments to keep track of how deep into the structure the scanning is and to prevent infinite
* recursion.</p>
*
* @param rootObjectType the root object type from which to perform the scan for auto-linking paths
* @param path the current property path relative to the original root object type at which the scan began, if null
* then we are scanning from the root-most object type. Each recursive call of this method will append
* a new property to this path
* @param scanned used to track classes that have already been scanned and prevent infinite recursion
* @return a set of property paths that should be auto linked
*/
protected Set<String> determineRootAutoLinkingPaths(Class<?> rootObjectType, String path, Set<Class<?>> scanned) {
Set<String> autoLinkingPaths = new HashSet<String>();
if (scanned.contains(rootObjectType)) {
return autoLinkingPaths;
} else {
scanned.add(rootObjectType);
}
Link autoLink = AnnotationUtils.findAnnotation(rootObjectType, Link.class);
if (autoLink != null && autoLink.cascade()) {
autoLinkingPaths.addAll(assembleAutoLinkingPaths(path, autoLink));
} else if (autoLink == null) {
Field[] fields = FieldUtils.getAllFields(rootObjectType);
for (Field field : fields) {
autoLink = field.getAnnotation(Link.class);
if (autoLink != null) {
if (autoLink.cascade()) {
String fieldPath = appendToPath(path, field.getName());
autoLinkingPaths.addAll(assembleAutoLinkingPaths(fieldPath, autoLink));
}
} else {
autoLinkingPaths.addAll(determineRootAutoLinkingPaths(field.getType(), appendToPath(path,
field.getName()), scanned));
}
}
}
return autoLinkingPaths;
}