Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report