本文整理汇总了Java中javax.annotation.processing.Filer类的典型用法代码示例。如果您正苦于以下问题:Java Filer类的具体用法?Java Filer怎么用?Java Filer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Filer类属于javax.annotation.processing包,在下文中一共展示了Filer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeServices
import javax.annotation.processing.Filer; //导入依赖的package包/类
private void writeServices() {
for (Map.Entry<Filer,Map<String,SortedSet<ServiceLoaderLine>>> outputFiles : outputFilesByProcessor.entrySet()) {
Filer filer = outputFiles.getKey();
for (Map.Entry<String,SortedSet<ServiceLoaderLine>> entry : outputFiles.getValue().entrySet()) {
try {
FileObject out = filer.createResource(StandardLocation.CLASS_OUTPUT, "", entry.getKey(),
originatingElementsByProcessor.get(filer).get(entry.getKey()).toArray(new Element[0]));
OutputStream os = out.openOutputStream();
try {
PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
for (ServiceLoaderLine line : entry.getValue()) {
line.write(w);
}
w.flush();
w.close();
} finally {
os.close();
}
} catch (IOException x) {
processingEnv.getMessager().printMessage(Kind.ERROR, "Failed to write to " + entry.getKey() + ": " + x.toString());
}
}
}
}
示例2: process
import javax.annotation.processing.Filer; //导入依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> typeElements, RoundEnvironment roundEnv) {
if (typeElements.size() == 0) {
return true;
}
for (TypeElement typeElement : typeElements) {
for (Element element : roundEnv.getElementsAnnotatedWith(typeElement)) {
T entity = element.getAnnotation(targetAnnotation());
SourceGenerator generator = createGenerator(element, entity, roundEnv);
Filer filer = super.processingEnv.getFiler();
try {
List<JavaFile> javaFile = generator.createJavaFile(element);
for (JavaFile file : javaFile) {
file.writeTo(filer);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
示例3: writeInto
import javax.annotation.processing.Filer; //导入依赖的package包/类
public void writeInto(final Filer pFiler, final Messager pMessager) {
// prepare generated class
final GeneratedClass lGeneratedClass = new GeneratedClass(mEnclosingClass, mMethods);
final TypeSpec lTypeSpecGeneratedClass = lGeneratedClass.buildTypeSpec();
final String lPackageName = lGeneratedClass.packageName();
// create generated class to a file
try {
JavaFile.builder(lPackageName, lTypeSpecGeneratedClass)
.build()
.writeTo(pFiler);
} catch (IOException pE) {
logError(pMessager, mEnclosingClass, "error while writing generated class");
}
}
示例4: generateMappingTable
import javax.annotation.processing.Filer; //导入依赖的package包/类
public void generateMappingTable(Filer filer) throws IOException {
if (routerTable.isEmpty()) {
return;
}
FieldSpec tableFieldSpec = FieldSpec.builder(Map.class, "routerTable")
.addModifiers(Modifier.PRIVATE)
.build();
MethodSpec initTableMethodSpec = getInitTableMethodSpec();
MethodSpec queryTableMethodSpec = getqueryTableMethodSpec();
MethodSpec constructorMethodSpec = getconstructorMethodSpec(tableFieldSpec, initTableMethodSpec);
MethodSpec addRouterMethodSpec = getAddRouterMethodSpec();
TypeSpec routerTableClassTypeSpec = getRouterTableClassTypeSpec(tableFieldSpec, initTableMethodSpec, queryTableMethodSpec, constructorMethodSpec, addRouterMethodSpec);
String packageName = Constants.GENERATED_PACKAGE_NAME;
JavaFile javaFile = JavaFile.builder(packageName, routerTableClassTypeSpec).build();
javaFile.writeTo(filer);
}
示例5: write_withIOException
import javax.annotation.processing.Filer; //导入依赖的package包/类
@Test
public void write_withIOException() throws Exception {
final ProcessingEnvironment env = mock(ProcessingEnvironment.class);
final Messager messager = mock(Messager.class);
final Filer filer = mock(Filer.class);
when(env.getMessager()).thenReturn(messager);
when(env.getFiler()).thenReturn(filer);
compiler.init(env);
final JavaFile javaFile = mock(JavaFile.class);
doThrow(IOException.class).when(javaFile).writeTo(any(Filer.class));
compiler.writeSource(javaFile);
verify(messager, times(1)).printMessage(eq(Diagnostic.Kind.ERROR), any());
}
示例6: generateStaticCodeLoader
import javax.annotation.processing.Filer; //导入依赖的package包/类
public static boolean generateStaticCodeLoader(Filer filer, ProcessorPrinter pp){
CodeBlock.Builder staticBuilder = CodeBlock.builder()
//GlobalSetting.getgetDefault().setGsonVersion(xxx)
.add("$T.getDefault().setGsonVersion($L);\n", ClassName.get(PKG_PROP, SN_GLOBAL_SETTING),
GlobalConfig.getInstance().getVersion());
TypeSpec typeSpec = TypeSpec.classBuilder(SN_STATIC_LOADER)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addStaticBlock(staticBuilder.build())
.addJavadoc(CodeBlock.of(DOC))
.build();
try {
JavaFile javaFile = JavaFile.builder(PKG_DM_INTERNAL, typeSpec)
.build();
// System.out.println(javaFile.toString());
javaFile.writeTo(filer);
} catch (IOException e) {
pp.error(TAG, "generateSharedProperties", Util.toString(e));
return false;
}
return true;
}
示例7: generateSharedProperties
import javax.annotation.processing.Filer; //导入依赖的package包/类
public static boolean generateSharedProperties(Collection<FieldData> fields,
Elements elements, Filer filer, ProcessorPrinter pp){
final ClassName cn_sp = ClassName.get(PKG_DM_INTERNAL, SIMPLE_NAME_SHARED_PROP);
CodeBlock.Builder staticBuilder = CodeBlock.builder();
for(FieldData fd : fields){
staticBuilder.add("$T.putToCache($S, $S, $L);\n", cn_sp, fd.getTypeCompat().toString(),
fd.getPropertyName(), fd.getComplexType());
}
String classSimpleName = SIMPLE_NAME_SHARED_PROP + "_" + findBestIndex(elements);
TypeSpec typeSpec = TypeSpec.classBuilder(classSimpleName)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addStaticBlock(staticBuilder.build())
.addJavadoc(CodeBlock.of(DOC))
.build();
try {
JavaFile javaFile = JavaFile.builder(PKG_DM_INTERNAL, typeSpec)
.build();
// System.out.println(javaFile.toString());
javaFile.writeTo(filer);
} catch (IOException e) {
pp.error(TAG, "generateSharedProperties", Util.toString(e));
return false;
}
return true;
}
示例8: BindingManager
import javax.annotation.processing.Filer; //导入依赖的package包/类
/**
* Initialize with the given details from the annotation processing enviornment
*/
public BindingManager(Elements elementUtils, Filer filer, Messager messager, Types typeUtils) {
this.elementUtils = elementUtils;
this.filer = filer;
this.messager = messager;
this.typeUtils = typeUtils;
this.typeBuilderMap = new HashMap<>();
stringTypeMirror = getType("java.lang.String");
listTypeMirror = getType("java.util.List");
mapTypeMirror = getType("java.util.Map");
charSequenceTypeMirror = getType("java.lang.CharSequence");
parcellableTypeMirror = getType("android.os.Parcelable");
try {
parcelClass = Class.forName("org.parceler.Parcel");
} catch (ClassNotFoundException ignored) {
}
}
示例9: write
import javax.annotation.processing.Filer; //导入依赖的package包/类
public void write(Filer filer) throws IOException {
FragmentCreatorBuilderGenerator fragmentCreatorBuilderGenerator =
new FragmentCreatorBuilderGenerator(environment);
FragmentCreatorReadGenerator fragmentCreatorReadGenerator = new FragmentCreatorReadGenerator(environment);
TypeSpec.Builder classBuilder = createClassBuilder();
classBuilder.addType(fragmentCreatorBuilderGenerator.create(model));
classBuilder.addMethod(fragmentCreatorReadGenerator.createReadMethod(model));
classBuilder.addMethod(fragmentCreatorBuilderGenerator.createBuilderNewBuilder(model, model.getArgsList()));
TypeSpec outClass = classBuilder.build();
JavaFile.builder(model.getPackageName(), outClass)
.addFileComment("This file was generated by fragment-creator. Do not modify!")
.build()
.writeTo(filer);
}
示例10: toJavaFile
import javax.annotation.processing.Filer; //导入依赖的package包/类
public static void toJavaFile(Filer filer, Builder classBuilder, ClassName className,
TypeElement... originatingElement)
{
try
{
JavaFile javaFile =
JavaFile.builder(className.packageName(), classBuilder.build()).build();
JavaFileObject javaFileObject =
filer.createSourceFile(className.reflectionName(), originatingElement);
Writer writer = javaFileObject.openWriter();
javaFile.writeTo(writer);
writer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
示例11: doWithOriAndPrintWriter
import javax.annotation.processing.Filer; //导入依赖的package包/类
public static void doWithOriAndPrintWriter(Filer filer, JavaFileManager.Location location, String relativePath, String filename, BiConsumer<String, PrintWriter> consumer){
try {
FileObject resource = filer.getResource(location, relativePath, filename);
String data;
try{
CharSequence cs = resource.getCharContent(false);
data = cs.toString();
resource.delete();
}catch (FileNotFoundException ignored){
data = "";
}
resource = filer.createResource(location, relativePath, filename);
try(OutputStream outputStream = resource.openOutputStream()){
consumer.accept(data,new PrintWriter(outputStream));
}
} catch (IOException e) {
note("do with resource file failed"+relativePath+filename+" Exception: " + e.toString());
}
}
示例12: brewJava
import javax.annotation.processing.Filer; //导入依赖的package包/类
void brewJava(Filer filer) throws Exception {
final TypeSpec.Builder spec = TypeSpec.classBuilder(mClassName)
.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class)
.addMember("value", "$S", "unchecked")
.build())
.addSuperinterface(ClassName.get(SQLiteSchema.class))
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addField(makeTablesField())
.addField(makeMigrationsField())
.addField(makeVersionField())
.addStaticBlock(makeStaticInit())
.addMethod(makeInit())
.addMethod(makeGetVersion())
.addMethod(makeGetTable())
.addMethod(makeGetAllTables())
.addMethod(makeAddMigration())
.addMethod(makeGetAllMigrations());
JavaFile.builder(mClassName.packageName(), spec.build())
.addFileComment("Generated code from Alchemy. Do not modify!")
.skipJavaLangImports(true)
.build()
.writeTo(filer);
}
示例13: brewJava
import javax.annotation.processing.Filer; //导入依赖的package包/类
ClassName brewJava(Filer filer) throws Exception {
final ClassName modelName = ClassName.get(mElement);
final TypeSpec.Builder spec = TypeSpec.classBuilder(mClassName.simpleName())
.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class)
.addMember("value", "$S", "TryFinallyCanBeTryWithResources")
.build())
.addModifiers(Modifier.FINAL)
.superclass(ParameterizedTypeName.get(ClassName.bestGuess("alchemy.sqlite.AbstractTable"), modelName))
.addField(makeEntryField())
.addMethod(makeInit())
.addMethod(makeGetEntry())
.addMethod(makeCreate());
if (!mEntrySpec.getRelationSpecs().isEmpty()) {
spec.addMethod(makeOnInsert());
}
JavaFile.builder(mClassName.packageName(), spec.build())
.addFileComment("Generated code from Alchemy. Do not modify!")
.skipJavaLangImports(true)
.build()
.writeTo(filer);
return mClassName;
}
示例14: brewJava
import javax.annotation.processing.Filer; //导入依赖的package包/类
void brewJava(Filer filer) throws Exception {
for (final EntrySpec entrySpec : mEntries.values()) {
for (final RelationSpec relationSpec : entrySpec.getRelationSpecs()) {
relationSpec.brewJava(filer);
}
entrySpec.brewJava(filer);
entrySpec.getContractSpec().brewJava(filer);
}
for (final Map.Entry<String, Map<Element, TableSpec>> entry : mSchemas.entrySet()) {
final SchemaSpec schemaSpec = new SchemaSpec(entry.getKey());
for (final TableSpec tableSpec : entry.getValue().values()) {
schemaSpec.putTable(tableSpec.getElement(), tableSpec.brewJava(filer));
}
schemaSpec.brewJava(filer);
}
}
示例15: brewJava
import javax.annotation.processing.Filer; //导入依赖的package包/类
void brewJava(Filer filer) throws Exception {
final TypeSpec.Builder spec = TypeSpec.classBuilder(mClassName.simpleName())
.addOriginatingElement(mElement)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
for (final ColumnSpec columnSpec : mColumnSpecs) {
final String columnName = CaseFormat.LOWER_UNDERSCORE
.to(CaseFormat.UPPER_UNDERSCORE, columnSpec.getColumnName());
spec.addField(FieldSpec.builder(String.class, columnName, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer("$S", columnSpec.getColumnName())
.build());
}
spec.addMethod(MethodSpec.constructorBuilder()
.addModifiers(Modifier.PRIVATE)
.build());
JavaFile.builder(mClassName.packageName(), spec.build())
.addFileComment("Generated code from Alchemy. Do not modify!")
.skipJavaLangImports(true)
.build()
.writeTo(filer);
}