Convenience accessor for all field members of the supplied type element. - Java Reflection

Java examples for Reflection:Field

Description

Convenience accessor for all field members of the supplied type element.

Demo Code

/**********************************************************************
Copyright (c) 2010 Andy Jefferson and others. All rights reserved.
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//from  w  w w  .  ja  va  2s  .c  om

    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.

Contributors:
   ...
 **********************************************************************/
//package com.java2s;
import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;

import javax.lang.model.element.TypeElement;

public class Main {
    /**
     * Convenience accessor for all field members of the supplied type element.
     * @param el The type element
     * @return The field members
     */
    public static List<? extends Element> getFieldMembers(TypeElement el) {
        List<? extends Element> members = el.getEnclosedElements();
        List<Element> fieldMembers = new ArrayList<Element>();
        Iterator<? extends Element> memberIter = members.iterator();
        while (memberIter.hasNext()) {
            Element member = memberIter.next();
            if (member.getKind() == ElementKind.FIELD) {
                fieldMembers.add(member);
            }
        }
        return fieldMembers;
    }
}

Related Tutorials