• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * * Copyright 2022 Google LLC. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package kapt;
18 
19 import static java.nio.charset.StandardCharsets.UTF_8;
20 
21 import com.google.auto.service.AutoService;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.io.UncheckedIOException;
25 import java.util.Set;
26 import javax.annotation.processing.AbstractProcessor;
27 import javax.annotation.processing.ProcessingEnvironment;
28 import javax.annotation.processing.Processor;
29 import javax.annotation.processing.RoundEnvironment;
30 import javax.annotation.processing.SupportedAnnotationTypes;
31 import javax.lang.model.SourceVersion;
32 import javax.lang.model.element.Element;
33 import javax.lang.model.element.TypeElement;
34 
35 /** Simple test processor that numbered classes in rounds as specified in {@link Count}. */
36 @AutoService(Processor.class)
37 @SupportedAnnotationTypes("*")
38 public class AP extends AbstractProcessor {
39 
40   private volatile String prefix = "";
41 
42   @Override
init(ProcessingEnvironment processingEnv)43   public synchronized void init(ProcessingEnvironment processingEnv) {
44     super.init(processingEnv);
45     prefix = processingEnv.getOptions().getOrDefault("kapt.AP.indexPrefix", "");
46   }
47 
48   @Override
getSupportedSourceVersion()49   public SourceVersion getSupportedSourceVersion() {
50     return SourceVersion.latestSupported();
51   }
52 
53   @Override
process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)54   public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
55     for (Element element : roundEnv.getElementsAnnotatedWith(Count.class)) {
56       Count c = element.getAnnotation(Count.class);
57       if (c.value() > 0) {
58         int next = c.value() - 1;
59         String simpleName = element.getSimpleName() + "_" + prefix + next;
60         try (OutputStream os =
61             processingEnv
62                 .getFiler()
63                 .createSourceFile("kapt." + simpleName, element)
64                 .openOutputStream()) {
65           os.write(
66               String.format("package kapt; @Count(%d) public class %s {}", next, simpleName)
67                   .getBytes(UTF_8));
68         } catch (IOException e) {
69           throw new UncheckedIOException(e);
70         }
71       }
72     }
73     return false;
74   }
75 }
76