您的位置:首页 > 其它

如何获取方法参数的描述信息

2011-03-07 16:30 288 查看
项目需求:

AtELite中选择控件的某个方法,希望返回该方法的参数个数和各个参数的描述说明。其中参数个数用于构建输入框,各个参数的描述说明用于当鼠标放到输入框上时,用alt的形式显示该输入框对应参数的描述说明。

引申场景:

选择某个类的方法名称,希望能返回该方法产生的个数以及各个参数的描述说明。如:

/**

*设置id和name

*@param 用户Id

*@param 用户姓名

/**

public void setIdAndName(

int id,String name){

}

要求返回setIdAndName的参数个数2和每个参数的描述,如:用户Id,用户姓名

解决方法,引进Annotation来解决该问题。

1.定义Annotation。

2.定义相关类。

3.写测试类。

4.测试结果

原文代码参考:/article/6354728.html

1.定义Annotation。

package com.xyp;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})   //用于字段,方法,参数
@Retention(RetentionPolicy.RUNTIME) //在运行时加载到Annotation到JVM中
public @interface Field_Method_Parameter_Annotation {
Class<?> type() default void.class;  //定义一个具有默认值的Class型成员
String describ();    //定义一个没有默认值的String成员
}


2.定义相关类。

package com.xyp;
public class AnnotationTest {

// 注释字段
@Field_Method_Parameter_Annotation(describ = "字段编号", type = int.class)
int id;

// 注释字段
@Field_Method_Parameter_Annotation(describ = "字段姓名", type = String.class)
String name;
public AnnotationTest() {
}
@Field_Method_Parameter_Annotation(describ = "获得编号", type = int.class)
public int getId() {
return id;
}
@Field_Method_Parameter_Annotation(describ = "设置编号")
public void setId(
// 注释参数
@Field_Method_Parameter_Annotation(describ = "设置编号", type = int.class) int id) {
this.id = id;
}
@Field_Method_Parameter_Annotation(describ = "获得姓名", type = String.class)
public String getName() {
return name;
}
@Field_Method_Parameter_Annotation(describ = "设置姓名")
public void setName(
@Field_Method_Parameter_Annotation(describ = "姓名", type = String.class) String name) {
this.name = name;
}

@Field_Method_Parameter_Annotation(describ = "设置编号和姓名")
public void setIdAndName(
// 注释参数
@Field_Method_Parameter_Annotation(describ = "设置编号", type = int.class) int id,
@Field_Method_Parameter_Annotation(describ = "姓名", type = String.class) String name){
this.id = id;
this.name = name;
}

@Field_Method_Parameter_Annotation(describ = "设置编号和姓名")
public void setIdAndName2(
// 注释参数			@Field_Method_Parameter_Annotation(describ = "设置编号", type = int.class) int id,
String name){
this.id = id;
this.name = name;
}
}


3.写测试类。

package com.xyp;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test {
/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException {
Class<?> cls = Class.forName("com.xyp.AnnotationTest");

//字段
System.out.println("********字段的Annotation*************");
Field[] declaredFields = AnnotationTest.class.getDeclaredFields();   //获得所有的字段
for(int i=0;i<declaredFields.length;i++)
{
Field field = declaredFields[i];
System.out.print("字段" + field.getName() + "的Annotation:");   //获得字段描述
//查看是否具有指定类型的注释:
if(field.isAnnotationPresent(Field_Method_Parameter_Annotation.class))
{
Field_Method_Parameter_Annotation fa = field.getAnnotation(Field_Method_Parameter_Annotation.class);
System.out.print(" " + fa.describ());   //获得字段描述
System.out.println(" " + fa.type());    //获得字段类型
}
}

//方法
System.out.println("********方法的Annotation********");
Method [] methods = AnnotationTest.class.getDeclaredMethods();    //获得所有的方法
for(int i=0;i<methods.length;i++)
{
Method method = methods[i];
//查看是否指定注释:
System.out.println("方法" + method.getName() + "的Annotation:");
if(method.isAnnotationPresent(Field_Method_Parameter_Annotation.class))

{
Field_Method_Parameter_Annotation ma = method.getAnnotation(Field_Method_Parameter_Annotation.class);
System.out.print("   " + ma.describ());   //获得方法描述
System.out.println("   " + ma.type());    //获得方法类型
}
System.out.println("方法" + method.getName() + "的参数的Annotation");
Annotation[][]parameterAnnotations = method.getParameterAnnotations();    //获得所有参数
if (parameterAnnotations.length == 0) {
System.out.println("方法没有参数");
}
for(int j = 0;j < parameterAnnotations.length;j++)
{
int length = parameterAnnotations[j].length;
if(length==0)
{
System.out.println("没有添加Annotation参数");
}
else
{
for(int k=0;k<length;k++)
{
//获得指定的注释:
Field_Method_Parameter_Annotation pa = (Field_Method_Parameter_Annotation)parameterAnnotations[j][k];
System.out.print(" " + pa.describ());   //获得参数描述
System.out.println(" " + pa.type());    //获得参数类型
}
}
}

}
}
}


4.测试结果

********字段的Annotation*************

字段id的Annotation: 字段编号 int

字段name的Annotation: 字段姓名 class java.lang.String

********方法的Annotation********

方法setId的Annotation:

设置编号 void

方法setId的参数的Annotation

设置编号 int

方法setIdAndName的Annotation:

设置编号和姓名 void

方法setIdAndName的参数的Annotation

设置编号 int

姓名 class java.lang.String

方法setIdAndName2的Annotation:

设置编号和姓名 void

方法setIdAndName2的参数的Annotation

设置编号 int

没有添加Annotation参数

方法getName的Annotation:

获得姓名 class java.lang.String

方法getName的参数的Annotation

方法没有参数

方法getId的Annotation:

获得编号 int

方法getId的参数的Annotation

方法没有参数

方法setName的Annotation:

设置姓名 void

方法setName的参数的Annotation

姓名 class java.lang.String

总结:

1.若方法没有参数,则method.getParameterAnnotations返回数组长度为0,否则大于0

2.若方法参数没有Annotation,则该参数的method.getParameterAnnotation[i]的长度为0

3.getParameterAnnotations的返回类型之所以为一个二维数组主要是由于一个参数上可以存在多个annotation。这样第一维分别对应方法的各个参数,长度也和参数个数相等,而第二维则分别对应于一个参数上的多个注annotation,长度和参数上annotation的个数相等
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐