Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use reflection to get fields and methods in class in Java

Shulou Source: shulou.com Published: 2022-06-02 10:47:16 09月28日 Update

Java how to use reflection to get fields and methods in the class, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

1. Custom annotation 1, field annotation import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target; @ Documented@Target ({ElementType.FIELD}) / / annotation application type @ Retention (RetentionPolicy.RUNTIME) / / annotation type public @ interface FieldInterface {String name () default ";} 2. Method annotation import java.lang.annotation.Documented Whether the import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target; @ Documented / / tag includes annotations when generating javadoc, but not @ Target ({ElementType.METHOD}) / / annotation application type @ Retention (RetentionPolicy.RUNTIME) / / annotation type public @ interface MethodInterface {String name () default "" Create entity class public class TestClass {@ FieldInterface (name = "field annotations") private String name = "ls"; private Constructor [] declaredConstructors; @ MethodInterface (name = "method annotations") public void isNot () {}} III. Get annotation value / / method annotation test public void getMethodAnnotationValue () {/ / get all methods Method [] name = TestClass.class.getDeclaredMethods () For (Method str: name) {/ / determine whether there is a note on the method boolean annotationPresent = str.isAnnotationPresent (MethodInterface.class) If (annotationPresent) {/ / get custom annotation object MethodInterface methodAnno = str.getAnnotation (MethodInterface.class); / / get annotation value String isNotNullStr = methodAnno.name () based on the object System.out.println (isNotNullStr);} / / attribute annotation test public void getFieldAnnotationValue () throws NoSuchFieldException, SecurityException {/ / get all fields Field [] fields = TestClass.class.getDeclaredFields () For (Field f: fields) {/ / determines whether a field annotation exists boolean annotationPresent2 = f.isAnnotationPresent (FieldInterface.class); if (annotationPresent2) {FieldInterface name = f.getAnnotation (FieldInterface.class) / / get the annotation value String nameStr = name.name (); System.out.println (nameStr);} IV. Test public static void main (String [] args) throws NoSuchFieldException, SecurityException {TestClass c = new TestClass () / / get the annotation value c.getMethodAnnotationValue () on the method; / / get the annotation value c.getFieldAnnotationValue () on the field;}

Output:

Is it helpful for you to read the above content after reading the comments in the method comment field? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Annotations methods fields types tests objects help applications reflections clarity dispensable content entities this attributes articles novices more tags knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Linux MySQL Docker Shulou Information