Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 367|回复: 0

[注解学习]注释处理一例

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-29 23:58:06 | 显示全部楼层 |阅读模式
    通过注释给按钮添加动作监听器。来自一本教科书。
        一、自定义注释
    import java.lang.annotation.*;
        @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)

    public @interface ActionListenerFor
    {
        String source();
    }
        这个注释能用于方法,且编译器会将注释信息加入类文件中,运行时虚拟机将载入这些信息。
       
       
       
       
       

       
      
      
      二、给方法添加注释信息
    1. /**
    2.    @author Cay Horstmann
    3. */
    4. import java.awt.*;
    5. import java.awt.event.*;
    6. import javax.swing.*;
    7. public class ButtonTest
    8. {  
    9.    public static void main(String[] args)
    10.    {  
    11.       ButtonFrame frame = new ButtonFrame();
    12.       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    13.       frame.setVisible(true);
    14.    }
    15. }
    16. /**
    17.    A frame with a button panel
    18. */
    19. class ButtonFrame extends JFrame
    20. {
    21.    public ButtonFrame()
    22.    {
    23.       setTitle("ButtonTest");
    24.       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    25.       panel = new JPanel();
    26.       add(panel);
    27.       // create buttons
    28.       yellowButton = new JButton("Yellow");
    29.       blueButton = new JButton("Blue");
    30.       redButton = new JButton("Red");
    31.       // add buttons to panel
    32.       panel.add(yellowButton);
    33.       panel.add(blueButton);
    34.       panel.add(redButton);
    35.       ActionListenerInstaller.processAnnotations(this);
    36.    }
    37.    @ActionListenerFor(source="yellowButton")
    38.    public void yellowBackground()
    39.    {
    40.       panel.setBackground(Color.YELLOW);
    41.    }
    42.    @ActionListenerFor(source="blueButton")
    43.    public void blueBackground()
    44.    {
    45.       panel.setBackground(Color.BLUE);
    46.    }
    47.    @ActionListenerFor(source="redButton")
    48.    public void redBackground()
    49.    {
    50.       panel.setBackground(Color.RED);
    51.    }
    52.    public static final int DEFAULT_WIDTH = 300;
    53.    public static final int DEFAULT_HEIGHT = 200;  
    54.    private JPanel panel;
    55.    private JButton yellowButton;
    56.    private JButton blueButton;
    57.    private JButton redButton;
    58. }

    59. 三、利用反射和动态代理处理注释。

    60. import java.awt.event.*;
    61. import java.lang.annotation.*;
    62. import java.lang.reflect.*;
    63. public class ActionListenerInstaller
    64. {
    65.    /**
    66.       Processes all ActionListenerFor annotations in the given object.
    67.       @param obj an object whose methods may have ActionListenerFor annotations
    68.    */
    69.    public static void processAnnotations(Object obj)
    70.    {
    71.       try
    72.       {
    73.          Class cl = obj.getClass();
    74.          
    75.          for (Method m : cl.getDeclaredMethods())
    76.          {
    77.             System.out.println(m.getName());
    78.             ActionListenerFor a = m.getAnnotation(ActionListenerFor.class);
    79.             if (a != null)
    80.             {
    81.                Field f = cl.getDeclaredField(a.source());
    82.                f.setAccessible(true);
    83.                addListener(f.get(obj), obj, m);
    84.             }
    85.          }
    86.       }
    87.       catch (Exception e)
    88.       {
    89.          e.printStackTrace();
    90.       }
    91.    }
    92.    /**
    93.       Adds an action listener that calls a given method.
    94.       @param source the event source to which an action listener is added
    95.       @param param the implicit parameter of the method that the listener calls
    96.       @param m the method that the listener calls
    97.    */
    98.    public static void addListener(Object source, final Object param, final Method m)
    99.       throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
    100.    {
    101.       InvocationHandler handler = new
    102.          InvocationHandler()
    103.          {
    104.             public Object invoke(Object proxy, Method mm, Object[] args) throws Throwable
    105.             {
    106.                return m.invoke(param);                    
    107.             }
    108.          };
    109.    
    110.       Object listener = Proxy.newProxyInstance(null, new Class[] { java.awt.event.ActionListener.class },
    111.          handler);
    112.       Method adder = source.getClass().getMethod("addActionListener", ActionListener.class);
    113.       adder.invoke(source, listener);
    114.    }
    115. }
    复制代码

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/29/235805906.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 23:27 , Processed in 0.369678 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表