当前位置: 移动技术网 > IT编程>开发语言>Java > Java8 Lambda表达式模板方法实现解析

Java8 Lambda表达式模板方法实现解析

2020年08月30日  | 移动技术网IT编程  | 我要评论
java注解提供了关于代码的一些信息,但并不直接作用于它所注解的代码内容。在这个教程当中,我们将学习java的注解,如何定制注解,注解的使用以及如何通过反射解析注解。java1.5引入了注解,当前许多

java注解提供了关于代码的一些信息,但并不直接作用于它所注解的代码内容。在这个教程当中,我们将学习java的注解,如何定制注解,注解的使用以及如何通过反射解析注解。

java1.5引入了注解,当前许多java框架中大量使用注解,如hibernate、jersey、spring。注解作为程序的元数据嵌入到程序当中。注解可以被一些解析工具或者是编译工具进行解析。我们也可以声明注解在编译过程或执行时产生作用。

在使用注解之前,程序源数据只是通过java注释和javadoc,但是注解提供的功能要远远超过这些。注解不仅包含了元数据,它还可以作用于程序运行过程中、注解解释器可以通过注解决定程序的执行顺序。例如,在jersey webservice 我们为方法添加uri字符串的形式的path注解,那么在程序运行过程中jerser解释程序将决定该方法去调用所给的uri。

创建java自定义注解

创建自定义注解和创建一个接口相似,但是注解的interface关键字需要以@符号开头。我们可以为注解声明方法。我们先来看看注解的例子,然后我们将讨论他的一些特性。

package com.journaldev.annotations;

import java.lang.annotation.documented;
import java.lang.annotation.elementtype;
import java.lang.annotation.inherited;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;

@documented
@target(elementtype.method)
@inherited
@retention(retentionpolicy.runtime)
  public @interface methodinfo{
  string author() default 'pankaj';
  string date();
  int revision() default 1;
  string comments();
}
  • 注解方法不能带有参数;
  • 注解方法返回值类型限定为:基本类型、string、enums、annotation或者是这些类型的数组;
  • 注解方法可以有默认值;
  • 注解本身能够包含元注解,元注解被用来注解其它注解。

这里有四种类型的元注解:

  • @documented —— 指明拥有这个注解的元素可以被javadoc此类的工具文档化。这种类型应该用于注解那些影响客户使用带注释的元素声明的类型。如果一种声明使用documented进行注解,这种类型的注解被作为被标注的程序成员的公共api。
  • @target——指明该类型的注解可以注解的程序元素的范围。该元注解的取值可以为type,method,constructor,field等。如果target元注解没有出现,那么定义的注解可以应用于程序的任何元素。
  • @inherited——指明该注解类型被自动继承。如果用户在当前类中查询这个元注解类型并且当前类的声明中不包含这个元注解类型,那么也将自动查询当前类的父类是否存在inherited元注解,这个动作将被重复执行知道这个标注类型被找到,或者是查询到顶层的父类。
  • @retention——指明了该annotation被保留的时间长短。retentionpolicy取值为source,class,runtime。

java内建注解

java提供了三种内建注解。

  • @override——当我们想要复写父类中的方法时,我们需要使用该注解去告知编译器我们想要复写这个方法。这样一来当父类中的方法移除或者发生更改时编译器将提示错误信息。
  • @deprecated——当我们希望编译器知道某一方法不建议使用时,我们应该使用这个注解。java在javadoc 中推荐使用该注解,我们应该提供为什么该方法不推荐使用以及替代的方法。
  • @suppresswarnings——这个仅仅是告诉编译器忽略特定的警告信息,例如在泛型中使用原生数据类型。它的保留策略是source(译者注:在源文件中有效)并且被编译器丢弃。

我们来看一个java内建注解的例子参照上边提到的自定义注解。

package com.journaldev.annotations;

import java.io.filenotfoundexception;
import java.util.arraylist;
import java.util.list;

public class annotationexample {

public static void main(string[] args) {
}

@override
@methodinfo(author = 'pankaj', comments = 'main method', date = 'nov 17 2012', revision = 1)
public string tostring() {
  return 'overriden tostring method';
}

@deprecated
@methodinfo(comments = 'deprecated method', date = 'nov 17 2012')
public static void oldmethod() {
  system.out.println('old method, don't use it.');
}

@suppresswarnings({ 'unchecked', 'deprecation' })
@methodinfo(author = 'pankaj', comments = 'main method', date = 'nov 17 2012', revision = 10)
public static void genericstest() throws filenotfoundexception {
  list l = new arraylist();
  l.add('abc');
  oldmethod();
}

}

相信这个例子可以不言自明并能展示在不同场景下的应用。

java注解解析

我们将使用反射技术来解析java类的注解。那么注解的retentionpolicy应该设置为runtime否则java类的注解信息在执行过程中将不可用那么我们也不能从中得到任何和注解有关的数据。

package com.journaldev.annotations;

import java.lang.annotation.annotation;
import java.lang.reflect.method;

public class annotationparsing {

public static void main(string[] args) {
  try {
  for (method method : annotationparsing.class
    .getclassloader()
    .loadclass(('com.journaldev.annotations.annotationexample'))
    .getmethods()) {
    // checks if methodinfo annotation is present for the method
    if (method.isannotationpresent(com.journaldev.annotations.methodinfo.class)) {
      try {
    // iterates all the annotations available in the method
        for (annotation anno : method.getdeclaredannotations()) {
          system.out.println('annotation in method ''+ method + '' : ' + anno);
          }
        methodinfo methodanno = method.getannotation(methodinfo.class);
        if (methodanno.revision() == 1) {
          system.out.println('method with revision no 1 = '+ method);
          }

      } catch (throwable ex) {
          ex.printstacktrace();
          }
    }
  }
  } catch (securityexception | classnotfoundexception e) {
      e.printstacktrace();
     }
  }

}

运行上面程序将输出:

annotation in method 'public java.lang.string com.journaldev.annotations.annotationexample.tostring()' : @com.journaldev.annotations.methodinfo(author=pankaj, revision=1, comments=main method, date=nov 17 2012)
method with revision no 1 = public java.lang.string com.journaldev.annotations.annotationexample.tostring()
annotation in method 'public static void com.journaldev.annotations.annotationexample.oldmethod()' : @java.lang.deprecated()
annotation in method 'public static void com.journaldev.annotations.annotationexample.oldmethod()' : @com.journaldev.annotations.methodinfo(author=pankaj, revision=1, comments=deprecated method, date=nov 17 2012)
method with revision no 1 = public static void com.journaldev.annotations.annotationexample.oldmethod()
annotation in method 'public static void com.journaldev.annotations.annotationexample.genericstest() throws java.io.filenotfoundexception' : @com.journaldev.annotations.methodinfo(author=pankaj, revision=10, comments=main method, date=nov 17 2012)

这就是该教程的全部内容,希望你可以从中学到些东西。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网