
com.strategicgains.syntaxe.util.ClassUtils Maven / Gradle / Ivy
/*
Copyright 2008, Strategic Gains, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.strategicgains.syntaxe.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author toddf
* @since Aug 18, 2008
*/
public class ClassUtils
{
// SECTION: CONSTANTS
public static final int IGNORED_FIELD_MODIFIERS = Modifier.FINAL | Modifier.STATIC | Modifier.TRANSIENT;
// SECTION: CONSTRUCTOR - PRIVATE
private ClassUtils()
{
// Prevents instantiation.
}
// SECTION: CLASS UTILITIES
/**
* Traverses from the given object up the inheritance hierarchy to list all the
* declared fields.
*
* @param object
* @return
*/
public static List getAllDeclaredFields(Class> aClass)
{
return getAllDeclaredFields(aClass, IGNORED_FIELD_MODIFIERS);
}
public static List getAllDeclaredFields(Class> aClass, int modifiers)
{
FieldListClosure closure = new ClassUtils.FieldListClosure(new ArrayList(), modifiers);
computeDeclaredFields(aClass, closure);
return closure.getValues();
}
public static HashMap getAllDeclaredFieldsByName(Class> aClass)
{
return getAllDeclaredFieldsByName(aClass, IGNORED_FIELD_MODIFIERS);
}
public static HashMap getAllDeclaredFieldsByName(Class> aClass, int modifiers)
{
FieldMapClosure closure = new ClassUtils.FieldMapClosure(new HashMap(), modifiers);
computeDeclaredFields(aClass, closure);
return closure.getValues();
}
public static T computeDeclaredFields(Class> aClass, FieldClosure function)
{
for (Field field : aClass.getDeclaredFields())
{
function.perform(field);
}
if (aClass.getSuperclass() != null)
{
computeDeclaredFields(aClass.getSuperclass(), function);
}
return function.getValues();
}
// SECTION: INNER CLASSES
public interface FieldClosure
{
void perform(Field argument);
T getValues();
}
private static class FieldListClosure
implements FieldClosure>
{
private List values;
private int ignoredModifiers;
public FieldListClosure(List values, int modifiers)
{
super();
this.values = values;
this.ignoredModifiers = modifiers;
}
@Override
public void perform(Field field)
{
if ((field.getModifiers() & ignoredModifiers) == 0)
{
values.add(field);
}
}
@Override
public List getValues()
{
return values;
}
}
private static class FieldMapClosure
implements FieldClosure
© 2015 - 2025 Weber Informatics LLC | Privacy Policy