作为我类(class)项目的一部分,我们被要求注释我们的代码。
对于下面的代码
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,te DBAnnotation 会被认为是 variable = "fileLocation", table = "files", column = "fileLocation",isSource = true
或者会被认为是 变量 = "文件位置", 表 = "", 列 = "文件位置",isSource =
最佳答案
如果你这样设置:
variable = {"fileName","fileLocation"},
table = "files",
column = {"FileName","fileLocation"},
isSource = true
那么变量和列将是两个值,因为您将它们定义为字符串数组。
这里重要的是,您对自定义注解的操作完全取决于您(您在运行时对其进行的操作),因此:
getAnnotation(DBAnnotation.class).variable(); // will return the String array with both values.
关于java - 数据库注释 - 重复注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23037117/