public class Computers {
@OS(type = osType.linux, name = "red hat", softwares = {
@Software(name = "rpm", type = sofwareType.SYSTEM),
@Software(name = "oracle", type = sofwareType.USER) })
public void computer1(){}
@OS(type = osType.linux, name = "ubuntu", softwares = {
@Software(name = "wget", type = sofwareType.USER),
@Software(name = "firefox", type = sofwareType.USER) })
public void computer2(){}
@OS(type = osType.unix, name = "freeBSD", softwares = {
@Software(name = "ping", type = sofwareType.SYSTEM) })
public void computer3(){}
}
在第三方获取注解
public class Main {
public static void main(String[] args)
{
int softwareLen;
for(Method method : Computers.class.getDeclaredMethods())
{
OS os = (OS)method.getAnnotation(OS.class);
if(os == null)
{
continue;
}
softwareLen = os.softwares().length;
System.out.println(os.type());
System.out.println(os.name());
if(softwareLen > 0)
{
for(Software software : os.softwares())
{
System.out.println(software.name() + " " + software.type());
}
}
}
}
}