Beruflich Dokumente
Kultur Dokumente
Compiler
amonn McManus <emcmanus@google.com>
Christian Gruber <cgruber@google.com>
JavaOne 2014
Test Mercenary
Mobile testing and development infrastructure
Java Core Librarian
Overview
JavaOne 2014
Reminder: Annotations
@SuppressWarnings(unchecked)
public class Foo {
@SafeVarargs
public void bar(Optional<String>... args) {...}
public void baz(@Nullable String s) {...}
}
JavaOne 2014
javax.annotation.processing, javax.lang.model
supported by javac (JDK) and ecj (Eclipse)
JavaOne 2014
JavaOne 2014
Au am
to pl
Va e:
lu
e
Ex
Value Types
A value type is a class where:
Immutability is good!
JavaOne 2014
JavaOne 2014
JavaOne 2014
postCode
JavaOne 2014
Bar.java
compile
@AutoValue
generate
AutoValue_Address.java
AutoValueProcessor
@AutoValue
Address.java
JavaOne 2014
AutoValue_Address.java
compile
No further
annotations
JavaOne 2014
Foo.class
Bar.java
Bar.class
generate
code
Address.java
Address.class
AutoValue_Address.java
AutoValue_Address.class
JavaOne 2014
o
D
em
JavaOne 2014
JavaOne 2014
Ex
D am
ag p
ge le
r :
Dagger
Dependency-injection framework using JSR 330
Directed Acyclic Graph... of classes and their dependencies.
Dagger 1.x
Dagger 2.x
Concept by Greg Kick and Christian Gruber, impl mainly by Greg Kick
JavaOne 2014
Dependency Injection
Having a class know how to obtain its collaborators is fragile
http://www.martinfowler.com/articles/injection.html
Early examples:
Later examples:
JavaOne 2014
public MailServer(
MessageStore messageStore,
UserService userService) {
this.messageStore = messageStore;
this.userService = userService;
// ...
}
}
JavaOne 2014
Developer productivity:
Design
o
D
em
Note: Dagger 2 is pre-release, and has some issues, include IDE integration issues
JavaOne 2014
JavaOne 2014
Yo rite
O ur
w
n
JavaOne 2014
"Annotation" processors
Annotation processors are usually associated with annotations,
obviously
But, you can also write a processor that analyzes all input classes,
whether annotated or not
JavaOne 2014
Defining an annotation
import java.lang.annotation.*;
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
// @Documented
public @interface MyAnnotation {
String value() default "";
}
// @MyAnnotation class Foo {...}
// @MyAnnotation("bar") interface Baz {...}
// @MyAnnotation(value = "buh") enum Wibble {...}
JavaOne 2014
JavaOne 2014
Claiming annotations
An annotation processor can say that it "supports" one or more
annotations (getSupportedAnnotationTypes)
Its process method will be called only for program elements
where those annotations appear
If it returns true, it has "claimed" the annotations and a later
processor that also supports them will not be called
We recommend never claiming an annotation
You don't know what that later processor is or whether it should run
JavaOne 2014
JavaOne 2014
TypeElement TypeMirror:
TypeMirror typeMirror = typeElement.asType();
or
DeclaredType typeMirror = typeUtils.getDeclaredType(typeElement);
JavaOne 2014
JavaOne 2014
JavaOne 2014
SuperficialValidation
JavaOne 2014
Cons:
Code that generates code can look quite different in shape than output
JavaOne 2014
JavaOne 2014
JavaOne 2014
Functional tests:
JavaOne 2014
JavaOne 2014
y
m
ar
Su
m
JavaOne 2014
Summary
Annotation processors are a powerful way to plug in to javac
An ecosystem is evolving to ease writing custom processors
AutoValue, Dagger
Early error-checking
Performance improvements
Viewable generated source code
JavaOne 2014
JavaWriter: https://github.com/square/javawriter
CompileTesting: https://github.com/google/compile-testing
Google Auto common utilities: https://github.
com/google/auto/tree/master/common
JavaOne 2014
&A
Q
JavaOne 2014
NetBeans:
Eclipse:
JavaOne 2014