Java Reflection Generic Type getGenericString(AccessibleObject ao)

Here you can find the source of getGenericString(AccessibleObject ao)

Description

Gets the generic string of the passed AccessibleObject

License

Apache License

Parameter

Parameter Description
ao the AccessibleObject to get the generic string for

Return

the generic string of the Accessible Object

Declaration

public static String getGenericString(AccessibleObject ao) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w. ja  v  a2s.  com*/
 * Copyright 2015 the original author or authors.
 *
 * 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.
 */

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Main {
    /**
     * Gets the generic string of the passed AccessibleObject
     * @param ao the AccessibleObject to get the generic string for
     * @return the generic string of the Accessible Object
     */
    public static String getGenericString(AccessibleObject ao) {
        if (ao == null)
            return null;
        if (ao instanceof Method) {
            return ((Method) ao).toGenericString();
        } else if (ao instanceof Constructor) {
            return ((Constructor<?>) ao).toGenericString();
        } else if (ao instanceof Field) {
            Field f = (Field) ao;
            return String.format("%s:%s(%s)", f.getDeclaringClass().getName(), f.getName(), f.getType().getName());
        } else {
            throw new RuntimeException("Unknow AccessibleObject type [" + ao.getClass().getName() + "]");
        }
    }

    /**
     * Gets the name of the passed AccessibleObject
     * @param ao the AccessibleObject to get the name for
     * @return the name of the Accessible Object
     */
    public static String getName(AccessibleObject ao) {
        if (ao == null)
            return null;
        if (ao instanceof Method) {
            return ((Method) ao).getName();
        } else if (ao instanceof Constructor) {
            return ((Constructor<?>) ao).getDeclaringClass().getSimpleName();
        } else if (ao instanceof Field) {
            return ((Field) ao).getName();
        } else {
            throw new RuntimeException("Unknow AccessibleObject type [" + ao.getClass().getName() + "]");
        }
    }

    /**
     * Gets the declaring class of the passed AccessibleObject
     * @param ao the AccessibleObject to get the declaring class for
     * @return the declaring class
     */
    public static Class<?> getDeclaringClass(AccessibleObject ao) {
        if (ao == null)
            return null;
        if (ao instanceof Method) {
            return ((Method) ao).getDeclaringClass();
        } else if (ao instanceof Constructor) {
            return ((Constructor<?>) ao).getDeclaringClass();
        } else if (ao instanceof Field) {
            return ((Field) ao).getDeclaringClass();
        } else {
            throw new RuntimeException("Unknow AccessibleObject type [" + ao.getClass().getName() + "]");
        }
    }
}

Related

  1. getGenericArguments(Type type)
  2. getGenericElementType(Type type)
  3. getGenericFirst(Object obj)
  4. getGenerics(Type genericType)
  5. getGenerics(Type t)
  6. getGenericSuperType(Type t)
  7. getGenericType(@Nullable Type genericType)
  8. getGenericType(Member member, int index)
  9. getGenericType(Method setter)