diff options
author | Yohann Roussel <yroussel@google.com> | 2014-12-18 12:17:52 +0100 |
---|---|---|
committer | Yohann Roussel <yroussel@google.com> | 2015-03-10 17:11:56 +0100 |
commit | d6ccb85505c0d7a0bd6544d67f6d43e40775a76b (patch) | |
tree | 62938b6bc8fb0f1b78d844dcaf4884330315234e /jack-tests/tests/com/android/jack/annotation | |
parent | b001ab34cbf7a73ccfe749be405db9fd67efaa95 (diff) | |
download | toolchain_jack-d6ccb85505c0d7a0bd6544d67f6d43e40775a76b.zip toolchain_jack-d6ccb85505c0d7a0bd6544d67f6d43e40775a76b.tar.gz toolchain_jack-d6ccb85505c0d7a0bd6544d67f6d43e40775a76b.tar.bz2 |
Jack support for annotation processor.
Does not include any support for including class files generated
by annotation processors.
Change-Id: Id4006301d588875f822ff84fa90db10fd616ab00
Diffstat (limited to 'jack-tests/tests/com/android/jack/annotation')
9 files changed, 673 insertions, 0 deletions
diff --git a/jack-tests/tests/com/android/jack/annotation/processor/AnnotationProcessorTests.java b/jack-tests/tests/com/android/jack/annotation/processor/AnnotationProcessorTests.java new file mode 100644 index 0000000..2d4be98 --- /dev/null +++ b/jack-tests/tests/com/android/jack/annotation/processor/AnnotationProcessorTests.java @@ -0,0 +1,292 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.annotation.processor; + +import com.android.jack.annotation.processor.sample.processors.ResourceAnnotationProcessor; +import com.android.jack.annotation.processor.sample.processors.SourceAnnotationProcessor; +import com.android.jack.library.FileType; +import com.android.jack.library.FileTypeDoesNotExistException; +import com.android.jack.library.InputJackLibrary; +import com.android.jack.library.JackLibraryFactory; +import com.android.jack.test.toolchain.AbstractTestTools; +import com.android.jack.test.toolchain.JackBasedToolchain; +import com.android.jack.test.toolchain.LegacyToolchain; +import com.android.jack.test.toolchain.Toolchain; +import com.android.sched.util.config.ConfigurationException; +import com.android.sched.util.config.GatherConfigBuilder; +import com.android.sched.util.config.ThreadConfig; +import com.android.sched.util.file.Directory; +import com.android.sched.util.file.FileOrDirectory.ChangePermission; +import com.android.sched.util.file.FileOrDirectory.Existence; +import com.android.sched.util.file.FileOrDirectory.Permission; +import com.android.sched.vfs.DirectFS; +import com.android.sched.vfs.VPath; + +import junit.framework.Assert; + +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; +import java.util.Collections; + +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; + +public class AnnotationProcessorTests { + + @Nonnull + private static final File ANNOTATED_DIR = AbstractTestTools.getTestRootDir( + "com.android.jack.annotation.processor.sample.annotated"); + @Nonnull + private static final File ANNOTATIONS_DIR = AbstractTestTools.getTestRootDir( + "com.android.jack.annotation.processor.sample.annotations"); + + @CheckForNull + private static File noConfigProcessors; + + @CheckForNull + private static File autoProcessors; + + @BeforeClass + public static void setupClass() throws ConfigurationException { + // required for creating InputJackLibrary when running tests on cli + ThreadConfig.setConfig(new GatherConfigBuilder().build()); + noConfigProcessors = null; + autoProcessors = null; + } + + @Nonnull + private static File getNoConfigProcessors() throws Exception { + if (noConfigProcessors == null) { + Toolchain toolchain = AbstractTestTools.getReferenceToolchain(); + File processorsDir = AbstractTestTools.createTempDir(); + File processorsSrcDir = AbstractTestTools.getTestRootDir( + "com.android.jack.annotation.processor.sample.processors"); + toolchain.srcToLib(processorsDir, /*zipFiles=*/ false, + ANNOTATIONS_DIR, + processorsSrcDir + ); + noConfigProcessors = processorsDir; + } + return noConfigProcessors; + } + + @Nonnull + private static File getAutoProcessors() throws Exception { + if (autoProcessors == null) { + Toolchain toolchain = AbstractTestTools.getReferenceToolchain(); + File processorsDir = AbstractTestTools.createTempDir(); + File processorsSrcDir = AbstractTestTools.getTestRootDir( + "com.android.jack.annotation.processor.sample.processors"); + toolchain.srcToLib(processorsDir, /*zipFiles=*/ false, + ANNOTATIONS_DIR, + processorsSrcDir + ); + AbstractTestTools.copyFileToDir(new File(processorsSrcDir, + "javax.annotation.processing.Processor"), + "META-INF/services/javax.annotation.processing.Processor", processorsDir); + autoProcessors = processorsDir; + } + return autoProcessors; + } + + @Test + public void compileWithAnnotationProcessorAuto_classpath() throws Exception { + JackBasedToolchain jack = AbstractTestTools.getCandidateToolchain(JackBasedToolchain.class); + File jackOut = AbstractTestTools.createTempDir(); + File processors = getAutoProcessors(); + jack.addToClasspath(jack.getDefaultBootClasspath()); + jack.addToClasspath(processors); + jack.srcToLib(jackOut, /*zipFiles=*/false, + ANNOTATIONS_DIR, + ANNOTATED_DIR + ); + InputJackLibrary libOut = + JackLibraryFactory.getInputLibrary( + new DirectFS(new Directory(jackOut.getPath(), /* hooks = */ null, + Existence.MUST_EXIST, Permission.READ, ChangePermission.NOCHANGE), + Permission.READ)); + libOut.getFile(FileType.RSC, new VPath("rscGeneratedFile0", '/')); + libOut.getFile(FileType.JAYCE, new VPath("Annotated2Duplicated", '/')); + } + + @Test + public void compileWithAnnotationProcessorAuto_processorPath() throws Exception { + JackBasedToolchain jack = AbstractTestTools.getCandidateToolchain(JackBasedToolchain.class); + File jackOut = AbstractTestTools.createTempDir(); + File processors = getAutoProcessors(); + jack.setAnnotationProcessorPath(processors.getPath()); + jack.addToClasspath(jack.getDefaultBootClasspath()); + jack.srcToLib(jackOut, + /*zipFiles=*/false, + ANNOTATIONS_DIR, + ANNOTATED_DIR + ); + InputJackLibrary libOut = + JackLibraryFactory.getInputLibrary( + new DirectFS(new Directory(jackOut.getPath(), /* hooks = */ null, + Existence.MUST_EXIST, Permission.READ, ChangePermission.NOCHANGE), + Permission.READ)); + libOut.getFile(FileType.RSC, new VPath("rscGeneratedFile0", '/')); + libOut.getFile(FileType.JAYCE, new VPath("Annotated2Duplicated", '/')); + } + + @Test + public void compileWithAnnotationProcessorNoAuto_processorPath() throws Exception { + JackBasedToolchain jack = AbstractTestTools.getCandidateToolchain(JackBasedToolchain.class); + File jackOut = AbstractTestTools.createTempDir(); + File processors = getNoConfigProcessors(); + jack.setAnnotationProcessorPath(processors.getPath()); + jack.setAnnotationProcessorClasses( + Collections.singletonList(ResourceAnnotationProcessor.class.getName())); + jack.addToClasspath(jack.getDefaultBootClasspath()); + jack.srcToLib(jackOut, + /*zipFiles=*/false, + ANNOTATIONS_DIR, + ANNOTATED_DIR + ); + InputJackLibrary libOut = + JackLibraryFactory.getInputLibrary( + new DirectFS(new Directory(jackOut.getPath(), /* hooks = */ null, + Existence.MUST_EXIST, Permission.READ, ChangePermission.NOCHANGE), + Permission.READ)); + libOut.getFile(FileType.RSC, new VPath("rscGeneratedFile0", '/')); + try { + libOut.getFile(FileType.JAYCE, new VPath("Annotated2Duplicated", '/')); + Assert.fail(); + } catch (FileTypeDoesNotExistException e) { + // expected + } + } + + @Test + public void compileWithAnnotationProcessorNoAutoNoClasses_processorPath() throws Exception { + JackBasedToolchain jack = AbstractTestTools.getCandidateToolchain(JackBasedToolchain.class); + File jackOut = AbstractTestTools.createTempDir(); + File processors = getNoConfigProcessors(); + jack.setAnnotationProcessorPath(processors.getPath()); + jack.addToClasspath(jack.getDefaultBootClasspath()); + jack.srcToLib(jackOut, + /*zipFiles=*/false, + ANNOTATIONS_DIR, + ANNOTATED_DIR + ); + InputJackLibrary libOut = + JackLibraryFactory.getInputLibrary( + new DirectFS(new Directory(jackOut.getPath(), /* hooks = */ null, + Existence.MUST_EXIST, Permission.READ, ChangePermission.NOCHANGE), + Permission.READ)); + try { + libOut.getFile(FileType.RSC, new VPath("rscGeneratedFile0", '/')); + Assert.fail(); + } catch (FileTypeDoesNotExistException e) { + // expected + } + try { + libOut.getFile(FileType.JAYCE, new VPath("Annotated2Duplicated", '/')); + Assert.fail(); + } catch (FileTypeDoesNotExistException e) { + // expected + } + } + + @Test + public void compileWithAnnotationProcessorNoAuto_classpath() throws Exception { + JackBasedToolchain jack = AbstractTestTools.getCandidateToolchain(JackBasedToolchain.class); + File jackOut = AbstractTestTools.createTempDir(); + File processors = getNoConfigProcessors(); + jack.setAnnotationProcessorClasses( + Collections.singletonList(SourceAnnotationProcessor.class.getName())); + jack.addToClasspath(jack.getDefaultBootClasspath()); + jack.addToClasspath(processors); + jack.srcToLib(jackOut, /*zipFiles=*/false, + ANNOTATIONS_DIR, + ANNOTATED_DIR + ); + InputJackLibrary libOut = + JackLibraryFactory.getInputLibrary( + new DirectFS(new Directory(jackOut.getPath(), /* hooks = */ null, + Existence.MUST_EXIST, Permission.READ, ChangePermission.NOCHANGE), + Permission.READ)); + try { + libOut.getFile(FileType.RSC, new VPath("rscGeneratedFile0", '/')); + Assert.fail(); + } catch (FileTypeDoesNotExistException e) { + // expected + } + libOut.getFile(FileType.JAYCE, new VPath("Annotated2Duplicated", '/')); + } + + @Test + public void compileWithAnnotationProcessorReuseClassOut() throws Exception { + File classesOut = AbstractTestTools.createTempDir(); + File jackOut = AbstractTestTools.createTempDir(); + File processors = getAutoProcessors(); + { + JackBasedToolchain jack = AbstractTestTools.getCandidateToolchain(JackBasedToolchain.class); + jack.setAnnotationProcessorPath(processors.getPath()); + jack.addResource(classesOut); + jack.addToClasspath(jack.getDefaultBootClasspath()); + jack.srcToLib(jackOut, + /*zipFiles=*/false, + ANNOTATIONS_DIR, + ANNOTATED_DIR + ); + } + { + JackBasedToolchain jack = AbstractTestTools.getCandidateToolchain(JackBasedToolchain.class); + jack.setAnnotationProcessorPath(processors.getPath()); + jack.addResource(classesOut); + jack.addToClasspath(jack.getDefaultBootClasspath()); + jack.srcToLib(jackOut, + /*zipFiles=*/false, + ANNOTATIONS_DIR, + ANNOTATED_DIR + ); + } + InputJackLibrary libOut = + JackLibraryFactory.getInputLibrary( + new DirectFS(new Directory(jackOut.getPath(), /* hooks = */ null, + Existence.MUST_EXIST, Permission.READ, ChangePermission.NOCHANGE), + Permission.READ)); + libOut.getFile(FileType.RSC, new VPath("rscGeneratedFile0", '/')); + libOut.getFile(FileType.RSC, new VPath("rscGeneratedFile1", '/')); + libOut.getFile(FileType.JAYCE, new VPath("Annotated2Duplicated", '/')); + } + + @Test + public void compileWithAnnotationProcessorOption() throws Exception { + JackBasedToolchain jack = AbstractTestTools.getCandidateToolchain(JackBasedToolchain.class); + File jackOut = AbstractTestTools.createTempDir(); + File processors = getAutoProcessors(); + jack.addAnnotationProcessorOption( + SourceAnnotationProcessor.SOURCE_ANNOTATION_PROCESSOR_SUFFIX, "WithOption"); + jack.addToClasspath(jack.getDefaultBootClasspath()); + jack.addToClasspath(processors); + jack.srcToLib(jackOut, /*zipFiles=*/false, + ANNOTATIONS_DIR, + ANNOTATED_DIR + ); + InputJackLibrary libOut = + JackLibraryFactory.getInputLibrary( + new DirectFS(new Directory(jackOut.getPath(), /* hooks = */ null, + Existence.MUST_EXIST, Permission.READ, ChangePermission.NOCHANGE), + Permission.READ)); + libOut.getFile(FileType.JAYCE, new VPath("Annotated2WithOption", '/')); + } +} diff --git a/jack-tests/tests/com/android/jack/annotation/processor/sample/annotated/Annotated1.java b/jack-tests/tests/com/android/jack/annotation/processor/sample/annotated/Annotated1.java new file mode 100644 index 0000000..1e5fed6 --- /dev/null +++ b/jack-tests/tests/com/android/jack/annotation/processor/sample/annotated/Annotated1.java @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.annotation.processor.sample.annotated; + +import com.android.jack.annotation.processor.sample.annotations.ResourceAnnotationTest; + +@ResourceAnnotationTest +public class Annotated1 { + +} diff --git a/jack-tests/tests/com/android/jack/annotation/processor/sample/annotated/Annotated2.java b/jack-tests/tests/com/android/jack/annotation/processor/sample/annotated/Annotated2.java new file mode 100644 index 0000000..c5c00aa --- /dev/null +++ b/jack-tests/tests/com/android/jack/annotation/processor/sample/annotated/Annotated2.java @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.annotation.processor.sample.annotated; + +import com.android.jack.annotation.processor.sample.annotations.SourceAnnotationTest; + +@SourceAnnotationTest +public class Annotated2 { + +} diff --git a/jack-tests/tests/com/android/jack/annotation/processor/sample/annotated/Annotated3.java b/jack-tests/tests/com/android/jack/annotation/processor/sample/annotated/Annotated3.java new file mode 100644 index 0000000..bd47b55 --- /dev/null +++ b/jack-tests/tests/com/android/jack/annotation/processor/sample/annotated/Annotated3.java @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.annotation.processor.sample.annotated; + +public class Annotated3 { + +} diff --git a/jack-tests/tests/com/android/jack/annotation/processor/sample/annotations/ResourceAnnotationTest.java b/jack-tests/tests/com/android/jack/annotation/processor/sample/annotations/ResourceAnnotationTest.java new file mode 100644 index 0000000..48b88ef --- /dev/null +++ b/jack-tests/tests/com/android/jack/annotation/processor/sample/annotations/ResourceAnnotationTest.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.annotation.processor.sample.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotates a class or interface with an annotation used by tests. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface ResourceAnnotationTest {} diff --git a/jack-tests/tests/com/android/jack/annotation/processor/sample/annotations/SourceAnnotationTest.java b/jack-tests/tests/com/android/jack/annotation/processor/sample/annotations/SourceAnnotationTest.java new file mode 100644 index 0000000..54b6957 --- /dev/null +++ b/jack-tests/tests/com/android/jack/annotation/processor/sample/annotations/SourceAnnotationTest.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.annotation.processor.sample.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotates a class or interface with an annotation used by tests to generate source code. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface SourceAnnotationTest {} diff --git a/jack-tests/tests/com/android/jack/annotation/processor/sample/processors/ResourceAnnotationProcessor.java b/jack-tests/tests/com/android/jack/annotation/processor/sample/processors/ResourceAnnotationProcessor.java new file mode 100644 index 0000000..d6c61d7 --- /dev/null +++ b/jack-tests/tests/com/android/jack/annotation/processor/sample/processors/ResourceAnnotationProcessor.java @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.annotation.processor.sample.processors; + +import com.android.jack.annotation.processor.sample.annotations.ResourceAnnotationTest; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.lang.annotation.Annotation; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.ProcessingEnvironment; +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.Element; +import javax.lang.model.element.TypeElement; +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.TypeMirror; +import javax.tools.Diagnostic.Kind; +import javax.tools.FileObject; +import javax.tools.StandardLocation; + +/** + * Annotation processor generating a dedicated resource file called {@code rscGeneratedFile}. + */ +@SupportedSourceVersion(SourceVersion.RELEASE_6) +@SupportedAnnotationTypes("*") +public class ResourceAnnotationProcessor extends AbstractProcessor { + @CheckForNull + private ProcessingEnvironment env; + + @Nonnull + private final List<String> data = new ArrayList<String>(); + + @Nonnull + public final static String FILENAME = "rscGeneratedFile"; + + @Override + public synchronized void init(@Nonnull ProcessingEnvironment env) { + this.env = env; + try { + env.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", FILENAME); + } catch (IOException e) { + // Best effort + } + } + + @Override + public boolean process(@Nonnull Set<? extends TypeElement> annotations, + @Nonnull RoundEnvironment roundEnv) { + assert env != null; + env.getMessager().printMessage(Kind.NOTE, + "ResourceAnnotationProcessor.process"); + if (roundEnv.processingOver()) { + try { + assert env != null; + + int count = 0; + while (true) { + try { + FileObject fo = env.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", FILENAME + count); + fo.openInputStream().close(); + count ++; + } catch (IOException e) { + break; + } + } + OutputStream os = env.getFiler() + .createResource(StandardLocation.CLASS_OUTPUT, "", FILENAME + count) + .openOutputStream(); + Writer writer = new OutputStreamWriter(os); + try { + for (String val : data) { + writer.write(val); + writer.write("\n"); + } + } finally { + writer.close(); + } + } catch (IOException e) { + env.getMessager().printMessage(Kind.ERROR, + "Can not write resource file for '" + FILENAME + "': " + e.getMessage()); + } + } else { + // + // @ResourceAnnotationTest + // + + for (Element element : getElementsAnnotatedWith(roundEnv, ResourceAnnotationTest.class)) { + assert data != null; + + TypeMirror type = element.asType(); + + if (type.getKind() == TypeKind.DECLARED) { + data.add(ResourceAnnotationTest.class.getCanonicalName()); + assert env != null; + data.add(env.getElementUtils().getBinaryName((TypeElement) element).toString()); + + } + } + } + + return false; + } + + @Nonnull + private Set<? extends Element> getElementsAnnotatedWith(@Nonnull RoundEnvironment roundEnv, + @Nonnull Class<? extends Annotation> cls) { + assert env != null; + + String name = cls.getCanonicalName(); + assert name != null; + + TypeElement element = env.getElementUtils().getTypeElement(name); + assert element != null; + + return roundEnv.getElementsAnnotatedWith(element); + } +} diff --git a/jack-tests/tests/com/android/jack/annotation/processor/sample/processors/SourceAnnotationProcessor.java b/jack-tests/tests/com/android/jack/annotation/processor/sample/processors/SourceAnnotationProcessor.java new file mode 100644 index 0000000..7ef7a7a --- /dev/null +++ b/jack-tests/tests/com/android/jack/annotation/processor/sample/processors/SourceAnnotationProcessor.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.jack.annotation.processor.sample.processors; + +import com.android.jack.annotation.processor.sample.annotations.SourceAnnotationTest; + +import java.io.IOException; +import java.io.Writer; +import java.lang.annotation.Annotation; +import java.util.Set; + +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.ProcessingEnvironment; +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.Element; +import javax.lang.model.element.TypeElement; +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.TypeMirror; +import javax.tools.Diagnostic.Kind; +import javax.tools.JavaFileObject; + +/** + * Annotation processor to generate a new source file. + */ +@SupportedSourceVersion(SourceVersion.RELEASE_6) +@SupportedAnnotationTypes("*") +public class SourceAnnotationProcessor extends AbstractProcessor { + @Nonnull + public static final String SOURCE_ANNOTATION_PROCESSOR_SUFFIX = + "SourceAnnotationProcessor.suffix"; + @CheckForNull + private ProcessingEnvironment env; + @Nonnull + private String suffix; + + @Override + public synchronized void init(@Nonnull ProcessingEnvironment env) { + this.env = env; + suffix = env.getOptions().get(SOURCE_ANNOTATION_PROCESSOR_SUFFIX); + if (suffix == null) { + suffix = "Duplicated"; + } + } + + @Override + public boolean process(@Nonnull Set<? extends TypeElement> annotations, @Nonnull RoundEnvironment roundEnv) { + assert env != null; + env.getMessager().printMessage(Kind.NOTE, + "SourceAnnotationProcessor.process"); + if (!roundEnv.processingOver()) { + + // + // @SourceAnnotationTest + // + + for (Element element : getElementsAnnotatedWith(roundEnv, SourceAnnotationTest.class)) { + TypeMirror type = element.asType(); + + if (type.getKind() == TypeKind.DECLARED) { + TypeElement classElement = (TypeElement) element; + try { + assert env != null; + JavaFileObject jfo = + env.getFiler().createSourceFile(classElement.getQualifiedName() + suffix); + Writer writer = jfo.openWriter(); + writer.write("public class " + classElement.getSimpleName() + suffix + " {}"); + writer.close(); + } catch (IOException e) { + throw new AssertionError(e); + } + } + } + } + + return false; + } + + @Nonnull + private Set<? extends Element> getElementsAnnotatedWith(@Nonnull RoundEnvironment roundEnv, + @Nonnull Class<? extends Annotation> cls) { + assert env != null; + + String name = cls.getCanonicalName(); + assert name != null; + + TypeElement element = env.getElementUtils().getTypeElement(name); + assert element != null; + + return roundEnv.getElementsAnnotatedWith(element); + } +} diff --git a/jack-tests/tests/com/android/jack/annotation/processor/sample/processors/javax.annotation.processing.Processor b/jack-tests/tests/com/android/jack/annotation/processor/sample/processors/javax.annotation.processing.Processor new file mode 100644 index 0000000..975dacb --- /dev/null +++ b/jack-tests/tests/com/android/jack/annotation/processor/sample/processors/javax.annotation.processing.Processor @@ -0,0 +1,2 @@ +com.android.jack.annotation.processor.sample.processors.SourceAnnotationProcessor +com.android.jack.annotation.processor.sample.processors.ResourceAnnotationProcessor |