From 833315a08961afe8bbd539b84bd8a71bf87db4c0 Mon Sep 17 00:00:00 2001 From: Frank Benoit Date: Sun, 27 Sep 2020 21:35:58 +0200 Subject: [PATCH] Add null annotation for eclipse JDT Signed-off-by: Frank Benoit --- factory/pom.xml | 5 +++ .../processor/AutoFactoryProcessor.java | 17 +++++++++- .../auto/factory/processor/FactoryWriter.java | 6 ++++ .../factory/processor/NullAnnotation.java | 6 ++++ .../processor/AutoFactoryProcessorTest.java | 12 +++++++ .../expected/NullAnnotationJdtFactory.java | 33 +++++++++++++++++++ .../resources/good/NullAnnotationJdt.java | 21 ++++++++++++ 7 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 factory/src/main/java/com/google/auto/factory/processor/NullAnnotation.java create mode 100644 factory/src/test/resources/expected/NullAnnotationJdtFactory.java create mode 100644 factory/src/test/resources/good/NullAnnotationJdt.java diff --git a/factory/pom.xml b/factory/pom.xml index 48bcfeffde..5eaf3318c2 100644 --- a/factory/pom.xml +++ b/factory/pom.xml @@ -151,6 +151,11 @@ 2.5.5 test + + org.eclipse.jdt + org.eclipse.jdt.annotation + 2.2.600 + diff --git a/factory/src/main/java/com/google/auto/factory/processor/AutoFactoryProcessor.java b/factory/src/main/java/com/google/auto/factory/processor/AutoFactoryProcessor.java index 5cc1d94d53..a914347fa3 100644 --- a/factory/src/main/java/com/google/auto/factory/processor/AutoFactoryProcessor.java +++ b/factory/src/main/java/com/google/auto/factory/processor/AutoFactoryProcessor.java @@ -59,6 +59,7 @@ @IncrementalAnnotationProcessor(IncrementalAnnotationProcessorType.ISOLATING) @AutoService(Processor.class) public final class AutoFactoryProcessor extends AbstractProcessor { + private static final String NULL_ANNOTATION = "nullAnnotations"; private FactoryDescriptorGenerator factoryDescriptorGenerator; private AutoFactoryDeclaration.Factory declarationFactory; private ProvidedChecker providedChecker; @@ -125,7 +126,7 @@ private void doProcess(RoundEnvironment roundEnv) { indexedMethodsBuilder.build(); ImmutableSetMultimap factoriesBeingCreated = simpleNamesToNames(indexedMethods.keySet()); - FactoryWriter factoryWriter = new FactoryWriter(processingEnv, factoriesBeingCreated); + FactoryWriter factoryWriter = new FactoryWriter( getNullAnnotation(), processingEnv, factoriesBeingCreated); indexedMethods.asMap().forEach( (factoryName, methodDescriptors) -> { @@ -172,6 +173,15 @@ private void doProcess(RoundEnvironment roundEnv) { }); } + private NullAnnotation getNullAnnotation(){ + String name = processingEnv.getOptions().get(NULL_ANNOTATION); + for( NullAnnotation anno : NullAnnotation.values()){ + if( anno.name().equals(name)) { + return anno; + } + } + return NullAnnotation.NONE; + } private ImmutableSet implementationMethods( TypeElement supertype, Element autoFactoryElement) { ImmutableSet.Builder implementationMethodsBuilder = @@ -232,4 +242,9 @@ public ImmutableSet getSupportedAnnotationTypes() { public SourceVersion getSupportedSourceVersion() { return SourceVersion.latestSupported(); } + + @Override + public Set getSupportedOptions() { + return ImmutableSet.of(NULL_ANNOTATION); + } } diff --git a/factory/src/main/java/com/google/auto/factory/processor/FactoryWriter.java b/factory/src/main/java/com/google/auto/factory/processor/FactoryWriter.java index 53b99cb5ba..e37dba0a1b 100644 --- a/factory/src/main/java/com/google/auto/factory/processor/FactoryWriter.java +++ b/factory/src/main/java/com/google/auto/factory/processor/FactoryWriter.java @@ -61,13 +61,16 @@ final class FactoryWriter { private final Elements elements; private final SourceVersion sourceVersion; private final ImmutableSetMultimap factoriesBeingCreated; + private final NullAnnotation nullAnnotation; FactoryWriter( + NullAnnotation nullAnnotation, ProcessingEnvironment processingEnv, ImmutableSetMultimap factoriesBeingCreated) { this.filer = processingEnv.getFiler(); this.elements = processingEnv.getElementUtils(); this.sourceVersion = processingEnv.getSourceVersion(); + this.nullAnnotation = nullAnnotation; this.factoriesBeingCreated = factoriesBeingCreated; } @@ -85,6 +88,9 @@ void writeFactory(FactoryDescriptor descriptor) AutoFactoryProcessor.class, "https://github.com/google/auto/tree/master/factory") .ifPresent(factory::addAnnotation); + if( nullAnnotation == nullAnnotation.JDT ) { + factory.addAnnotation(org.eclipse.jdt.annotation.NonNullByDefault.class); + } if (!descriptor.allowSubclasses()) { factory.addModifiers(FINAL); } diff --git a/factory/src/main/java/com/google/auto/factory/processor/NullAnnotation.java b/factory/src/main/java/com/google/auto/factory/processor/NullAnnotation.java new file mode 100644 index 0000000000..08c68d2610 --- /dev/null +++ b/factory/src/main/java/com/google/auto/factory/processor/NullAnnotation.java @@ -0,0 +1,6 @@ +package com.google.auto.factory.processor; + +public enum NullAnnotation { + JDT, + NONE; +} diff --git a/factory/src/test/java/com/google/auto/factory/processor/AutoFactoryProcessorTest.java b/factory/src/test/java/com/google/auto/factory/processor/AutoFactoryProcessorTest.java index 3088bb2e15..6589b86f02 100644 --- a/factory/src/test/java/com/google/auto/factory/processor/AutoFactoryProcessorTest.java +++ b/factory/src/test/java/com/google/auto/factory/processor/AutoFactoryProcessorTest.java @@ -434,6 +434,18 @@ public void defaultPackage() { .generatesSources(loadExpectedFile("expected/DefaultPackageFactory.java")); } + @Test + public void nullAnnotationJdt() { + JavaFileObject file = JavaFileObjects.forResource("good/NullAnnotationJdt.java"); + assertAbout(javaSource()) + .that(file) + .withCompilerOptions("-AnullAnnotations=JDT") + .processedWith(new AutoFactoryProcessor()) + .compilesWithoutError() + .and() + .generatesSources(loadExpectedFile("expected/NullAnnotationJdtFactory.java")); + } + private JavaFileObject loadExpectedFile(String resourceName) { try { List sourceLines = Resources.readLines(Resources.getResource(resourceName), UTF_8); diff --git a/factory/src/test/resources/expected/NullAnnotationJdtFactory.java b/factory/src/test/resources/expected/NullAnnotationJdtFactory.java new file mode 100644 index 0000000000..4de2ab4f0d --- /dev/null +++ b/factory/src/test/resources/expected/NullAnnotationJdtFactory.java @@ -0,0 +1,33 @@ +/* + * Copyright 2013 Google LLC + * + * 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 tests; + +import javax.annotation.processing.Generated; +import javax.inject.Inject; +import org.eclipse.jdt.annotation.NonNullByDefault; + +@Generated( + value = "com.google.auto.factory.processor.AutoFactoryProcessor", + comments = "https://github.com/google/auto/tree/master/factory" + ) +@NonNullByDefault +final class NullAnnotationJdtFactory { + @Inject NullAnnotationJdtFactory() {} + + NullAnnotationJdt create() { + return new NullAnnotationJdt(); + } +} diff --git a/factory/src/test/resources/good/NullAnnotationJdt.java b/factory/src/test/resources/good/NullAnnotationJdt.java new file mode 100644 index 0000000000..e94be8f07a --- /dev/null +++ b/factory/src/test/resources/good/NullAnnotationJdt.java @@ -0,0 +1,21 @@ +/* + * Copyright 2013 Google LLC + * + * 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 tests; + +import com.google.auto.factory.AutoFactory; + +@AutoFactory +final class NullAnnotationJdt {}