|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package mcompiler395; |
| 20 | + |
| 21 | +import javax.annotation.processing.AbstractProcessor; |
| 22 | +import javax.annotation.processing.Filer; |
| 23 | +import javax.annotation.processing.RoundEnvironment; |
| 24 | +import javax.annotation.processing.SupportedAnnotationTypes; |
| 25 | +import javax.annotation.processing.SupportedSourceVersion; |
| 26 | +import javax.lang.model.SourceVersion; |
| 27 | +import javax.lang.model.element.Element; |
| 28 | +import javax.lang.model.element.Name; |
| 29 | +import javax.lang.model.element.PackageElement; |
| 30 | +import javax.lang.model.element.TypeElement; |
| 31 | +import javax.lang.model.util.Elements; |
| 32 | +import javax.tools.FileObject; |
| 33 | +import javax.tools.StandardLocation; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.io.Writer; |
| 37 | +import java.util.Set; |
| 38 | + |
| 39 | +@SupportedSourceVersion(SourceVersion.RELEASE_6) |
| 40 | +@SupportedAnnotationTypes("mcompiler395.SimpleAnnotation") |
| 41 | +public class SimpleAnnotationProcessor extends AbstractProcessor { |
| 42 | + |
| 43 | + @Override |
| 44 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 45 | + if (annotations.isEmpty()) { |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + // assert that mcompiler395-annotation-processor-dep is NOT on the processorpath, since it is excluded |
| 50 | + // in the plugin configuration |
| 51 | + try { |
| 52 | + getClass().getClassLoader().loadClass("mcompiler395.AnnotationProcessorDependency"); |
| 53 | + throw new RuntimeException("Expected a ClassNotFoundException, because " |
| 54 | + + "mcompiler395.AnnotationProcessorDependency is not supposed to be on the processorpath."); |
| 55 | + } catch (ClassNotFoundException expected) { |
| 56 | + // expected |
| 57 | + } |
| 58 | + |
| 59 | + Filer filer = processingEnv.getFiler(); |
| 60 | + |
| 61 | + Elements elementUtils = processingEnv.getElementUtils(); |
| 62 | + |
| 63 | + Set<? extends Element> elements = |
| 64 | + roundEnv.getElementsAnnotatedWith(annotations.iterator().next()); |
| 65 | + |
| 66 | + for (Element element : elements) { |
| 67 | + Name name = element.getSimpleName(); |
| 68 | + |
| 69 | + PackageElement packageElement = elementUtils.getPackageOf(element); |
| 70 | + |
| 71 | + try { |
| 72 | + Name packageName = packageElement.getQualifiedName(); |
| 73 | + FileObject resource = |
| 74 | + filer.createResource(StandardLocation.SOURCE_OUTPUT, packageName, name + ".txt", element); |
| 75 | + |
| 76 | + Writer writer = resource.openWriter(); |
| 77 | + writer.write(name.toString()); |
| 78 | + writer.close(); |
| 79 | + } catch (IOException e) { |
| 80 | + throw new RuntimeException(e); |
| 81 | + } |
| 82 | + } |
| 83 | + return !elements.isEmpty(); |
| 84 | + } |
| 85 | +} |
0 commit comments