Debian Codegraph @

chunkio-processor/src/com/android/tools/chunkio/ChunkProcessor.java

raw
1 /*
2 * Copyright (C) 2015 The Android Open Source Project
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 com.android.tools.chunkio;
18
19 import com.android.tools.chunkio.processor.ClassEmitter;
20 import com.android.tools.chunkio.processor.Environment;
21 import com.android.tools.chunkio.processor.Validator;
22
23 import javax.annotation.processing.AbstractProcessor;
24 import javax.annotation.processing.ProcessingEnvironment;
25 import javax.annotation.processing.RoundEnvironment;
26 import javax.annotation.processing.SupportedAnnotationTypes;
27 import javax.lang.model.SourceVersion;
28 import javax.lang.model.element.Element;
29 import javax.lang.model.element.TypeElement;
30 import java.io.DataInputStream;
31 import java.util.Set;
32
33 /**
34 * An annotation processor that finds fields annotated with {@link Chunk}
35 * in classes annotated with {@link Chunked}. This processor generates
36 * a new Java file for each annotated class it finds. The generated file
37 * can be used to read an instance of that class from a {@link DataInputStream}.
38 */
39 @SupportedAnnotationTypes({ "com.android.tools.chunkio.Chunked" })
40 public class ChunkProcessor extends AbstractProcessor {
41 private Validator validator;
42 private Environment environment;
43
44 @Override
45 public synchronized void init(ProcessingEnvironment processingEnvironment) {
46 super.init(processingEnvironment);
47 environment = Environment.from(processingEnvironment);
48 validator = new Validator(processingEnvironment.getTypeUtils(), environment);
49 }
50
51 @Override
52 public SourceVersion getSupportedSourceVersion() {
53 return SourceVersion.latestSupported();
54 }
55
56 @Override
57 public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
58 Set<? extends Element> chunked = roundEnvironment.getElementsAnnotatedWith(Chunked.class);
59 for (Element element : chunked) {
60 if (!validator.validate(element)) return true;
61
62 TypeElement typeElement = (TypeElement) element;
63
64 ClassEmitter generator = new ClassEmitter(typeElement, environment);
65 generator.emit();
66 }
67
68 return true;
69 }
70 }