| 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.codegen; |
| 18 |
|
| 19 |
import java.io.IOException; |
| 20 |
import java.util.Set; |
| 21 |
|
| 22 |
// The codegen API was inspired from JavaPoet (https://github.com/square/javapoet) |
| 23 |
// JavaPoet requires Java 7 and we need to support Java 6 |
| 24 |
// This package contains a subset of JavaPoet's capabilities but uses similar APIs |
| 25 |
// and similar substitution syntax ($L, $T, etc.) |
| 26 |
public final class JavaFile { |
| 27 |
private static final NullAppendable NULL_APPENDABLE = new NullAppendable(); |
| 28 |
|
| 29 |
private final String packageName; |
| 30 |
private final ClassDef classDef; |
| 31 |
private final String indent; |
| 32 |
|
| 33 |
private JavaFile(Builder builder) { |
| 34 |
packageName = builder.mPackageName; |
| 35 |
classDef = builder.mClass; |
| 36 |
indent = builder.mIndent; |
| 37 |
} |
| 38 |
|
| 39 |
public void emit(Appendable out) throws IOException { |
| 40 |
CodeGenerator generator = new CodeGenerator(NULL_APPENDABLE, ""); |
| 41 |
emit(generator); |
| 42 |
generator = new CodeGenerator(out, indent, generator.getImportCandidates()); |
| 43 |
emit(generator); |
| 44 |
} |
| 45 |
|
| 46 |
private void emit(CodeGenerator generator) throws IOException { |
| 47 |
if (!packageName.isEmpty()) { |
| 48 |
generator.emit("package $L;\n\n", packageName); |
| 49 |
generator.setPackage(packageName); |
| 50 |
} |
| 51 |
|
| 52 |
Set<String> imports = generator.getImports(); |
| 53 |
if (!imports.isEmpty()) { |
| 54 |
for (String imported : imports) { |
| 55 |
generator.emit("import $L;\n", imported); |
| 56 |
} |
| 57 |
generator.emit("\n"); |
| 58 |
} |
| 59 |
|
| 60 |
classDef.emit(generator); |
| 61 |
|
| 62 |
generator.setPackage(null); |
| 63 |
} |
| 64 |
|
| 65 |
public static Builder builder(String packageName, ClassDef classDef) { |
| 66 |
return new Builder(packageName, classDef); |
| 67 |
} |
| 68 |
|
| 69 |
public static final class Builder { |
| 70 |
private final String mPackageName; |
| 71 |
private final ClassDef mClass; |
| 72 |
private String mIndent = " "; |
| 73 |
|
| 74 |
private Builder(String packageName, ClassDef classDef) { |
| 75 |
mPackageName = packageName; |
| 76 |
mClass = classDef; |
| 77 |
} |
| 78 |
|
| 79 |
public JavaFile build() { |
| 80 |
return new JavaFile(this); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
private static class NullAppendable implements Appendable { |
| 85 |
@Override |
| 86 |
public Appendable append(CharSequence charSequence) throws IOException { |
| 87 |
return this; |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public Appendable append(CharSequence charSequence, int i, int i1) throws IOException { |
| 92 |
return this; |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public Appendable append(char c) throws IOException { |
| 97 |
return this; |
| 98 |
} |
| 99 |
} |
| 100 |
} |