Java Code Examples for javax.annotation.processing.Filer.createResource()

The following are Jave code examples for showing how to use createResource() of the javax.annotation.processing.Filer class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: AbstractServiceProviderProcessor.java   View Source Code Vote up 6 votes
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());
            }
        }
    }
}
 
Example 2
Project: pndaox   File: ResourceHelper.java   View Source Code Vote up 6 votes
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());
    }
}
 
Example 3
Project: java-code-gen   File: Generator.java   View Source Code Vote up 6 votes
default void writeServiceNames(Class<?> serviceClass, Filer filer, List<String> generatedClassNames) throws IOException {
    String resourceFile = "META-INF/services/" + serviceClass.getName();
    FileObject existingFile = filer.getResource(StandardLocation.CLASS_OUTPUT, "", resourceFile);
    List<String> lines = new ArrayList<>();
    try (Reader reader = existingFile.openReader(false)) {
        try (BufferedReader bufferedReader = new BufferedReader(reader)) {
            lines.addAll(bufferedReader.lines().collect(Collectors.toList()));
        }
    } catch (IOException e) {
        // Just ignore because reading from a non-existing file and there is no way to detect its existence
        // than open the input stream/reader.
    }

    lines.addAll(generatedClassNames);

    FileObject newFile = filer.createResource(StandardLocation.CLASS_OUTPUT, "", resourceFile);
    try (Writer writer = newFile.openWriter()) {
        try (BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
            for (String line : lines) {
                bufferedWriter.write(line);
                bufferedWriter.newLine();
            }
        }
    }
}
 
Example 4
Project: bridje-framework   File: ControlProcessor.java   View Source Code Vote up 6 votes
@Override
public synchronized void init(ProcessingEnvironment processingEnv)
{
    //Creating necesary objects for annotations procesing.
    super.init(processingEnv);
    Messager messager = processingEnv.getMessager();
    try
    {
        Filer filer = processingEnv.getFiler();
        //Creating output file
        FileObject fobj = filer.createResource(StandardLocation.CLASS_OUTPUT, "", CONTROLS_RESOURCE_FILE);
        writer = fobj.openWriter();
    }
    catch (IOException e)
    {
        messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage());
        LOG.severe(e.getMessage());
    }
}
 
Example 5
Project: bridje-framework   File: ClassListPropertyFile.java   View Source Code Vote up 6 votes
@Override
public synchronized void init(ProcessingEnvironment processingEnv)
{
    //Creating necessary objects for annotations processing.
    super.init(processingEnv);
    Messager messager = processingEnv.getMessager();
    try
    {
        Filer filer = processingEnv.getFiler();
        //Creating output file
        FileObject fobj = filer.createResource(StandardLocation.CLASS_OUTPUT, "", getFileName());
        writer = fobj.openWriter();
    }
    catch (IOException e)
    {
        messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage());
        LOG.severe(e.getMessage());
    }
}
 
Example 6
Project: pndao   File: ResourceHelper.java   View Source Code Vote up 5 votes
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());
    }
}
 
Example 7
Project: sql-first-mapper   File: DaoWriter.java   View Source Code Vote up 5 votes
/**
 * Write <query>.sql to resources folder
 *
 * @param queryDesc information on query
 */
private void writeResource(QueryDesc queryDesc) {
    Filer filer = processingEnv.getFiler();
    try {
        FileObject o = filer.createResource(StandardLocation.CLASS_OUTPUT,
                queryDesc.getPackageName() + ".sql", queryDesc.getMethodNameFirstUpper() + ".sql");
        Writer w = o.openWriter();
        w.append(queryDesc.getQuery());
        w.flush();
        w.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 8
Project: ngAndroid   File: FileHelper.java   View Source Code Vote up 5 votes
/**
 * We use a dirty trick to find the AndroidManifest.xml file, since it's not
 * available in the classpath. The idea is quite simple : create a fake
 * class file, retrieve its URI, and start going up in parent folders to
 * find the AndroidManifest.xml file. Any better solution will be
 * appreciated.
 */
public Option<FileHolder> findRootProjectHolder() {
	Filer filer = processingEnv.getFiler();

	FileObject dummySourceFile;
	try {
		dummySourceFile = filer.createResource(StandardLocation.SOURCE_OUTPUT, "com", "dummy" + System.currentTimeMillis());
	} catch (IOException ignored) {
		return Option.absent();
	}
	String dummySourceFilePath = dummySourceFile.toUri().toString();

	if (dummySourceFilePath.startsWith("file:")) {
		if (!dummySourceFilePath.startsWith("file://")) {
			dummySourceFilePath = "file://" + dummySourceFilePath.substring("file:".length());
		}
	} else {
		dummySourceFilePath = "file://" + dummySourceFilePath;
	}

	URI cleanURI;
	try {
		cleanURI = new URI(dummySourceFilePath);
	} catch (URISyntaxException e) {
		return Option.absent();
	}
	try {
		File dummyFile = new File(cleanURI);
		File sourcesGenerationFolder = dummyFile.getParentFile();
		File projectRoot = sourcesGenerationFolder.getParentFile();

		return Option.of(new FileHolder(dummySourceFilePath, sourcesGenerationFolder, projectRoot));
	}catch(IllegalArgumentException ex){
		return Option.absent();
	}
}
 
Example 9
Project: sundrio   File: ResourcecifyProcessor.java   View Source Code Vote up 5 votes
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
    Filer filer = processingEnv.getFiler();

    for (TypeElement typeElement : annotations) {
        for (Element element : env.getElementsAnnotatedWith(typeElement)) {
            Resourcecify resourcecify = element.getAnnotation(Resourcecify.class);
            if (resourcecify == null) {
                continue;
            }

            if (element instanceof Symbol.ClassSymbol) {
                Symbol.ClassSymbol s = (Symbol.ClassSymbol) element;
                try {
                    String packageName = getPackageName(s);

                    JavaFileObject source = s.sourcefile;
                    File sourceFile = new File(source.getName()).getAbsoluteFile();
                    String sourceFileName = sourceFile.getName();

                    FileObject target = filer.createResource(StandardLocation.CLASS_OUTPUT, packageName, sourceFileName, s);
                    IOUtils.copy(source, target);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }
    }
    return false;
}
 
Example 10
Project: aidl2   File: Logger.java   View Source Code Vote up 4 votes
private static FileObject createLogFile(ProcessingEnvironment pe) throws IOException {
    final Filer filer = pe.getFiler();

    return filer.createResource(StandardLocation.SOURCE_OUTPUT, "", "aidl2.log");
}
 
Example 11
Project: jackdaw   File: JServiceCodeGenerator.java   View Source Code Vote up 4 votes
private FileObject createResource(final Filer filer, final String path) throws Exception {
    return filer.createResource(CLASS_OUTPUT, StringUtils.EMPTY, path);
}