Professional Documents
Culture Documents
JAVA Annotation
JAVA Annotation
Problem [1]
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
Person p = new Person();
p.setName("Daniel");
System.out.println("Name: "+p.getName());
}
}
Problem [2]
@Data
public class Person {
private String name;
public static void main(String[] args) {
Person p = new Person();
p.setName("Daniel");
System.out.println("Name:
"+p.getName());
}
}
Problem [3]
public class MyUtils
{
private static final Logger log =
LoggerFactory.getLogger(MyUtils.class);
public void myAlgorithm()
{
log.info("This is my algorithm.");
}
}
Problem [4]
@Slf4j
public class MyUtils
{
public void myAlgorithm()
{
log.info("This is my algorithm.");
}
}
Goal
Understanding how to Annotate.
Understanding how annotation
preprocessor work.
Understanding how Project Lombok
work.
Definition
Anotasi adalah catatan yg dibuat oleh
pengarang atau orang lain untuk
menerangkan, mengomentari, atau
mengkritik teks karya sastra atau
bahan tertulis lain. (KBBI)
A note by way of explanation or
comment added to a text or diagram.
(Oxford Dictionary)
Introduction
Annotation Example
@API
package com.djph.annotation.lombok1;
@Entity
public class MyObject
{
@NotNull
private String name;
@Autowired
public MyObject()
{
}
@Transactional
public void myMethod(@Valid Object obj)
{
}
}
History [1]
The Java platform has various ad-hoc
annotation mechanismsfor example,
the transient modifier, or the
@deprecated javadoc tag.
The general purpose annotation (also
known as metadata) facility was
introduced to the Java Community
Process as JSR-175 in 2002 and
approved in September 2004.
History [2]
Annotations became available in the
language itself beginning with version
1.5 of the JDK.
A provisional interface for compiletime annotation processing was
provided by the apt tool in JDK version
1.5, and was formalized through JSR269 and integrated into the javac
compiler in version 1.6.
@Override
@Deprecated
@SuppressWarnings
@SafeVarargs (Since Java 7)
@FunctionalInterface (Since Java 8)
@Retention
@Documented
@Target
@Inherited
@Repeatable (Since Java 8)
Custom annotations
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target
({
ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR,
ElementType.FIELD, ElementType.LOCAL_VARIABLE,
ElementType.METHOD, ElementType.PACKAGE,
ElementType.PARAMETER, ElementType.TYPE
})
@Retention(RetentionPolicy.RUNTIME) //RetentionPolicy.CLASS,
RetentionPolicy.RUNTIME
public @interface MyAnnotation
{
String value();
}
Type cast:
myString = (@NonNull String) str;
implements clause:
class UnmodifiableList<T> implements
@Readonly List<@Readonly T> { ... }
Create Annotation
Preprocessor
@SupportedAnnotationTypes({"*"})
@SupportedSourceVersion(SourceVersion.RELEAS
E_7)
public class SimpleProcessor extends
AbstractProcessor
{
@Override
public boolean process(Set<? extends
TypeElement> annotations, RoundEnvironment
roundEnv)
{
return false;
}
}
Only that???
Context context = ((JavacProcessingEnvironment)
processingEnv).getContext();
TreeMaker treeMaker = TreeMaker.instance(context);
JavacElements elementUtils = (JavacElements)
processingEnv.getElementUtils();
JCTree.JCClassDecl classDecl = (JCTree.JCClassDecl)
elementUtils.getTree(element);
AST
How to run???
Using apt processor (JDK 1.5)
Using javac processor (JDK 1.6)
Create file
javax.annotation.processing.Processo
r that contains all processor class,
separate by new line at folder METAINF.services
Project Lombok
Project Lombok is annotation
processor compiler plugin for javac
that provides a handful of very
focused annotations for your classes
to generate you highly optimized
implementations of some of the most
common boilerplate code that Java
developer needs.
Created by Reinier Zwitserloot & Roel
Spilker.
Use for?
@Data
public class Person {
private String name;
public static void main(String[] args) {
Person p = new Person();
p.setName("Daniel");
System.out.println("Name:
"+p.getName());
}
}
Trick or Hack???
The annotation processing spec
doesn't allow you to modify existing
classes.
The annotation processing API doesn't
provide a mechanism for changing the
AST of a class.
Disadvantages
Debug
WYSIWYG
Time Bomb
Thank You