Package juzu.impl.compiler

Examples of juzu.impl.compiler.ProcessingContext


  }
  // end::prePassivate[]

  // tag::processBundle[]
  private void processBundle(ApplicationMetaModel application, ElementHandle.Package pkg, String bundleName) {
    ProcessingContext context = application.getProcessingContext();
    PackageElement pkgElt = context.get(pkg);
    Properties properties = loadBundle(context, pkg, bundleName);
    if (properties == null) {
      throw BUNDLE_NOT_FOUND.failure(pkgElt, bundleName);
    } else {
      try {
View Full Code Here


    return Collections.<Class<? extends java.lang.annotation.Annotation>>singleton(Bindings.class);
  }

  @Override
  public void processAnnotationAdded(ApplicationMetaModel metaModel, AnnotationKey key, AnnotationState added) {
    ProcessingContext env = metaModel.model.processingContext;

    //
    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);

    //
    List<Map<String, Object>> bindings = (List<Map<String, Object>>)added.get("value");
    ArrayList<JSON> list = new ArrayList<JSON>();
    if (bindings != null) {
      for (Map<String, Object> binding : bindings) {
        ElementHandle.Type bindingValue = (ElementHandle.Type)binding.get("value");
        ElementHandle.Type bindingImplementation = (ElementHandle.Type)binding.get("implementation");
        String scope = (String)binding.get("scope");

        //
        JSON bindingJSON = new JSON().set("value", bindingValue.getName().toString());

        //
        TypeElement valueElt = env.get(bindingValue);
        TypeMirror valueTM = valueElt.asType();

        //
        if (bindingImplementation != null) {
          TypeElement implementationElt = env.get(bindingImplementation);
          DeclaredType implementationTM = (DeclaredType)implementationElt.asType();

          // Check class
          if (implementationElt.getKind() != ElementKind.CLASS) {
            throw IMPLEMENTATION_INVALID_TYPE.failure(env.get(key.getElement()), providerElt.getQualifiedName());
          }

          //
          Set<Modifier> modifiers = implementationElt.getModifiers();

          // Check not abstract
          if (modifiers.contains(Modifier.ABSTRACT)) {
            throw IMPLEMENTATION_NOT_ABSTRACT.failure(env.get(key.getElement()), implementationElt.getQualifiedName());
          }

          //
          if (env.isAssignable(implementationTM, providerFactoryTM)) {
            // Check public
            if (!modifiers.contains(Modifier.PUBLIC)) {
              throw PROVIDER_FACTORY_NOT_PUBLIC.failure(env.get(key.getElement()), implementationElt.getQualifiedName());
            }

            // Find zero arg constructor
            ExecutableElement emptyCtor = null;
            for (ExecutableElement ctorElt : ElementFilter.constructorsIn(env.getAllMembers(implementationElt))) {
              if (ctorElt.getParameters().isEmpty()) {
                emptyCtor = ctorElt;
                break;
              }
            }

            // Validate constructor
            if (emptyCtor == null) {
              throw PROVIDER_FACTORY_NO_ZERO_ARG_CTOR.failure(env.get(key.getElement()), implementationElt.getQualifiedName());
            }
            if (!emptyCtor.getModifiers().contains(Modifier.PUBLIC)) {
              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);
            TypeMirror resolved = env.asMemberOf(implementationTM, T.asElement());
            if (env.isAssignable(resolved, valueTM)) {
              // OK
            }
            else {
              throw PROVIDER_NOT_ASSIGNABLE.failure(
                  env.get(key.getElement()),
                implementationElt.getQualifiedName(),
                resolved,
                valueElt.getQualifiedName());
            }
          }
          else if (env.isAssignable(implementationTM, valueTM)) {
            // OK
          }
          else {
            throw IMPLEMENTATION_NOT_ASSIGNABLE.failure(
                env.get(key.getElement()),
              implementationElt.getQualifiedName(),
              valueElt.getQualifiedName());
          }

          //
View Full Code Here

    return assets;
  }

  @Override
  public void prePassivate(ApplicationMetaModel metaModel) {
    ProcessingContext context = metaModel.getProcessingContext();
    AssetsMetaModel assetMetaMode = metaModel.getChild(AssetsMetaModel.KEY);

    // Check duplicate ids
    HashSet<String> ids = new HashSet<String>();
    for (Asset asset : assetMetaMode.getAssets()) {
      if (!ids.add(asset.id)) {
        throw DUPLICATE_ASSET_ID.failure(asset.id);
      }
    }

    //
    Name qn = metaModel.getHandle().getPackageName().append("assets");
    if(!context.isCopyFromSourcesExternallyManaged()) {

      //
      HashMap<String, URL> bilta = new HashMap<String, URL>();
      HashMap<URL, Asset> bilto = new HashMap<URL, Asset>();
      for (Asset asset : assetMetaMode.getAssets()) {
        if (asset.isApplication()) {
          for (Map.Entry<String, String> entry : asset.getSources().entrySet()) {
            String source = entry.getValue();
            if (!source.startsWith("/")) {
              URL resource = assetMetaMode.getResources().get(source);
              if (resource == null) {
                resource = assetMetaMode.resolveResource(source);
              }
              if (resource != null) {
                bilto.put(resource, asset);
                bilta.put(entry.getKey(), resource);
              } else {
                throw ASSET_NOT_FOUND.failure(source);
              }
            }
          }
        }
      }
      bilta.putAll(assetMetaMode.getResources());

      // Process all resources
      for (Map.Entry<String, URL> entry : bilta.entrySet()) {
        InputStream in = null;
        OutputStream out = null;
        try {
          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) {
              in = r.open(entry.getKey(), conn);
            } else {
              in = conn.getInputStream();
            }
            out = dst.openOutputStream();
            Tools.copy(in, out);
          } else {
            context.info("Found up to date related asset in class output for " + src);
          }
        }
        catch (IOException e) {
          throw CANNOT_PROCESS_ASSET.failure(entry.getKey(), e.getMessage());
        }
View Full Code Here

  }

  public final void prePassivate() {

    //
    ProcessingContext env = this.processingContext;

    //
    emitConfig(env);

    //
View Full Code Here

   * @return the related resource URL or null if it cannot be resolved
   * @throws ProcessingException relate any processing issue
   */
  public URL resolveResource(String path) throws ProcessingException {
    ApplicationMetaModel application = (ApplicationMetaModel)metaModel;
    ProcessingContext context = application.getProcessingContext();
    boolean relative = path.length() == 0 || path.charAt(0) != '/';
    if (relative) {
      context.info("Resolving classpath asset " + path);
      Name qn = application.getHandle().getPackageName().append("assets");
      FileObject src;
      try {
        src = context.resolveResourceFromSourcePath(pkg, qn, path);
      }
      catch (Exception e) {
        throw UNRESOLVED_ASSET.failure(path).initCause(e);
      }
      if (src != null) {
        URI uri = src.toUri();
        context.info("Found asset " + path + " on source path " + uri);
        try {
          String scheme = uri.getScheme();
          if (scheme == null) {
            uri = new URI("file:" + uri);
          }
          return uri.toURL();
        }
        catch (URISyntaxException e) {
          throw UNRESOLVED_ASSET.failure(uri).initCause(e);
        }
        catch (MalformedURLException e) {
          throw UNRESOLVED_ASSET.failure(uri).initCause(e);
        }
      } else {
        context.info("Could not find asset " + path + " on source path");
        return null;
      }
    } else {
      return null;
    }
View Full Code Here

    //
    Map<String, URL> ret = Collections.emptyMap();

    //
    Name pkg = metaModel.getHandle().getPackageName();
    ProcessingContext env = metaModel.processingContext;
    List<AnnotationState> webJars = (List<AnnotationState>)annotation.get("value");
    if (webJars != null && webJars.size() > 0) {
      for(AnnotationState webJar : webJars) {
        log.info("Processing declared webjars " + webJar);
        String id = (String)webJar.get("value");
        String version = (String)webJar.get("version");

        //
        if (version == null || version.length() == 0) {
          String path = "META-INF/maven/org.webjars/" + id + "/pom.properties";
          URL resource = WebJarAssetLocator.class.getClassLoader().getResource(path);
          ElementHandle.Package pkgHandle = ElementHandle.Package.create(pkg);
          PackageElement pkgElt = env.get(pkgHandle);
          if (resource == null) {
            throw MISSING_WEBJAR.failure(pkgElt, id);
          } else {
            Properties props = new Properties();
            InputStream in = null;
View Full Code Here

    //
    for (Map.Entry<Name, AnnotationState> entry : clone.entrySet()) {
      AnnotationState annotation = entry.getValue();
      Name pkg = entry.getKey();
      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");

      // WARNING THIS IS NOT CORRECT BUT WORK FOR NOW
      AnnotationMirror annotationMirror = Tools.getAnnotation(pkgElt, Less.class.getName());

      //
      log.info("Handling less annotation for package " + pkg + ": minify=" + minify + " resources=" + resources);

      //
      if (resources != null && resources.size() > 0) {

        // For now we use the hardcoded assets package
        Name assetPkg = pkg.append("assets");

        //
        CompilerLessContext clc = new CompilerLessContext(env, pkgHandle, assetPkg);

        //
        for (String resource : resources) {
          log.info("Processing declared resource " + resource);

          //
          Path path;
          try {
            path = Path.parse(resource);
          }
          catch (IllegalArgumentException e) {
            throw MALFORMED_PATH.failure(pkgElt, annotationMirror, resource).initCause(e);
          }

          //
          Path.Absolute to = assetPkg.resolve(path.as("css"));
          log.info("Resource " + resource + " destination resolved to " + to);

          //
          Lesser lesser;
          Result result;
          try {
            lesser = new Lesser(JSContext.create());
            result = lesser.compile(clc, resource, Boolean.TRUE.equals(minify));
          }
          catch (Exception e) {
            log.info("Unexpected exception", e);
            throw new UnsupportedOperationException(e);
          }

          //
          if (result instanceof Compilation) {
            try {
              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 {
                writer.write(compilation.getValue());
              }
              finally {
View Full Code Here

TOP

Related Classes of juzu.impl.compiler.ProcessingContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.