README.md 9.2 KB
Newer Older
Daniel Bluhm's avatar
Daniel Bluhm committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
Eclipse ICE Annotations
=======================

> TODO Intro to annotations included in this bundle

## Annotations
### Data Elements

> TODO Data Element Annotation walkthrough, what is generated, examples

### Persistence

> TODO Persistence Annotation walkthrough, what is generated, examples

## Creating New Annotations

Eclipse ICE Dev Annotations provides tools and interfaces to split Annotation
Processing into two phases: (1) metadata extraction from the element and
annotations in process and (2) file generation.

Currently, using these tools the two phases are occurring manually and the flow
through the "pipeline" is conceptual. This leaves the door open for implementing
an actual pipeline in future revisions.

As an example, suppose we would like to create an annotation that writes some
helpful information to a generated file. Let's call it `@Debug`. Such an
annotation's definition might look something like the following:

```java
/**
 * Simple annotation for printing metadata about the annotated element.
 */
@Retention(SOURCE)
@Target({
	TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE
})
public @interface Debug {
	/**
	 * Value prepended to printed metadata.
	 * @return prefix.
	 */
	public String value() default "Debug:";
}
```

### Phase 1: Metadata Extraction

Metadata extraction occurs through the use of classes extending the
`AbstractAnnotationExtractor<T>` abstract class. Classes extending it must meet
the `AnnotationExtractor<T>` interface which defines the `extract` method:

```java
/**
 * Extract information from element and annotations found on or within
 * element. The subclass of element is dependent on the annotation and
 * implementation of the extractor.
 * @param element from which information will be extracted.
 * @return extracted information
 * @throws InvalidElementException if element is not annotated as expected
 *         for this annotation extractor.
 */
public T extract(Element element) throws InvalidElementException;
```

In this interface, the type parameter `T` is the type of the metadata returned
by the Extractor. The `AbstractAnnotationExtractor` abstract class contains
methods that might be helpful while extracting metadata but it is not strictly
required that Annotation Extractors extend this abstract base class as long as
it implements the `AnnotationExtractor` interface.

When creating a new annotation, you will likely need to define a new
`AnnotationExtractor` and POJO to hold the metadata. Continuing the `@Debug`
example, a `DebugExtractor` might look something like the following:

```java
/**
 * Extract metadata from {@link Debug} annotated elements.
 */
public class DebugExtractor extends AbstractAnnotationExtractor<DebugMetadata> {

	public DebugExtractor(Elements elementUtils) {
		super(elementUtils);
	}

	@Override
	public DebugMetadata extract(Element element) throws InvalidElementException {
		Optional<Debug> debug = getAnnotation(element, Debug.class);
		if (debug.isEmpty()) {
			throw new InvalidElementException(
				"Element is not annotated with @Debug"
			);
		}
		DebugMetadata data = DebugMetadata.builder()
			.prefix(debug.get().value())
			.name(element.getSimpleName().toString())
			.type(element.getKind().toString())
			.annotations(extractAnnotations(element))
			.parentElement(element.getEnclosingElement().getSimpleName().toString())
			.build();
		return data;
	}

	private Set<String> extractAnnotations(Element element) {
		return element.getAnnotationMirrors().stream()
			.map(AnnotationMirror::toString)
			.collect(Collectors.toSet());
	}
}
```

And the corresponding `DebugMetadata` POJO might look something like:

```java
/**
 * Container for extracted metadata associated with the Debug annotation.
 */
@Getter
@Builder
public class DebugMetadata {

	/**
	 * Prefix defined in annotation.
	 */
	private String prefix;

	/**
	 * Name of the element.
	 */
	private String name;

	/**
	 * Type of the element
	 */
	private String type;

	/**
	 * Annotations on the element (excluding Debug).
	 */
	private Set<String> annotations;

	/**
	 * Parent element of the annotated element.
	 */
	private String parentElement;
}
```

### Phase 2: File Generation

To streamline file generation, the `WriterGenerator` interface is used which
defines the `generate` method:

```java
/**
 * Interface for classes that generate one or more file writers from a given
 * set of data.
 *
 * @author Michael Walsh
 * @author Daniel Bluhm
 */
public interface WriterGenerator {

	/**
	 * Generate one or more FileWriters from the passed data.
	 * @return
	 */
	public List<GeneratedFileWriter> generate();
}
```

Writer Generators produce a list of `GeneratedFileWriter`s representing a file
to be generated, ready for writing. All of the current writers are subclasses of
`VelocitySourceWriters` which provide tooling for using Apache Velocity to
generate a file from a template.

For the `@Debug` annotation, the writer generator and generated file writer
might look like this:

```java
public class DebugWriterGenerator implements WriterGenerator {

	/**
	 * Extracted metadata.
	 */
	private DebugMetadata data;

	public DebugWriterGenerator(DebugMetadata data) {
		this.data = data;
	}

	@Override
	public List<GeneratedFileWriter> generate() {
		List<GeneratedFileWriter> writers = List.of(
			new DebugWriter(data)
		);
		return writers;
	}
}
```

```java
public class DebugWriter
	extends VelocitySourceWriter
	implements GeneratedFileWriter
{
	private static final String DEBUG_TEMPLATE = "templates/Debug.vm";
	private static final String DEBUG_KEY = "debug";
	private DebugMetadata data;
	public DebugWriter(DebugMetadata data) {
		super(DEBUG_TEMPLATE);
		this.data = data;
		this.context.put(DEBUG_KEY, data);
	}

	@Override
	public Writer openWriter(Filer filer) throws IOException {
		return filer.createResource(
			StandardLocation.SOURCE_OUTPUT,
			"",
			String.format("debug/%s.debug.txt", data.getName())
		).openWriter();
	}
}
```

### Tying it all together

These elements can be combined in a number of ways. You might add this to an
already existing processor or create a new one. Here's the outline of an
Annotation Processor for the `@Debug` annotation:

```java
package org.eclipse.ice.dev.annotations.processors;

import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;

import com.google.auto.service.AutoService;

@SupportedAnnotationTypes({
	"org.eclipse.ice.dev.annotations.Debug"
})
@SupportedSourceVersion(SourceVersion.RELEASE_11)
@AutoService(Processor.class)
public class DebugProcessor extends AbstractProcessor {

	@Override
	public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
		// TODO Auto-generated method stub
		return false;
	}
}
```

The simplest way to add the extractors and writers is to just manually wire up
these different pieces:

```java
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
	// Setup extractor
	Elements elementUtils = processingEnv.getElementUtils();
	DebugExtractor extractor = new DebugExtractor(elementUtils);
	try {
		for (Element element : roundEnv.getElementsAnnotatedWith(Debug.class)) {
			DebugMetadata debug = extractor.extract(element);
			DebugWriter debugWriter = new DebugWriter(debug);
			try(Writer writer = debugWriter.openWriter(processingEnv.getFiler())) {
				debugWriter.write(writer);
			}
		}
	} catch (IOException | InvalidElementException e) {
		processingEnv.getMessager()
			.printMessage(Kind.ERROR, e.getMessage());
	}
	return false;
}
```

However, it is also likely that we might want to combine multiple extraction
steps and multiple writers with overlapping metadata for different writers. To
simplify this scenario, `FromDataBuilder` (a sort of runtime dependency
injector) can be used:

```java
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
	// Setup extractors
	Elements elementUtils = processingEnv.getElementUtils();
	DebugExtractor extractor = new DebugExtractor(elementUtils);

	// Setup FromDataBuilder
	FromDataBuilder<WriterGenerator> builder = new FromDataBuilder<>(
		Set.of(DebugWriterGenerator.class)
	);
	try {
		for (Element element : roundEnv.getElementsAnnotatedWith(Debug.class)) {
			DebugMetadata debug = extractor.extract(element);
			List<GeneratedFileWriter> files = builder.create(debug).stream()
				.flatMap(generators -> generators.generate().stream())
				.collect(Collectors.toList());
			for (GeneratedFileWriter file : files) {
				try (Writer writer = file.openWriter(processingEnv.getFiler())){
					file.write(writer);
				}
			}
		}
	} catch (IOException | InvalidElementException e) {
		processingEnv.getMessager()
			.printMessage(Kind.ERROR, e.getMessage());
	}
	return false;
}
```

For just one extractor and writer, this is almost certainly overkill. But this
setup can be easily combined into a pipeline runner that performs many
extraction steps and writing steps. The exact implementation of such a pipeline
is left as an exercise for the reader (and for future revisions).