@Override protected Element[] getElements(TemplateMetaModel template) { return new Element[]{application.getProcessingContext().get(application.getHandle())}; }
public JavaFileObject createSourceFile(Name name, Element... originatingElements) throws IOException { return createSourceFile((CharSequence)name, originatingElements); }
public String load(String ref) { try { Path.Absolute path = pkg.resolve(ref); FileObject c = processingContext.resolveResourceFromSourcePath(context, path); if (c != null) { try { return c.getCharContent(true).toString(); } catch (IOException e) { processingContext.info("Could not get content of " + path, e); } } } catch (IllegalArgumentException e) { // Log ? } // return null; } }
@Override public void postActivate(ModuleMetaModel applications) { // Load the tag handlers for (TagHandler handler : applications.getProcessingContext().loadServices(TagHandler.class)) { applications.getProcessingContext().info("Loaded tag handler " + handler.getClass().getName() + " as " + handler.getName()); tags.put(handler.getName(), handler); } // Load the template providers Iterable<TemplateProvider> loader = applications.processingContext.loadServices(TemplateProvider.class); Map<String, TemplateProvider> providers = new HashMap<String, TemplateProvider>(); for (TemplateProvider provider : loader) { providers.put(provider.getSourceExtension(), provider); } // this.providers = providers; }
private Properties loadBundle(ProcessingContext context, ElementHandle.Package pkg, String bundleName) { Path.Absolute absolute = pkg.getPackageName().resolve(bundleName + ".properties"); FileObject bundle = context.resolveResourceFromSourcePath(pkg, absolute); if (bundle != null) { try { InputStream in = bundle.openInputStream(); Properties properties = new Properties(); properties.load(in); return properties; } catch (IOException e) { context.log(Level.SEVERE, "Could not load resource bundle", e); } } return null; } // end::loadBundle[]
void emitApplication(ProcessingContext env, ApplicationMetaModel application) throws ProcessingException { PackageElement elt = env.get(application.getHandle()); Name fqn = application.getName().append("Application"); JavaFileObject applicationFile = env.createSourceFile(fqn, elt); writer = applicationFile.openWriter(); env.info("Generated application " + fqn + " as " + applicationFile.toUri());
if(!context.isCopyFromSourcesExternallyManaged()) { URL src = entry.getValue(); URLConnection conn = src.openConnection(); FileObject dst = context.getResource(StandardLocation.CLASS_OUTPUT, qn, entry.getKey()); if (dst == null || dst.getLastModified() < conn.getLastModified()) { dst = context.createResource(StandardLocation.CLASS_OUTPUT, qn, entry.getKey(), context.get(metaModel.getHandle())); context.info("Copying asset from source path " + src + " to class output " + dst.toUri()); Asset r = bilto.get(entry.getValue()); if (r != null) { Tools.copy(in, out); } else { context.info("Found up to date related asset in class output for " + src);
metaModel.processingContext.info("Emitting application " + application.getHandle() + " config"); FileObject fo = metaModel.processingContext.createResource(StandardLocation.CLASS_OUTPUT, application.getName(), "config.json"); writer = fo.openWriter(); descriptor.toString(writer, 2); throw ApplicationMetaModel.CANNOT_WRITE_APPLICATION_CONFIG.failure(e, metaModel.processingContext.get(application.getHandle()), application.getName());
TypeMirror providerFactoryTM = env.getTypeElement(ProviderFactory.class.getName()).asType(); TypeElement providerElt = env.getTypeElement("javax.inject.Provider"); DeclaredType providerTM = (DeclaredType)providerElt.asType(); TypeMirror rawProviderTM = env.erasure(providerTM); TypeElement valueElt = env.get(bindingValue); TypeMirror valueTM = valueElt.asType(); TypeElement implementationElt = env.get(bindingImplementation); DeclaredType implementationTM = (DeclaredType)implementationElt.asType(); throw IMPLEMENTATION_INVALID_TYPE.failure(env.get(key.getElement()), providerElt.getQualifiedName()); throw IMPLEMENTATION_NOT_ABSTRACT.failure(env.get(key.getElement()), implementationElt.getQualifiedName()); if (env.isAssignable(implementationTM, providerFactoryTM)) { throw PROVIDER_FACTORY_NOT_PUBLIC.failure(env.get(key.getElement()), implementationElt.getQualifiedName()); for (ExecutableElement ctorElt : ElementFilter.constructorsIn(env.getAllMembers(implementationElt))) { if (ctorElt.getParameters().isEmpty()) { emptyCtor = ctorElt; throw PROVIDER_FACTORY_NO_ZERO_ARG_CTOR.failure(env.get(key.getElement()), implementationElt.getQualifiedName()); throw PROVIDER_FACTORY_NO_PUBLIC_CTOR.failure(env.get(key.getElement()), implementationElt.getQualifiedName()); else if (env.isAssignable(implementationTM, rawProviderTM)) { TypeVariable T = (TypeVariable)providerTM.getTypeArguments().get(0);
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { if (count++ == 0) { try { ProcessingContext ctx = new ProcessingContext(processingEnv); ElementHandle.Package pkg = ElementHandle.Package.create(ctx.getPackageElement("compiler.dot.foo")); FileObject file = ctx.resolveResourceFromSourcePath(pkg, FileKey.newName("compiler.dot.foo", "a.b.txt")); InputStream in = file.openInputStream(); FileObject o = ctx.createResource(StandardLocation.CLASS_OUTPUT, FileKey.newName("compiler.dot.foo", "a.b.css")); OutputStream out = o.openOutputStream(); Tools.copy(in, out); Tools.safeClose(in); Tools.safeClose(out); } catch (Exception e) { throw failure(e); } } return true; } });
AnnotationState annotation = servlets.remove(pkg); if (annotation != null) { PackageElement pkgElt = metaModel.processingContext.get(pkg); String urlPattern = (String)annotation.get("value"); String simpleName = (String)annotation.get("name"); Writer writer = null; try { JavaFileObject file = metaModel.processingContext.createSourceFile(clazz, pkgElt); writer = file.openWriter(); writer.append("package ").append(pkg.getPackageName()).append(";\n");
ProcessingContext env = metaModel.processingContext; ElementHandle.Package pkgHandle = ElementHandle.Package.create(pkg); PackageElement pkgElt = env.get(pkgHandle); Boolean minify = (Boolean)annotation.get("minify"); List<String> resources = (List<String>)annotation.get("value"); log.info("Resource " + resource + " compiled about to write on disk as " + to); Compilation compilation = (Compilation)result; FileObject fo = env.createResource(StandardLocation.CLASS_OUTPUT, to); Writer writer = fo.openWriter(); try {
for (Map.Entry<AnnotationKey, AnnotationState> entry : knownAnnotations.entrySet()) { AnnotationKey key = entry.getKey(); Element element = processingContext.get(key.element); if (element == null) { delta.add(new AnnotationChange(key, entry.getValue(), null)); processingContext.log(Level.FINER, "Annotation removed " + key); } else { AnnotationMirror found = null; processingContext.log(Level.FINER, "Annotation removed " + key); AnnotationState knownAnnotation = knownAnnotations.get(annotation.getKey()); if (knownAnnotation != null) { processingContext.log(Level.FINER, "Annotation updated " + annotation.getKey()); } else { processingContext.log(Level.FINER, "Annotation added " + annotation.getKey());
/** * Resolve a resource for this application. * * @param path the path of the resource to resolve * @return the resolved resource or null if it cannot be determined * @throws NullPointerException if any argument is null * @throws IllegalArgumentException if the context package is not valid */ public FileObject resolveResource(Path.Absolute path) throws NullPointerException, IllegalArgumentException { return model.processingContext.resolveResourceFromSourcePath(handle, path); }
public FileObject createResource(JavaFileManager.Location location, FileKey key, Element... originatingElements) throws IOException { return createResource(location, key.packageFQN, key.name, originatingElements); }
context.report(Diagnostic.Kind.ERROR, msg, element, annotation, null); context.report(Diagnostic.Kind.ERROR, msg, null, null, null); InputStream in = null; try { FileObject file = context.getResource(StandardLocation.SOURCE_OUTPUT, "juzu", "processor.log"); in = file.openInputStream(); s = Tools.read(in, Tools.UTF_8); FileObject file = context.createResource(StandardLocation.SOURCE_OUTPUT, "juzu", "processor.log"); out = file.openOutputStream(); if (s != null) {
private boolean _process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) throws IOException { if (roundEnv.processingOver()) { // Read an existing resource FileObject foo = processingContext.getResource(location, "", "foo.txt"); assertNotNull(foo); String s = Tools.read(foo.openInputStream()); assertEquals("foo_value", s); // Now we overwrite the resource foo = processingContext.createResource(location, "", "foo.txt"); OutputStream out = foo.openOutputStream(); out.write("new_foo_value".getBytes()); out.close(); // Read an non existing resource // JDK 6 strange behavior / bug happens here, we should get bar=null but we don't // JDK 7 should return null FileObject bar = processingContext.getResource(location, "", "bar.txt"); assertNull(bar); // Now create new resource foo = processingContext.createResource(location, "", "juu.txt"); out = foo.openOutputStream(); out.write("juu_value".getBytes()); out.close(); } return true; } }
private void emitConfig(ProcessingContext env) { env.info("Emitting module config"); Writer writer = null; try { FileObject fo = env.createResource(StandardLocation.CLASS_OUTPUT, "juzu", "config.json"); writer = fo.openWriter(); descriptor.toString(writer, 2);
@Override public void prePassivate(ModuleMetaModel module) { module.processingContext.info("Passivating templates"); tags.clear(); this.providers = null; }
public FileObject getResource(JavaFileManager.Location location, FileKey key) throws IOException { return getResource(location, key.packageFQN, key.name); }