
org.scijava.annotations.AnnotationProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scijava-common Show documentation
Show all versions of scijava-common Show documentation
SciJava Common is a shared library for SciJava software. It provides a plugin framework, with an extensible mechanism for service discovery, backed by its own annotation processor, so that plugins can be loaded dynamically. It is used by both ImageJ and SCIFIO.
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2016 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.annotations;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
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.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic.Kind;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import org.scijava.annotations.AbstractIndexWriter.StreamFactory;
/**
* The annotation processor for use with Java 6 and above.
*
* @author Johannes Schindelin
*/
@SupportedSourceVersion(SourceVersion.RELEASE_6)
@SupportedAnnotationTypes("*")
public class AnnotationProcessor extends AbstractProcessor {
private RoundEnvironment roundEnv;
@Override
public boolean process(final Set extends TypeElement> elements,
final RoundEnvironment env)
{
roundEnv = env;
final Writer writer = new Writer();
for (final TypeElement element : elements) {
writer.add(element);
}
try {
writer.write(writer);
}
catch (final IOException e) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(out));
try {
out.close();
processingEnv.getMessager().printMessage(Kind.ERROR, out.toString());
}
catch (final IOException e2) {
processingEnv.getMessager().printMessage(Kind.ERROR,
e2.getMessage() + " while printing " + e.getMessage());
}
}
return false;
}
private class Writer extends AbstractIndexWriter implements StreamFactory {
private final Map> originatingElements =
new HashMap>();
private final Filer filer = processingEnv.getFiler();
private final Elements utils = processingEnv.getElementUtils();
private final Types typeUtils = processingEnv.getTypeUtils();
public void add(final TypeElement element) {
final AnnotationMirror mirror = getMirror(element);
if (mirror != null) {
final String annotationName = utils.getBinaryName(element).toString();
// remember originating elements
List originating = originatingElements.get(annotationName);
if (originating == null) {
originating = new ArrayList();
originatingElements.put(annotationName, originating);
}
for (final Element annotated : roundEnv
.getElementsAnnotatedWith(element))
{
switch (annotated.getKind()) {
case ANNOTATION_TYPE:
case CLASS:
case ENUM:
case INTERFACE:
final String className =
utils.getBinaryName((TypeElement) annotated).toString();
final Map values =
adapt(annotated.getAnnotationMirrors(), element.asType());
super.add(values, annotationName, className);
originating.add(annotated);
break;
default:
processingEnv.getMessager().printMessage(
Kind.ERROR,
"Cannot handle annotated element of kind " +
annotated.getKind());
}
}
}
//
}
@SuppressWarnings("unchecked")
private Map adapt(
final List extends AnnotationMirror> mirrors,
final TypeMirror annotationType)
{
final Map result = new TreeMap();
for (final AnnotationMirror mirror : mirrors) {
if (typeUtils.isSameType(mirror.getAnnotationType(), annotationType)) {
return (Map) adapt(mirror);
}
}
return result;
}
@Override
protected Object adapt(final Object o) {
if (o instanceof AnnotationMirror) {
final AnnotationMirror mirror = (AnnotationMirror) o;
final Map result = new TreeMap();
for (final Entry extends ExecutableElement, ? extends AnnotationValue> entry : mirror
.getElementValues().entrySet())
{
final String key = entry.getKey().getSimpleName().toString();
final Object value = adapt(entry.getValue().getValue());
result.put(key, value);
}
return result;
}
else if (o instanceof List) {
final List> list = (List>) o;
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy