Diagnostic.Kind.ERROR, "Problem with Filer: " + filerEx.getMessage() );
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(RouterMap.class); try { TypeSpec type = getRouterTableInitializer(elements); if(type != null) { JavaFile.builder("cn.campusapp.router.router", type).build().writeTo(mFiler); } } catch (FilerException e){ e.printStackTrace(); } catch (Exception e) { error(e.getMessage()); } return true; }
@Override public FileObject getResource(Location location, CharSequence pkg, CharSequence relativeName) throws IOException { FileObject file = fileManager.getFileForInput(location, pkg.toString(), relativeName.toString()); if (file == null) { throw new FileNotFoundException("Resource does not exist " + location + '/' + pkg + '/' + relativeName); } if (createdResources.contains(file.toUri())) { throw new FilerException("Resource already created " + pkg + "." + relativeName); } return file; }
private PrintWriter getWriter(String serviceName) throws IOException { String name = servicePath + serviceName; PrintWriter pw = writer.get(name); if (pw == null) { try { FileObject fo = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", name); pw = new PrintWriter(fo.openWriter()); } catch (FilerException ex) { processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, ex.getLocalizedMessage()); } writer.put(name, pw); } return pw; }
@Override public FileObject getResource(Location location, CharSequence pkg, CharSequence relativeName) throws IOException { FileObject file = fileManager.getFileForInput(location, pkg.toString(), relativeName.toString()); if (file == null) { throw new FileNotFoundException("Resource does not exist " + location + '/' + pkg + '/' + relativeName); } if (createdResources.contains(file.toUri())) { throw new FilerException("Resource already created " + pkg + "." + relativeName); } return file; }
@Override public FileObject getResource(Location location, CharSequence pkg, CharSequence relativeName) throws IOException { //TODO: do we need to check validity of 'name', or can we trust the filemanager to handle that? FileObject fo = _fileManager.getFileForInput( location, pkg.toString(), relativeName.toString()); URI uri = fo.toUri(); if (_createdFiles.contains(uri)) { throw new FilerException("Resource already created : " + location + '/' + pkg + '/' + relativeName); //$NON-NLS-1$ } _createdFiles.add(uri); return fo; }
@Override public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { String qualifiedClassName = toQualifiedClassName(pkg, fileName); LOGGER.debug("Generating class: {}", qualifiedClassName); Element[] classOriginatingElements = originatingElements.getClassOriginatingElements(qualifiedClassName); try { JavaFileObject sourceFile; if (classOriginatingElements.length == 0) { LOGGER.info("Generating class with no originating element: {}", qualifiedClassName); } sourceFile = filer.createSourceFile(qualifiedClassName, classOriginatingElements); return sourceFile.openOutputStream(); } catch (FilerException e) { LOGGER.error("Could not generate source file for {} due to error: {}", qualifiedClassName, e.getMessage()); /* * This exception is expected, when some files are created twice. We cannot * delete existing files, unless using a dirty hack. Files a created twice when * the same file is created from different annotation rounds. Happens when * renaming classes, and for Background executor. It also probably means I * didn't fully understand how annotation processing works. If anyone can point * me out... */ return VOID_OUTPUT_STREAM; } }
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { Set<? extends Element> elementDispatchers = roundEnv.getElementsAnnotatedWith(DisPatcher.class); Set<? extends Element> elementModuleServices = roundEnv.getElementsAnnotatedWith(ModuleService.class); String[] moduleNames = null; Set<? extends Element> modulesList = roundEnv.getElementsAnnotatedWith(DispatcherModules.class); if (modulesList != null && modulesList.size() > 0) { Element modules = modulesList.iterator().next(); moduleNames = modules.getAnnotation(DispatcherModules.class).value(); } try { TypeSpec type = getRouterTableInitializer(elementDispatchers, elementModuleServices); if (type != null) { builder(CompilerConstant.AutoCreateDispatcherPackage, type).build().writeTo(mFiler); } if (moduleNames != null && moduleNames.length > 0) { TypeSpec typeInit = generateModulesRouterInit(moduleNames); if (typeInit != null) { builder(CompilerConstant.AutoCreateDispatcherPackage, typeInit).build().writeTo(mFiler); } } } catch (FilerException e) { e.printStackTrace(); } catch (Exception e) { error(e.getMessage()); } return true; }
private void checkNameAndExistence(String typename, boolean allowUnnamedPackageInfo) throws FilerException { // TODO: Check if type already exists on source or class path? // If so, use warning message key proc.type.already.exists checkName(typename, allowUnnamedPackageInfo); if (aggregateGeneratedSourceNames.contains(typename) || aggregateGeneratedClassNames.contains(typename)) { if (lint) log.warning("proc.type.recreate", typename); throw new FilerException("Attempt to recreate a file for type " + typename); } }
processingEnv.getMessager().printMessage( Kind.WARNING, "Error producing Builder: " + e.getMessage(), type, findAnnotationMirror(type, "org.inferred.freebuilder.FreeBuilder").get());
private void checkNameAndExistence(String typename, boolean allowUnnamedPackageInfo) throws FilerException { // TODO: Check if type already exists on source or class path? // If so, use warning message key proc.type.already.exists checkName(typename, allowUnnamedPackageInfo); if (aggregateGeneratedSourceNames.contains(typename) || aggregateGeneratedClassNames.contains(typename)) { if (lint) log.warning("proc.type.recreate", typename); throw new FilerException("Attempt to recreate a file for type " + typename); } }
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { if (abortProcessing.get() == Boolean.TRUE) { // Another instance of AutoDelegateProcessor is running in the current processing environment. return false; } Set<String> generatedTypes = new HashSet<>(); for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(AutoDelegate.class)) { try { validateAnnotatedElement(annotatedElement); TypeElement typeElement = (TypeElement) annotatedElement; TypeToExtend typeToExtend = createTypeToExtend(typeElement); if (generatedTypes.contains(typeToExtend.getCanonicalName())) { continue; } generatedTypes.add(typeToExtend.getCanonicalName()); generateCode(typeToExtend); } catch (FilerException e) { // Happens when same file is written twice. warn(annotatedElement, e.getMessage()); } catch (ProcessingException e) { error(e.getElement(), e.getMessage()); } catch (IOException | RuntimeException e) { error(annotatedElement, e.getMessage()); } } return false; }
private void checkNameAndExistence(String typename, boolean allowUnnamedPackageInfo) throws FilerException { // TODO: Check if type already exists on source or class path? // If so, use warning message key proc.type.already.exists checkName(typename, allowUnnamedPackageInfo); if (aggregateGeneratedSourceNames.contains(typename) || aggregateGeneratedClassNames.contains(typename)) { if (lint) log.warning("proc.type.recreate", typename); throw new FilerException("Attempt to recreate a file for type " + typename); } }
public String filePath(Location location, TypeElement element) { try { String qualifiedName = element.asType().toString(); String fileName = qualifiedName.replace(".", FILE_SEPARATOR); Filer filer = environment.getFiler(); FileObject file = filer.getResource(location, "", fileName + ".java"); return file.toUri().getPath(); } catch (FilerException e) { // this is clearly very weak... String message = e.getMessage(); return message.substring(message.indexOf(FILE_SEPARATOR)); } catch (IOException e) { throw new UncheckedIOException(e); } }
@Override public FileObject createResource(Location location, CharSequence pkg, CharSequence relativeName, Element... originatingElements) throws IOException { //TODO: do we need to check validity of 'name', or can we trust the filemanager to handle that? FileObject fo = _fileManager.getFileForOutput( location, pkg.toString(), relativeName.toString(), null); URI uri = fo.toUri(); if (_createdFiles.contains(uri)) { throw new FilerException("Resource already created : " + location + '/' + pkg + '/' + relativeName); //$NON-NLS-1$ } _createdFiles.add(uri); return fo; }
public String filePath(Location location, TypeElement element) { try { String qualifiedName = element.asType().toString(); String fileName = qualifiedName.replace(".", FILE_SEPARATOR); Filer filer = environment.getFiler(); FileObject file = filer.getResource(location, "", fileName + ".java"); return file.toUri().getPath(); } catch (FilerException e) { // this is clearly very weak... String message = e.getMessage(); return message.substring(message.indexOf(FILE_SEPARATOR)); } catch (IOException e) { throw new UncheckedIOException(e); } }
private void checkNameAndExistence(String typename, boolean allowUnnamedPackageInfo) throws FilerException { // TODO: Check if type already exists on source or class path? // If so, use warning message key proc.type.already.exists checkName(typename, allowUnnamedPackageInfo); if (aggregateGeneratedSourceNames.contains(typename) || aggregateGeneratedClassNames.contains(typename)) { if (lint) log.warning("proc.type.recreate", typename); throw new FilerException("Attempt to recreate a file for type " + typename); } }
public String filePath(Location location, TypeElement element) { try { String qualifiedName = element.asType().toString(); String fileName = qualifiedName.replace(".", FILE_SEPARATOR); Filer filer = environment.getFiler(); FileObject file = filer.getResource(location, "", fileName + ".java"); return file.toUri().getPath(); } catch (FilerException e) { // this is clearly very weak... String message = e.getMessage(); return message.substring(message.indexOf(FILE_SEPARATOR)); } catch (IOException e) { throw new UncheckedIOException(e); } }
@Override public FileObject createResource(Location location, CharSequence pkg, CharSequence relativeName, Element... originatingElements) throws IOException { validateName(relativeName); FileObject fo = _fileManager.getFileForOutput( location, pkg.toString(), relativeName.toString(), null); URI uri = fo.toUri(); if (_createdFiles.contains(uri)) { throw new FilerException("Resource already created : " + location + '/' + pkg + '/' + relativeName); //$NON-NLS-1$ } _createdFiles.add(uri); return fo; }
@Override public void init(ProcessingEnvironment pe) { super.init(pe); messager = pe.getMessager(); filer = pe.getFiler(); if (filer == null) messager.printMessage(Kind.ERROR, "No filer!"); // create the batch.xml try { if (logger.isLoggable(Level.FINE)) { logger.fine("Overwriting " + BATCHXML); } resourceFile = filer.createResource(StandardLocation.CLASS_OUTPUT, "", BATCHXML, (Element[]) null); if (logger.isLoggable(Level.FINE)) { logger.fine("Successfully overwrote " + BATCHXML); } } catch (FilerException e) { if (logger.isLoggable(Level.SEVERE)) { logger.severe("Cannot create " + BATCHXML + " :" + e.getMessage()); } throw new IllegalStateException(e); } catch (IOException e) { throw new RuntimeException(e); } }