使用 Java 反射显示 java class 中可用的方法注释
Display the method annotations available in a java class using Java reflection
我目前正在做一个 java 项目,需要显示 java class 的元信息,例如为方法、参数等声明的注释。
我有以下 classes
Employee.java
package labsheet;
public class Employee {
private String eid = "E001";
String ename = "Anura";
public String address = "Batticaloa";
protected double salary = 60_000.00;
public String getEid() {
return eid;
}
public void setEid(String eid) {
this.eid = eid;
}
String getEname() {
return ename;
}
void setEname(String ename) {
this.ename = ename;
}
protected String getAddress() {
return address;
}
protected void setAddress(String address) {
this.address = address;
}
@SuppressWarnings("unused")
private double getSalary() {
return salary;
}
@SuppressWarnings("unused")
private void setSalary(double salary) {
this.salary = salary;
}
public String display(String eid, String ename, String address, double salary){
System.out.println("Method invoked successfully");
return eid+" , "+ename+" , "+address+" , "+salary;
}
}
在我的主要 class Task3.java 我试图显示在Employee.java。因此,我打算使用 Java 反射显示方法 getSalary() 和 setSalary() 的 @SuppressWarnings("unused")
详细信息。
我目前已经将 Task3.java 编码如下。
Task3.java
package labsheet;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
public class Task3 {
public static void main(String[] args) {
try {
Class<?> clazz = Class.forName("labsheet.Employee");
Method[] methods = clazz.getDeclaredMethods();
for(int m =0; m<methods.length;m++){
System.out.println("Modifier=> "+ Modifier.toString(methods[m].getModifiers()));
Annotation [] annotations = methods[m].getAnnotations();
System.out.println("Annotation count: "+annotations.length);
for(int o =0; o<annotations.length;o++){
System.out.println("Annotation "+(o+1)+": "+annotations[o]);
}
System.out.print("|| Return Type=> "+ methods[m].getReturnType());
System.out.print("|| Method Name=> "+ methods[m].getName());
Parameter[] parameters = methods[m].getParameters();
if(parameters.length != 0){
System.out.print("|| Method Parameters=> ");
for(int u = 0; u<parameters.length;u++){
if(u== parameters.length-1){
System.out.print(parameters[u].getType().getSimpleName());
}
else{
System.out.print(parameters[u].getType().getSimpleName()+" ");
}
}
}
System.out.println();
System.out.println();
}
System.out.println("*******************************");
executeMethod();
System.out.println("*******************************");
executeMethodWithDefault();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
...
//Methods declaration for executeMethod() and executeMethodWithDefault()
}
但是我得到一个输出,其中显示 getSalary() 和 setSalary() 方法也没有注释,而我需要在此处显示 @SuppressWarnings("unused")
注释详细信息。
输出
Modifier=> protected
Annotation count: 0
|| Return Type=> class java.lang.String|| Method Name=> getAddress
Modifier=> public
Annotation count: 0
|| Return Type=> class java.lang.String|| Method Name=> display|| Method Parameters=> String String String double
Modifier=> private
Annotation count: 0
|| Return Type=> void|| Method Name=> setSalary|| Method Parameters=> double
Modifier=>
Annotation count: 0
|| Return Type=> class java.lang.String|| Method Name=> getEname
Modifier=> protected
Annotation count: 0
|| Return Type=> void|| Method Name=> setAddress|| Method Parameters=> String
Modifier=>
Annotation count: 0
|| Return Type=> void|| Method Name=> setEname|| Method Parameters=> String
Modifier=> private
Annotation count: 0
|| Return Type=> double|| Method Name=> getSalary
Modifier=> public
Annotation count: 0
|| Return Type=> class java.lang.String|| Method Name=> getEid
Modifier=> public
Annotation count: 0
|| Return Type=> void|| Method Name=> setEid|| Method Parameters=> String
如有任何建议,我们将不胜感激。
看java.lang.SuppressWarnings
class:
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
// code...
}
它有 SOURCE
保留政策(参见 java.lang.annotation.RetentionPolicy
class):
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE,
// code...
}
这意味着无法在运行时获取有关注释的信息,因为编译器不会将其放入 class 文件中。
我目前正在做一个 java 项目,需要显示 java class 的元信息,例如为方法、参数等声明的注释。
我有以下 classes
Employee.java
package labsheet;
public class Employee {
private String eid = "E001";
String ename = "Anura";
public String address = "Batticaloa";
protected double salary = 60_000.00;
public String getEid() {
return eid;
}
public void setEid(String eid) {
this.eid = eid;
}
String getEname() {
return ename;
}
void setEname(String ename) {
this.ename = ename;
}
protected String getAddress() {
return address;
}
protected void setAddress(String address) {
this.address = address;
}
@SuppressWarnings("unused")
private double getSalary() {
return salary;
}
@SuppressWarnings("unused")
private void setSalary(double salary) {
this.salary = salary;
}
public String display(String eid, String ename, String address, double salary){
System.out.println("Method invoked successfully");
return eid+" , "+ename+" , "+address+" , "+salary;
}
}
在我的主要 class Task3.java 我试图显示在Employee.java。因此,我打算使用 Java 反射显示方法 getSalary() 和 setSalary() 的 @SuppressWarnings("unused")
详细信息。
我目前已经将 Task3.java 编码如下。
Task3.java
package labsheet;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
public class Task3 {
public static void main(String[] args) {
try {
Class<?> clazz = Class.forName("labsheet.Employee");
Method[] methods = clazz.getDeclaredMethods();
for(int m =0; m<methods.length;m++){
System.out.println("Modifier=> "+ Modifier.toString(methods[m].getModifiers()));
Annotation [] annotations = methods[m].getAnnotations();
System.out.println("Annotation count: "+annotations.length);
for(int o =0; o<annotations.length;o++){
System.out.println("Annotation "+(o+1)+": "+annotations[o]);
}
System.out.print("|| Return Type=> "+ methods[m].getReturnType());
System.out.print("|| Method Name=> "+ methods[m].getName());
Parameter[] parameters = methods[m].getParameters();
if(parameters.length != 0){
System.out.print("|| Method Parameters=> ");
for(int u = 0; u<parameters.length;u++){
if(u== parameters.length-1){
System.out.print(parameters[u].getType().getSimpleName());
}
else{
System.out.print(parameters[u].getType().getSimpleName()+" ");
}
}
}
System.out.println();
System.out.println();
}
System.out.println("*******************************");
executeMethod();
System.out.println("*******************************");
executeMethodWithDefault();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
...
//Methods declaration for executeMethod() and executeMethodWithDefault()
}
但是我得到一个输出,其中显示 getSalary() 和 setSalary() 方法也没有注释,而我需要在此处显示 @SuppressWarnings("unused")
注释详细信息。
输出
Modifier=> protected
Annotation count: 0
|| Return Type=> class java.lang.String|| Method Name=> getAddress
Modifier=> public
Annotation count: 0
|| Return Type=> class java.lang.String|| Method Name=> display|| Method Parameters=> String String String double
Modifier=> private
Annotation count: 0
|| Return Type=> void|| Method Name=> setSalary|| Method Parameters=> double
Modifier=>
Annotation count: 0
|| Return Type=> class java.lang.String|| Method Name=> getEname
Modifier=> protected
Annotation count: 0
|| Return Type=> void|| Method Name=> setAddress|| Method Parameters=> String
Modifier=>
Annotation count: 0
|| Return Type=> void|| Method Name=> setEname|| Method Parameters=> String
Modifier=> private
Annotation count: 0
|| Return Type=> double|| Method Name=> getSalary
Modifier=> public
Annotation count: 0
|| Return Type=> class java.lang.String|| Method Name=> getEid
Modifier=> public
Annotation count: 0
|| Return Type=> void|| Method Name=> setEid|| Method Parameters=> String
如有任何建议,我们将不胜感激。
看java.lang.SuppressWarnings
class:
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
// code...
}
它有 SOURCE
保留政策(参见 java.lang.annotation.RetentionPolicy
class):
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE,
// code...
}
这意味着无法在运行时获取有关注释的信息,因为编译器不会将其放入 class 文件中。