Feature Overview
This feature adds support for custom annotation on classes.
This feature adds support for custom annotation on classes.
None
@MyAnnotation(name='Test')
public class AnnotationDemo {
public @interface MyAnnotation {
public String name();
public Integer number() default 10;
}
}
@MyAnnotation(name='Test')
public class AnnotationDemo {
public @interface MyAnnotation {
public String name();
public Integer number() default 10;
}
}
public class AnnotationDemo {
public class MyAnnotation {
private String m_name;
private Integer m_number = 10;
public MyAnnotation name(String m_name) {
this.m_name = m_name;
return this;
}
public String name() {
return this.m_name;
}
public MyAnnotation number(Integer m_number) {
this.m_number = m_number;
return this;
}
public Integer number() {
return this.m_number;
}
}
}
public class AnnotationDemo {
public class MyAnnotation {
private String m_name;
private Integer m_number = 10;
public MyAnnotation name(String m_name) {
this.m_name = m_name;
return this;
}
public String name() {
return this.m_name;
}
public MyAnnotation number(Integer m_number) {
this.m_number = m_number;
return this;
}
public Integer number() {
return this.m_number;
}
}
}
public class SweetAnnotations implements Sweet.Annotations {
private final Map<String, List<Object>> annotations = new Map<String, List<Object>>();
public List<Object> getAnnotations(String name) {
List<Object> aList = annotations.get(name);
return aList == null ? new List<Object>() : aList;
}
public Object getAnnotation(String name) {
List<Object> aList = getAnnotations(name);
return aList.isEmpty() ? null : aList.get(0);
}
private void registerAnnotation(String targetName, Object annotation) {
List<Object> aList = annotations.get(targetName);
if(aList == null) {
aList = new List<Object>();
}
aList.add(annotation);
annotations.put(targetName, aList);
}
{
registerAnnotation(AnnotationDemo.class.getName(), new AnnotationDemo.MyAnnotation().name('Test'));
}
}
public class SweetAnnotations implements Sweet.Annotations {
private final Map<String, List<Object>> annotations = new Map<String, List<Object>>();
public List<Object> getAnnotations(String name) {
List<Object> aList = annotations.get(name);
return aList == null ? new List<Object>() : aList;
}
public Object getAnnotation(String name) {
List<Object> aList = getAnnotations(name);
return aList.isEmpty() ? null : aList.get(0);
}
private void registerAnnotation(String targetName, Object annotation) {
List<Object> aList = annotations.get(targetName);
if(aList == null) {
aList = new List<Object>();
}
aList.add(annotation);
annotations.put(targetName, aList);
}
{
registerAnnotation(AnnotationDemo.class.getName(), new AnnotationDemo.MyAnnotation().name('Test'));
}
}
Define custom annotations.
public @interface MyAnnotation {
public String name();
public Integer number() default 10;
}
public @interface MyAnnotation {
public String name();
public Integer number() default 10;
}
Apply custom annotations on classes.
@MyAnnotation(name='Test')
public class AnnotationDemo {
}
@MyAnnotation(name='Test')
public class AnnotationDemo {
}
Retrieve annotation information from the instance of the class.
AnnotationDemo demo = new AnnotationDemo();
AnnotationDemo.MyAnnotation myAnn = (AnnotationDemo.MyAnnotation)Sweet.getAnnotation(demo);
System.debug(myAnn.name());
AnnotationDemo demo = new AnnotationDemo();
AnnotationDemo.MyAnnotation myAnn = (AnnotationDemo.MyAnnotation)Sweet.getAnnotation(demo);
System.debug(myAnn.name());
Contribute on Github! Edit this section.