Accessor for the declared type of this element. - Java javax.lang.model.element

Java examples for javax.lang.model.element:Element

Description

Accessor for the declared type of this 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/*  w  ww .  j ava  2 s. c  o  m*/

    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 javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;

import javax.lang.model.type.TypeMirror;

public class Main {
    /**
     * Accessor for the declared type of this element.
     * If this is a field then returns the declared type of the field.
     * If this is a java bean getter then returns the return type.
     * @param elem The element
     * @return The declared type
     */
    public static TypeMirror getDeclaredType(Element elem) {
        if (elem.getKind() == ElementKind.FIELD) {
            return elem.asType();
        } else if (elem.getKind() == ElementKind.METHOD) {
            return ((ExecutableElement) elem).getReturnType();
        } else {
            throw new IllegalArgumentException("Unable to get type for "
                    + elem);
        }
    }
}

Related Tutorials