SoFunction
Updated on 2025-03-08

Example of Java's method to simplify programming using annotations and reflections

This article describes the method of simplifying programming using annotations and reflections in Java. Share it for your reference, as follows:

A finishing touch

When a large number of methods are called, reflection and annotation can be used to simplify programming.

Two code

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@Documented
@Retention()
@Target()
@interface testAnnoation10 {
  public String name() default "methodname";
  public String unit() default "unit";
}
public class ch11_10 {
  public static void main( String[] args ) throws Exception {
    ch11_10 ch9 = new ch11_10();
    Method method[] = ().getMethods();
    for (Method method2 : method) {
      Annotation annotation = ();
      Class<?> ts[] = ();
      if (().indexOf("getData") == -1) continue;
      ArrayList<Object> params = new ArrayList<Object>();
      for (Class<?> class1 : ts) {
        if (().equals("int")) {
          (10);
        }
        if (().equals("String")) {
          ("100");
        }
      }
      if (annotation != null) {
        testAnnoation10 t9 = (testAnnoation10) annotation;
        (() + " is " + (ch9, ()) + " " + ());
      }
    }
  }
  @testAnnoation10(name = "SOC", unit = "%")
  public int getData1( int a ) {
    return a;
  }
  @testAnnoation10(name = "Electricity", unit = "Ah")
  public String getData2( String b ) {
    return b;
  }
  @testAnnoation10(name = "Tempreture", unit = "AF")
  public int getData3( int a, int b ) {
    return a + b;
  }
}

Three Run

Tempreture is 20 AF
Electricity is 100 Ah
SOC is 10 %

For more Java-related content, please view the topic of this site:Introduction and Advanced Tutorial on Object-Oriented Programming in Java》、《Java Data Structure and Algorithm Tutorial》、《Summary of Java operating DOM node skills》、《Summary of Java files and directory operation skills"and"Summary of Java cache operation skills

I hope this article will be helpful to everyone's Java programming.