当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Method类 getAnnotatedReturnType()用法及代码示例


java.lang.reflect.Method.getAnnotatedReturnType()方法返回一个AnnotatedType对象,该对象表示AnnotatedType以指定方法对象的返回类型。如果为构造函数创建了Method对象,则AnnotatedType对象将指定所构造对象的类型。如果为方法创建了Method对象,则AnnotatedType对象将指定使用类型来指定源代码中指定的方法的返回类型。

返回的AnnotatedType表示AnnotatedType本身或其任何sub-interfaces的实现,例如AnnotatedArrayType,AnnotatedParameterizedType,AnnotatedTypeVariable,AnnotatedWildcardType。 AnnotatedType表示可能注释的任何类型的使用,包括Java虚拟机中当前正在运行的任何类型,包括数组类型,参数化类型,类型变量或通配符类型。

用法:



public AnnotatedType getAnnotatedReturnType()

返回值:此方法返回一个AnnotatedType对象,该对象表示AnnotatedType,以指定方法对象的返回类型。

以下示例程序旨在说明Method类的getAnnotatedReturnType()方法:

范例1:将AnnotatedType()用于指定为Input的特定方法。

该程序包含一个方法名称getAddressMethod,其中包含AnnotatedType。因此,该程序将获取由getAddressMethod方法包含的AnnotatedType的详细信息。

// Java program to demonstrate how to 
// apply getAnnotatedReturnType() method 
// of Method Class. 
  
import java.lang.annotation.*; 
import java.lang.reflect.AnnotatedType; 
import java.lang.reflect.Method; 
import java.util.Arrays; 
  
public class GFG { 
  
    // Creating custom AnnotatedType 
    @Target({ ElementType.TYPE_USE }) 
    @Retention(RetentionPolicy.RUNTIME) 
    private @interface customAnnotatedType { 
    } 
  
    // a sample method with return type String and 
    // AnnotatedType is @customAnnotatedType 
    public @customAnnotatedType String getAddress() 
    { 
        return null; 
    } 
  
    // main method 
    public static void main(String[] args) 
    { 
  
        try { 
            // create class object 
            Class classobj = GFG.class; 
  
            // create method object of getAddress 
            Method getAddressMethod = null; 
  
            Method[] methods = classobj.getMethods(); 
            for (Method m:methods) { 
                if (m.getName().equals("getAddress")) 
                    getAddressMethod = m; 
            } 
  
            // get AnnotatedType for return type 
            AnnotatedType annotatedType = getAddressMethod 
                                              .getAnnotatedReturnType(); 
  
            // print AnnotatedType details with Method name 
            System.out.println("Method Name:"
                               + getAddressMethod.getName()); 
  
            System.out.println("Type:"
                               + annotatedType.getType().getTypeName()); 
  
            System.out.println("Annotations:"
                               + Arrays.toString(annotatedType.getAnnotations())); 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
}
输出:
Method Name:getAddress
Type:java.lang.String
Annotations:[@GFG$customAnnotatedType()]

范例2:在数组类型或多维数组或GFG类的方法的泛型上打印注释。

// Java program to demonstrate how to 
// apply getAnnotatedReturnType() method 
// of Method Class. 
  
import java.lang.annotation.*; 
import java.lang.reflect.*; 
import java.util.List; 
  
public class systemUTCMethodDemo<T> { 
  
    // Creating custom AnnotatedType 
    @Target({ ElementType.TYPE_USE }) 
    @Retention(RetentionPolicy.RUNTIME) 
    private @interface customAnnotatedType { 
    } 
  
    // a sample method with Annotation on array type 
    public @customAnnotatedType String[] getAddress() 
    { 
        return null; 
    } 
  
    // a sample method on Annotation on multidimensional array 
    public String[] @customAnnotatedType[] getvalues() 
    { 
        return null; 
    } 
  
    // a sample method on Annotation on a type with generic 
    public @customAnnotatedType List<T> getWords() 
    { 
        return null; 
    } 
  
    // main method 
    public static void main(String[] args) 
    { 
  
        try { 
            // create class object 
            Class classobj = systemUTCMethodDemo.class; 
  
            // create method object of getAddress and getValues 
            Method[] methods = classobj.getMethods(); 
  
            for (Method m:methods) { 
  
                // if method object is for getAddress and getValues 
                // then print @customAnnotatedType for both 
                if (m.getName().equals("getAddress") 
                    || m.getName().equals("getvalues") 
                    || m.getName().equals("getWords")) { 
                    printDetails(m); 
                } 
            } 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
  
    public static void printDetails(Method m) 
    { 
  
        // get AnnotatedType for return type 
        AnnotatedType annotatedType = m.getAnnotatedReturnType(); 
  
        // print AnnotatedType details with Method name 
  
        System.out.println("Method Name:" + m.getName()); 
        System.out.println("Type:" + annotatedType.getType().getTypeName()); 
        System.out.println("Annotations:" + annotatedType); 
        System.out.println(); 
    } 
}
输出:
Method Name:getWords
Type:java.util.List
Annotations:sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl@12a3a380

Method Name:getvalues
Type:java.lang.String[][]
Annotations:sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl@29453f44

Method Name:getAddress
Type:java.lang.String[]
Annotations:sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedArrayTypeImpl@5cad8086

参考: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/AnnotatedType.html




相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Method Class | getAnnotatedReturnType() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。