Collect all fields having given Modifier(s) I want to collect all fields which have given Modifier(s) of a class. I have written following pieces of codes:

/**
 * Method getFields
 * @author TapasB
 * @since 03-Nov-2013 - 2:03:17 pm
 * @version DAM 1.0
 * @param instanceClass
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static List&lt;Field&gt; getFields(Class&lt;?&gt; instanceClass) {
    List&lt;Field&gt; fields = new ArrayList&lt;Field&gt;();        
    Class&lt;?&gt; searchType = instanceClass;

    while (!Object.class.equals(searchType) &amp;&amp; searchType != null) {
        fields.addAll(Arrays.asList(searchType.getDeclaredFields()));
        searchType = searchType.getSuperclass();
    }

    return Collections.unmodifiableList(new ArrayList(selectDistinct(fields, "name")));
}

/**
 * Method getFields
 * @author TapasB
 * @since 03-Nov-2013 - 2:31:59 pm
 * @version DAM 1.0
 * @param instanceClass
 * @param includedModifiers
 * @param excludedModifiers
 * @return
 */
public static List&lt;Field&gt; getFields(Class&lt;?&gt; instanceClass, int[] includedModifiers, int[] excludedModifiers) {
    List&lt;Field&gt; fields = getFields(instanceClass);
    List&lt;Field&gt; filteredFields = new ArrayList&lt;Field&gt;();

    if(ArrayUtils.isEmpty(includedModifiers)) {
        filteredFields.addAll(fields);
    } else {
        for(Field field : fields) {
            boolean fieldCanBeAdded = false;

            for(int includedModifier : includedModifiers) {
                if((includedModifier &amp; field.getModifiers()) != 0) {
                    fieldCanBeAdded = true;
                }
            }

            if(fieldCanBeAdded) {
                filteredFields.add(field);
            }
        }
    }               

    if(ArrayUtils.isNotEmpty(excludedModifiers)) {
        Iterator&lt;Field&gt; fieldIterator = filteredFields.iterator();

        while (fieldIterator.hasNext()) {
            Field field = fieldIterator.next();
            boolean fieldNeedsToBeRemoved = false;

            for(int excludedModifier : excludedModifiers) {
                if((excludedModifier &amp; field.getModifiers()) != 0) {
                    fieldNeedsToBeRemoved = true;
                }
            }

            if(fieldNeedsToBeRemoved) {
                fieldIterator.remove();
            }
        }
    }   

    return Collections.unmodifiableList(filteredFields);
}


The method getFields(Class&lt;?&gt; instanceClass) searchs for fields in an iterative manner to all the super classes of the given class and stops at Object. 

Here the selectDistinct is the method of LambdaJ API. It removes the fields with same name which is present in the parent class also in child class.

I was wondering if it is a good way to achieve what I want.

To run it:

public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    int[] includedModifiers = new int[]{Modifier.PRIVATE};
    int[] excludedModifiers = new int[]{Modifier.FINAL};
    List&lt;Field&gt; fields = getFields(ArrayList.class, includedModifiers, excludedModifiers);

    for(Field field : fields) {
        System.out.println(field.getName());
    }
}


The main will print only the fields which have modifier only private and exclude the private final.

There is no issue so far I found with this code. I just want to know if there is any better solution exists or not.
I've played with your code and have a few broad observations....


The code is neat enough and generally readable.
JavaDocs are not complete - return values are not documented
@since JavaDoc tag traditionally records a version number, not a date.


I don't think there is a generally 'better' way to do what you are doing, but I think some of the specific details are worth commenting on..... and my last paragraph is an important one, so please make sure you consider it....

Some more specific comments:


Just check that you really need both getFields methods to be public. Exposing just the second version may be a better option and make the getFields(Class&lt;?&gt; instanceClass) version private. This is a matter of style and usage. In general I feel people make things public when they shouldn't. Whether that is right this time or not is your decision though
I feel you overuse the Collections.unmodifiableList(...). Generally this is only useful to protect data that is encapsulated in your class. In this case, the data is not something you need to protect, and worse, you need to 'undo' the unmodifiable state of the list in your exclude/include function. In this case, there is no value in making the lists read-only and if a 'user' decides they want to change the list they can do it without damaging your class, and all you do is make it harder for them (and yourself).
including the LambdaJ function dependency is perhaps unnecessary. The logic you want to do can be accomplished in a few lines with an iterator and a set. The burden of maintaining a dependency on an external library is potentially significant. If you have other needs for LambdaJ then sure, use it, but otherwise you may be satisfied with:

Iterator&lt;Field&gt; it = fields.iterator();
Set&lt;String&gt; names = new HashSet&lt;String&gt;();
while (it.hasNext()) {
    Field f = it.next();
    if (!names.add(f.getName())) {
        it.remove();
    }
}



Finally, I have to question why you want to remove 'shadowed' fields at all. They are different fields and you are losing information if you remove them. Are you sure you want them gone?