作为课程计划的一部分,我们被要求对代码进行注释。
对于以下代码

import java.lang.annotations.*;
@Target({ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface DBAnnotation {
 String variable () default "";
 String table () default "";
 String column () default "";
 boolean isSource () default false;
}


public static void addFileToDB(String fileName, String fileLocation, int offerID){
    @DBAnnotation (variable = "fileName",  table = "files", column = "FileName", isSource = true)
    @DBAnnotation (variable = "fileLocation",  table = "files", column = "fileLocation", isSource = true)




    String SQLFileSelect = "SELECT FileName FROM files WHERE OfferID = ? AND FileLocation = ?;";
.
.
.
}

我得到以下错误。
Duplicate annotation @File.DBAnnotation. Repeated annotations are allowed only at source level 1.8 or above

但如果我把它改成。。。
public @interface DBAnnotation {
     String[] variable () default "";
     String table () default "";
     String[] column () default "";
     boolean[] isSource () default false;
    }
.
.
.
@DBAnnotation (
            variable = {"fileName","fileLocation"},
            table = "files",
            column = {"FileName","fileLocation"},
            isSource = true)

那么它不会给出任何错误。
我关心的是,对于变量fileLocation,DBAnnotation是否被认为是
variable=“fileLocation”,table=“files”,column=“fileLocation”,isSource=true
或者会被认为
variable=“fileLocation”,table=”,column=“fileLocation”,isSource=

最佳答案

如果你这样设置:

variable = {"fileName","fileLocation"},
            table = "files",
            column = {"FileName","fileLocation"},
            isSource = true

那么variable和column将同时是两个值,因为您将它们定义为一个字符串数组。
这里很重要的一点是,您对自定义注释的处理只取决于您(在运行时如何处理它),因此:
  getAnnotation(DBAnnotation.class).variable(); // will return the String array with both values.

07-27 21:47