Graphic jdk1.8 new features (3) --- Notes and optimized type inference
Get the same type more notes
jdk1.8 the java.lang.Class
class of the new method getAnnotationsByType
, the method can obtain a list of one type of annotation, the specific code examples are as follows:
public class AnnotationTest {
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Filters {
Filter[] value();
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Filters.class)
public @interface Filter {
String value();
}
@Filter("filter1")
@Filter("filter2")
public interface Filterable {
}
@Filters({@Filter("filter1"), @Filter("filter2")})
public interface Filterable2 {
}
public static void main(String[] args) {
for (Filter filter : Filterable.class.getAnnotationsByType(Filter.class)) {
System.out.println(filter.value());
}
for (Filters filter : Filterable.class.getAnnotationsByType(Filters.class)) {
System.out.println(filter.value().length);
}
for (Filter filter : Filterable2.class.getAnnotationsByType(Filter.class)) {
System.out.println(filter.value());
}
for (Filters filter : Filterable2.class.getAnnotationsByType(Filters.class)){
System.out.println(filter.value().length);
}
}
}
输出如下:
filter1
filter2
2
filter1
filter2
2
Better type inference
The above can be seen that the getOrDefault
second parameter may be inferred by the first parameter, if before jdk1.8, to be written in the following form:
value.getOrDefault("asas", Value.<String>defaultValue());
Further expansion of annotations
- jdk1.8 extended context annotations, annotation is now added almost anywhere: local variables, generic class, and class implements ⽗ Connector, and even abnormal Remedies can also add annotations
- ElementType.TYPE_USE and ElementType.TYPE_PARAMETER are two newly added Use files described in the appropriate annotation type vertical element