Skip to content

Commit

Permalink
Add null annotation for eclipse JDT
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Benoit <[email protected]>
  • Loading branch information
frankbenoit authored and Frank Benoit committed Sep 27, 2020
1 parent 6bed859 commit 833315a
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 1 deletion.
5 changes: 5 additions & 0 deletions factory/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@
<version>2.5.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.annotation</artifactId>
<version>2.2.600</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -125,7 +126,7 @@ private void doProcess(RoundEnvironment roundEnv) {
indexedMethodsBuilder.build();
ImmutableSetMultimap<String, PackageAndClass> factoriesBeingCreated =
simpleNamesToNames(indexedMethods.keySet());
FactoryWriter factoryWriter = new FactoryWriter(processingEnv, factoriesBeingCreated);
FactoryWriter factoryWriter = new FactoryWriter( getNullAnnotation(), processingEnv, factoriesBeingCreated);

indexedMethods.asMap().forEach(
(factoryName, methodDescriptors) -> {
Expand Down Expand Up @@ -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<ImplementationMethodDescriptor> implementationMethods(
TypeElement supertype, Element autoFactoryElement) {
ImmutableSet.Builder<ImplementationMethodDescriptor> implementationMethodsBuilder =
Expand Down Expand Up @@ -232,4 +242,9 @@ public ImmutableSet<String> getSupportedAnnotationTypes() {
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}

@Override
public Set<String> getSupportedOptions() {
return ImmutableSet.of(NULL_ANNOTATION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ final class FactoryWriter {
private final Elements elements;
private final SourceVersion sourceVersion;
private final ImmutableSetMultimap<String, PackageAndClass> factoriesBeingCreated;
private final NullAnnotation nullAnnotation;

FactoryWriter(
NullAnnotation nullAnnotation,
ProcessingEnvironment processingEnv,
ImmutableSetMultimap<String, PackageAndClass> factoriesBeingCreated) {
this.filer = processingEnv.getFiler();
this.elements = processingEnv.getElementUtils();
this.sourceVersion = processingEnv.getSourceVersion();
this.nullAnnotation = nullAnnotation;
this.factoriesBeingCreated = factoriesBeingCreated;
}

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.google.auto.factory.processor;

public enum NullAnnotation {
JDT,
NONE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> sourceLines = Resources.readLines(Resources.getResource(resourceName), UTF_8);
Expand Down
33 changes: 33 additions & 0 deletions factory/src/test/resources/expected/NullAnnotationJdtFactory.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
21 changes: 21 additions & 0 deletions factory/src/test/resources/good/NullAnnotationJdt.java
Original file line number Diff line number Diff line change
@@ -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 {}

0 comments on commit 833315a

Please sign in to comment.