DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Filtering Java Collections via Annotation-Driven Introspection
  • Mastering Spring: Synchronizing @Transactional and @Async Annotations With Various Propagation Strategies
  • Implementing Persistence With Clean Architecture
  • Composing Custom Annotations in Spring

Trending

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Metrics at a Glance for Production Clusters
  • A Modern Stack for Building Scalable Systems
  • Fraud Detection Using Artificial Intelligence and Machine Learning
  1. DZone
  2. Coding
  3. Languages
  4. Java 8 Type Annotations

Java 8 Type Annotations

By 
Michael Scharhag user avatar
Michael Scharhag
·
Feb. 11, 14 · Interview
Likes (4)
Comment
Save
Tweet
Share
82.9K Views

Join the DZone community and get the full member experience.

Join For Free
Lambda expressions are by far the most discussed and promoted feature of Java 8. While I agree that Lambdas are a large improvement I think that some other Java 8 feature go a bit short because of the Lambda hype. In this post I want to show a number of examples from another nice Java 8 feature: Type Annotations.

Type Annotations are annotations that can be placed anywhere you use a type. This includes the new operator, type casts, implements clauses and throws clauses. Type Annotations allow improved analysis of Java code and can ensure even stronger type checking.

In source code this means we get two new ElementTypes for annotations:
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
public @interface Test {
}
The enum value TYPE_PARAMETER allows an annotation to be applied at type variables (e.g. MyClass<T>). Annotations with target TYPE_USE can be applied at any type use.

Please note that the annotations from the following examples will not work out of the box when Java 8 is released. Java 8 only provides the ability to define these types of annotations. It is then up to framework and tool developers to actually make use of it. So this is a collection of annotations frameworks could give us in the future. Most of the examples are taken from the Type Annotations specification and various Java 8 presentations.

Simple type definitions with type annotations look like this: 
@NotNull String str1 = ...
@Email String str2 = ...
@NotNull @NotBlank String str3 = ...
Type annotations can also be applied to nested types 
Map.@NonNull Entry = ...
Constructors with type annotations: 
new @Interned MyObject()
new @NonEmpty @Readonly List<String>(myNonEmptyStringSet)
They work with nested (non static) class constructors too: 
myObject.new @Readonly NestedClass()
Type casts: 
myString = (@NonNull String) myObject;
query = (@Untainted String) str;
Inheritance: 
class UnmodifiableList<T> implements @Readonly List<T> { ... }
We can use type Annotations with generic type arguments: 
List<@Email String> emails = ...
List<@ReadOnly @Localized Message> messages = ...
Graph<@Directional Node> directedGraph = ...
Of course we can nest them: 
Map<@NonNull String, @NonEmpty List<@Readonly Document>> documents;
Or apply them to intersection Types: 
public <E extends @ReadOnly Composable<E> & @Localized MessageSource> void foo(...) { ... }
Including parameter bounds and wildcard bounds: 
class Folder<F extends @Existing File> { ... }
Collection<? super @Existing File> c = ...
List<@Immutable ? extends Comparable<T>> unchangeable = ...
Generic method invocation with type annotations looks like this: 
myObject.<@NotBlank String>myMethod(...);
For generic constructors, the annotation follows the explicit type arguments: 
1
new <String> @Interned MyObject()
Throwing exceptions: 
void monitorTemperature() throws @Critical TemperatureException { ... }
void authenticate() throws @Fatal @Logged AccessDeniedException { ... }
Type annotations in instanceof statements: 
boolean isNonNull = myString instanceof @NonNull String;
boolean isNonBlankEmail = myString instanceof @NotBlank @Email String;
And finally Java 8 method and constructor references: 
@Vernal Date::getDay
List<@English String>::size
Arrays::<@NonNegative Integer>sort

Conclusion
Type annotations are an interesting addition to the Java type system. They can be applied to any use of a type and enable a more detailed code analysis. If you want to use Type annotations right now you should have a look at the Checker Framework.
Annotation Java (programming language)

Published at DZone with permission of Michael Scharhag, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Filtering Java Collections via Annotation-Driven Introspection
  • Mastering Spring: Synchronizing @Transactional and @Async Annotations With Various Propagation Strategies
  • Implementing Persistence With Clean Architecture
  • Composing Custom Annotations in Spring

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: