java注解是怎么实现的

java注解是怎么实现的,第1张

注解的使用一般是与java的反射一起使用,下面是羡腔磨一个例子

注解相当于一种标记,在程序中加了注解就等于为程序打上了某种标记,没加,则等于没有某种标记,以后,javac编译器,开发工具和其他程序可以用反射来了解你的类及各种元素上有无何种标记,看你有什么标记,就去干相应的事。标记可以加在包,类,字段,方法,方法的参数以及局部变量上。

自定义注解及其应用

1)、定义一个最简单的注解

public @interface MyAnnotation {

//......

}

2)、把注解加在某个类上:

@MyAnnotation

public class AnnotationTest{

//......

}

以下为模拟案例

自定义注解@MyAnnotation

1 package com.ljq.test

2

3 import java.lang.annotation.ElementType

4 import java.lang.annotation.Retention

5 import java.lang.annotation.RetentionPolicy

6 import java.lang.annotation.Target

7

8 /**

9 * 定义一个注解

10 *

11 *

12 * @author jiqinlin

13 *

14 */

15 //Java中提供了四种元注解,专门负责注解其他的注解,分别如下

16

17 //@Retention元注解,表示需要在什么级圆者别保存该注释信息(生命周期)。可选的RetentionPoicy参数包括:

18 //RetentionPolicy.SOURCE: 停留在java源文件,编译器被丢掉

19 //RetentionPolicy.CLASS:停留在class文件中,但会被VM丢弃(默认)

20 //RetentionPolicy.RUNTIME:内存中的字节码,VM将兄斗在运行时也保留注解,因此可以通过反射机制读取注解的信息

21

22 //@Target元注解,默认值为任何元素,表示该注解用于什么地方。可用的ElementType参数包括

23 //ElementType.CONSTRUCTOR: 构造器声明

24 //ElementType.FIELD: 成员变量、对象、属性(包括enum实例)

25 //ElementType.LOCAL_VARIABLE: 局部变量声明

26 //ElementType.METHOD: 方法声明

27 //ElementType.PACKAGE: 包声明

28 //ElementType.PARAMETER: 参数声明

29 //ElementType.TYPE: 类、接口(包括注解类型)或enum声明

30

31 //@Documented将注解包含在JavaDoc中

32

33 //@Inheried允许子类继承父类中的注解

34

35

36 @Retention(RetentionPolicy.RUNTIME)

37 @Target({ElementType.METHOD, ElementType.TYPE})

38 public @interface MyAnnotation {

39 //为注解添加属性

40 String color()

41 String value() default "我是林计钦"//为属性提供默认值

42 int[] array() default {1, 2, 3}

43 Gender gender() default Gender.MAN//添加一个枚举

44 MetaAnnotation metaAnnotation() default @MetaAnnotation(birthday="我的出身日期为1988-2-18")

45 //添加枚举属性

46

47 }

注解测试类AnnotationTest

1 package com.ljq.test

2

3 /**

4 * 注解测试类

5 *

6 *

7 * @author jiqinlin

8 *

9 */

10 //调用注解并赋值

11 @MyAnnotation(metaAnnotation=@MetaAnnotation(birthday = "我的出身日期为1988-2-18"),color="red", array={23, 26})

12 public class AnnotationTest {

13

14 public static void main(String[] args) {

15 //检查类AnnotationTest是否含有@MyAnnotation注解

16 if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){

17 //若存在就获取注解

18 MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class)

19 System.out.println(annotation)

20 //获取注解属性

21 System.out.println(annotation.color())

22 System.out.println(annotation.value())

23 //数组

24 int[] arrs=annotation.array()

25 for(int arr:arrs){

26 System.out.println(arr)

27 }

28 //枚举

29 Gender gender=annotation.gender()

30 System.out.println("性别为:"+gender)

31 //获取注解属性

32 MetaAnnotation meta=annotation.metaAnnotation()

33 System.out.println(meta.birthday())

34 }

35 }

36 }

枚举类Gender,模拟注解中添加枚举属性

1 package com.ljq.test

2 /**

3 * 枚举,模拟注解中添加枚举属性

4 *

5 * @author jiqinlin

6 *

7 */

8 public enum Gender {

9 MAN{

10 public String getName(){return "男"}

11 },

12 WOMEN{

13 public String getName(){return "女"}

14 }//记得有“”

15 public abstract String getName()

16 }

注解类MetaAnnotation,模拟注解中添加注解属性

1 package com.ljq.test

2

3 /**

4 * 定义一个注解,模拟注解中添加注解属性

5 *

6 * @author jiqinlin

7 *

8 */

9 public @interface MetaAnnotation {

10 String birthday()

11 }

package com.syl.demo.test

import java.io.*

/**

* java代码行数统计工具类

* Created by 孙义朗 on 2017/11/17 0017.

*/

public class CountCodeLineUtil {

private static int normalLines = 0 //有效程序行数

private static int whiteLines = 0 //空白行数

private static int commentLines = 0//注释行数

public static void countCodeLine(File file) {

System.out.println("代码行数统计:" + file.getAbsolutePath())

if (file.exists()) {

try {

scanFile(file)

} catch (IOException e) {

e.printStackTrace()

}

} else {

System.out.println("文件不存在!")

System.exit(0)

}

System.out.println(file.getAbsolutePath() + " ,java文件统计:" +

"总有效代铅知纳码行数: " + normalLines +

" ,总空白行数:" + whiteLines +

" ,总注释行数:" + commentLines +

" ,总行数:" + (normalLines + whiteLines + commentLines))

}

private static void scanFile(File file) throws IOException {

if (file.isDirectory()) {

File[] files = file.listFiles()

for (int i = 0i <files.lengthi++) {

scanFile(files[i])

}

}

if (file.isFile()) {

if (file.getName().endsWith(".java")) {

count(file)

}

}

}

private static void count(File file) {

BufferedReader br = null

// 判断此猛咐行是否为注释行

boolean comment = false

int temp_whiteLines = 0

int temp_commentLines = 0

int temp_normalLines = 0

try {

br = new BufferedReader(new FileReader(file))

String line = ""

while ((line = br.readLine()) != null) {

line = line.trim()

if (line.matches("^[//s&&[^//n]]*$")) {

// 空行

whiteLines++

temp_whiteLines++

} else if (line.startsWith("/*") &&!line.endsWith("*/")) {

// 判断此行为"/*"开头的注释行

commentLines++

comment = true

} else if (comment == true &&!line.endsWith("*/")) {

// 为多行注释槐没中的一行(不是开头和结尾)

commentLines++

temp_commentLines++

} else if (comment == true &&line.endsWith("*/")) {

// 为多行注释的结束行

commentLines++

temp_commentLines++

comment = false

} else if (line.startsWith("//")) {

// 单行注释行

commentLines++

temp_commentLines++

} else {

// 正常代码行

normalLines++

temp_normalLines++

}

}

System.out.println(file.getName() +

" ,有效行数" + temp_normalLines +

" ,空白行数" + temp_whiteLines +

" ,注释行数" + temp_commentLines +

" ,总行数" + (temp_normalLines + temp_whiteLines + temp_commentLines))

} catch (FileNotFoundException e) {

e.printStackTrace()

} catch (IOException e) {

e.printStackTrace()

} finally {

if (br != null) {

try {

br.close()

br = null

} catch (IOException e) {

e.printStackTrace()

}

}

}

}

//测试

public static void main(String[] args) {

File file = new File("F:\\myweb")

countCodeLine(file)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12364440.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存